From b4428e7e419f999a7a88fc68bd5691769058d81c Mon Sep 17 00:00:00 2001 From: Michael Kaylan <1063516+kaylanm@users.noreply.github.com> Date: Mon, 21 Aug 2017 00:46:10 -0400 Subject: [PATCH] :apple: Add macOS native tab methods to window API --- atom/browser/api/atom_api_window.cc | 25 ++++++++++++ atom/browser/api/atom_api_window.h | 6 +++ atom/browser/native_window.cc | 15 ++++++++ atom/browser/native_window.h | 7 ++++ atom/browser/native_window_mac.h | 6 +++ atom/browser/native_window_mac.mm | 35 +++++++++++++++++ docs/api/browser-window.md | 25 ++++++++++++ spec/api-browser-window-spec.js | 60 +++++++++++++++++++++++++++++ 8 files changed, 179 insertions(+) diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index 34366e2db5d4..efb55a6cc13d 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -907,6 +907,26 @@ void Window::SetAutoHideCursor(bool auto_hide) { window_->SetAutoHideCursor(auto_hide); } +void Window::SelectPreviousTab() { + window_->SelectPreviousTab(); +} + +void Window::SelectNextTab() { + window_->SelectNextTab(); +} + +void Window::MergeAllWindows() { + window_->MergeAllWindows(); +} + +void Window::MoveTabToNewWindow() { + window_->MoveTabToNewWindow(); +} + +void Window::ToggleTabBar() { + window_->ToggleTabBar(); +} + void Window::SetVibrancy(mate::Arguments* args) { std::string type; @@ -1047,6 +1067,11 @@ void Window::BuildPrototype(v8::Isolate* isolate, &Window::IsVisibleOnAllWorkspaces) #if defined(OS_MACOSX) .SetMethod("setAutoHideCursor", &Window::SetAutoHideCursor) + .SetMethod("mergeAllWindows", &Window::MergeAllWindows) + .SetMethod("selectPreviousTab", &Window::SelectPreviousTab) + .SetMethod("selectNextTab", &Window::SelectNextTab) + .SetMethod("moveTabToNewWindow", &Window::MoveTabToNewWindow) + .SetMethod("toggleTabBar", &Window::ToggleTabBar) #endif .SetMethod("setVibrancy", &Window::SetVibrancy) .SetMethod("_setTouchBarItems", &Window::SetTouchBar) diff --git a/atom/browser/api/atom_api_window.h b/atom/browser/api/atom_api_window.h index 22f7e730778f..540054eca224 100644 --- a/atom/browser/api/atom_api_window.h +++ b/atom/browser/api/atom_api_window.h @@ -212,6 +212,12 @@ class Window : public mate::TrackableObject, void SetAutoHideCursor(bool auto_hide); + void SelectPreviousTab(); + void SelectNextTab(); + void MergeAllWindows(); + void MoveTabToNewWindow(); + void ToggleTabBar(); + void SetVibrancy(mate::Arguments* args); void SetTouchBar(const std::vector& items); void RefreshTouchBarItem(const std::string& item_id); diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index ddb4a030079c..e47a4f7e8ceb 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -336,6 +336,21 @@ void NativeWindow::SetParentWindow(NativeWindow* parent) { void NativeWindow::SetAutoHideCursor(bool auto_hide) { } +void NativeWindow::SelectPreviousTab() { +} + +void NativeWindow::SelectNextTab() { +} + +void NativeWindow::MergeAllWindows() { +} + +void NativeWindow::MoveTabToNewWindow() { +} + +void NativeWindow::ToggleTabBar() { +} + void NativeWindow::SetVibrancy(const std::string& filename) { } diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index dd874057895d..2120414c41c8 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -182,6 +182,13 @@ class NativeWindow : public base::SupportsUserData, virtual void RefreshTouchBarItem(const std::string& item_id); virtual void SetEscapeTouchBarItem(const mate::PersistentDictionary& item); + // Native Tab API + virtual void SelectPreviousTab(); + virtual void SelectNextTab(); + virtual void MergeAllWindows(); + virtual void MoveTabToNewWindow(); + virtual void ToggleTabBar(); + // Webview APIs. virtual void FocusOnWebView(); virtual void BlurWebView(); diff --git a/atom/browser/native_window_mac.h b/atom/browser/native_window_mac.h index 3dfb88d5398f..54ed9d7275c7 100644 --- a/atom/browser/native_window_mac.h +++ b/atom/browser/native_window_mac.h @@ -101,6 +101,12 @@ class NativeWindowMac : public NativeWindow, void SetAutoHideCursor(bool auto_hide) override; + void SelectPreviousTab() override; + void SelectNextTab() override; + void MergeAllWindows() override; + void MoveTabToNewWindow() override; + void ToggleTabBar() override; + void SetVibrancy(const std::string& type) override; void SetTouchBar( const std::vector& items) override; diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index 2719c672954a..70a110344fde 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -469,6 +469,11 @@ enum { @interface NSWindow (SierraSDK) - (void)setTabbingMode:(NSInteger)mode; - (void)setTabbingIdentifier:(NSString*)identifier; +- (IBAction)selectPreviousTab:(id)sender; +- (IBAction)selectNextTab:(id)sender; +- (IBAction)mergeAllWindows:(id)sender; +- (IBAction)moveTabToNewWindow:(id)sender; +- (IBAction)toggleTabBar:(id)sender; @end #endif // MAC_OS_X_VERSION_10_12 @@ -1523,6 +1528,36 @@ void NativeWindowMac::SetAutoHideCursor(bool auto_hide) { [window_ setDisableAutoHideCursor:!auto_hide]; } +void NativeWindowMac::SelectPreviousTab() { + if ([window_ respondsToSelector:@selector(selectPreviousTab:)]) { + [window_ selectPreviousTab:nil]; + } +} + +void NativeWindowMac::SelectNextTab() { + if ([window_ respondsToSelector:@selector(selectNextTab:)]) { + [window_ selectNextTab:nil]; + } +} + +void NativeWindowMac::MergeAllWindows() { + if ([window_ respondsToSelector:@selector(mergeAllWindows:)]) { + [window_ mergeAllWindows:nil]; + } +} + +void NativeWindowMac::MoveTabToNewWindow() { + if ([window_ respondsToSelector:@selector(moveTabToNewWindow:)]) { + [window_ moveTabToNewWindow:nil]; + } +} + +void NativeWindowMac::ToggleTabBar() { + if ([window_ respondsToSelector:@selector(toggleTabBar:)]) { + [window_ toggleTabBar:nil]; + } +} + void NativeWindowMac::SetVibrancy(const std::string& type) { if (!base::mac::IsAtLeastOS10_10()) return; diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index e08c8de8c915..edceec925f43 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -1357,6 +1357,31 @@ Returns `BrowserWindow[]` - All child windows. Controls whether to hide cursor when typing. +#### `win.selectPreviousTab()` _macOS_ + +Selects the previous tab when native tabs are enabled and there are other +tabs in the window. + +#### `win.selectNextTab()` _macOS_ + +Selects the next tab when native tabs are enabled and there are other +tabs in the window. + +#### `win.mergeAllWindows()` _macOS_ + +Merges all windows into one window with multiple tabs when native tabs +are enabled and there is more than one open window. + +#### `win.moveTabToNewWindow()` _macOS_ + +Moves the current tab into a new window if native tabs are enabled and +there is more than one tab in the current window. + +#### `win.toggleTabBar()` _macOS_ + +Toggles the visibility of the tab bar if native tabs are enabled and +there is only one tab in the current window. + #### `win.setVibrancy(type)` _macOS_ * `type` String - Can be `appearance-based`, `light`, `dark`, `titlebar`, diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 52c9b5fb47e3..07743bc39121 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -648,6 +648,66 @@ describe('BrowserWindow module', function () { }) }) + describe('BrowserWindow.selectPreviousTab()', () => { + it('does not throw', () => { + if (process.platform !== 'darwin') { + return + } + + assert.doesNotThrow(() => { + w.selectPreviousTab() + }) + }) + }) + + describe('BrowserWindow.selectNextTab()', () => { + it('does not throw', () => { + if (process.platform !== 'darwin') { + return + } + + assert.doesNotThrow(() => { + w.selectNextTab() + }) + }) + }) + + describe('BrowserWindow.mergeAllWindows()', () => { + it('does not throw', () => { + if (process.platform !== 'darwin') { + return + } + + assert.doesNotThrow(() => { + w.mergeAllWindows() + }) + }) + }) + + describe('BrowserWindow.moveTabToNewWindow()', () => { + it('does not throw', () => { + if (process.platform !== 'darwin') { + return + } + + assert.doesNotThrow(() => { + w.moveTabToNewWindow() + }) + }) + }) + + describe('BrowserWindow.toggleTabBar()', () => { + it('does not throw', () => { + if (process.platform !== 'darwin') { + return + } + + assert.doesNotThrow(() => { + w.toggleTabBar() + }) + }) + }) + describe('BrowserWindow.setVibrancy(type)', function () { it('allows setting, changing, and removing the vibrancy', function () { assert.doesNotThrow(function () {