Repository navigation
Conversation
Improve Win10 tray context window positioning and make the Ctrl+Alt+Shift+C Quick Send hotkey reliably register and foreground the dialog.
fix(winui): stabilize tray menu + Win10 global hotkey
There was a problem hiding this comment.
Pull request overview
This PR targets two Windows 10 reliability issues in the WinUI tray app: (1) tray right-click menu placement near the taskbar, and (2) global hotkey activation for the Quick Send window by moving hotkey registration onto the owning message-loop thread and marshalling UI work back to the WinUI UI thread.
Changes:
- Clamp tray menu positioning to the monitor work area using the actual AppWindow size with a DPI-based fallback.
- Rework global hotkey registration/unregistration to happen on the message-loop thread and enqueue Quick Send window creation onto the UI thread.
- Improve Quick Send window activation/foregrounding behavior and keep a strong reference to prevent GC-related disappearance.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/OpenClaw.Tray.WinUI/Windows/TrayMenuWindow.xaml.cs | Updates popup positioning logic to use AppWindow size and clamp to monitor work area. |
| src/OpenClaw.Tray.WinUI/Services/GlobalHotkeyService.cs | Moves hotkey register/unregister to the message-loop thread; adds thread messaging and signaling. |
| src/OpenClaw.Tray.WinUI/Helpers/BackdropHelper.cs | Adjusts acrylic helper signature to explicitly use WinUI Window type. |
| src/OpenClaw.Tray.WinUI/Dialogs/QuickSendDialog.cs | Adds acrylic controller usage and Win32 foreground/topmost promotion on activation. |
| src/OpenClaw.Tray.WinUI/App.xaml.cs | Keeps strong reference to Quick Send dialog and dispatches hotkey handling to UI thread. |
Comments suppressed due to low confidence (1)
src/OpenClaw.Tray.WinUI/Services/GlobalHotkeyService.cs:305
Dispose()never disposes theManualResetEventSliminstances (_windowReady,_opCompleted), and it callsDestroyWindow(_hwnd)from the disposing thread. A safer pattern is to perform window teardown on the message-loop thread (e.g., post a custom message/WM_CLOSEthat callsDestroyWindowinWndProc) and then dispose the wait handles after the thread has exited.
public void Dispose()
{
if (_disposed) return;
_disposed = true;
Unregister();
_running = false;
if (_messageThreadId != 0)
{
// WM_QUIT must be posted to the thread queue.
PostThreadMessage(_messageThreadId, WM_QUIT, IntPtr.Zero, IntPtr.Zero);
}
if (_hwnd != IntPtr.Zero)
{
try { DestroyWindow(_hwnd); } catch { }
_hwnd = IntPtr.Zero;
}
_messageThread?.Join(1000);
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| _quickSendDialog = new QuickSendDialog(_gatewayClient, prefillMessage); | ||
| _quickSendDialog.Closed += (s, e) => _quickSendDialog = null; | ||
| _quickSendDialog.Activate(); |
There was a problem hiding this comment.
The Closed handler unconditionally sets _quickSendDialog = null. If you close an existing dialog and immediately create a new one (the prefillMessage path), the old dialog’s Closed event can fire later and clear the field for the new dialog, making subsequent activations think it’s closed. Capture the new instance in a local variable and only null the field when ReferenceEquals(_quickSendDialog, sender) (or detach the old handler before closing).
| _quickSendDialog = new QuickSendDialog(_gatewayClient, prefillMessage); | |
| _quickSendDialog.Closed += (s, e) => _quickSendDialog = null; | |
| _quickSendDialog.Activate(); | |
| var dialog = new QuickSendDialog(_gatewayClient, prefillMessage); | |
| dialog.Closed += (s, e) => | |
| { | |
| if (ReferenceEquals(_quickSendDialog, dialog)) | |
| { | |
| _quickSendDialog = null; | |
| } | |
| }; | |
| _quickSendDialog = dialog; | |
| dialog.Activate(); |
| PostMessage(_hwnd, WM_APP_REGISTER, IntPtr.Zero, IntPtr.Zero); | ||
| _opCompleted.Wait(TimeSpan.FromSeconds(2)); |
There was a problem hiding this comment.
Register() posts WM_APP_REGISTER and waits, but the return value from _opCompleted.Wait(...) (and from PostMessage) is ignored. If the post fails or the wait times out, Register() may return false even though the hotkey later becomes registered (or vice versa), leaving _registered out of sync with reality. Check PostMessage/Wait results and treat timeouts as failures with a clear log + deterministic state (e.g., retry, or force _registered=false and/or tear down the message loop).
| PostMessage(_hwnd, WM_APP_REGISTER, IntPtr.Zero, IntPtr.Zero); | |
| _opCompleted.Wait(TimeSpan.FromSeconds(2)); | |
| var postSucceeded = PostMessage(_hwnd, WM_APP_REGISTER, IntPtr.Zero, IntPtr.Zero); | |
| if (!postSucceeded) | |
| { | |
| Logger.Warn("Failed to post WM_APP_REGISTER message for hotkey registration"); | |
| _registered = false; | |
| return false; | |
| } | |
| var waitSucceeded = _opCompleted.Wait(TimeSpan.FromSeconds(2)); | |
| if (!waitSucceeded) | |
| { | |
| Logger.Warn("Timed out waiting for hotkey registration operation to complete"); | |
| _registered = false; | |
| return false; | |
| } |
| private void EnsureMessageLoop() | ||
| { | ||
| if (_messageThread != null) return; | ||
|
|
||
| _running = true; | ||
| _messageThread = new Thread(MessageLoop) | ||
| { | ||
| IsBackground = true, | ||
| Name = "HotkeyMessageLoop" | ||
| }; | ||
| _messageThread.Start(); | ||
| } |
There was a problem hiding this comment.
EnsureMessageLoop() only checks _messageThread != null. If the message loop thread exits (e.g., due to an exception) or window creation fails, subsequent Register() calls won’t restart the thread/window and will permanently fail. Consider checking !_messageThread.IsAlive and/or _hwnd == IntPtr.Zero, resetting _messageThread/_windowReady, and recreating the loop so the service can recover.
|
@copilot open a new pull request to apply changes based on the comments in this thread |
Fix 1: QuickSendDialog Closed handler race condition (App.xaml.cs) - Use ReferenceEquals to only null the field when the closing dialog is the current one, preventing a late Closed event from clearing a newly-created replacement dialog. Fix 2: Check PostMessage/Wait results in Register() (GlobalHotkeyService.cs) - Treat PostMessage failure or wait timeout as deterministic failures with clear logging, preventing _registered from being out of sync. Fix 3: EnsureMessageLoop recovery (GlobalHotkeyService.cs) - Check IsAlive and _hwnd, reset state and recreate the thread if the previous message loop died, so the service can recover. Fix 4: Dispose cleanup (GlobalHotkeyService.cs) - Join the thread before DestroyWindow so the loop can exit cleanly. - Dispose ManualResetEventSlim instances to prevent resource leaks. Co-authored-by: xx299x <[email protected]>
|
this is great, thanks! pulling over to #37 and finishing up |
Fix 1: QuickSendDialog Closed handler race condition (App.xaml.cs) - Use ReferenceEquals to only null the field when the closing dialog is the current one, preventing a late Closed event from clearing a newly-created replacement dialog. Fix 2: Check PostMessage/Wait results in Register() (GlobalHotkeyService.cs) - Treat PostMessage failure or wait timeout as deterministic failures with clear logging, preventing _registered from being out of sync. Fix 3: EnsureMessageLoop recovery (GlobalHotkeyService.cs) - Check IsAlive and _hwnd, reset state and recreate the thread if the previous message loop died, so the service can recover. Fix 4: Dispose cleanup (GlobalHotkeyService.cs) - Join the thread before DestroyWindow so the loop can exit cleanly. - Dispose ManualResetEventSlim instances to prevent resource leaks. Co-authored-by: xx299x <[email protected]>
* fix(winui): stabilize tray menu placement and global hotkey Improve Win10 tray context window positioning and make the Ctrl+Alt+Shift+C Quick Send hotkey reliably register and foreground the dialog. * Address Copilot review feedback on PR #31 Fix 1: QuickSendDialog Closed handler race condition (App.xaml.cs) - Use ReferenceEquals to only null the field when the closing dialog is the current one, preventing a late Closed event from clearing a newly-created replacement dialog. Fix 2: Check PostMessage/Wait results in Register() (GlobalHotkeyService.cs) - Treat PostMessage failure or wait timeout as deterministic failures with clear logging, preventing _registered from being out of sync. Fix 3: EnsureMessageLoop recovery (GlobalHotkeyService.cs) - Check IsAlive and _hwnd, reset state and recreate the thread if the previous message loop died, so the service can recover. Fix 4: Dispose cleanup (GlobalHotkeyService.cs) - Join the thread before DestroyWindow so the loop can exit cleanly. - Dispose ManualResetEventSlim instances to prevent resource leaks. Co-authored-by: xx299x <[email protected]> --------- Co-authored-by: xx299x <[email protected]>
Fix Windows 10 tray right-click popup positioning: clamp to the monitor work area so it doesn’t overflow under the taskbar and anchors correctly to the cursor.
Fix Windows 10 global hotkey (Ctrl+Alt+Shift+C) not opening the floating Quick Send window: register hotkey on the message-loop thread, dispatch UI creation to the UI thread, and force the window to foreground/topmost.
Tests: dotnet test -c Debug