Skip to content

Add modern window context menu - #20673

Open
NNN (hellodda) wants to merge 2 commits into
microsoft:mainfrom
hellodda:window-context-menu
Open

Add modern window context menu#20673
NNN (hellodda) wants to merge 2 commits into
microsoft:mainfrom
hellodda:window-context-menu

Conversation

@hellodda

Copy link
Copy Markdown

Summary of the Pull Request

Replaces the legacy Win32 system menu with a modern XAML-based WindowContextMenu implementation (MenuFlyout). This enables the title bar context menu to fully adapt to the application's XAML theme (Dark/Light mode) while maintaining full backward compatibility with native system commands.

References and Relevant Issues

Detailed Description of the Pull Request / Additional comments

Key Implementation Details:

Screenshot 2026-09-13 211206 Screenshot 2026-09-13 211113
  • XAML MenuFlyout Integration: Created WindowContextMenu inheriting from MenuFlyout to replace the legacy Win32 popup menu with modern UI/UX matching the application theme.
  • Win32 Message Hooking via Subclassing: Attached a window subclass procedure (SetWindowSubclass) to intercept WM_CONTEXTMENU, WM_NCRBUTTONUP, WM_NCRBUTTONDOWN, WM_STYLECHANGED, and WM_SIZE messages.
  • DPI & Coordinate Calculation: Converts screen coordinates from Win32 physical pixels to DPI-aware logical coordinates using GetDpiForWindow. Handles keyboard invocation (Shift+F10 / Apps key with (X: -1, Y: -1) coordinates) by automatically centering the menu relative to the parent window.
  • Win32 Localization & Fallback: Queries localized strings dynamically from GetSystemMenu via GetMenuItemInfoW and strips accelerator keys (& escaping and \t shortcut strings) so text renders cleanly in XAML.
  • Dynamic Menu Item Extensions: Detects and appends custom menu items inserted into HMENU by third-party window managers or extensions.
  • State Management & Command Routing: Automatically toggles item enable/visibility states based on window style (WS_THICKFRAME) and state (SIZE_MAXIMIZED). Maps click events back to native WM_SYSCOMMAND messages (SC_RESTORE, SC_MOVE, SC_SIZE, SC_MINIMIZE, SC_MAXIMIZE, SC_CLOSE).

Validation Steps Performed

  1. Theme Adaptation: Verified that right-clicking the window caption/titlebar renders a XAML context menu using the active application theme (Dark and Light modes).
  2. System Commands: Verified that clicking Restore, Move, Size, Minimize, Maximize, and Close correctly dispatches WM_SYSCOMMAND to the host window.
  3. DPI Scaling: Tested menu popup positioning across high-DPI displays (125%, 150%, 200%) and multi-monitor setups.
  4. Keyboard Invocation: Pressed Shift+F10 and the Apps key to verify the menu centers correctly on screen without crashing or rendering off-screen.
  5. Window State Sync: Verified menu items automatically enable/disable when switching between windowed and maximized states.

PR Checklist

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Comment thread src/cascadia/TerminalApp/WindowContextMenu.xaml.cpp Fixed
Comment thread src/cascadia/TerminalApp/WindowContextMenu.xaml.cpp Fixed
Comment thread src/cascadia/TerminalApp/WindowContextMenu.xaml.cpp Fixed
Comment thread src/cascadia/TerminalApp/WindowContextMenu.xaml.cpp Fixed
Comment thread src/cascadia/TerminalApp/WindowContextMenu.xaml.cpp Fixed
@github-actions

This comment has been minimized.

@hellodda

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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.

2 participants