-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrayService.cs
More file actions
254 lines (220 loc) · 9.43 KB
/
Copy pathTrayService.cs
File metadata and controls
254 lines (220 loc) · 9.43 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using System.IO;
using Forms = System.Windows.Forms;
namespace OpenSnap;
/// <summary>
/// Manages the system-tray icon with a right-click context menu,
/// including screenshot history submenu.
/// </summary>
public sealed class TrayService : IDisposable
{
private readonly Forms.NotifyIcon _icon;
private readonly Forms.ContextMenuStrip _menu;
private readonly Forms.ToolStripMenuItem _startupItem;
private readonly Forms.ToolStripMenuItem _historyItem;
private readonly Forms.ToolStripMenuItem _openLastItem;
private readonly Forms.ToolStripMenuItem _copyPathItem;
private readonly Forms.ToolStripMenuItem _revealItem;
public event Action? CaptureRequested;
public event Action? AreaCaptureRequested;
public event Action? ActiveWindowRequested;
public event Action? CaptureOcrRequested;
public event Action? OpenFolderRequested;
public event Action? ChangeFolderRequested;
public event Action<bool>? StartupToggleRequested;
public event Action? OpenLastScreenshotRequested;
public event Action? CopyFilePathRequested;
public event Action? RevealInExplorerRequested;
public event Action<int>? OpenHistoryItemRequested;
public event Action<int>? PinHistoryItemRequested;
public event Action<int>? DeleteHistoryItemRequested;
public event Action? ClearHistoryRequested;
public event Action? SearchHistoryRequested;
public event Action? SettingsRequested;
public event Action? CheckUpdateRequested;
public event Action? QuitRequested;
public TrayService()
{
_icon = new Forms.NotifyIcon
{
Icon = LoadTrayIcon(),
Text = "OpenSnap — Screenshot widget",
Visible = false,
};
_menu = new Forms.ContextMenuStrip();
var captureItem = new Forms.ToolStripMenuItem("Capture");
captureItem.Click += (_, _) => CaptureRequested?.Invoke();
var areaCaptureItem = new Forms.ToolStripMenuItem("Area Capture");
areaCaptureItem.Click += (_, _) => AreaCaptureRequested?.Invoke();
var activeWinItem = new Forms.ToolStripMenuItem("Active Window Capture");
activeWinItem.Click += (_, _) => ActiveWindowRequested?.Invoke();
var captureOcrItem = new Forms.ToolStripMenuItem("Capture + OCR");
captureOcrItem.Click += (_, _) => CaptureOcrRequested?.Invoke();
var settingsItem = new Forms.ToolStripMenuItem("Settings");
settingsItem.Click += (_, _) => SettingsRequested?.Invoke();
var updateItem = new Forms.ToolStripMenuItem("Check for Updates");
updateItem.Click += (_, _) => CheckUpdateRequested?.Invoke();
var openFolderItem = new Forms.ToolStripMenuItem("Open Save Folder");
openFolderItem.Click += (_, _) => OpenFolderRequested?.Invoke();
// History submenu
_historyItem = new Forms.ToolStripMenuItem("Recent Screenshots");
_openLastItem = new Forms.ToolStripMenuItem("Open last screenshot");
_openLastItem.Click += (_, _) => OpenLastScreenshotRequested?.Invoke();
_copyPathItem = new Forms.ToolStripMenuItem("Copy file path");
_copyPathItem.Click += (_, _) => CopyFilePathRequested?.Invoke();
_revealItem = new Forms.ToolStripMenuItem("Reveal in Explorer");
_revealItem.Click += (_, _) => RevealInExplorerRequested?.Invoke();
_startupItem = new Forms.ToolStripMenuItem("Launch at Startup");
_startupItem.Click += (_, _) =>
{
_startupItem.Checked = !_startupItem.Checked;
StartupToggleRequested?.Invoke(_startupItem.Checked);
};
var quitItem = new Forms.ToolStripMenuItem("Quit");
quitItem.Click += (_, _) => QuitRequested?.Invoke();
_menu.Items.AddRange(new Forms.ToolStripItem[]
{
captureItem,
areaCaptureItem,
activeWinItem,
captureOcrItem,
new Forms.ToolStripSeparator(),
settingsItem,
updateItem,
new Forms.ToolStripSeparator(),
openFolderItem,
_historyItem,
new Forms.ToolStripSeparator(),
_startupItem,
new Forms.ToolStripSeparator(),
quitItem,
});
_icon.ContextMenuStrip = _menu;
_icon.DoubleClick += (_, _) => CaptureRequested?.Invoke();
}
public void SetStartupChecked(bool enabled)
{
_startupItem.Checked = enabled;
}
/// <summary>Update the recent-screenshots submenu with the latest entries.</summary>
public void UpdateHistory(List<string> history, List<string>? pinned = null, int maxDisplay = 5)
{
_historyItem.DropDownItems.Clear();
// Pinned section
if (pinned is { Count: > 0 })
{
foreach (var pinPath in pinned)
{
if (!File.Exists(pinPath)) continue;
var pinName = Path.GetFileName(pinPath);
var pinItem = new Forms.ToolStripMenuItem($"📌 {pinName}");
pinItem.Click += (_, _) =>
{
try { System.Diagnostics.Process.Start("explorer.exe", $"\"{pinPath}\""); }
catch { }
};
_historyItem.DropDownItems.Add(pinItem);
}
_historyItem.DropDownItems.Add(new Forms.ToolStripSeparator());
}
// Recent captures
if (history.Count == 0)
{
_historyItem.DropDownItems.Add(new Forms.ToolStripMenuItem("(none)") { Enabled = false });
return;
}
int start = Math.Max(0, history.Count - maxDisplay);
for (int i = start; i < history.Count; i++)
{
var path = history[i];
var fileName = Path.GetFileName(path);
var index = i;
var item = new Forms.ToolStripMenuItem(fileName);
item.Click += (_, _) => OpenHistoryItemRequested?.Invoke(index);
// Pin action
var pinSub = new Forms.ToolStripMenuItem("Pin to top");
pinSub.Click += (_, _) => PinHistoryItemRequested?.Invoke(index);
item.DropDownItems.Add(pinSub);
// Delete action
var delSub = new Forms.ToolStripMenuItem("Delete from history");
delSub.Click += (_, _) => DeleteHistoryItemRequested?.Invoke(index);
item.DropDownItems.Add(delSub);
_historyItem.DropDownItems.Add(item);
}
_historyItem.DropDownItems.Add(new Forms.ToolStripSeparator());
var clearItem = new Forms.ToolStripMenuItem("Clear history");
clearItem.Click += (_, _) => ClearHistoryRequested?.Invoke();
_historyItem.DropDownItems.Add(clearItem);
_historyItem.DropDownItems.Add(new Forms.ToolStripSeparator());
var searchItem = new Forms.ToolStripMenuItem("Search history…");
searchItem.Click += (_, _) => SearchHistoryRequested?.Invoke();
_historyItem.DropDownItems.Add(searchItem);
}
public void SetHistoryActionsEnabled(bool hasHistory)
{
_openLastItem.Enabled = hasHistory;
_revealItem.Enabled = hasHistory;
_copyPathItem.Enabled = hasHistory;
}
public bool IsVisible => _icon.Visible;
public void Show() => _icon.Visible = true;
public void Hide() => _icon.Visible = false;
public void Notify(string title, string text)
{
_icon.ShowBalloonTip(3000, title, text, Forms.ToolTipIcon.Info);
}
/// <summary>Fired when the user clicks an update notification.</summary>
public event Action? UpdateNotificationClicked;
/// <summary>Show a balloon tip that opens a URL or triggers an action when clicked.</summary>
public void NotifyWithLink(string title, string text, string url)
{
EventHandler handler = null!;
handler = (_, _) =>
{
_icon.BalloonTipClicked -= handler;
try
{
if (url == "checkupdate://")
{
UpdateNotificationClicked?.Invoke();
}
else if (url.StartsWith("install://"))
{
var path = url["install://".Length..];
if (File.Exists(path))
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
{
FileName = path,
Arguments = "/VERYSILENT /SUPPRESSMSGBOXES /CURRENTUSER",
UseShellExecute = true,
});
}
else
{
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(url) { UseShellExecute = true });
}
}
catch { /* best-effort */ }
};
_icon.BalloonTipClicked += handler;
_icon.ShowBalloonTip(3000, title, text, Forms.ToolTipIcon.Info);
}
private static System.Drawing.Icon LoadTrayIcon()
{
try
{
var asm = System.Reflection.Assembly.GetExecutingAssembly();
using var stream = asm.GetManifestResourceStream("OpenSnap.Resources.app.ico");
if (stream is not null)
return new System.Drawing.Icon(stream);
}
catch { }
return System.Drawing.Icon.ExtractAssociatedIcon(
System.Diagnostics.Process.GetCurrentProcess().MainModule!.FileName)!;
}
public void Dispose()
{
_icon.Visible = false;
_icon.Dispose();
_menu.Dispose();
}
}