Skip to content

Commit

Permalink
fix: fullscreen windows aren't resizable on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed May 30, 2022
1 parent 03d9615 commit e9fd621
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
1 change: 1 addition & 0 deletions shell/browser/native_window_mac.h
Expand Up @@ -167,6 +167,7 @@ class NativeWindowMac : public NativeWindow,
void UpdateVibrancyRadii(bool fullscreen);

// Set the attribute of NSWindow while work around a bug of zoom button.
bool GetStyleMask(NSUInteger flag) const;
void SetStyleMask(bool on, NSUInteger flag);
void SetCollectionBehavior(bool on, NSUInteger flag);
void SetWindowLevel(int level);
Expand Down
20 changes: 13 additions & 7 deletions shell/browser/native_window_mac.mm
Expand Up @@ -641,7 +641,7 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) {
}

bool NativeWindowMac::IsMaximized() {
if (([window_ styleMask] & NSWindowStyleMaskResizable) != 0)
if (GetStyleMask(NSWindowStyleMaskResizable) != 0)
return [window_ isZoomed];

NSRect rectScreen = GetAspectRatio() > 0.0
Expand Down Expand Up @@ -715,7 +715,7 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) {
}

bool NativeWindowMac::IsFullscreen() const {
return [window_ styleMask] & NSWindowStyleMaskFullScreen;
return GetStyleMask(NSWindowStyleMaskFullScreen);
}

void NativeWindowMac::SetBounds(const gfx::Rect& bounds, bool animate) {
Expand Down Expand Up @@ -815,7 +815,10 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) {
}

bool NativeWindowMac::IsResizable() {
return [window_ styleMask] & NSWindowStyleMaskResizable;
bool in_fs_transition =
fullscreen_transition_state() != FullScreenTransitionState::NONE;
bool has_rs_mask = GetStyleMask(NSWindowStyleMaskResizable);
return has_rs_mask && !IsFullscreen() && !in_fs_transition;
}

void NativeWindowMac::SetMovable(bool movable) {
Expand All @@ -831,7 +834,7 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) {
}

bool NativeWindowMac::IsMinimizable() {
return [window_ styleMask] & NSMiniaturizableWindowMask;
return GetStyleMask(NSMiniaturizableWindowMask);
}

void NativeWindowMac::SetMaximizable(bool maximizable) {
Expand Down Expand Up @@ -861,7 +864,7 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) {
}

bool NativeWindowMac::IsClosable() {
return [window_ styleMask] & NSWindowStyleMaskClosable;
return GetStyleMask(NSWindowStyleMaskClosable);
}

void NativeWindowMac::SetAlwaysOnTop(ui::ZOrderLevel z_order,
Expand Down Expand Up @@ -1374,8 +1377,7 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) {
NSVisualEffectView* vibrantView = [window_ vibrantView];

if (vibrantView != nil && !vibrancy_type_.empty()) {
const bool no_rounded_corner =
!([window_ styleMask] & NSWindowStyleMaskTitled);
const bool no_rounded_corner = !GetStyleMask(NSWindowStyleMaskTitled);
if (!has_frame() && !is_modal() && !no_rounded_corner) {
CGFloat radius;
if (fullscreen) {
Expand Down Expand Up @@ -1743,6 +1745,10 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) {
AddContentViewLayers();
}

bool NativeWindowMac::GetStyleMask(NSUInteger flag) const {
return [window_ styleMask] & flag;
}

void NativeWindowMac::SetStyleMask(bool on, NSUInteger flag) {
// Changing the styleMask of a frameless windows causes it to change size so
// we explicitly disable resizing while setting it.
Expand Down
6 changes: 4 additions & 2 deletions shell/browser/ui/cocoa/electron_ns_window_delegate.mm
Expand Up @@ -234,12 +234,14 @@ - (void)windowDidEndLiveResize:(NSNotification*)notification {
}

- (void)windowWillEnterFullScreen:(NSNotification*)notification {
// Store resizable mask so it can be restored after exiting fullscreen.
is_resizable_ = shell_->GetStyleMask(NSResizableWindowMask);

shell_->SetFullScreenTransitionState(FullScreenTransitionState::ENTERING);

shell_->NotifyWindowWillEnterFullScreen();

// Setting resizable to true before entering fullscreen.
is_resizable_ = shell_->IsResizable();
// Set resizable to true before entering fullscreen.
shell_->SetResizable(true);
}

Expand Down
32 changes: 30 additions & 2 deletions spec-main/api-browser-window-spec.ts
Expand Up @@ -4208,6 +4208,16 @@ describe('BrowserWindow module', () => {
}
});

ifit(process.platform === 'darwin')('is false when the window is fullscreen', async () => {
const w = new BrowserWindow();

const enterFS = emittedOnce(w, 'enter-full-screen');
w.setFullScreen(true);
await enterFS;

expect(w.resizable).to.be.false('resizable');
});

// On Linux there is no "resizable" property of a window.
ifit(process.platform !== 'linux')('does affect maximizability when disabled and enabled', () => {
const w = new BrowserWindow({ show: false });
Expand Down Expand Up @@ -4650,19 +4660,37 @@ describe('BrowserWindow module', () => {
});

ifdescribe(process.platform === 'darwin')('fullscreen state with resizable set', () => {
it('resizable flag should be set to true and restored', async () => {
it('resizable flag should be set to false and restored', async () => {
const w = new BrowserWindow({ resizable: false });
expect(w.resizable).to.be.false('resizable');

const enterFullScreen = emittedOnce(w, 'enter-full-screen');
w.setFullScreen(true);
await enterFullScreen;
expect(w.resizable).to.be.true('resizable');
expect(w.resizable).to.be.false('resizable');

await delay();
const leaveFullScreen = emittedOnce(w, 'leave-full-screen');
w.setFullScreen(false);
await leaveFullScreen;
expect(w.resizable).to.be.false('resizable');
});

it('default resizable flag should be restored after entering/exiting fullscreen', async () => {
const w = new BrowserWindow();
expect(w.resizable).to.be.true('resizable');

const enterFullScreen = emittedOnce(w, 'enter-full-screen');
w.setFullScreen(true);
await enterFullScreen;
expect(w.resizable).to.be.false('resizable');

await delay();
const leaveFullScreen = emittedOnce(w, 'leave-full-screen');
w.setFullScreen(false);
await leaveFullScreen;
expect(w.resizable).to.be.true('resizable');
});
});

ifdescribe(process.platform === 'darwin')('fullscreen state', () => {
Expand Down

0 comments on commit e9fd621

Please sign in to comment.