120 lines
3.4 KiB
C#
120 lines
3.4 KiB
C#
using System;
|
||
using System.IO;
|
||
using Microsoft.UI.Xaml;
|
||
using Microsoft.UI.Xaml.Controls;
|
||
using Microsoft.UI.Xaml.Media.Imaging;
|
||
using EveryPublish.Client.Pages;
|
||
using EveryPublish.Client.Services;
|
||
using H.NotifyIcon;
|
||
|
||
namespace EveryPublish.Client;
|
||
|
||
public sealed partial class MainWindow : Window
|
||
{
|
||
private readonly string _dataDir = Path.Combine(
|
||
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
|
||
"EveryPublish");
|
||
private readonly CoreClient _core;
|
||
private readonly Watchdog _watchdog;
|
||
private readonly ClientContext _context;
|
||
private TaskbarIcon? _tray;
|
||
private bool _exiting;
|
||
|
||
/// <summary>供设置页「退出客户端」调用</summary>
|
||
public static Action? ExitAction;
|
||
|
||
public MainWindow()
|
||
{
|
||
InitializeComponent();
|
||
Directory.CreateDirectory(_dataDir);
|
||
_core = new CoreClient(_dataDir);
|
||
_watchdog = new Watchdog(_core, _dataDir, OnCoreFatal);
|
||
_context = new ClientContext(_core, _dataDir, _watchdog);
|
||
ExitAction = ExitApp;
|
||
AppWindow.Closing += OnClosing;
|
||
InitTray();
|
||
NavView.SelectedItem = NavView.MenuItems[0];
|
||
Navigate("status");
|
||
_watchdog.Start();
|
||
}
|
||
|
||
private void OnCoreFatal()
|
||
{
|
||
DispatcherQueue.TryEnqueue(async () =>
|
||
{
|
||
var dialog = new ContentDialog
|
||
{
|
||
Title = "核心进程异常",
|
||
Content = "agent-core 连续重启失败,请检查安装目录或重新安装。",
|
||
CloseButtonText = "知道了",
|
||
XamlRoot = Content.XamlRoot,
|
||
};
|
||
await dialog.ShowAsync();
|
||
});
|
||
}
|
||
|
||
private void InitTray()
|
||
{
|
||
try
|
||
{
|
||
_tray = new TaskbarIcon { ToolTipText = "EveryPublish 客户端" };
|
||
var iconPath = Path.Combine(AppContext.BaseDirectory, "Assets", "app.ico");
|
||
if (File.Exists(iconPath))
|
||
{
|
||
_tray.IconSource = new BitmapImage(new Uri(iconPath));
|
||
}
|
||
// H.NotifyIcon 2.x:左/右键命令打开主窗口(退出入口在设置页)
|
||
_tray.LeftClickCommand = new SimpleCommand(ShowWindow);
|
||
_tray.RightClickCommand = new SimpleCommand(ShowWindow);
|
||
}
|
||
catch
|
||
{
|
||
// 托盘不可用不影响主功能
|
||
}
|
||
}
|
||
|
||
private void ShowWindow()
|
||
{
|
||
AppWindow.Show();
|
||
Activate();
|
||
}
|
||
|
||
private void OnClosing(AppWindow sender, AppWindowClosingEventArgs args)
|
||
{
|
||
if (!_exiting)
|
||
{
|
||
args.Cancel = true;
|
||
AppWindow.Hide(); // 隐藏到托盘(WinUI 3 无 Window.Hide,用 AppWindow.Hide)
|
||
}
|
||
}
|
||
|
||
private void ExitApp()
|
||
{
|
||
_exiting = true;
|
||
_tray?.Dispose();
|
||
_watchdog.Dispose();
|
||
_core.Dispose();
|
||
Close();
|
||
}
|
||
|
||
private void NavView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
|
||
{
|
||
if (args.SelectedItem is NavigationViewItem item && item.Tag is string tag)
|
||
{
|
||
Navigate(tag);
|
||
}
|
||
}
|
||
|
||
private void Navigate(string tag)
|
||
{
|
||
Type page = tag switch
|
||
{
|
||
"pair" => typeof(PairPage),
|
||
"challenges" => typeof(ChallengesPage),
|
||
"settings" => typeof(SettingsPage),
|
||
_ => typeof(StatusPage),
|
||
};
|
||
ContentFrame.Navigate(page, _context);
|
||
}
|
||
}
|