Files
Nebula-Auth/NebulaAuth/Core/TrayManager.cs
T
Давид Чернопятов 1e65cd4a06 1.5.3 progress. Added multi-confirmation and code refactorings
NebulaAuth:
- Added MAAC and PortableMaClient for multi-confirmations
- Code clean-ups, removed unused classes, refactoring of old files
- SteamLib updated
- Localization and UI updates
- Dependcies updated to the last version

LegacyConverter:
 - Added mode to decrypt mafiles
2024-10-16 16:31:53 +03:00

83 lines
2.2 KiB
C#

using NebulaAuth.Model;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Windows;
using System.Windows.Forms;
using Application = System.Windows.Application;
namespace NebulaAuth.Core;
public static class TrayManager
{
private static NotifyIcon? _notifyIcon;
private static readonly Window MainWindow = Application.Current.MainWindow!;
private static bool IsEnabled => Settings.Instance.HideToTray;
[MemberNotNullWhen(true, nameof(_notifyIcon))]
private static bool Init { get; set; }
public static void InitializeTray()
{
_notifyIcon = new NotifyIcon();
_notifyIcon.Text = "NebulaAuth";
var iconStream = Application.GetResourceStream(new Uri("pack://application:,,,/Theme/lock.ico"))!.Stream;
_notifyIcon.Icon = new Icon(iconStream);
_notifyIcon.MouseDoubleClick += NotifyIcon_MouseDoubleClick;
var contextMenu = new ContextMenuStrip();
contextMenu.Items.Add("Выйти", null!, onClick: OnExitClick);
_notifyIcon.ContextMenuStrip = contextMenu;
MainWindow.StateChanged += MainWindow_StateChanged;
Init = true;
}
private static void OnExitClick(object? sender, EventArgs e)
{
ExitApplication();
}
private static void NotifyIcon_MouseDoubleClick(object? sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ShowMainWindow();
}
}
private static void MainWindow_StateChanged(object? sender, EventArgs e)
{
if (MainWindow.WindowState == WindowState.Minimized)
{
HideMainWindow();
}
}
private static void ShowMainWindow()
{
if (!Init) return;
MainWindow.Show();
MainWindow.WindowState = WindowState.Normal;
_notifyIcon.Visible = false;
}
private static void HideMainWindow()
{
if (!Init) return;
if (IsEnabled == false) return;
_notifyIcon.Visible = true;
MainWindow.Hide();
}
private static void ExitApplication()
{
if (!Init) return;
_notifyIcon.Dispose();
Application.Current.Shutdown();
}
}