Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: child window alwaysOnTop level persistence #29813

Merged
merged 4 commits into from Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions shell/browser/api/electron_api_base_window.cc
Expand Up @@ -559,6 +559,10 @@ bool BaseWindow::IsAlwaysOnTop() {
return window_->GetZOrderLevel() != ui::ZOrderLevel::kNormal;
}

std::string BaseWindow::GetAlwaysOnTopLevel() {
return window_->GetAlwaysOnTopLevel();
}

void BaseWindow::Center() {
window_->Center();
}
Expand Down Expand Up @@ -1271,6 +1275,7 @@ void BaseWindow::BuildPrototype(v8::Isolate* isolate,
.SetMethod("isVisibleOnAllWorkspaces",
&BaseWindow::IsVisibleOnAllWorkspaces)
#if defined(OS_MAC)
.SetMethod("getAlwaysOnTopLevel", &BaseWindow::GetAlwaysOnTopLevel)
codebytere marked this conversation as resolved.
Show resolved Hide resolved
.SetMethod("setAutoHideCursor", &BaseWindow::SetAutoHideCursor)
#endif
.SetMethod("setVibrancy", &BaseWindow::SetVibrancy)
Expand Down
1 change: 1 addition & 0 deletions shell/browser/api/electron_api_base_window.h
Expand Up @@ -142,6 +142,7 @@ class BaseWindow : public gin_helper::TrackableObject<BaseWindow>,
bool IsClosable();
void SetAlwaysOnTop(bool top, gin_helper::Arguments* args);
bool IsAlwaysOnTop();
std::string GetAlwaysOnTopLevel();
void Center();
void SetPosition(int x, int y, gin_helper::Arguments* args);
std::vector<int> GetPosition();
Expand Down
1 change: 1 addition & 0 deletions shell/browser/native_window.h
Expand Up @@ -129,6 +129,7 @@ class NativeWindow : public base::SupportsUserData,
virtual void SetAlwaysOnTop(ui::ZOrderLevel z_order,
const std::string& level = "floating",
int relativeLevel = 0) = 0;
virtual std::string GetAlwaysOnTopLevel() = 0;
virtual ui::ZOrderLevel GetZOrderLevel() = 0;
virtual void Center() = 0;
virtual void Invalidate() = 0;
Expand Down
1 change: 1 addition & 0 deletions shell/browser/native_window_mac.h
Expand Up @@ -78,6 +78,7 @@ class NativeWindowMac : public NativeWindow,
void SetAlwaysOnTop(ui::ZOrderLevel z_order,
const std::string& level,
int relative_level) override;
std::string GetAlwaysOnTopLevel() override;
ui::ZOrderLevel GetZOrderLevel() override;
void Center() override;
void Invalidate() override;
Expand Down
32 changes: 31 additions & 1 deletion shell/browser/native_window_mac.mm
Expand Up @@ -869,6 +869,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)),
Expand Down Expand Up @@ -1805,10 +1830,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) {
Expand Down
1 change: 1 addition & 0 deletions shell/browser/native_window_views.h
Expand Up @@ -93,6 +93,7 @@ class NativeWindowViews : 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;
Expand Down
17 changes: 12 additions & 5 deletions spec-main/api-browser-window-spec.ts
Expand Up @@ -1430,15 +1430,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');
Expand Down Expand Up @@ -1466,6 +1463,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', () => {
Expand Down