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: unify BrowserWindow.isVisible() logic cross-platform #38314

Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion docs/api/browser-window.md
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ Hides the window.

#### `win.isVisible()`

Returns `boolean` - Whether the window is visible to the user.
Returns `boolean` - Whether the window is visible to the user in the foreground of the app.

#### `win.isModal()`

Expand Down
4 changes: 0 additions & 4 deletions shell/browser/native_window_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -491,10 +491,6 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) {

bool NativeWindowMac::IsVisible() {
bool occluded = [window_ occlusionState] == NSWindowOcclusionStateVisible;

// For a window to be visible, it must be visible to the user in the
// foreground of the app, which means that it should not be minimized or
// occluded
return [window_ isVisible] && !occluded && !IsMinimized();
}

Expand Down
10 changes: 10 additions & 0 deletions shell/browser/native_window_views.cc
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,17 @@ void NativeWindowViews::Hide() {
}

bool NativeWindowViews::IsVisible() {
#if BUILDFLAG(IS_WIN)
// widget()->IsVisible() calls ::IsWindowVisible, which returns non-zero if a
// window or any of its parent windows are visible. We want to only check the
// current window.
bool visible =
::GetWindowLong(GetAcceleratedWidget(), GWL_STYLE) & WS_VISIBLE;
// WS_VISIBLE is true even if a window is miminized - explicitly check that.
return visible && !IsMinimized();
#else
return widget()->IsVisible();
#endif
}

bool NativeWindowViews::IsEnabled() {
Expand Down
12 changes: 12 additions & 0 deletions spec/api-browser-window-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,18 @@ describe('BrowserWindow module', () => {
});
});

describe('BrowserWindow.minimize()', () => {
// TODO(codebytere): Enable for Linux once maximize/minimize events work in CI.
ifit(process.platform !== 'linux')('should not be visible when the window is minimized', async () => {
const minimize = once(w, 'minimize');
w.minimize();
await minimize;

expect(w.isMinimized()).to.equal(true);
expect(w.isVisible()).to.equal(false);
});
});

describe('BrowserWindow.showInactive()', () => {
it('should not focus on window', () => {
w.showInactive();
Expand Down