From 9d81e60813be042cbf12a9a5e58bd59094728fc3 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sat, 22 Mar 2025 13:26:12 +0800 Subject: [PATCH] Improve dispose logic --- Flow.Launcher/App.xaml.cs | 6 +-- Flow.Launcher/MainWindow.xaml.cs | 57 ++++++++++++++---------- Flow.Launcher/ViewModel/MainViewModel.cs | 1 + 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/Flow.Launcher/App.xaml.cs b/Flow.Launcher/App.xaml.cs index 016c2d06c..d78c6c47b 100644 --- a/Flow.Launcher/App.xaml.cs +++ b/Flow.Launcher/App.xaml.cs @@ -298,15 +298,12 @@ namespace Flow.Launcher _disposed = true; } - Stopwatch.Normal("|App.Dispose|Dispose cost", async () => + Stopwatch.Normal("|App.Dispose|Dispose cost", () => { Log.Info("|App.Dispose|Begin Flow Launcher dispose ----------------------------------------------------"); if (disposing) { - API?.SaveAppAllSettings(); - await PluginManager.DisposePluginsAsync(); - // Dispose needs to be called on the main Windows thread, // since some resources owned by the thread need to be disposed. _mainWindow?.Dispatcher.Invoke(_mainWindow.Dispose); @@ -319,6 +316,7 @@ namespace Flow.Launcher public void Dispose() { + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method Dispose(disposing: true); GC.SuppressFinalize(this); } diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index e7be15081..adbb6f329 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -39,11 +39,13 @@ namespace Flow.Launcher private NotifyIcon _notifyIcon; // Window Context Menu - private readonly ContextMenu contextMenu = new(); + private readonly ContextMenu _contextMenu = new(); private readonly MainViewModel _viewModel; + // Window Event: Close Event + private bool _canClose = false; // Window Event: Key Event - private bool isArrowKeyPressed = false; + private bool _isArrowKeyPressed = false; // Window Sound Effects private MediaPlayer animationSoundWMP; @@ -60,7 +62,7 @@ namespace Flow.Launcher private bool _isClockPanelAnimating = false; // IDisposable - private bool _disposedValue = false; + private bool _disposed = false; #endregion @@ -231,10 +233,18 @@ namespace Flow.Launcher .AddValueChanged(History, (s, e) => UpdateClockPanelVisibility()); } - private void OnClosing(object sender, CancelEventArgs e) + private async void OnClosing(object sender, CancelEventArgs e) { - _notifyIcon.Visible = false; - Notification.Uninstall(); + if (!_canClose) + { + _notifyIcon.Visible = false; + App.API.SaveAppAllSettings(); + e.Cancel = true; + await PluginManager.DisposePluginsAsync(); + Notification.Uninstall(); + _canClose = true; + Close(); + } } private void OnClosed(object sender, EventArgs e) @@ -292,12 +302,12 @@ namespace Flow.Launcher switch (e.Key) { case Key.Down: - isArrowKeyPressed = true; + _isArrowKeyPressed = true; _viewModel.SelectNextItemCommand.Execute(null); e.Handled = true; break; case Key.Up: - isArrowKeyPressed = true; + _isArrowKeyPressed = true; _viewModel.SelectPrevItemCommand.Execute(null); e.Handled = true; break; @@ -355,13 +365,13 @@ namespace Flow.Launcher { if (e.Key == Key.Up || e.Key == Key.Down) { - isArrowKeyPressed = false; + _isArrowKeyPressed = false; } } private void OnPreviewMouseMove(object sender, MouseEventArgs e) { - if (isArrowKeyPressed) + if (_isArrowKeyPressed) { e.Handled = true; // Ignore Mouse Hover when press Arrowkeys } @@ -531,11 +541,11 @@ namespace Flow.Launcher gamemode.ToolTip = App.API.GetTranslation("GameModeToolTip"); positionreset.ToolTip = App.API.GetTranslation("PositionResetToolTip"); - contextMenu.Items.Add(open); - contextMenu.Items.Add(gamemode); - contextMenu.Items.Add(positionreset); - contextMenu.Items.Add(settings); - contextMenu.Items.Add(exit); + _contextMenu.Items.Add(open); + _contextMenu.Items.Add(gamemode); + _contextMenu.Items.Add(positionreset); + _contextMenu.Items.Add(settings); + _contextMenu.Items.Add(exit); _notifyIcon.MouseClick += (o, e) => { @@ -546,14 +556,14 @@ namespace Flow.Launcher break; case MouseButtons.Right: - contextMenu.IsOpen = true; + _contextMenu.IsOpen = true; // Get context menu handle and bring it to the foreground - if (PresentationSource.FromVisual(contextMenu) is HwndSource hwndSource) + if (PresentationSource.FromVisual(_contextMenu) is HwndSource hwndSource) { Win32Helper.SetForegroundWindow(hwndSource.Handle); } - contextMenu.Focus(); + _contextMenu.Focus(); break; } }; @@ -561,7 +571,7 @@ namespace Flow.Launcher private void UpdateNotifyIconText() { - var menu = contextMenu; + var menu = _contextMenu; ((MenuItem)menu.Items[0]).Header = App.API.GetTranslation("iconTrayOpen") + " (" + _settings.Hotkey + ")"; ((MenuItem)menu.Items[1]).Header = App.API.GetTranslation("GameMode"); @@ -757,7 +767,7 @@ namespace Flow.Launcher if (_animating) return; - isArrowKeyPressed = true; + _isArrowKeyPressed = true; _animating = true; UpdatePosition(false); @@ -835,7 +845,7 @@ namespace Flow.Launcher clocksb.Completed += (_, _) => _animating = false; _settings.WindowLeft = Left; - isArrowKeyPressed = false; + _isArrowKeyPressed = false; if (QueryTextBox.Text.Length == 0) { @@ -1009,14 +1019,15 @@ namespace Flow.Launcher protected virtual void Dispose(bool disposing) { - if (!_disposedValue) + if (!_disposed) { if (disposing) { _hwndSource?.Dispose(); + _notifyIcon?.Dispose(); } - _disposedValue = true; + _disposed = true; } } diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 18e61914d..1668bac3a 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -1561,6 +1561,7 @@ namespace Flow.Launcher.ViewModel public void Dispose() { + // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method Dispose(disposing: true); GC.SuppressFinalize(this); }