Files
WorkIndicator/NotificationIcon.cs
ckaczor 4b9145054b Start modernization
# Conflicts:
#	.gitignore
#	App.xaml
#	App.xaml.cs
#	AudioWatcher.cs
#	Delcom/DeviceManagement.cs
#	Delcom/StoplightIndicator.cs
#	LICENSE.md
#	LightController.cs
#	Properties/AssemblyInfo.cs
#	Properties/Resources.Designer.cs
#	Properties/Resources.resx
#	Properties/Settings.Designer.cs
#	Properties/Settings.settings
#	README.md
#	UpdateCheck.cs
#	WorkIndicator.csproj
#	WorkIndicator.sln
#	WorkIndicator.sln.DotSettings
2026-08-07 10:46:25 -04:00

107 lines
3.3 KiB
C#

using ChrisKaczor.Wpf.Windows;
using H.NotifyIcon;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using WorkIndicator.Properties;
using WorkIndicator.SettingsWindow;
namespace WorkIndicator;
internal static class NotificationIcon
{
private static TaskbarIcon _notificationIcon;
private static readonly Dictionary<Status, MenuItem> StatusMenuItems = new();
public static void Initialize()
{
// Create the notification icon
_notificationIcon = new TaskbarIcon { Icon = Resources.ApplicationIcon };
// Set up the menu
var contextMenu = new ContextMenu();
contextMenu.Opened += HandleContextMenuOpening;
AddStatusMenuItem(contextMenu, Resources.StatusAuto, Status.Auto);
AddStatusMenuItem(contextMenu, Resources.StatusFree, Status.Free);
AddStatusMenuItem(contextMenu, Resources.StatusWorking, Status.Working);
AddStatusMenuItem(contextMenu, Resources.StatusOnPhone, Status.OnPhone);
AddStatusMenuItem(contextMenu, Resources.StatusTalking, Status.Talking);
contextMenu.Items.Add(new Separator());
var menuItem = new MenuItem
{
Header = Resources.NotificationIconContextMenuSettings
};
menuItem.Click += ShowSettings;
contextMenu.Items.Add(menuItem);
contextMenu.Items.Add(new Separator());
menuItem = new MenuItem
{
Header = Resources.NotificationIconContextMenuExit
};
menuItem.Click += HandleContextMenuExitClick;
contextMenu.Items.Add(menuItem);
// Set the menu into the icon
_notificationIcon.ContextMenu = contextMenu;
// Show the icon
_notificationIcon.ForceCreate(false);
}
private static void ShowSettings(object sender, RoutedEventArgs e)
{
var categoryPanels = new List<CategoryPanelBase>
{
new GeneralSettingsPanel(),
new UpdateSettingsPanel(),
new AboutSettingsPanel()
};
var settingsWindow = new CategoryWindow(categoryPanels, Resources.SettingsTitle, Resources.CloseButtonText);
settingsWindow.ShowDialog();
}
private static void HandleContextMenuOpening(object sender, RoutedEventArgs e)
{
foreach (var menuItem in StatusMenuItems)
{
menuItem.Value.IsChecked = menuItem.Key == LightController.Status;
}
}
private static void HandleContextMenuExitClick(object sender, System.EventArgs e)
{
Application.Current.Shutdown();
}
public static void Dispose()
{
// Get rid of the icon
_notificationIcon?.Dispose();
_notificationIcon = null;
}
public static void ShowBalloonTip(string text, H.NotifyIcon.Core.NotificationIcon icon)
{
_notificationIcon.ShowNotification(Resources.ApplicationName, text, icon);
}
private static void AddStatusMenuItem(ContextMenu contextMenu, string text, Status status)
{
var menuItem = new MenuItem
{
Header = text,
IsCheckable = true,
IsChecked = status == LightController.Status
};
menuItem.Click += (_, _) => LightController.Status = status;
contextMenu.Items.Add(menuItem);
StatusMenuItems[status] = menuItem;
}
}