-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrayIconService.cs
More file actions
97 lines (77 loc) · 2.45 KB
/
Copy pathTrayIconService.cs
File metadata and controls
97 lines (77 loc) · 2.45 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
using System.Drawing;
using System.Windows.Input;
using H.NotifyIcon;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
namespace CodingWithCalvin.VSToolbox.Services;
public sealed class TrayIconService : IDisposable
{
private TaskbarIcon? _taskbarIcon;
private Window? _window;
public void Initialize(Window window)
{
_window = window;
_taskbarIcon = new TaskbarIcon
{
ToolTipText = "Visual Studio Toolbox",
ContextMenuMode = ContextMenuMode.SecondWindow,
Icon = GetAppIcon()
};
// Set up context menu
var contextMenu = new MenuFlyout();
var showItem = new MenuFlyoutItem { Text = "Show" };
showItem.Click += (_, _) => ShowWindow();
contextMenu.Items.Add(showItem);
contextMenu.Items.Add(new MenuFlyoutSeparator());
var exitItem = new MenuFlyoutItem { Text = "Exit" };
exitItem.Click += (_, _) => ExitApplication();
contextMenu.Items.Add(exitItem);
_taskbarIcon.ContextFlyout = contextMenu;
// Handle tray icon click
_taskbarIcon.LeftClickCommand = new SimpleCommand(ShowWindow);
// Create and set the icon
_taskbarIcon.ForceCreate();
}
private static Icon? GetAppIcon()
{
// Try to load the VS icon from Assets
var appDir = AppContext.BaseDirectory;
var iconPath = Path.Combine(appDir, "Assets", "vs2026_icon.png");
if (File.Exists(iconPath))
{
try
{
using var bitmap = new Bitmap(iconPath);
var hIcon = bitmap.GetHicon();
return Icon.FromHandle(hIcon);
}
catch
{
// Fall back to default
}
}
return SystemIcons.Application;
}
public void ShowWindow()
{
_window?.Activate();
}
private void ExitApplication()
{
Dispose();
Application.Current.Exit();
}
public void Dispose()
{
_taskbarIcon?.Dispose();
_taskbarIcon = null;
}
private sealed class SimpleCommand(Action execute) : ICommand
{
#pragma warning disable CS0067 // Event is never used - required by ICommand interface
public event EventHandler? CanExecuteChanged;
#pragma warning restore CS0067
public bool CanExecute(object? parameter) => true;
public void Execute(object? parameter) => execute();
}
}