From 6e220073d855722f928186f4dfd6b8ff7d43433c Mon Sep 17 00:00:00 2001 From: Pato05 Date: Thu, 15 Feb 2024 22:58:46 +0100 Subject: [PATCH] dispose resources on app close (prevents audio glitch) --- lib/api/player/systray.dart | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/api/player/systray.dart b/lib/api/player/systray.dart index b4e984f..b21d388 100644 --- a/lib/api/player/systray.dart +++ b/lib/api/player/systray.dart @@ -10,7 +10,8 @@ import 'package:window_manager/window_manager.dart'; final sysTray = SysTray._(); -class SysTray with TrayListener { +/// Handles system tray and window events +class SysTray with TrayListener, WindowListener { SysTray._(); static String getIcon({bool forcePng = false}) { @@ -33,6 +34,8 @@ class SysTray with TrayListener { if (_inited) return; _inited = true; + windowManager.addListener(this); + updateIcon(); try { await trayManager.setToolTip('freezer'); @@ -65,22 +68,19 @@ class SysTray with TrayListener { MenuItem.separator(), MenuItem( label: 'Previous'.i18n, - onClick: (menuItem) => audioHandler.skipToPrevious()), + onClick: (_) => audioHandler.skipToPrevious()), playing - ? MenuItem( - label: 'Pause'.i18n, onClick: (menuItem) => audioHandler.pause()) - : MenuItem( - label: 'Play'.i18n, onClick: (menuItem) => audioHandler.play()), - MenuItem( - label: 'Next'.i18n, onClick: (menuItem) => audioHandler.skipToNext()), + ? MenuItem(label: 'Pause'.i18n, onClick: (_) => audioHandler.pause()) + : MenuItem(label: 'Play'.i18n, onClick: (_) => audioHandler.play()), + MenuItem(label: 'Next'.i18n, onClick: (_) => audioHandler.skipToNext()), MenuItem.separator(), MenuItem( label: 'Show'.i18n, // we can safely ignore it if it errors, as it's expected - onClick: (menuItem) => windowManager.show().catchError((e) {})), + onClick: (_) => windowManager.show().catchError((e) {})), MenuItem( label: 'Exit'.i18n, - onClick: (menuItem) async { + onClick: (_) async { await audioHandler.pause(); SystemNavigator.pop(); }, @@ -103,5 +103,8 @@ class SysTray with TrayListener { void onTrayIconRightMouseUp() => trayManager.popUpContextMenu(); @override - void onTrayMenuItemClick(MenuItem menuItem) {} + void onWindowClose() { + // release resources before closing + audioHandler.stop(); + } }