76 lines
1.9 KiB
C#
76 lines
1.9 KiB
C#
using System.Drawing;
|
|
using System.Windows;
|
|
using Forms = System.Windows.Forms;
|
|
|
|
namespace FoxBrowser.UI;
|
|
|
|
public sealed class TrayIconManager : IDisposable
|
|
{
|
|
private readonly Forms.NotifyIcon _icon;
|
|
private readonly Window _window;
|
|
private readonly Action _exit;
|
|
|
|
public TrayIconManager(Window window, Action exit)
|
|
{
|
|
_window = window;
|
|
_exit = exit;
|
|
|
|
Icon? icon = null;
|
|
try
|
|
{
|
|
var icoPath = System.IO.Path.Combine(AppContext.BaseDirectory, "FoxBrowser.ico");
|
|
if (System.IO.File.Exists(icoPath)) icon = new Icon(icoPath);
|
|
}
|
|
catch { }
|
|
|
|
_icon = new Forms.NotifyIcon
|
|
{
|
|
Icon = icon ?? SystemIcons.Application,
|
|
Text = "FoxBrowser",
|
|
Visible = true,
|
|
ContextMenuStrip = BuildMenu()
|
|
};
|
|
_icon.MouseClick += OnMouseClick;
|
|
}
|
|
|
|
public void MinimizeToTray()
|
|
{
|
|
_window.ShowInTaskbar = false;
|
|
_window.Hide();
|
|
}
|
|
|
|
public void ToggleWindow()
|
|
{
|
|
if (_window.IsVisible && _window.WindowState != WindowState.Minimized)
|
|
{
|
|
MinimizeToTray();
|
|
return;
|
|
}
|
|
|
|
_window.ShowInTaskbar = true;
|
|
_window.Show();
|
|
_window.WindowState = WindowState.Normal;
|
|
_window.Activate();
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_icon.MouseClick -= OnMouseClick;
|
|
_icon.Visible = false;
|
|
_icon.Dispose();
|
|
}
|
|
|
|
private void OnMouseClick(object? sender, Forms.MouseEventArgs e)
|
|
{
|
|
if (e.Button == Forms.MouseButtons.Left) ToggleWindow();
|
|
}
|
|
|
|
private Forms.ContextMenuStrip BuildMenu()
|
|
{
|
|
var menu = new Forms.ContextMenuStrip();
|
|
menu.Items.Add("Показати / сховати", null, (_, _) => ToggleWindow());
|
|
menu.Items.Add("Вийти", null, (_, _) => _exit());
|
|
return menu;
|
|
}
|
|
}
|