-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathTrayMenuWindow.xaml.cs
More file actions
358 lines (301 loc) · 11.5 KB
/
TrayMenuWindow.xaml.cs
File metadata and controls
358 lines (301 loc) · 11.5 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using OpenClaw.Shared;
using OpenClawTray.Helpers;
using System;
using System.Runtime.InteropServices;
using WinUIEx;
namespace OpenClawTray.Windows;
/// <summary>
/// A popup window that displays the tray menu at the cursor position.
/// Uses Win32 to remove title bar (workaround for Bug 57667927).
/// </summary>
public sealed partial class TrayMenuWindow : WindowEx
{
#region Win32 Imports
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetCursorPos(out POINT lpPoint);
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern IntPtr MonitorFromPoint(POINT pt, uint dwFlags);
[DllImport("user32.dll")]
private static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO lpmi);
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
private static extern uint GetDpiForWindow(IntPtr hwnd);
[DllImport("Shcore.dll")]
private static extern int GetDpiForMonitor(IntPtr hmonitor, MonitorDpiType dpiType, out uint dpiX, out uint dpiY);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
private const uint MONITOR_DEFAULTTONEAREST = 2;
private const int GWL_STYLE = -16;
private const int WS_CAPTION = 0x00C00000;
private const int WS_THICKFRAME = 0x00040000;
private const int WS_SYSMENU = 0x00080000;
// SetWindowPos flags
private const uint SWP_NOMOVE = 0x0002;
private const uint SWP_NOSIZE = 0x0001;
private const uint SWP_NOZORDER = 0x0004;
private const uint SWP_FRAMECHANGED = 0x0020;
[StructLayout(LayoutKind.Sequential)]
private struct POINT { public int X; public int Y; }
[StructLayout(LayoutKind.Sequential)]
private struct RECT { public int Left, Top, Right, Bottom; }
[StructLayout(LayoutKind.Sequential)]
private struct MONITORINFO
{
public int cbSize;
public RECT rcMonitor;
public RECT rcWork;
public uint dwFlags;
}
private enum MonitorDpiType
{
MDT_EFFECTIVE_DPI = 0
}
#endregion
public event EventHandler<string>? MenuItemClicked;
private int _menuHeight = 400;
private int _itemCount = 0;
private int _separatorCount = 0;
private int _headerCount = 0;
private bool _styleApplied = false;
public TrayMenuWindow()
{
InitializeComponent();
// Configure as popup-style window
this.IsMaximizable = false;
this.IsMinimizable = false;
this.IsResizable = false;
this.IsAlwaysOnTop = true;
// NOTE: Do NOT set IsTitleBarVisible = false!
// Bug 57667927: causes fail-fast in WndProc during dictionary enumeration.
// We remove the caption via Win32 SetWindowLong instead.
// Hide when focus lost
Activated += OnActivated;
}
private void OnActivated(object sender, WindowActivatedEventArgs args)
{
if (args.WindowActivationState == WindowActivationState.Deactivated)
{
this.Hide();
}
}
public void ShowAtCursor()
{
// Remove title bar via Win32 (once, on first show)
if (!_styleApplied)
{
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
int style = GetWindowLong(hwnd, GWL_STYLE);
style &= ~(WS_CAPTION | WS_THICKFRAME | WS_SYSMENU);
SetWindowLong(hwnd, GWL_STYLE, style);
// Must call SetWindowPos with SWP_FRAMECHANGED to apply the style change
SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
_styleApplied = true;
}
if (GetCursorPos(out POINT pt))
{
// Get work area of monitor where cursor is
var hMonitor = MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST);
var monitorInfo = new MONITORINFO { cbSize = Marshal.SizeOf<MONITORINFO>() };
GetMonitorInfo(hMonitor, ref monitorInfo);
var workArea = monitorInfo.rcWork;
// Prefer using the AppWindow's actual pixel size. This is more reliable than
// estimating based on DPI and item counts, especially on Windows 10 when the
// cursor can be in the taskbar region (outside rcWork).
int menuWidthPx;
int menuHeightPx;
try
{
menuWidthPx = this.AppWindow.Size.Width;
menuHeightPx = this.AppWindow.Size.Height;
}
catch
{
menuWidthPx = 0;
menuHeightPx = 0;
}
// Fallback to a conservative estimate if AppWindow size isn't available yet.
if (menuWidthPx <= 0 || menuHeightPx <= 0)
{
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
uint dpi = GetEffectiveMonitorDpi(hMonitor, hwnd);
double scale = dpi / 96.0;
menuWidthPx = (int)(280 * scale);
menuHeightPx = (int)(_menuHeight * scale);
}
const int margin = 8;
var (x, y) = OpenClaw.Shared.MenuPositioner.CalculatePosition(
pt.X, pt.Y,
menuWidthPx, menuHeightPx,
workArea.Left, workArea.Top, workArea.Right, workArea.Bottom,
margin);
this.Move(x, y);
}
Activate();
SetForegroundWindow(WinRT.Interop.WindowNative.GetWindowHandle(this));
}
public void AddMenuItem(string text, string? icon, string action, bool isEnabled = true, bool indent = false)
{
var content = new TextBlock
{
Text = string.IsNullOrEmpty(icon) ? text : $"{icon} {text}",
TextTrimming = TextTrimming.CharacterEllipsis,
IsTextSelectionEnabled = false
};
var leftPadding = indent ? 28 : 12;
var button = new Button
{
Content = content,
HorizontalAlignment = HorizontalAlignment.Stretch,
HorizontalContentAlignment = HorizontalAlignment.Left,
Padding = new Thickness(leftPadding, 8, 12, 8),
Background = new Microsoft.UI.Xaml.Media.SolidColorBrush(Microsoft.UI.Colors.Transparent),
BorderThickness = new Thickness(0),
IsEnabled = isEnabled,
Tag = action,
CornerRadius = new CornerRadius(4)
};
if (!isEnabled)
content.Opacity = 0.5;
button.Click += (s, e) =>
{
MenuItemClicked?.Invoke(this, action);
this.Hide(); // Hide instead of close - window is reused
};
// Hover effect
button.PointerEntered += (s, e) =>
{
if (button.IsEnabled)
button.Background = (Microsoft.UI.Xaml.Media.Brush)Application.Current.Resources["SubtleFillColorSecondaryBrush"];
};
button.PointerExited += (s, e) =>
{
button.Background = new Microsoft.UI.Xaml.Media.SolidColorBrush(Microsoft.UI.Colors.Transparent);
};
MenuPanel.Children.Add(button);
_itemCount++;
}
public void AddSeparator()
{
MenuPanel.Children.Add(new Border
{
Height = 1,
Margin = new Thickness(8, 6, 8, 6),
Background = (Microsoft.UI.Xaml.Media.Brush)Application.Current.Resources["DividerStrokeColorDefaultBrush"]
});
_separatorCount++;
}
public void AddBrandHeader(string emoji, string text)
{
var panel = new StackPanel
{
Orientation = Orientation.Horizontal,
Padding = new Thickness(12, 12, 12, 8),
Spacing = 8
};
panel.Children.Add(new TextBlock
{
Text = emoji,
FontSize = 28
});
panel.Children.Add(new TextBlock
{
Text = text,
FontSize = 18,
FontWeight = Microsoft.UI.Text.FontWeights.SemiBold,
VerticalAlignment = VerticalAlignment.Center
});
MenuPanel.Children.Add(panel);
_headerCount += 2; // Counts as larger
}
public void AddHeader(string text)
{
MenuPanel.Children.Add(new TextBlock
{
Text = text,
FontWeight = Microsoft.UI.Text.FontWeights.SemiBold,
Padding = new Thickness(12, 10, 12, 4),
Opacity = 0.7
});
_headerCount++;
}
public void ClearItems()
{
MenuPanel.Children.Clear();
_itemCount = 0;
_separatorCount = 0;
_headerCount = 0;
}
/// <summary>
/// Adjusts the window height to fit content and stores it for positioning
/// </summary>
public void SizeToContent()
{
// Calculate height based on item counts
// Menu items: ~36px each (button with padding)
// Separators: ~13px each
// Headers: ~30px each
// Plus padding: ~16px
var contentHeight = (_itemCount * 36) + (_separatorCount * 13) + (_headerCount * 30) + 16;
_menuHeight = Math.Max(contentHeight, 100); // minimum
if (TryGetCurrentMonitorMetrics(out var workAreaHeightPx, out var dpi))
{
// Constrain the popup to the visible work area so the ScrollViewer gets
// a viewport and the menu stays reachable near the tray/taskbar.
var workAreaHeight = MenuSizingHelper.ConvertPixelsToViewUnits(workAreaHeightPx, dpi);
_menuHeight = MenuSizingHelper.CalculateWindowHeight(contentHeight, workAreaHeight);
}
this.SetWindowSize(280, _menuHeight);
}
private bool TryGetCurrentMonitorMetrics(out int workAreaHeight, out uint dpi)
{
workAreaHeight = 0;
dpi = 96;
if (!GetCursorPos(out POINT pt))
return false;
var hMonitor = MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST);
if (hMonitor == IntPtr.Zero)
return false;
var monitorInfo = new MONITORINFO { cbSize = Marshal.SizeOf<MONITORINFO>() };
if (!GetMonitorInfo(hMonitor, ref monitorInfo))
return false;
workAreaHeight = monitorInfo.rcWork.Bottom - monitorInfo.rcWork.Top;
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
dpi = GetEffectiveMonitorDpi(hMonitor, hwnd);
return workAreaHeight > 0;
}
private static uint GetEffectiveMonitorDpi(IntPtr hMonitor, IntPtr hwnd)
{
if (hMonitor != IntPtr.Zero)
{
try
{
var hr = GetDpiForMonitor(hMonitor, MonitorDpiType.MDT_EFFECTIVE_DPI, out var dpiX, out var dpiY);
if (hr == 0)
{
if (dpiY != 0)
return dpiY;
if (dpiX != 0)
return dpiX;
}
}
catch (DllNotFoundException)
{
}
catch (EntryPointNotFoundException)
{
}
}
var dpi = hwnd != IntPtr.Zero ? GetDpiForWindow(hwnd) : 0;
return dpi == 0 ? 96u : dpi;
}
}