BililiveRecorder/BililiveRecorder.WPF/NewMainWindow.xaml.cs

131 lines
4.6 KiB
C#
Raw Permalink Normal View History

2020-11-27 18:51:02 +08:00
using System;
2021-01-06 20:42:50 +08:00
using System.Globalization;
2020-11-27 18:51:02 +08:00
using System.Threading;
using System.Windows;
using System.Windows.Threading;
2020-11-27 18:51:02 +08:00
using BililiveRecorder.WPF.Controls;
2020-11-28 13:02:57 +08:00
using Hardcodet.Wpf.TaskbarNotification;
2020-11-27 18:51:02 +08:00
using ModernWpf.Controls;
2021-04-20 20:41:26 +08:00
using Serilog;
2021-01-03 13:21:36 +08:00
using WPFLocalizeExtension.Engine;
2021-01-04 14:01:11 +08:00
using WPFLocalizeExtension.Extensions;
2020-11-27 18:51:02 +08:00
2021-02-23 18:03:37 +08:00
#nullable enable
2020-11-27 18:51:02 +08:00
namespace BililiveRecorder.WPF
{
/// <summary>
/// Interaction logic for NewMainWindow.xaml
/// </summary>
public partial class NewMainWindow : Window
{
2021-01-03 13:21:36 +08:00
public string SoftwareVersion { get; }
2020-11-27 18:51:02 +08:00
public NewMainWindow()
{
2021-02-26 21:57:10 +08:00
this.SoftwareVersion = GitVersionInformation.FullSemVer;
2021-01-04 14:01:11 +08:00
2021-01-06 20:42:50 +08:00
Pages.AnnouncementPage.CultureInfo = CultureInfo.CurrentUICulture;
LocalizeDictionary.Instance.Culture = CultureInfo.CurrentUICulture;
2021-01-04 14:01:11 +08:00
#if DEBUG
2021-01-03 13:21:36 +08:00
LocalizeDictionary.Instance.OutputMissingKeys = true;
2021-01-04 14:01:11 +08:00
LocalizeDictionary.Instance.MissingKeyEvent += (object sender, MissingKeyEventArgs e) => MessageBox.Show("Missing: " + e.Key);
#endif
2021-01-03 13:21:36 +08:00
this.InitializeComponent();
2020-11-27 18:51:02 +08:00
2021-01-03 13:21:36 +08:00
SingleInstance.NotificationReceived += this.SingleInstance_NotificationReceived;
}
private void SingleInstance_NotificationReceived(object sender, EventArgs e) => this.SuperActivateAction();
2021-02-23 18:03:37 +08:00
public event EventHandler? NativeBeforeWindowClose;
2020-11-28 13:02:57 +08:00
2021-02-23 18:03:37 +08:00
internal Action<string, string, BalloonIcon>? ShowBalloonTipCallback { get; set; }
2020-11-28 13:02:57 +08:00
internal void CloseWithoutConfirmAction()
2020-11-27 18:51:02 +08:00
{
2021-01-03 13:21:36 +08:00
this.CloseConfirmed = true;
2022-05-17 00:53:37 +08:00
_ = this.Dispatcher.BeginInvoke(this.Close, DispatcherPriority.Normal);
2020-11-28 13:02:57 +08:00
}
internal void SuperActivateAction()
{
try
{
2021-01-03 13:21:36 +08:00
this.Show();
this.WindowState = WindowState.Normal;
this.Topmost = true;
this.Activate();
this.Topmost = false;
this.Focus();
}
catch (Exception)
{ }
2020-11-27 18:51:02 +08:00
}
2021-01-09 16:25:55 +08:00
private bool notification_showed = false;
2021-02-27 18:14:44 +08:00
public bool HideToTray { get; set; } = false;
public bool HideToTrayBlockedByContentDialog { get; set; } = false;
2021-01-09 16:25:55 +08:00
2020-11-28 13:02:57 +08:00
private void Window_StateChanged(object sender, EventArgs e)
{
if (this.HideToTray && !this.HideToTrayBlockedByContentDialog && this.WindowState == WindowState.Minimized)
2020-11-28 13:02:57 +08:00
{
2021-01-03 13:21:36 +08:00
this.Hide();
2021-01-09 16:25:55 +08:00
if (!this.notification_showed)
{
this.notification_showed = true;
var title = LocExtension.GetLocalizedValue<string>("BililiveRecorder.WPF:Strings:TaskbarIconControl_Title");
var body = LocExtension.GetLocalizedValue<string>("BililiveRecorder.WPF:Strings:TaskbarIconControl_MinimizedNotification");
this.ShowBalloonTipCallback?.Invoke(title, body, BalloonIcon.Info);
}
2020-11-28 13:02:57 +08:00
}
}
2020-11-27 18:51:02 +08:00
2020-11-28 13:02:57 +08:00
#region Confirm Close Window
private bool CloseConfirmed = false;
private readonly SemaphoreSlim CloseWindowSemaphoreSlim = new SemaphoreSlim(1, 1);
public bool PromptCloseConfirm { get; set; } = true;
#pragma warning disable VSTHRD100 // Avoid async void methods
private async void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
#pragma warning restore VSTHRD100 // Avoid async void methods
2020-11-27 18:51:02 +08:00
{
2021-01-03 13:21:36 +08:00
if (this.PromptCloseConfirm && !this.CloseConfirmed)
2020-11-27 18:51:02 +08:00
{
e.Cancel = true;
if (await this.CloseWindowSemaphoreSlim.WaitAsync(0))
2020-11-27 18:51:02 +08:00
{
try
2020-11-27 18:51:02 +08:00
{
if (await new CloseWindowConfirmDialog { Owner = Application.Current.MainWindow }.ShowAndDisableMinimizeToTrayAsync() == ContentDialogResult.Primary)
2020-11-27 18:51:02 +08:00
{
this.CloseConfirmed = true;
_ = this.Dispatcher.BeginInvoke(this.Close, DispatcherPriority.Normal);
return;
2020-11-27 18:51:02 +08:00
}
}
catch (Exception) { }
finally
{
this.CloseWindowSemaphoreSlim.Release();
}
}
2020-11-27 18:51:02 +08:00
}
else
{
2021-01-03 13:21:36 +08:00
SingleInstance.NotificationReceived -= this.SingleInstance_NotificationReceived;
2021-04-20 20:41:26 +08:00
Log.Logger.ForContext<NewMainWindow>().Debug("Window Closing");
2020-12-03 07:01:12 +08:00
NativeBeforeWindowClose?.Invoke(this, EventArgs.Empty);
2020-11-27 18:51:02 +08:00
return;
}
}
2020-11-28 13:02:57 +08:00
#endregion
2020-11-27 18:51:02 +08:00
}
}