From 56b5c003122562066971c4fb44ab933563bb10db Mon Sep 17 00:00:00 2001 From: Mikhail Leliakin Date: Mon, 10 Jul 2023 18:43:37 +1000 Subject: [PATCH] feat: add `BrowserWindow.showAllTabs()` for macOS (#38965) --- docs/api/browser-window.md | 4 ++++ docs/api/menu-item.md | 5 +++-- shell/browser/api/electron_api_base_window.cc | 5 +++++ shell/browser/api/electron_api_base_window.h | 1 + shell/browser/native_window.cc | 2 ++ shell/browser/native_window.h | 1 + shell/browser/native_window_mac.h | 1 + shell/browser/native_window_mac.mm | 4 ++++ shell/browser/ui/cocoa/electron_menu_controller.mm | 1 + spec/api-browser-window-spec.ts | 8 ++++++++ spec/ts-smoke/electron/main.ts | 1 + 11 files changed, 31 insertions(+), 2 deletions(-) diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index 3528a13139e5..e072a2f77a98 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -1531,6 +1531,10 @@ tabs in the window. Selects the next tab when native tabs are enabled and there are other tabs in the window. +#### `win.showAllTabs()` _macOS_ + +Shows or hides the tab overview when native tabs are enabled. + #### `win.mergeAllWindows()` _macOS_ Merges all windows into one window with multiple tabs when native tabs diff --git a/docs/api/menu-item.md b/docs/api/menu-item.md index 408bc4c81b16..ad74bb57e68e 100644 --- a/docs/api/menu-item.md +++ b/docs/api/menu-item.md @@ -14,7 +14,7 @@ See [`Menu`](menu.md) for examples. * `menuItem` MenuItem * `browserWindow` [BrowserWindow](browser-window.md) | undefined - This will not be defined if no window is open. * `event` [KeyboardEvent](structures/keyboard-event.md) - * `role` string (optional) - Can be `undo`, `redo`, `cut`, `copy`, `paste`, `pasteAndMatchStyle`, `delete`, `selectAll`, `reload`, `forceReload`, `toggleDevTools`, `resetZoom`, `zoomIn`, `zoomOut`, `toggleSpellChecker`, `togglefullscreen`, `window`, `minimize`, `close`, `help`, `about`, `services`, `hide`, `hideOthers`, `unhide`, `quit`, `showSubstitutions`, `toggleSmartQuotes`, `toggleSmartDashes`, `toggleTextReplacement`, `startSpeaking`, `stopSpeaking`, `zoom`, `front`, `appMenu`, `fileMenu`, `editMenu`, `viewMenu`, `shareMenu`, `recentDocuments`, `toggleTabBar`, `selectNextTab`, `selectPreviousTab`, `mergeAllWindows`, `clearRecentDocuments`, `moveTabToNewWindow` or `windowMenu` - Define the action of the menu item, when specified the + * `role` string (optional) - Can be `undo`, `redo`, `cut`, `copy`, `paste`, `pasteAndMatchStyle`, `delete`, `selectAll`, `reload`, `forceReload`, `toggleDevTools`, `resetZoom`, `zoomIn`, `zoomOut`, `toggleSpellChecker`, `togglefullscreen`, `window`, `minimize`, `close`, `help`, `about`, `services`, `hide`, `hideOthers`, `unhide`, `quit`, `showSubstitutions`, `toggleSmartQuotes`, `toggleSmartDashes`, `toggleTextReplacement`, `startSpeaking`, `stopSpeaking`, `zoom`, `front`, `appMenu`, `fileMenu`, `editMenu`, `viewMenu`, `shareMenu`, `recentDocuments`, `toggleTabBar`, `selectNextTab`, `selectPreviousTab`, `showAllTabs`, `mergeAllWindows`, `clearRecentDocuments`, `moveTabToNewWindow` or `windowMenu` - Define the action of the menu item, when specified the `click` property will be ignored. See [roles](#roles). * `type` string (optional) - Can be `normal`, `separator`, `submenu`, `checkbox` or `radio`. @@ -111,6 +111,7 @@ The following additional roles are available on _macOS_: * `toggleTabBar` - Map to the `toggleTabBar` action. * `selectNextTab` - Map to the `selectNextTab` action. * `selectPreviousTab` - Map to the `selectPreviousTab` action. +* `showAllTabs` - Map to the `showAllTabs` action. * `mergeAllWindows` - Map to the `mergeAllWindows` action. * `moveTabToNewWindow` - Map to the `moveTabToNewWindow` action. * `window` - The submenu is a "Window" menu. @@ -159,7 +160,7 @@ A `string` indicating the type of the item. Can be `normal`, `separator`, `subme #### `menuItem.role` -A `string` (optional) indicating the item's role, if set. Can be `undo`, `redo`, `cut`, `copy`, `paste`, `pasteAndMatchStyle`, `delete`, `selectAll`, `reload`, `forceReload`, `toggleDevTools`, `resetZoom`, `zoomIn`, `zoomOut`, `toggleSpellChecker`, `togglefullscreen`, `window`, `minimize`, `close`, `help`, `about`, `services`, `hide`, `hideOthers`, `unhide`, `quit`, `startSpeaking`, `stopSpeaking`, `zoom`, `front`, `appMenu`, `fileMenu`, `editMenu`, `viewMenu`, `shareMenu`, `recentDocuments`, `toggleTabBar`, `selectNextTab`, `selectPreviousTab`, `mergeAllWindows`, `clearRecentDocuments`, `moveTabToNewWindow` or `windowMenu` +A `string` (optional) indicating the item's role, if set. Can be `undo`, `redo`, `cut`, `copy`, `paste`, `pasteAndMatchStyle`, `delete`, `selectAll`, `reload`, `forceReload`, `toggleDevTools`, `resetZoom`, `zoomIn`, `zoomOut`, `toggleSpellChecker`, `togglefullscreen`, `window`, `minimize`, `close`, `help`, `about`, `services`, `hide`, `hideOthers`, `unhide`, `quit`, `startSpeaking`, `stopSpeaking`, `zoom`, `front`, `appMenu`, `fileMenu`, `editMenu`, `viewMenu`, `shareMenu`, `recentDocuments`, `toggleTabBar`, `selectNextTab`, `selectPreviousTab`, `showAllTabs`, `mergeAllWindows`, `clearRecentDocuments`, `moveTabToNewWindow` or `windowMenu` #### `menuItem.accelerator` diff --git a/shell/browser/api/electron_api_base_window.cc b/shell/browser/api/electron_api_base_window.cc index 91655efe61a5..5c680782e43f 100644 --- a/shell/browser/api/electron_api_base_window.cc +++ b/shell/browser/api/electron_api_base_window.cc @@ -915,6 +915,10 @@ void BaseWindow::SelectNextTab() { window_->SelectNextTab(); } +void BaseWindow::ShowAllTabs() { + window_->ShowAllTabs(); +} + void BaseWindow::MergeAllWindows() { window_->MergeAllWindows(); } @@ -1280,6 +1284,7 @@ void BaseWindow::BuildPrototype(v8::Isolate* isolate, #if BUILDFLAG(IS_MAC) .SetMethod("selectPreviousTab", &BaseWindow::SelectPreviousTab) .SetMethod("selectNextTab", &BaseWindow::SelectNextTab) + .SetMethod("showAllTabs", &BaseWindow::ShowAllTabs) .SetMethod("mergeAllWindows", &BaseWindow::MergeAllWindows) .SetMethod("moveTabToNewWindow", &BaseWindow::MoveTabToNewWindow) .SetMethod("toggleTabBar", &BaseWindow::ToggleTabBar) diff --git a/shell/browser/api/electron_api_base_window.h b/shell/browser/api/electron_api_base_window.h index f95ca67d3005..ba5c0399cf2a 100644 --- a/shell/browser/api/electron_api_base_window.h +++ b/shell/browser/api/electron_api_base_window.h @@ -210,6 +210,7 @@ class BaseWindow : public gin_helper::TrackableObject, void SetEscapeTouchBarItem(gin_helper::PersistentDictionary item); void SelectPreviousTab(); void SelectNextTab(); + void ShowAllTabs(); void MergeAllWindows(); void MoveTabToNewWindow(); void ToggleTabBar(); diff --git a/shell/browser/native_window.cc b/shell/browser/native_window.cc index 1c6a6b0bc7d9..a24a4c131be7 100644 --- a/shell/browser/native_window.cc +++ b/shell/browser/native_window.cc @@ -457,6 +457,8 @@ void NativeWindow::SelectPreviousTab() {} void NativeWindow::SelectNextTab() {} +void NativeWindow::ShowAllTabs() {} + void NativeWindow::MergeAllWindows() {} void NativeWindow::MoveTabToNewWindow() {} diff --git a/shell/browser/native_window.h b/shell/browser/native_window.h index fcdf419d7e90..f0bb45d545b6 100644 --- a/shell/browser/native_window.h +++ b/shell/browser/native_window.h @@ -245,6 +245,7 @@ class NativeWindow : public base::SupportsUserData, // Native Tab API virtual void SelectPreviousTab(); virtual void SelectNextTab(); + virtual void ShowAllTabs(); virtual void MergeAllWindows(); virtual void MoveTabToNewWindow(); virtual void ToggleTabBar(); diff --git a/shell/browser/native_window_mac.h b/shell/browser/native_window_mac.h index 15e00717bf57..c255f950e432 100644 --- a/shell/browser/native_window_mac.h +++ b/shell/browser/native_window_mac.h @@ -139,6 +139,7 @@ class NativeWindowMac : public NativeWindow, void SetEscapeTouchBarItem(gin_helper::PersistentDictionary item) override; void SelectPreviousTab() override; void SelectNextTab() override; + void ShowAllTabs() override; void MergeAllWindows() override; void MoveTabToNewWindow() override; void ToggleTabBar() override; diff --git a/shell/browser/native_window_mac.mm b/shell/browser/native_window_mac.mm index 774b8482f92d..195a684c7564 100644 --- a/shell/browser/native_window_mac.mm +++ b/shell/browser/native_window_mac.mm @@ -1535,6 +1535,10 @@ void NativeWindowMac::SelectNextTab() { [window_ selectNextTab:nil]; } +void NativeWindowMac::ShowAllTabs() { + [window_ toggleTabOverview:nil]; +} + void NativeWindowMac::MergeAllWindows() { [window_ mergeAllWindows:nil]; } diff --git a/shell/browser/ui/cocoa/electron_menu_controller.mm b/shell/browser/ui/cocoa/electron_menu_controller.mm index d7ceb05fd19b..67bead165769 100644 --- a/shell/browser/ui/cocoa/electron_menu_controller.mm +++ b/shell/browser/ui/cocoa/electron_menu_controller.mm @@ -65,6 +65,7 @@ Role kRolesMap[] = { {@selector(toggleFullScreenMode:), "togglefullscreen"}, {@selector(toggleTabBar:), "toggletabbar"}, {@selector(selectNextTab:), "selectnexttab"}, + {@selector(toggleTabOverview:), "showalltabs"}, {@selector(selectPreviousTab:), "selectprevioustab"}, {@selector(mergeAllWindows:), "mergeallwindows"}, {@selector(moveTabToNewWindow:), "movetabtonewwindow"}, diff --git a/spec/api-browser-window-spec.ts b/spec/api-browser-window-spec.ts index de52ce8377cb..6fd7fb9d8825 100644 --- a/spec/api-browser-window-spec.ts +++ b/spec/api-browser-window-spec.ts @@ -2072,6 +2072,14 @@ describe('BrowserWindow module', () => { }); }); + describe('BrowserWindow.showAllTabs()', () => { + it('does not throw', () => { + expect(() => { + w.showAllTabs(); + }).to.not.throw(); + }); + }); + describe('BrowserWindow.mergeAllWindows()', () => { it('does not throw', () => { expect(() => { diff --git a/spec/ts-smoke/electron/main.ts b/spec/ts-smoke/electron/main.ts index cf1492f4751e..05829f4d6e21 100644 --- a/spec/ts-smoke/electron/main.ts +++ b/spec/ts-smoke/electron/main.ts @@ -824,6 +824,7 @@ Menu.buildFromTemplate([ { role: 'toggleTabBar' }, { role: 'selectNextTab' }, { role: 'selectPreviousTab' }, + { role: 'showAllTabs' }, { role: 'mergeAllWindows' }, { role: 'clearRecentDocuments' }, { role: 'moveTabToNewWindow' }