-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrayNotificationHelper.cs
More file actions
141 lines (127 loc) · 4.37 KB
/
Copy pathTrayNotificationHelper.cs
File metadata and controls
141 lines (127 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Drawing;
using System.Windows.Forms;
namespace NaturalCommands
{
public static class TrayNotificationHelper
{
private static NotifyIcon? _notifyIcon;
private static Icon? _appIcon;
private static ContextMenuStrip? _contextMenu;
private static void EnsureNotifyIcon()
{
if (_notifyIcon != null) return;
_notifyIcon = new NotifyIcon();
// Use custom icon by default
_appIcon = AppIconGenerator.CreateAppIcon();
_notifyIcon.Icon = _appIcon;
_notifyIcon.Visible = true;
_notifyIcon.Text = "NaturalCommands.NET";
}
public static void InitializeResidentTray(string tooltip, ContextMenuStrip menu, Icon? icon = null)
{
EnsureNotifyIcon();
try
{
_contextMenu = menu;
_notifyIcon!.ContextMenuStrip = _contextMenu;
}
catch { }
try
{
if (icon != null)
{
_appIcon = icon;
_notifyIcon!.Icon = _appIcon;
}
}
catch { }
SetTooltip(tooltip);
}
public static void SetTooltip(string tooltip)
{
EnsureNotifyIcon();
try
{
// NotifyIcon.Text is limited (~63 chars); keep it short.
if (tooltip.Length > 60) tooltip = tooltip.Substring(0, 60);
var ni = _notifyIcon;
if (ni == null) return;
ni.Text = tooltip;
}
catch { }
}
/// <summary>
/// Updates the tray icon to indicate auto-click mode status.
/// </summary>
public static void SetAutoClickActive(bool isActive)
{
EnsureNotifyIcon();
try
{
// Dispose old icon
var oldIcon = _appIcon;
// Create new icon with appropriate color
_appIcon = AppIconGenerator.CreateAppIcon(isActive ? AppIconGenerator.IconState.AutoClickActive : AppIconGenerator.IconState.Normal);
if (_notifyIcon != null)
{
_notifyIcon.Icon = _appIcon;
// Update tooltip to show status
string status = isActive ? "[Auto-Click ON]" : "";
string baseText = "NaturalCommands";
_notifyIcon.Text = string.IsNullOrEmpty(status) ? baseText : $"{baseText} {status}";
}
// Dispose old icon after setting new one
if (oldIcon != null && oldIcon != _appIcon)
{
oldIcon.Dispose();
}
}
catch (Exception ex)
{
// Log but don't crash
System.Diagnostics.Debug.WriteLine($"Failed to update tray icon: {ex.Message}");
}
}
public static void SetStatusIcon(AppIconGenerator.IconState state)
{
EnsureNotifyIcon();
try
{
try { NaturalCommands.Helpers.Logger.LogInfo($"[Tray] SetStatusIcon -> {state}"); } catch { }
var oldIcon = _appIcon;
_appIcon = AppIconGenerator.CreateAppIcon(state);
if (_notifyIcon != null)
{
_notifyIcon.Icon = _appIcon;
}
if (oldIcon != null && oldIcon != _appIcon)
{
oldIcon.Dispose();
}
}
catch { }
}
public static void ShowNotification(string title, string message, int timeout = 5000)
{
EnsureNotifyIcon();
var ni = _notifyIcon;
if (ni == null) return;
ni.BalloonTipTitle = title;
ni.BalloonTipText = message;
ni.BalloonTipIcon = ToolTipIcon.Info;
ni.ShowBalloonTip(timeout);
}
public static void Dispose()
{
if (_notifyIcon != null)
{
_notifyIcon.Visible = false;
_notifyIcon.Dispose();
_notifyIcon = null;
}
try { _contextMenu?.Dispose(); } catch { }
_contextMenu = null;
}
}
}