Skip to content

Commit 9d81e60

Browse files
committed
Improve dispose logic
1 parent 09bc2bc commit 9d81e60

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

Flow.Launcher/App.xaml.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,15 +298,12 @@ protected virtual void Dispose(bool disposing)
298298
_disposed = true;
299299
}
300300

301-
Stopwatch.Normal("|App.Dispose|Dispose cost", async () =>
301+
Stopwatch.Normal("|App.Dispose|Dispose cost", () =>
302302
{
303303
Log.Info("|App.Dispose|Begin Flow Launcher dispose ----------------------------------------------------");
304304

305305
if (disposing)
306306
{
307-
API?.SaveAppAllSettings();
308-
await PluginManager.DisposePluginsAsync();
309-
310307
// Dispose needs to be called on the main Windows thread,
311308
// since some resources owned by the thread need to be disposed.
312309
_mainWindow?.Dispatcher.Invoke(_mainWindow.Dispose);
@@ -319,6 +316,7 @@ protected virtual void Dispose(bool disposing)
319316

320317
public void Dispose()
321318
{
319+
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
322320
Dispose(disposing: true);
323321
GC.SuppressFinalize(this);
324322
}

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ public partial class MainWindow : IDisposable
3939
private NotifyIcon _notifyIcon;
4040

4141
// Window Context Menu
42-
private readonly ContextMenu contextMenu = new();
42+
private readonly ContextMenu _contextMenu = new();
4343
private readonly MainViewModel _viewModel;
4444

45+
// Window Event: Close Event
46+
private bool _canClose = false;
4547
// Window Event: Key Event
46-
private bool isArrowKeyPressed = false;
48+
private bool _isArrowKeyPressed = false;
4749

4850
// Window Sound Effects
4951
private MediaPlayer animationSoundWMP;
@@ -60,7 +62,7 @@ public partial class MainWindow : IDisposable
6062
private bool _isClockPanelAnimating = false;
6163

6264
// IDisposable
63-
private bool _disposedValue = false;
65+
private bool _disposed = false;
6466

6567
#endregion
6668

@@ -231,10 +233,18 @@ private async void OnLoaded(object sender, RoutedEventArgs _)
231233
.AddValueChanged(History, (s, e) => UpdateClockPanelVisibility());
232234
}
233235

234-
private void OnClosing(object sender, CancelEventArgs e)
236+
private async void OnClosing(object sender, CancelEventArgs e)
235237
{
236-
_notifyIcon.Visible = false;
237-
Notification.Uninstall();
238+
if (!_canClose)
239+
{
240+
_notifyIcon.Visible = false;
241+
App.API.SaveAppAllSettings();
242+
e.Cancel = true;
243+
await PluginManager.DisposePluginsAsync();
244+
Notification.Uninstall();
245+
_canClose = true;
246+
Close();
247+
}
238248
}
239249

240250
private void OnClosed(object sender, EventArgs e)
@@ -292,12 +302,12 @@ private void OnKeyDown(object sender, KeyEventArgs e)
292302
switch (e.Key)
293303
{
294304
case Key.Down:
295-
isArrowKeyPressed = true;
305+
_isArrowKeyPressed = true;
296306
_viewModel.SelectNextItemCommand.Execute(null);
297307
e.Handled = true;
298308
break;
299309
case Key.Up:
300-
isArrowKeyPressed = true;
310+
_isArrowKeyPressed = true;
301311
_viewModel.SelectPrevItemCommand.Execute(null);
302312
e.Handled = true;
303313
break;
@@ -355,13 +365,13 @@ private void OnKeyUp(object sender, KeyEventArgs e)
355365
{
356366
if (e.Key == Key.Up || e.Key == Key.Down)
357367
{
358-
isArrowKeyPressed = false;
368+
_isArrowKeyPressed = false;
359369
}
360370
}
361371

362372
private void OnPreviewMouseMove(object sender, MouseEventArgs e)
363373
{
364-
if (isArrowKeyPressed)
374+
if (_isArrowKeyPressed)
365375
{
366376
e.Handled = true; // Ignore Mouse Hover when press Arrowkeys
367377
}
@@ -531,11 +541,11 @@ private void InitializeNotifyIcon()
531541
gamemode.ToolTip = App.API.GetTranslation("GameModeToolTip");
532542
positionreset.ToolTip = App.API.GetTranslation("PositionResetToolTip");
533543

534-
contextMenu.Items.Add(open);
535-
contextMenu.Items.Add(gamemode);
536-
contextMenu.Items.Add(positionreset);
537-
contextMenu.Items.Add(settings);
538-
contextMenu.Items.Add(exit);
544+
_contextMenu.Items.Add(open);
545+
_contextMenu.Items.Add(gamemode);
546+
_contextMenu.Items.Add(positionreset);
547+
_contextMenu.Items.Add(settings);
548+
_contextMenu.Items.Add(exit);
539549

540550
_notifyIcon.MouseClick += (o, e) =>
541551
{
@@ -546,22 +556,22 @@ private void InitializeNotifyIcon()
546556
break;
547557
case MouseButtons.Right:
548558

549-
contextMenu.IsOpen = true;
559+
_contextMenu.IsOpen = true;
550560
// Get context menu handle and bring it to the foreground
551-
if (PresentationSource.FromVisual(contextMenu) is HwndSource hwndSource)
561+
if (PresentationSource.FromVisual(_contextMenu) is HwndSource hwndSource)
552562
{
553563
Win32Helper.SetForegroundWindow(hwndSource.Handle);
554564
}
555565

556-
contextMenu.Focus();
566+
_contextMenu.Focus();
557567
break;
558568
}
559569
};
560570
}
561571

562572
private void UpdateNotifyIconText()
563573
{
564-
var menu = contextMenu;
574+
var menu = _contextMenu;
565575
((MenuItem)menu.Items[0]).Header = App.API.GetTranslation("iconTrayOpen") +
566576
" (" + _settings.Hotkey + ")";
567577
((MenuItem)menu.Items[1]).Header = App.API.GetTranslation("GameMode");
@@ -757,7 +767,7 @@ private void WindowAnimation()
757767
if (_animating)
758768
return;
759769

760-
isArrowKeyPressed = true;
770+
_isArrowKeyPressed = true;
761771
_animating = true;
762772
UpdatePosition(false);
763773

@@ -835,7 +845,7 @@ private void WindowAnimation()
835845

836846
clocksb.Completed += (_, _) => _animating = false;
837847
_settings.WindowLeft = Left;
838-
isArrowKeyPressed = false;
848+
_isArrowKeyPressed = false;
839849

840850
if (QueryTextBox.Text.Length == 0)
841851
{
@@ -1009,14 +1019,15 @@ private void QueryTextBox_OnPreviewDragOver(object sender, DragEventArgs e)
10091019

10101020
protected virtual void Dispose(bool disposing)
10111021
{
1012-
if (!_disposedValue)
1022+
if (!_disposed)
10131023
{
10141024
if (disposing)
10151025
{
10161026
_hwndSource?.Dispose();
1027+
_notifyIcon?.Dispose();
10171028
}
10181029

1019-
_disposedValue = true;
1030+
_disposed = true;
10201031
}
10211032
}
10221033

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,6 +1561,7 @@ protected virtual void Dispose(bool disposing)
15611561

15621562
public void Dispose()
15631563
{
1564+
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
15641565
Dispose(disposing: true);
15651566
GC.SuppressFinalize(this);
15661567
}

0 commit comments

Comments
 (0)