Skip to content

refactor: move Menu.buildFromTemplate's ordering to C++ - #53875

Merged
MarshallOfSound merged 1 commit into
perf/menu-2-rolesfrom
perf/menu-3-template
Sep 14, 2026
Merged

MarshallOfSound merged 1 commit into
perf/menu-2-rolesfrom
perf/menu-3-template

Conversation

@MarshallOfSound

@MarshallOfSound MarshallOfSound commented Sep 13, 2026 •

Copy link
Copy Markdown
Member

Description of Change

Moves Menu.buildFromTemplate()'s before / after / beforeGroupContaining / afterGroupContaining ordering and separator folding from lib/browser/api/menu-utils.ts to shell/browser/api/electron_api_menu_template.cc. Same semantics (ids compared with ===, Proxy-wrapped and sparse templates accepted); the sort is iterative.

Perf: neutral on its own (Release, Linux x64: within noise on all menu benchmarks). Groundwork for the native buildFromTemplate.

Checklist

Release Notes

Notes: none

Comment thread shell/browser/api/electron_api_menu_template.cc
The before/after/beforeGroupContaining/afterGroupContaining ordering and
the separator clean-up move from lib/browser/api/menu-utils.ts to
shell/browser/api/electron_api_menu_template.cc. Ids are still compared
with ===, and Proxy-wrapped and sparse template arrays are still accepted.
@MarshallOfSound MarshallOfSound added backport-check-skip Skip trop's backport validity checking and removed backport-check-skip Skip trop's backport validity checking labels Sep 14, 2026
@MarshallOfSound
MarshallOfSound merged commit 473519e into main Sep 14, 2026
95 checks passed
@release-clerk

release-clerk Bot commented Sep 14, 2026

Copy link
Copy Markdown

No Release Notes

@MarshallOfSound
MarshallOfSound deleted the perf/menu-3-template branch September 14, 2026 16:57
@trop

trop Bot commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

I was unable to backport this PR to "45-x-y" cleanly;
you will need to perform this backport manually.

@trop trop Bot added needs-manual-bp/45-x-y and removed target/45-x-y PR should also be added to the "45-x-y" branch. labels Sep 14, 2026
MarshallOfSound added a commit that referenced this pull request Sep 14, 2026
* refactor: add gin_helper and BaseWindow/WebContents helpers for native menus (#53873)

- gin_helper::ConvertedValue<T>: a property store for cppgc wrappables
  that keeps the JS value a script assigned (for identity on read) together
  with its native conversion, done once at assignment.
- gin_helper::Constructible: an optional FillInstanceTemplate() hook for
  own (instance) properties.
- gin_helper::TrackableObject::GetAllNative(): the tracked instances
  without going through their wrappers.
- BaseWindow::GetFocusedWindow() (now also backing
  BaseWindow.getFocusedWindow()), FromValue(), IsLive(), SetMenuNatively().
- WebContents::GetFocusedWebContents() and session().

(cherry picked from commit 1fd543f)

* refactor: move the MenuItem role table to C++ (#53874)

The built-in roles (default label and accelerator, registerAccelerator,
the computed checked state for toggleSpellChecker, the default submenus
and what each role does when activated) move from
lib/browser/api/menu-item-roles.ts to
shell/browser/api/electron_api_menu_roles.{h,cc}; roles now act directly
on api::WebContents / NativeWindow / Browser / prefs. menu-item.ts reads
the table through the menu binding.

(cherry picked from commit 75bcb10)

* refactor: move Menu.buildFromTemplate's ordering to C++ (#53875)

The before/after/beforeGroupContaining/afterGroupContaining ordering and
the separator clean-up move from lib/browser/api/menu-utils.ts to
shell/browser/api/electron_api_menu_template.cc. Ids are still compared
with ===, and Proxy-wrapped and sparse template arrays are still accepted.

(cherry picked from commit 473519e)

* perf: implement MenuItem and Menu's item handling natively (#53876)

MenuItem was a JavaScript object that the native menu model called back
into for every property of every item each time a menu was shown, had its
accelerators registered or (on macOS) was checked for a key equivalent,
and menu.insert() and click dispatch bounced between lib/browser/api and
api::Menu several times per item.

MenuItem is now a gin/cppgc object in shell/browser/api whose documented
properties are native data properties; api::Menu holds its items natively
and answers the menu model from them, and insert/append, menu.items,
buildFromTemplate and click dispatch are native. The role submenus are
built from native items. Showing a menu no longer enters JS; the only JS
that runs is the app's own click handlers. lib/browser/api/menu-item.ts
becomes a re-export of the binding; menu.ts keeps popup(),
getMenuItemById() and set/getApplicationMenu().

(cherry picked from commit 6887c0e)

* perf: implement Menu.popup, getMenuItemById and setApplicationMenu natively (#53877)

The rest of lib/browser/api/menu.ts moves into api::Menu: popup() and
closePopup() (window resolution included), getMenuItemById(), and
Menu.setApplicationMenu()/getApplicationMenu(), which on Windows and Linux
now sets each window's menu without a round trip through the BaseWindow
wrappers. menu.ts becomes a re-export of the binding and default-menu.ts
asks api::Menu whether an application menu was set.

(cherry picked from commit ee3c6d7)

* refactor: make ElectronMenuModel a ui::MenuModel over the menu's items (#53878)

ElectronMenuModel was a ui::SimpleMenuModel: it kept its own copy of each
item (type, command id, label) plus per-command-id maps for tooltip, role,
custom type and badge, and asked api::Menu for everything else through a
by-command-id delegate, which api::Menu answered by looking the id up in a
process-wide MenuItem registry.

It is now a ui::MenuModel implemented directly over
ElectronMenuModel::Item pointers (api::MenuItem implements Item), so there
is one list of items, the platform menu code reads item state without a
delegate hop or an id lookup, and the command-id registry, the radio-group
table and the SimpleMenuModel mirroring in api::Menu go away. The
platform menu builders are unchanged.

(cherry picked from commit 9c69d2d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants