From 994c44b4a6dd549d3114d20742df8a16097ad3fe Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Mon, 30 May 2022 10:25:19 +0200 Subject: [PATCH 1/3] fix: fullscreen windows aren't resizable on macOS --- shell/browser/native_window_mac.h | 1 + shell/browser/native_window_mac.mm | 20 ++++++++++++------- .../ui/cocoa/electron_ns_window_delegate.mm | 6 ++++-- spec-main/api-browser-window-spec.ts | 20 +++++++++++++++++-- 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/shell/browser/native_window_mac.h b/shell/browser/native_window_mac.h index 9b6fcb14dda59..f7ddf7072f5b1 100644 --- a/shell/browser/native_window_mac.h +++ b/shell/browser/native_window_mac.h @@ -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); diff --git a/shell/browser/native_window_mac.mm b/shell/browser/native_window_mac.mm index 1563a7b2cf09c..e6670fbdfdd22 100644 --- a/shell/browser/native_window_mac.mm +++ b/shell/browser/native_window_mac.mm @@ -646,7 +646,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 @@ -729,7 +729,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) { @@ -829,7 +829,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) { @@ -845,7 +848,7 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) { } bool NativeWindowMac::IsMinimizable() { - return [window_ styleMask] & NSWindowStyleMaskMiniaturizable; + return GetStyleMask(NSMiniaturizableWindowMask); } void NativeWindowMac::SetMaximizable(bool maximizable) { @@ -875,7 +878,7 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) { } bool NativeWindowMac::IsClosable() { - return [window_ styleMask] & NSWindowStyleMaskClosable; + return GetStyleMask(NSWindowStyleMaskClosable); } void NativeWindowMac::SetAlwaysOnTop(ui::ZOrderLevel z_order, @@ -1383,8 +1386,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) { @@ -1752,6 +1754,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. diff --git a/shell/browser/ui/cocoa/electron_ns_window_delegate.mm b/shell/browser/ui/cocoa/electron_ns_window_delegate.mm index 2467be47424f4..99bf2947b3e02 100644 --- a/shell/browser/ui/cocoa/electron_ns_window_delegate.mm +++ b/shell/browser/ui/cocoa/electron_ns_window_delegate.mm @@ -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); } diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts index 0de2068f9590c..c140ae15ae28c 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -4650,12 +4650,13 @@ 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 }); + 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'); @@ -4663,6 +4664,21 @@ describe('BrowserWindow module', () => { 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(); + + 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', () => { From 1ba5bbd248a9ee1898b23bcf4c44a9b4dc1434ab Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Tue, 31 May 2022 09:36:38 +0200 Subject: [PATCH 2/3] chore: GetStyleMask -> HasStyleMask --- shell/browser/native_window_mac.h | 2 +- shell/browser/native_window_mac.mm | 14 +++++++------- .../ui/cocoa/electron_ns_window_delegate.mm | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/shell/browser/native_window_mac.h b/shell/browser/native_window_mac.h index f7ddf7072f5b1..9b02cb44327b2 100644 --- a/shell/browser/native_window_mac.h +++ b/shell/browser/native_window_mac.h @@ -167,7 +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; + bool HasStyleMask(NSUInteger flag) const; void SetStyleMask(bool on, NSUInteger flag); void SetCollectionBehavior(bool on, NSUInteger flag); void SetWindowLevel(int level); diff --git a/shell/browser/native_window_mac.mm b/shell/browser/native_window_mac.mm index e6670fbdfdd22..32a2ec6bb483e 100644 --- a/shell/browser/native_window_mac.mm +++ b/shell/browser/native_window_mac.mm @@ -646,7 +646,7 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) { } bool NativeWindowMac::IsMaximized() { - if (GetStyleMask(NSWindowStyleMaskResizable) != 0) + if (HasStyleMask(NSWindowStyleMaskResizable) != 0) return [window_ isZoomed]; NSRect rectScreen = GetAspectRatio() > 0.0 @@ -729,7 +729,7 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) { } bool NativeWindowMac::IsFullscreen() const { - return GetStyleMask(NSWindowStyleMaskFullScreen); + return HasStyleMask(NSWindowStyleMaskFullScreen); } void NativeWindowMac::SetBounds(const gfx::Rect& bounds, bool animate) { @@ -831,7 +831,7 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) { bool NativeWindowMac::IsResizable() { bool in_fs_transition = fullscreen_transition_state() != FullScreenTransitionState::NONE; - bool has_rs_mask = GetStyleMask(NSWindowStyleMaskResizable); + bool has_rs_mask = HasStyleMask(NSWindowStyleMaskResizable); return has_rs_mask && !IsFullscreen() && !in_fs_transition; } @@ -848,7 +848,7 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) { } bool NativeWindowMac::IsMinimizable() { - return GetStyleMask(NSMiniaturizableWindowMask); + return HasStyleMask(NSMiniaturizableWindowMask); } void NativeWindowMac::SetMaximizable(bool maximizable) { @@ -878,7 +878,7 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) { } bool NativeWindowMac::IsClosable() { - return GetStyleMask(NSWindowStyleMaskClosable); + return HasStyleMask(NSWindowStyleMaskClosable); } void NativeWindowMac::SetAlwaysOnTop(ui::ZOrderLevel z_order, @@ -1386,7 +1386,7 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) { NSVisualEffectView* vibrantView = [window_ vibrantView]; if (vibrantView != nil && !vibrancy_type_.empty()) { - const bool no_rounded_corner = !GetStyleMask(NSWindowStyleMaskTitled); + const bool no_rounded_corner = !HasStyleMask(NSWindowStyleMaskTitled); if (!has_frame() && !is_modal() && !no_rounded_corner) { CGFloat radius; if (fullscreen) { @@ -1754,7 +1754,7 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) { AddContentViewLayers(); } -bool NativeWindowMac::GetStyleMask(NSUInteger flag) const { +bool NativeWindowMac::HasStyleMask(NSUInteger flag) const { return [window_ styleMask] & flag; } diff --git a/shell/browser/ui/cocoa/electron_ns_window_delegate.mm b/shell/browser/ui/cocoa/electron_ns_window_delegate.mm index 99bf2947b3e02..6206891f0e69b 100644 --- a/shell/browser/ui/cocoa/electron_ns_window_delegate.mm +++ b/shell/browser/ui/cocoa/electron_ns_window_delegate.mm @@ -235,7 +235,7 @@ - (void)windowDidEndLiveResize:(NSNotification*)notification { - (void)windowWillEnterFullScreen:(NSNotification*)notification { // Store resizable mask so it can be restored after exiting fullscreen. - is_resizable_ = shell_->GetStyleMask(NSResizableWindowMask); + is_resizable_ = shell_->HasStyleMask(NSResizableWindowMask); shell_->SetFullScreenTransitionState(FullScreenTransitionState::ENTERING); From 5c8b6fa94656eb332cfaaff3659073b9f874af63 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 2 Jun 2022 15:24:24 +0900 Subject: [PATCH 3/3] chore: depreacate style names --- shell/browser/native_window_mac.mm | 2 +- shell/browser/ui/cocoa/electron_ns_window_delegate.mm | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/shell/browser/native_window_mac.mm b/shell/browser/native_window_mac.mm index 32a2ec6bb483e..8896662b1a9e1 100644 --- a/shell/browser/native_window_mac.mm +++ b/shell/browser/native_window_mac.mm @@ -848,7 +848,7 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) { } bool NativeWindowMac::IsMinimizable() { - return HasStyleMask(NSMiniaturizableWindowMask); + return HasStyleMask(NSWindowStyleMaskMiniaturizable); } void NativeWindowMac::SetMaximizable(bool maximizable) { diff --git a/shell/browser/ui/cocoa/electron_ns_window_delegate.mm b/shell/browser/ui/cocoa/electron_ns_window_delegate.mm index 6206891f0e69b..57b2b90ba0444 100644 --- a/shell/browser/ui/cocoa/electron_ns_window_delegate.mm +++ b/shell/browser/ui/cocoa/electron_ns_window_delegate.mm @@ -235,7 +235,7 @@ - (void)windowDidEndLiveResize:(NSNotification*)notification { - (void)windowWillEnterFullScreen:(NSNotification*)notification { // Store resizable mask so it can be restored after exiting fullscreen. - is_resizable_ = shell_->HasStyleMask(NSResizableWindowMask); + is_resizable_ = shell_->HasStyleMask(NSWindowStyleMaskResizable); shell_->SetFullScreenTransitionState(FullScreenTransitionState::ENTERING);