Skip to content

feat(desktop): add a reusable context menu provider - #182

Merged
dailker merged 1 commit into
codehubbers:mainfrom
jeffersongoncalves:feat/context-menu-provider
Dec 4, 2026
Merged

dailker merged 1 commit into
codehubbers:mainfrom
jeffersongoncalves:feat/context-menu-provider

Conversation

@jeffersongoncalves

Copy link
Copy Markdown
Contributor

Refs #94 (first of two PRs: provider + desktop menu here; the app icon and File Manager menus follow in a second PR, after #180 lands since it also touches AppIcon.js).

What and why

A reusable context menu service, as the issue describes, with the existing desktop menu from #93 as its first consumer instead of a second menu.

  • ContextMenuProvider + useContextMenu() (src/context/ContextMenuContext.js): openMenu(event, items, { label }) prevents the browser menu, stops the event so the innermost element wins (an icon over the desktop, later), and renders the menu in a portal on document.body. Opened from the keyboard (Shift+F10 / the menu key, which report no pointer position), it opens under the element.
  • ContextMenu (src/components/ContextMenu.js, renamed from DesktopContextMenu.js): items are { label, onClick, icon?, shortcut?, disabled? } or { separator: true }.
    • Measured before paint, then flipped to the other side of the cursor near the right/bottom edges and clamped so it never leaves the screen (placeMenu, unit tested).
    • Closes on Esc, Tab, outside click, scroll, window blur and resize. Esc/Tab give focus back to where it was.
    • role="menu" with an aria-label, role="menuitem" buttons, role="separator"; ↑ ↓ wrap and skip disabled items, Home/End jump; Enter/Space activate.
    • Theme-aware: theme.app.dropdown_bg, dropdown_item_hover, plus a new dropdown_item_focus key in all six themes (the hover colours with focus:), so the keyboard highlight matches the mouse one. Hovering an item also focuses it, so there's only ever one highlight. Themes without the new key still work.
  • Desktop menu: Sort by name and Auto-arrange icons keep working (same useDesktopLayout actions, tests/desktopLayout.test.js untouched and passing), plus Next wallpaper (same cycling as the launcher command) and Settings. New keys are in all five locales.

Left out for now, to keep this reviewable: submenus (and ← →), since no menu here needs one yet. The item shape leaves room for submenu when the File Manager's "Open with…" needs it.

How I tested it

  • tests/ContextMenu.test.js (12 tests): edge flip and clamping, browser menu prevented, label and theme classes, arrow/Home/End navigation skipping disabled items and separators, item runs and closes, disabled items don't run, Esc closes and restores focus, outside click / scroll / resize close, clicks inside keep it open, innermost element wins, clear error outside the provider.
  • npm test (36 suites, 430 tests), npm run lint (0 errors, only the existing warnings), npx prettier --check on the changed files and npm run build all pass.
  • By hand in Chromium (npm run dev, Local Admin, 1280×800): the steps below. Doing this caught one thing jsdom didn't: the menu is hidden while it's measured, and a hidden element can't take focus, so focus now moves to the first item once it's placed, and the keys are read on the window.

Keyboard-only steps

  1. Right-click an empty part of the desktop: the menu opens with Sort by name focused.
  2. ↓ / ↑ move through the items (wrapping, skipping the separator); Home / End jump to the first / last.
  3. Enter runs the focused item and closes the menu (e.g. Next wallpaper changes the background).
  4. Esc or Tab closes it without running anything.

Screenshots

Desktop menu, keyboard focus on "Next wallpaper" (same highlight as hover):

Desktop context menu with keyboard focus

Right-click near the bottom-right corner: the menu flips up and left to stay on screen:

Context menu flipped near the screen edge

After End, ↑, Enter on "Next wallpaper":

Wallpaper changed from the menu

Checklist

  • I read CONTRIBUTING.md
  • Commits follow type(scope): summary, for example fix(calculator): keep history after reload
  • Tests added or updated, and npm test passes
  • npx prettier --check passes on the files I changed
  • Screenshots attached for UI changes
  • CI is green

ContextMenuProvider and useContextMenu() let any component open a
right-click menu: rendered in a portal, flipped and clamped to stay on
screen, closed on Esc, Tab, outside click, scroll, blur and resize, and
navigable with the arrow keys, Home and End. Items take a label, icon,
shortcut, disabled flag or a separator, styled with the theme's dropdown
keys plus a new dropdown_item_focus key for the keyboard highlight.

The desktop menu from codehubbers#93 becomes its first consumer and gains Next
wallpaper and Settings.

Refs codehubbers#94

@Ziqian-Huang0607 Ziqian-Huang0607 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is the right way to do #94: the service first, with the existing desktop menu as its first consumer rather than a parallel implementation.

What I verified:

  • placeMenu flips and clamps correctly, and the unit tests cover the edge cases (right/bottom flip, clamping).
  • The keyboard story is complete: Shift+F10 opens under the element when there is no pointer position, arrows wrap and skip disabled items and separators, Home/End jump, Esc/Tab close and restore focus. The screenshots show the edge flip working in a real browser.
  • The measure-then-show pattern (hidden while measuring, focus after placement) is the correct fix for the focus-on-hidden-element trap, and the PR body is honest about catching it by hand outside jsdom.
  • Theme integration is done properly: dropdown_item_focus added to all six themes, with a graceful fallback for themes without it, and the ThemeRegistry key-parity test passes.
  • The desktop menu keeps its #93 behavior (desktopLayout tests untouched and green) and the new wallpaper/settings items reuse the same actions as the launcher.
  • Ran locally on current main: ContextMenu, desktopLayout, i18n parity and ThemeRegistry suites, 76/76 green. CI green.

Good call splitting icon and File Manager menus into a second PR after #180, since both touch AppIcon.js.

Approve.

@dailker dailker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Really solid first step for #94. The edge flip, focus handling and the extra theme key are all well done, and good catch on the hidden-while-measuring focus problem. One small thing for the follow-up PR: menu items use key={item.label}, so two items with the same label would collide. An index or an id would be safer. Go ahead with the app icon and File Manager menus now that #180 is in.

@dailker
dailker merged commit 20cdd18 into codehubbers:main Dec 4, 2026
5 checks passed
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