BililiveRecorder/BililiveRecorder.WPF/NewMainWindow.xaml.cs

126 lines
4.2 KiB
C#
Raw 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 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;
this.Close();
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;
2021-01-09 16:25:55 +08:00
2020-11-28 13:02:57 +08:00
private void Window_StateChanged(object sender, EventArgs e)
{
2021-02-27 18:14:44 +08:00
if (this.HideToTray && 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;
2021-05-01 17:57:43 +08:00
#pragma warning disable VSTHRD100 // Avoid async void methods
2020-11-27 18:51:02 +08:00
private async void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
2021-05-01 17:57:43 +08:00
#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;
2021-01-03 13:21:36 +08:00
if (this.CloseWindowSemaphoreSlim.Wait(0))
2020-11-27 18:51:02 +08:00
{
try
{
if (await new CloseWindowConfirmDialog().ShowAsync() == ContentDialogResult.Primary)
{
2021-01-03 13:21:36 +08:00
this.CloseConfirmed = true;
this.CloseWindowSemaphoreSlim.Release();
this.Close();
2020-11-27 18:51:02 +08:00
return;
}
}
catch (Exception) { }
2021-01-03 13:21:36 +08:00
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
}
}