Repository navigation
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
329 lines (278 loc) · 10.1 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
329 lines (278 loc) · 10.1 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using LlamaCppControlPanel.Pages;
using LlamaCppControlPanel.Services;
using System.Runtime.InteropServices;
using WinRT.Interop;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace LlamaCppControlPanel;
public sealed partial class MainWindow : Window
{
private readonly nint _windowHandle;
private readonly SubclassProc _subclassProc;
private nint _trayIconHandle;
private bool _trayIconVisible;
private bool _exitRequested;
public MainWindow()
{
InitializeComponent();
_windowHandle = WindowNative.GetWindowHandle(this);
ExtendsContentIntoTitleBar = true;
SetTitleBar(AppTitleBar);
AppWindow.TitleBar.PreferredHeightOption = TitleBarHeightOption.Tall;
AppWindow.SetIcon("Assets/newlogo.ico");
AppWindow.Closing += AppWindow_Closing;
Closed += MainWindow_Closed;
_subclassProc = WindowSubclassProc;
SetWindowSubclass(_windowHandle, _subclassProc, 1, 0);
}
private void AppWindow_Closing(AppWindow sender, AppWindowClosingEventArgs args)
{
if (_exitRequested || !AppServices.Settings.Current.ExitToSystemTray)
{
return;
}
args.Cancel = true;
HideToTray();
}
private void HideToTray()
{
AddTrayIcon();
ShowWindow(_windowHandle, ShowWindowCommand.Hide);
}
private void ShowFromTray()
{
ShowWindow(_windowHandle, ShowWindowCommand.Show);
SetForegroundWindow(_windowHandle);
RemoveTrayIcon();
}
private async Task ExitFromTrayAsync()
{
_exitRequested = true;
RemoveTrayIcon();
await AppServices.Runtime.StopAsync();
Close();
}
private async void MainWindow_Closed(object sender, WindowEventArgs args)
{
RemoveTrayIcon();
RemoveWindowSubclass(_windowHandle, _subclassProc, 1);
await AppServices.Runtime.StopAsync();
}
private nint WindowSubclassProc(nint hWnd, uint message, nint wParam, nint lParam, nuint subclassId, nuint refData)
{
if (message == TrayCallbackMessage)
{
var mouseMessage = lParam.ToInt32();
if (mouseMessage == WindowsMessageLeftButtonDoubleClick)
{
ShowFromTray();
return 0;
}
if (mouseMessage == WindowsMessageRightButtonUp)
{
ShowTrayMenu();
return 0;
}
}
return DefSubclassProc(hWnd, message, wParam, lParam);
}
private void AddTrayIcon()
{
if (_trayIconVisible)
{
return;
}
var iconPath = Path.Combine(AppContext.BaseDirectory, "Assets", "newlogo.ico");
_trayIconHandle = LoadImage(0, iconPath, ImageIcon, 0, 0, LoadImageFromFile | LoadImageDefaultSize);
var data = CreateNotifyIconData();
data.uFlags = NotifyIconFlags.Message | NotifyIconFlags.Icon | NotifyIconFlags.Tip;
data.hIcon = _trayIconHandle;
data.szTip = "llama.cpp Control Panel";
_trayIconVisible = Shell_NotifyIcon(NotifyIconMessage.Add, ref data);
}
private void RemoveTrayIcon()
{
if (_trayIconVisible)
{
var data = CreateNotifyIconData();
Shell_NotifyIcon(NotifyIconMessage.Delete, ref data);
_trayIconVisible = false;
}
if (_trayIconHandle != 0)
{
DestroyIcon(_trayIconHandle);
_trayIconHandle = 0;
}
}
private void ShowTrayMenu()
{
var menu = CreatePopupMenu();
if (menu == 0)
{
return;
}
try
{
AppendMenu(menu, MenuString, TrayCommandShow, "Show");
AppendMenu(menu, MenuString, TrayCommandExit, "Exit");
GetCursorPos(out var point);
SetForegroundWindow(_windowHandle);
var command = TrackPopupMenu(menu, TrackPopupReturnCommand | TrackPopupRightButton, point.X, point.Y, 0, _windowHandle, 0);
if (command == TrayCommandShow)
{
ShowFromTray();
}
else if (command == TrayCommandExit)
{
_ = ExitFromTrayAsync();
}
}
finally
{
DestroyMenu(menu);
}
}
private NotifyIconData CreateNotifyIconData()
{
return new NotifyIconData
{
cbSize = Marshal.SizeOf<NotifyIconData>(),
hWnd = _windowHandle,
uID = 1,
uCallbackMessage = TrayCallbackMessage
};
}
private void TitleBar_PaneToggleRequested(TitleBar sender, object args)
{
NavView.IsPaneOpen = !NavView.IsPaneOpen;
}
private void TitleBar_BackRequested(TitleBar sender, object args)
{
NavFrame.GoBack();
}
private void NavView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
{
if (args.IsSettingsSelected)
{
NavFrame.Navigate(typeof(SettingsPage));
}
else if (args.SelectedItem is NavigationViewItem item)
{
switch (item.Tag)
{
case "home":
NavFrame.Navigate(typeof(HomePage));
break;
case "models":
NavFrame.Navigate(typeof(ModelsPage));
break;
case "hf-library":
NavFrame.Navigate(typeof(HuggingFaceLibraryPage));
break;
case "runtime":
NavFrame.Navigate(typeof(RuntimePage));
break;
case "chat":
NavFrame.Navigate(typeof(ChatPage));
break;
case "monitor":
NavFrame.Navigate(typeof(MonitorPage));
break;
case "logs":
NavFrame.Navigate(typeof(LogsPage));
break;
case "about":
NavFrame.Navigate(typeof(AboutPage));
break;
default:
throw new InvalidOperationException($"Unknown navigation item tag: {item.Tag}");
}
}
}
[DllImport("user32.dll")]
private static extern bool ShowWindow(nint hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(nint hWnd);
[DllImport("comctl32.dll", SetLastError = true)]
private static extern bool SetWindowSubclass(nint hWnd, SubclassProc pfnSubclass, nuint uIdSubclass, nuint dwRefData);
[DllImport("comctl32.dll", SetLastError = true)]
private static extern bool RemoveWindowSubclass(nint hWnd, SubclassProc pfnSubclass, nuint uIdSubclass);
[DllImport("comctl32.dll")]
private static extern nint DefSubclassProc(nint hWnd, uint uMsg, nint wParam, nint lParam);
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
private static extern bool Shell_NotifyIcon(NotifyIconMessage dwMessage, ref NotifyIconData lpData);
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern nint LoadImage(nint hInst, string name, uint type, int cx, int cy, uint fuLoad);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool DestroyIcon(nint hIcon);
[DllImport("user32.dll", SetLastError = true)]
private static extern nint CreatePopupMenu();
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern bool AppendMenu(nint hMenu, uint uFlags, uint uIDNewItem, string lpNewItem);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool DestroyMenu(nint hMenu);
[DllImport("user32.dll", SetLastError = true)]
private static extern uint TrackPopupMenu(nint hMenu, uint uFlags, int x, int y, int nReserved, nint hWnd, nint prcRect);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool GetCursorPos(out Point point);
private delegate nint SubclassProc(nint hWnd, uint uMsg, nint wParam, nint lParam, nuint uIdSubclass, nuint dwRefData);
private static class ShowWindowCommand
{
public const int Hide = 0;
public const int Show = 5;
}
private const uint TrayCallbackMessage = 0x8000 + 1;
private const int WindowsMessageRightButtonUp = 0x0205;
private const int WindowsMessageLeftButtonDoubleClick = 0x0203;
private const uint ImageIcon = 1;
private const uint LoadImageFromFile = 0x00000010;
private const uint LoadImageDefaultSize = 0x00000040;
private const uint MenuString = 0x00000000;
private const uint TrackPopupRightButton = 0x0002;
private const uint TrackPopupReturnCommand = 0x0100;
private const uint TrayCommandShow = 1001;
private const uint TrayCommandExit = 1002;
private enum NotifyIconMessage : uint
{
Add = 0x00000000,
Delete = 0x00000002
}
[Flags]
private enum NotifyIconFlags : uint
{
Message = 0x00000001,
Icon = 0x00000002,
Tip = 0x00000004
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct NotifyIconData
{
public int cbSize;
public nint hWnd;
public uint uID;
public NotifyIconFlags uFlags;
public uint uCallbackMessage;
public nint hIcon;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szTip;
public uint dwState;
public uint dwStateMask;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string szInfo;
public uint uTimeoutOrVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string szInfoTitle;
public uint dwInfoFlags;
public Guid guidItem;
public nint hBalloonIcon;
}
[StructLayout(LayoutKind.Sequential)]
private struct Point
{
public int X;
public int Y;
}
}