Skip to content

Title: fix(win10): tray popup positioning and global hotkey reliability - #31

Closed
xx299x wants to merge 2 commits into
openclaw:masterfrom
xx299x:master
Closed

xx299x wants to merge 2 commits into
openclaw:masterfrom
xx299x:master

Conversation

@xx299x

@xx299x xx299x commented Mar 10, 2026

Copy link
Copy Markdown
Contributor
  • 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

xx299x and others added 2 commits March 10, 2026 23:46
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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 the ManualResetEventSlim instances (_windowReady, _opCompleted), and it calls DestroyWindow(_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_CLOSE that calls DestroyWindow in WndProc) 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.

Comment on lines +1699 to +1701
_quickSendDialog = new QuickSendDialog(_gatewayClient, prefillMessage);
_quickSendDialog.Closed += (s, e) => _quickSendDialog = null;
_quickSendDialog.Activate();

Copilot AI Mar 10, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
_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();

Copilot uses AI. Check for mistakes.
Comment on lines +144 to +145
PostMessage(_hwnd, WM_APP_REGISTER, IntPtr.Zero, IntPtr.Zero);
_opCompleted.Wait(TimeSpan.FromSeconds(2));

Copilot AI Mar 10, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
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;
}

Copilot uses AI. Check for mistakes.
Comment on lines +155 to +166
private void EnsureMessageLoop()
{
if (_messageThread != null) return;

_running = true;
_messageThread = new Thread(MessageLoop)
{
IsBackground = true,
Name = "HotkeyMessageLoop"
};
_messageThread.Start();
}

Copilot AI Mar 10, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 uses AI. Check for mistakes.
@xx299x

xx299x commented Mar 11, 2026

Copy link
Copy Markdown
Contributor Author

@copilot open a new pull request to apply changes based on the comments in this thread

@github-actions github-actions Bot mentioned this pull request Mar 17, 2026
36 tasks
shanselman added a commit that referenced this pull request Mar 17, 2026
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]>
@shanselman

Copy link
Copy Markdown
Collaborator

this is great, thanks! pulling over to #37 and finishing up

@shanselman shanselman closed this Mar 17, 2026
shanselman added a commit that referenced this pull request Mar 17, 2026
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]>
shanselman added a commit that referenced this pull request Mar 17, 2026
* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants