90 lines
2.4 KiB
C#
90 lines
2.4 KiB
C#
using H.NotifyIcon;
|
|||
|
|
using Microsoft.UI.Xaml;
|
||
|
|
using Microsoft.UI.Xaml.Controls;
|
||
|
|
using Microsoft.UI.Xaml.Media.Imaging;
|
||
|
|
using System;
|
||
|
|
using System.IO;
|
||
|
|
using System.Reflection;
|
||
|
|
|
||
|
|
namespace HearingProtector;
|
||
|
|
|
||
|
|
public partial class App : Application
|
||
|
|
{
|
||
|
|
// ← ДОБАВИТЬ: ссылка на единственный экземпляр App
|
||
|
|
public static App? CurrentApp { get; private set; }
|
||
|
|
|
||
|
|
public Window? MainWindow { get; private set; }
|
||
|
|
|
||
|
|
private TaskbarIcon? _trayIcon;
|
||
|
|
|
||
|
|
public App()
|
||
|
|
{
|
||
|
|
CurrentApp = this; // ← ДОБАВИТЬ до InitializeComponent()
|
||
|
|
InitializeComponent();
|
||
|
|
}
|
||
|
|
|
||
|
|
public static string Version { get; private set; } = "?";
|
||
|
|
|
||
|
|
protected override void OnLaunched(LaunchActivatedEventArgs args)
|
||
|
|
{
|
||
|
|
Version = typeof(App).Assembly
|
||
|
|
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?
|
||
|
|
.InformationalVersion ?? "?";
|
||
|
|
|
||
|
|
MainWindow = new MainWindow();
|
||
|
|
|
||
|
|
var openItem = new MenuFlyoutItem
|
||
|
|
{
|
||
|
|
Text = "Открыть",
|
||
|
|
Command = new RelayCommand(OpenFromTray)
|
||
|
|
};
|
||
|
|
|
||
|
|
var exitItem = new MenuFlyoutItem
|
||
|
|
{
|
||
|
|
Text = "Выход",
|
||
|
|
Command = new RelayCommand(ExitApp)
|
||
|
|
};
|
||
|
|
|
||
|
|
var menu = new MenuFlyout();
|
||
|
|
menu.Items.Add(openItem);
|
||
|
|
menu.Items.Add(new MenuFlyoutSeparator());
|
||
|
|
menu.Items.Add(exitItem);
|
||
|
|
|
||
|
|
_trayIcon = new TaskbarIcon
|
||
|
|
{
|
||
|
|
ToolTipText = $"Hearing Protector v{Version}",
|
||
|
|
IconSource = new BitmapImage(new Uri(
|
||
|
|
Path.Combine(AppContext.BaseDirectory, "Assets", "app.ico"))),
|
||
|
|
ContextFlyout = menu,
|
||
|
|
MenuActivation = H.NotifyIcon.Core.PopupActivationMode.RightClick,
|
||
|
|
NoLeftClickDelay = true,
|
||
|
|
DoubleClickCommand = new RelayCommand(OpenFromTray)
|
||
|
|
};
|
||
|
|
|
||
|
|
_trayIcon.ForceCreate();
|
||
|
|
|
||
|
|
MainWindow.Activate();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OpenFromTray()
|
||
|
|
{
|
||
|
|
MainWindow?.DispatcherQueue.TryEnqueue(() =>
|
||
|
|
{
|
||
|
|
(MainWindow as MainWindow)?.RestoreFromTray();
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
private void ExitApp()
|
||
|
|
{
|
||
|
|
MainWindow?.DispatcherQueue.TryEnqueue(() =>
|
||
|
|
{
|
||
|
|
if (MainWindow is MainWindow mw)
|
||
|
|
mw.ReadyToClose = true;
|
||
|
|
|
||
|
|
_trayIcon?.Dispose();
|
||
|
|
_trayIcon = null;
|
||
|
|
|
||
|
|
Exit();
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|