From f7be8e951688d7cac79d2fc73e20730d892b0cce Mon Sep 17 00:00:00 2001 From: genteure Date: Mon, 16 May 2022 21:06:49 +0800 Subject: [PATCH] WPF: Disable minimize to tray icon when a ContentDialog is open. fix #316 --- BannedSymbols.txt | 2 ++ .../BililiveRecorder.WPF.csproj | 3 +- .../Controls/ContentDialogExtensions.cs | 23 ++++++++++++ BililiveRecorder.WPF/Models/Commands.cs | 3 +- BililiveRecorder.WPF/NewMainWindow.xaml.cs | 6 ++-- .../Pages/RoomListPage.xaml.cs | 35 +++++++++++++++---- BililiveRecorder.WPF/Pages/RootPage.xaml.cs | 5 +-- .../Pages/ToolboxAutoFixPage.xaml.cs | 15 ++++---- .../Pages/ToolboxDanmakuMergerPage.xaml.cs | 5 +-- Directory.Build.props | 9 ++++- 10 files changed, 83 insertions(+), 23 deletions(-) create mode 100644 BannedSymbols.txt create mode 100644 BililiveRecorder.WPF/Controls/ContentDialogExtensions.cs diff --git a/BannedSymbols.txt b/BannedSymbols.txt new file mode 100644 index 0000000..ec7b619 --- /dev/null +++ b/BannedSymbols.txt @@ -0,0 +1,2 @@ +M:ModernWpf.Controls.ContentDialog.ShowAsync +M:ModernWpf.Controls.ContentDialog.ShowAsync(ModernWpf.Controls.ContentDialogPlacement) diff --git a/BililiveRecorder.WPF/BililiveRecorder.WPF.csproj b/BililiveRecorder.WPF/BililiveRecorder.WPF.csproj index bd27933..d52b997 100644 --- a/BililiveRecorder.WPF/BililiveRecorder.WPF.csproj +++ b/BililiveRecorder.WPF/BililiveRecorder.WPF.csproj @@ -92,6 +92,7 @@ CloseWindowConfirmDialog.xaml + DeleteRoomConfirmDialog.xaml @@ -407,4 +408,4 @@ - \ No newline at end of file + diff --git a/BililiveRecorder.WPF/Controls/ContentDialogExtensions.cs b/BililiveRecorder.WPF/Controls/ContentDialogExtensions.cs new file mode 100644 index 0000000..5b4b991 --- /dev/null +++ b/BililiveRecorder.WPF/Controls/ContentDialogExtensions.cs @@ -0,0 +1,23 @@ +using System.Threading.Tasks; +using System.Windows; +using ModernWpf.Controls; + +namespace BililiveRecorder.WPF.Controls +{ + internal static class ContentDialogExtensions + { + internal static async Task ShowAndDisableMinimizeToTrayAsync(this ContentDialog contentDialog) + { + var mw = (NewMainWindow)Application.Current.MainWindow; + mw.HideToTrayBlockedByContentDialog = true; + +#pragma warning disable RS0030 // Do not used banned APIs + var result = await contentDialog.ShowAsync(); +#pragma warning restore RS0030 // Do not used banned APIs + + mw.HideToTrayBlockedByContentDialog = false; + + return result; + } + } +} diff --git a/BililiveRecorder.WPF/Models/Commands.cs b/BililiveRecorder.WPF/Models/Commands.cs index f3a79d4..ce0a7fb 100644 --- a/BililiveRecorder.WPF/Models/Commands.cs +++ b/BililiveRecorder.WPF/Models/Commands.cs @@ -2,6 +2,7 @@ using System; using System.Diagnostics; using System.Windows; using System.Windows.Input; +using BililiveRecorder.WPF.Controls; using ModernWpf.Controls; #nullable enable @@ -19,7 +20,7 @@ namespace BililiveRecorder.WPF.Models public static Commands OpenContentDialog { get; } = new Commands { #pragma warning disable VSTHRD101 // Avoid unsupported async delegates - ExecuteDelegate = async o => { try { await (o as ContentDialog)!.ShowAsync(); } catch (Exception) { } } + ExecuteDelegate = async o => { try { await (o as ContentDialog)!.ShowAndDisableMinimizeToTrayAsync(); } catch (Exception) { } } #pragma warning restore VSTHRD101 // Avoid unsupported async delegates }; diff --git a/BililiveRecorder.WPF/NewMainWindow.xaml.cs b/BililiveRecorder.WPF/NewMainWindow.xaml.cs index dd2b13c..9add0fb 100644 --- a/BililiveRecorder.WPF/NewMainWindow.xaml.cs +++ b/BililiveRecorder.WPF/NewMainWindow.xaml.cs @@ -1,7 +1,6 @@ using System; using System.Globalization; using System.Threading; -using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; using BililiveRecorder.WPF.Controls; @@ -66,10 +65,11 @@ namespace BililiveRecorder.WPF private bool notification_showed = false; public bool HideToTray { get; set; } = false; + public bool HideToTrayBlockedByContentDialog { get; set; } = false; private void Window_StateChanged(object sender, EventArgs e) { - if (this.HideToTray && this.WindowState == WindowState.Minimized) + if (this.HideToTray && !this.HideToTrayBlockedByContentDialog && this.WindowState == WindowState.Minimized) { this.Hide(); if (!this.notification_showed) @@ -102,7 +102,7 @@ namespace BililiveRecorder.WPF { try { - if (await new CloseWindowConfirmDialog().ShowAsync() == ContentDialogResult.Primary) + if (await new CloseWindowConfirmDialog { Owner = Application.Current.MainWindow }.ShowAndDisableMinimizeToTrayAsync() == ContentDialogResult.Primary) { this.CloseConfirmed = true; _ = this.Dispatcher.BeginInvoke(this.Close, DispatcherPriority.Normal); diff --git a/BililiveRecorder.WPF/Pages/RoomListPage.xaml.cs b/BililiveRecorder.WPF/Pages/RoomListPage.xaml.cs index ecf6e33..ddc1117 100644 --- a/BililiveRecorder.WPF/Pages/RoomListPage.xaml.cs +++ b/BililiveRecorder.WPF/Pages/RoomListPage.xaml.cs @@ -119,10 +119,11 @@ namespace BililiveRecorder.WPF.Pages { var dialog = new DeleteRoomConfirmDialog { - DataContext = room + DataContext = room, + Owner = Application.Current.MainWindow }; - var result = await dialog.ShowAsync(); + var result = await dialog.ShowAndDisableMinimizeToTrayAsync(); if (result == ContentDialogResult.Primary) { @@ -139,7 +140,11 @@ namespace BililiveRecorder.WPF.Pages { try { - await new PerRoomSettingsDialog { DataContext = sender }.ShowAsync(); + await new PerRoomSettingsDialog + { + DataContext = sender, + Owner = Application.Current.MainWindow + }.ShowAndDisableMinimizeToTrayAsync(); } catch (Exception) { } } @@ -162,7 +167,11 @@ namespace BililiveRecorder.WPF.Pages { try { - await new AddRoomFailedDialog { DataContext = AddRoomFailedDialog.AddRoomFailedErrorText.InvalidInput }.ShowAsync(); + await new AddRoomFailedDialog + { + DataContext = AddRoomFailedDialog.AddRoomFailedErrorText.InvalidInput, + Owner = Application.Current.MainWindow + }.ShowAndDisableMinimizeToTrayAsync(); } catch (Exception) { } return; @@ -173,7 +182,11 @@ namespace BililiveRecorder.WPF.Pages { try { - await new AddRoomFailedDialog { DataContext = AddRoomFailedDialog.AddRoomFailedErrorText.RoomIdNegative }.ShowAsync(); + await new AddRoomFailedDialog + { + DataContext = AddRoomFailedDialog.AddRoomFailedErrorText.RoomIdNegative, + Owner = Application.Current.MainWindow + }.ShowAndDisableMinimizeToTrayAsync(); } catch (Exception) { } return; @@ -182,7 +195,11 @@ namespace BililiveRecorder.WPF.Pages { try { - await new AddRoomFailedDialog { DataContext = AddRoomFailedDialog.AddRoomFailedErrorText.RoomIdZero }.ShowAsync(); + await new AddRoomFailedDialog + { + DataContext = AddRoomFailedDialog.AddRoomFailedErrorText.RoomIdZero, + Owner = Application.Current.MainWindow + }.ShowAndDisableMinimizeToTrayAsync(); } catch (Exception) { } return; @@ -192,7 +209,11 @@ namespace BililiveRecorder.WPF.Pages { try { - await new AddRoomFailedDialog { DataContext = AddRoomFailedDialog.AddRoomFailedErrorText.Duplicate }.ShowAsync(); + await new AddRoomFailedDialog + { + DataContext = AddRoomFailedDialog.AddRoomFailedErrorText.Duplicate, + Owner = Application.Current.MainWindow + }.ShowAndDisableMinimizeToTrayAsync(); } catch (Exception) { } return; diff --git a/BililiveRecorder.WPF/Pages/RootPage.xaml.cs b/BililiveRecorder.WPF/Pages/RootPage.xaml.cs index fae2fdf..d7ff624 100644 --- a/BililiveRecorder.WPF/Pages/RootPage.xaml.cs +++ b/BililiveRecorder.WPF/Pages/RootPage.xaml.cs @@ -169,9 +169,10 @@ You can uninstall me in system settings.", "安装成功 Installed", MessageBoxB { Error = error, Path = lastdir, - SkipAsking = pathInfo.SkipAsking + SkipAsking = pathInfo.SkipAsking, + Owner = Application.Current.MainWindow }; - var dialogResult = await dialog.ShowAsync(); + var dialogResult = await dialog.ShowAndDisableMinimizeToTrayAsync(); switch (dialogResult) { case ContentDialogResult.Primary: diff --git a/BililiveRecorder.WPF/Pages/ToolboxAutoFixPage.xaml.cs b/BililiveRecorder.WPF/Pages/ToolboxAutoFixPage.xaml.cs index 8958326..9d9cd50 100644 --- a/BililiveRecorder.WPF/Pages/ToolboxAutoFixPage.xaml.cs +++ b/BililiveRecorder.WPF/Pages/ToolboxAutoFixPage.xaml.cs @@ -73,10 +73,11 @@ namespace BililiveRecorder.WPF.Pages progressDialog = new AutoFixProgressDialog() { CancelButtonVisibility = Visibility.Visible, - CancellationTokenSource = new CancellationTokenSource() + CancellationTokenSource = new CancellationTokenSource(), + Owner = Application.Current.MainWindow }; var token = progressDialog.CancellationTokenSource.Token; - var showTask = progressDialog.ShowAsync(); + var showTask = progressDialog.ShowAndDisableMinimizeToTrayAsync(); string? output_path; { @@ -170,10 +171,11 @@ namespace BililiveRecorder.WPF.Pages progressDialog = new AutoFixProgressDialog() { CancelButtonVisibility = Visibility.Visible, - CancellationTokenSource = new CancellationTokenSource() + CancellationTokenSource = new CancellationTokenSource(), + Owner = Application.Current.MainWindow }; var token = progressDialog.CancellationTokenSource.Token; - var showTask = progressDialog.ShowAsync(); + var showTask = progressDialog.ShowAndDisableMinimizeToTrayAsync(); var req = new AnalyzeRequest { @@ -245,10 +247,11 @@ namespace BililiveRecorder.WPF.Pages progressDialog = new AutoFixProgressDialog() { CancelButtonVisibility = Visibility.Visible, - CancellationTokenSource = new CancellationTokenSource() + CancellationTokenSource = new CancellationTokenSource(), + Owner = Application.Current.MainWindow }; var token = progressDialog.CancellationTokenSource.Token; - var showTask = progressDialog.ShowAsync(); + var showTask = progressDialog.ShowAndDisableMinimizeToTrayAsync(); var outputPath = string.Empty; { diff --git a/BililiveRecorder.WPF/Pages/ToolboxDanmakuMergerPage.xaml.cs b/BililiveRecorder.WPF/Pages/ToolboxDanmakuMergerPage.xaml.cs index a0eb44c..b5b7c4f 100644 --- a/BililiveRecorder.WPF/Pages/ToolboxDanmakuMergerPage.xaml.cs +++ b/BililiveRecorder.WPF/Pages/ToolboxDanmakuMergerPage.xaml.cs @@ -134,10 +134,11 @@ namespace BililiveRecorder.WPF.Pages progressDialog = new AutoFixProgressDialog() { CancelButtonVisibility = Visibility.Collapsed, - CancellationTokenSource = new CancellationTokenSource() + CancellationTokenSource = new CancellationTokenSource(), + Owner = Application.Current.MainWindow }; var token = progressDialog.CancellationTokenSource.Token; - var showTask = progressDialog.ShowAsync(); + var showTask = progressDialog.ShowAndDisableMinimizeToTrayAsync(); string? outputPath; { diff --git a/Directory.Build.props b/Directory.Build.props index 0eff234..8a5dc91 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -7,9 +7,16 @@ enable + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + all - runtime; build; native; contentfiles; analyzers + runtime; build; native; contentfiles; analyzers; buildtransitive + + +