diff --git a/shell/browser/api/electron_api_base_window.cc b/shell/browser/api/electron_api_base_window.cc index fa48c76b286ed..0787f18501e24 100644 --- a/shell/browser/api/electron_api_base_window.cc +++ b/shell/browser/api/electron_api_base_window.cc @@ -864,6 +864,10 @@ void BaseWindow::SetVibrancy(v8::Isolate* isolate, v8::Local value) { } #if defined(OS_MAC) +std::string BaseWindow::GetAlwaysOnTopLevel() { + return window_->GetAlwaysOnTopLevel(); +} + void BaseWindow::SetTrafficLightPosition(const gfx::Point& position) { window_->SetTrafficLightPosition(position); } @@ -1244,6 +1248,7 @@ void BaseWindow::BuildPrototype(v8::Isolate* isolate, .SetMethod("isVisibleOnAllWorkspaces", &BaseWindow::IsVisibleOnAllWorkspaces) #if defined(OS_MAC) + .SetMethod("_getAlwaysOnTopLevel", &BaseWindow::GetAlwaysOnTopLevel) .SetMethod("setAutoHideCursor", &BaseWindow::SetAutoHideCursor) #endif .SetMethod("setVibrancy", &BaseWindow::SetVibrancy) diff --git a/shell/browser/api/electron_api_base_window.h b/shell/browser/api/electron_api_base_window.h index 2cd0bf7fd41cf..c908c3411f7e6 100644 --- a/shell/browser/api/electron_api_base_window.h +++ b/shell/browser/api/electron_api_base_window.h @@ -191,6 +191,7 @@ class BaseWindow : public gin_helper::TrackableObject, virtual void SetVibrancy(v8::Isolate* isolate, v8::Local value); #if defined(OS_MAC) + std::string GetAlwaysOnTopLevel(); void SetTrafficLightPosition(const gfx::Point& position); gfx::Point GetTrafficLightPosition() const; #endif diff --git a/shell/browser/native_window.h b/shell/browser/native_window.h index a29039f16afc4..bc0eff8f3adef 100644 --- a/shell/browser/native_window.h +++ b/shell/browser/native_window.h @@ -203,6 +203,7 @@ class NativeWindow : public base::SupportsUserData, // Traffic Light API #if defined(OS_MAC) + virtual std::string GetAlwaysOnTopLevel() = 0; virtual void SetTrafficLightPosition(const gfx::Point& position) = 0; virtual gfx::Point GetTrafficLightPosition() const = 0; virtual void RedrawTrafficLights() = 0; diff --git a/shell/browser/native_window_mac.h b/shell/browser/native_window_mac.h index f440abfeb1181..4b8fc4d3d95ee 100644 --- a/shell/browser/native_window_mac.h +++ b/shell/browser/native_window_mac.h @@ -89,6 +89,7 @@ class NativeWindowMac : public NativeWindow, void SetAlwaysOnTop(ui::ZOrderLevel z_order, const std::string& level, int relativeLevel) override; + std::string GetAlwaysOnTopLevel() override; ui::ZOrderLevel GetZOrderLevel() override; void Center() override; void Invalidate() override; diff --git a/shell/browser/native_window_mac.mm b/shell/browser/native_window_mac.mm index 0e9f310417975..a7f14b4454379 100644 --- a/shell/browser/native_window_mac.mm +++ b/shell/browser/native_window_mac.mm @@ -1055,6 +1055,31 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) { SetWindowLevel(level + relative_level); } +std::string NativeWindowMac::GetAlwaysOnTopLevel() { + std::string level_name = "normal"; + + int level = [window_ level]; + if (level == NSFloatingWindowLevel) { + level_name = "floating"; + } else if (level == NSTornOffMenuWindowLevel) { + level_name = "torn-off-menu"; + } else if (level == NSModalPanelWindowLevel) { + level_name = "modal-panel"; + } else if (level == NSMainMenuWindowLevel) { + level_name = "main-menu"; + } else if (level == NSStatusWindowLevel) { + level_name = "status"; + } else if (level == NSPopUpMenuWindowLevel) { + level_name = "pop-up-menu"; + } else if (level == NSScreenSaverWindowLevel) { + level_name = "screen-saver"; + } else if (level == NSDockWindowLevel) { + level_name = "dock"; + } + + return level_name; +} + void NativeWindowMac::SetWindowLevel(int unbounded_level) { int level = std::min( std::max(unbounded_level, CGWindowLevelForKey(kCGMinimumWindowLevelKey)), @@ -1854,10 +1879,15 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) { // Set new parent window. // Note that this method will force the window to become visible. - if (parent && attach) + if (parent && attach) { + // Attaching a window as a child window resets its window level, so + // save and restore it afterwards. + NSInteger level = window_.level; [parent->GetNativeWindow().GetNativeNSWindow() addChildWindow:window_ ordered:NSWindowAbove]; + [window_ setLevel:level]; + } } void NativeWindowMac::SetForwardMouseMessages(bool forward) { diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts index c26f8b56b4196..7b117d96b5fd0 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -1418,15 +1418,12 @@ describe('BrowserWindow module', () => { describe('BrowserWindow.setAlwaysOnTop(flag, level)', () => { let w = null as unknown as BrowserWindow; + afterEach(closeAllWindows); + beforeEach(() => { w = new BrowserWindow({ show: true }); }); - afterEach(async () => { - await closeWindow(w); - w = null as unknown as BrowserWindow; - }); - it('sets the window as always on top', () => { expect(w.isAlwaysOnTop()).to.be.false('is alwaysOnTop'); w.setAlwaysOnTop(true, 'screen-saver'); @@ -1454,6 +1451,16 @@ describe('BrowserWindow module', () => { const [, alwaysOnTop] = await alwaysOnTopChanged; expect(alwaysOnTop).to.be.true('is not alwaysOnTop'); }); + + ifit(process.platform === 'darwin')('honors the alwaysOnTop level of a child window', () => { + w = new BrowserWindow({ show: false }); + const c = new BrowserWindow({ parent: w }); + c.setAlwaysOnTop(true, 'screen-saver'); + + expect(w.isAlwaysOnTop()).to.be.false(); + expect(c.isAlwaysOnTop()).to.be.true('child is not always on top'); + expect((c as any)._getAlwaysOnTopLevel()).to.equal('screen-saver'); + }); }); describe('preconnect feature', () => {