2020-11-27 18:51:02 +08:00
|
|
|
using System;
|
|
|
|
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;
|
|
|
|
|
|
|
|
namespace BililiveRecorder.WPF
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Interaction logic for NewMainWindow.xaml
|
|
|
|
/// </summary>
|
|
|
|
public partial class NewMainWindow : Window
|
|
|
|
{
|
|
|
|
public NewMainWindow()
|
|
|
|
{
|
|
|
|
InitializeComponent();
|
|
|
|
|
2020-12-01 08:35:10 +08:00
|
|
|
Title = "B站录播姬 " + BuildInfo.Version + " " + BuildInfo.HeadShaShort;
|
2020-11-27 18:51:02 +08:00
|
|
|
|
2020-12-19 17:27:45 +08:00
|
|
|
SingleInstance.NotificationReceived += SingleInstance_NotificationReceived;
|
2020-11-27 18:51:02 +08:00
|
|
|
}
|
|
|
|
|
2020-12-19 17:27:45 +08:00
|
|
|
private void SingleInstance_NotificationReceived(object sender, EventArgs e) => SuperActivateAction();
|
|
|
|
|
2020-12-03 07:01:12 +08:00
|
|
|
public event EventHandler NativeBeforeWindowClose;
|
2020-11-28 13:02:57 +08:00
|
|
|
|
2020-12-03 07:01:12 +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
|
|
|
{
|
2020-11-28 13:02:57 +08:00
|
|
|
CloseConfirmed = true;
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
internal void SuperActivateAction()
|
|
|
|
{
|
2020-12-19 17:27:45 +08:00
|
|
|
try
|
|
|
|
{
|
|
|
|
Show();
|
|
|
|
WindowState = WindowState.Normal;
|
|
|
|
Topmost = true;
|
|
|
|
Activate();
|
|
|
|
Topmost = false;
|
|
|
|
Focus();
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{ }
|
2020-11-27 18:51:02 +08:00
|
|
|
}
|
|
|
|
|
2020-11-28 13:02:57 +08:00
|
|
|
private void Window_StateChanged(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
if (WindowState == WindowState.Minimized)
|
|
|
|
{
|
|
|
|
Hide();
|
2020-12-01 08:35:10 +08:00
|
|
|
ShowBalloonTipCallback?.Invoke("B站录播姬", "录播姬已最小化到托盘,左键单击图标恢复界面", 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;
|
|
|
|
|
2020-11-27 18:51:02 +08:00
|
|
|
private async void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
|
|
|
|
{
|
|
|
|
if (PromptCloseConfirm && !CloseConfirmed)
|
|
|
|
{
|
|
|
|
e.Cancel = true;
|
|
|
|
if (CloseWindowSemaphoreSlim.Wait(0))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (await new CloseWindowConfirmDialog().ShowAsync() == ContentDialogResult.Primary)
|
|
|
|
{
|
|
|
|
CloseConfirmed = true;
|
|
|
|
CloseWindowSemaphoreSlim.Release();
|
|
|
|
Close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception) { }
|
|
|
|
CloseWindowSemaphoreSlim.Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-19 17:27:45 +08:00
|
|
|
SingleInstance.NotificationReceived -= SingleInstance_NotificationReceived;
|
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
|
|
|
}
|
|
|
|
}
|