WPF: Disable minimize to tray icon when a ContentDialog is open. fix #316

This commit is contained in:
genteure 2022-05-16 21:06:49 +08:00
parent fa9cb75c97
commit f7be8e9516
10 changed files with 83 additions and 23 deletions

2
BannedSymbols.txt Normal file
View File

@ -0,0 +1,2 @@
M:ModernWpf.Controls.ContentDialog.ShowAsync
M:ModernWpf.Controls.ContentDialog.ShowAsync(ModernWpf.Controls.ContentDialogPlacement)

View File

@ -92,6 +92,7 @@
<Compile Include="Controls\CloseWindowConfirmDialog.xaml.cs">
<DependentUpon>CloseWindowConfirmDialog.xaml</DependentUpon>
</Compile>
<Compile Include="Controls\ContentDialogExtensions.cs" />
<Compile Include="Controls\DeleteRoomConfirmDialog.xaml.cs">
<DependentUpon>DeleteRoomConfirmDialog.xaml</DependentUpon>
</Compile>
@ -407,4 +408,4 @@
</PropertyGroup>
<Exec Command="$(PkgNuGet_CommandLine)\tools\NuGet pack BililiveRecorder.nuspec -Version %(myAssemblyInfo.Version) -Properties Configuration=Release -OutputDirectory $(NupkgReleaseDir) -BasePath $(OutDir) -NonInteractive -ForceEnglishOutput" />
</Target>
</Project>
</Project>

View File

@ -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<ContentDialogResult> 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;
}
}
}

View File

@ -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
};

View File

@ -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);

View File

@ -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;

View File

@ -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:

View File

@ -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;
{

View File

@ -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;
{

View File

@ -7,9 +7,16 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="3.3.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="GitVersion.MsBuild" Version="5.6.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="$(MSBuildThisFileDirectory)BannedSymbols.txt" Link="BannedSymbols.txt"/>
</ItemGroup>
</Project>