Skip to content

Commit

Permalink
feat: add tabbingIdentifier property to BrowserWindow (#39980)
Browse files Browse the repository at this point in the history
feat: add tabbingIdentifier property to BrowserWindow
  • Loading branch information
codebytere committed Oct 3, 2023
1 parent 04b2ba8 commit 713d8c4
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 6 deletions.
4 changes: 4 additions & 0 deletions docs/api/browser-window.md
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,10 @@ events.

A `Integer` property representing the unique ID of the window. Each ID is unique among all `BrowserWindow` instances of the entire Electron application.

#### `win.tabbingIdentifier` _macOS_ _Readonly_

A `string` (optional) property that is equal to the `tabbingIdentifier` passed to the `BrowserWindow` constructor or `undefined` if none was set.

#### `win.autoHideMenuBar`

A `boolean` property that determines whether the window menu bar should hide itself automatically. Once set, the menu bar will only show when users press the single `Alt` key.
Expand Down
9 changes: 9 additions & 0 deletions shell/browser/api/electron_api_base_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,14 @@ void BaseWindow::AddTabbedWindow(NativeWindow* window,
args->ThrowError("AddTabbedWindow cannot be called by a window on itself.");
}

v8::Local<v8::Value> BaseWindow::GetTabbingIdentifier() {
auto tabbing_id = window_->GetTabbingIdentifier();
if (!tabbing_id.has_value())
return v8::Undefined(isolate());

return gin::ConvertToV8(isolate(), tabbing_id.value());
}

void BaseWindow::SetAutoHideMenuBar(bool auto_hide) {
window_->SetAutoHideMenuBar(auto_hide);
}
Expand Down Expand Up @@ -1305,6 +1313,7 @@ void BaseWindow::BuildPrototype(v8::Isolate* isolate,
.SetMethod("moveTabToNewWindow", &BaseWindow::MoveTabToNewWindow)
.SetMethod("toggleTabBar", &BaseWindow::ToggleTabBar)
.SetMethod("addTabbedWindow", &BaseWindow::AddTabbedWindow)
.SetProperty("tabbingIdentifier", &BaseWindow::GetTabbingIdentifier)
.SetMethod("setWindowButtonVisibility",
&BaseWindow::SetWindowButtonVisibility)
.SetMethod("_getWindowButtonVisibility",
Expand Down
3 changes: 1 addition & 2 deletions shell/browser/api/electron_api_base_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,7 @@ class BaseWindow : public gin_helper::TrackableObject<BaseWindow>,
bool GetWindowButtonVisibility() const;
void SetWindowButtonPosition(absl::optional<gfx::Point> position);
absl::optional<gfx::Point> GetWindowButtonPosition() const;
#endif

#if BUILDFLAG(IS_MAC)
bool IsHiddenInMissionControl();
void SetHiddenInMissionControl(bool hidden);
#endif
Expand All @@ -215,6 +213,7 @@ class BaseWindow : public gin_helper::TrackableObject<BaseWindow>,
void MoveTabToNewWindow();
void ToggleTabBar();
void AddTabbedWindow(NativeWindow* window, gin_helper::Arguments* args);
v8::Local<v8::Value> GetTabbingIdentifier();
void SetAutoHideMenuBar(bool auto_hide);
bool IsMenuBarAutoHide();
void SetMenuBarVisibility(bool visible);
Expand Down
4 changes: 4 additions & 0 deletions shell/browser/native_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,10 @@ bool NativeWindow::AddTabbedWindow(NativeWindow* window) {
return true; // for non-Mac platforms
}

absl::optional<std::string> NativeWindow::GetTabbingIdentifier() const {
return ""; // for non-Mac platforms
}

void NativeWindow::SetVibrancy(const std::string& type) {
vibrancy_ = type;
}
Expand Down
1 change: 1 addition & 0 deletions shell/browser/native_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ class NativeWindow : public base::SupportsUserData,
virtual void MoveTabToNewWindow();
virtual void ToggleTabBar();
virtual bool AddTabbedWindow(NativeWindow* window);
virtual absl::optional<std::string> GetTabbingIdentifier() const;

// Toggle the menu bar.
virtual void SetAutoHideMenuBar(bool auto_hide);
Expand Down
1 change: 1 addition & 0 deletions shell/browser/native_window_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ class NativeWindowMac : public NativeWindow,
void MoveTabToNewWindow() override;
void ToggleTabBar() override;
bool AddTabbedWindow(NativeWindow* window) override;
absl::optional<std::string> GetTabbingIdentifier() const override;
void SetAspectRatio(double aspect_ratio,
const gfx::Size& extra_size) override;
void PreviewFile(const std::string& path,
Expand Down
7 changes: 7 additions & 0 deletions shell/browser/native_window_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,13 @@ void ReorderChildWindowAbove(NSWindow* child_window, NSWindow* other_window) {
return true;
}

absl::optional<std::string> NativeWindowMac::GetTabbingIdentifier() const {
if ([window_ tabbingMode] == NSWindowTabbingModeDisallowed)
return absl::nullopt;

return base::SysNSStringToUTF8([window_ tabbingIdentifier]);
}

void NativeWindowMac::SetAspectRatio(double aspect_ratio,
const gfx::Size& extra_size) {
NativeWindow::SetAspectRatio(aspect_ratio, extra_size);
Expand Down
17 changes: 13 additions & 4 deletions spec/api-browser-window-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2060,10 +2060,7 @@ describe('BrowserWindow module', () => {
beforeEach(() => {
w = new BrowserWindow({ show: false });
});
afterEach(async () => {
await closeWindow(w);
w = null as unknown as BrowserWindow;
});
afterEach(closeAllWindows);

describe('BrowserWindow.selectPreviousTab()', () => {
it('does not throw', () => {
Expand Down Expand Up @@ -2132,6 +2129,18 @@ describe('BrowserWindow module', () => {
}).to.throw('AddTabbedWindow cannot be called by a window on itself.');
});
});

describe('BrowserWindow.tabbingIdentifier', () => {
it('is undefined if no tabbingIdentifier was set', () => {
const w = new BrowserWindow({ show: false });
expect(w.tabbingIdentifier).to.be.undefined('tabbingIdentifier');
});

it('returns the window tabbingIdentifier', () => {
const w = new BrowserWindow({ show: false, tabbingIdentifier: 'group1' });
expect(w.tabbingIdentifier).to.equal('group1');
});
});
});

describe('autoHideMenuBar state', () => {
Expand Down

0 comments on commit 713d8c4

Please sign in to comment.