From f4868c9a28392cd9cd78b37212b03d5d0000a69a Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Tue, 17 Mar 2020 10:58:57 -0700 Subject: [PATCH] fix: persist maximizable state through theme change (#22677) --- shell/browser/native_window_mac.h | 5 +++- shell/browser/native_window_mac.mm | 23 +++++++++++-------- .../ui/cocoa/electron_ns_window_delegate.mm | 6 ++--- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/shell/browser/native_window_mac.h b/shell/browser/native_window_mac.h index 869908c9f5e2..1c64a0fd730f 100644 --- a/shell/browser/native_window_mac.h +++ b/shell/browser/native_window_mac.h @@ -151,7 +151,7 @@ class NativeWindowMac : public NativeWindow, public ui::NativeThemeObserver { void SetWindowLevel(int level); // Custom traffic light positioning - void RepositionTrafficLights(); + void RedrawTrafficLights(); void SetExitingFullScreen(bool flag); void SetTrafficLightPosition(const gfx::Point& position) override; gfx::Point GetTrafficLightPosition() const override; @@ -222,6 +222,9 @@ class NativeWindowMac : public NativeWindow, public ui::NativeThemeObserver { // setWindowButtonVisibility(). base::Optional window_button_visibility_; + // Maximizable window state; necessary for persistence through redraws. + bool maximizable_ = true; + // Simple (pre-Lion) Fullscreen Settings bool always_simple_fullscreen_ = false; bool is_simple_fullscreen_ = false; diff --git a/shell/browser/native_window_mac.mm b/shell/browser/native_window_mac.mm index 57d0350dc44e..e19d6d33e795 100644 --- a/shell/browser/native_window_mac.mm +++ b/shell/browser/native_window_mac.mm @@ -47,13 +47,13 @@ // This view would inform Chromium to resize the hosted views::View. // // The overrided methods should behave the same with BridgedContentView. -@interface ElectronAdapatedContentView : NSView { +@interface ElectronAdaptedContentView : NSView { @private views::NativeWidgetMacNSWindowHost* bridge_host_; } @end -@implementation ElectronAdapatedContentView +@implementation ElectronAdaptedContentView - (id)initWithShell:(electron::NativeWindowMac*)shell { if ((self = [self init])) { @@ -519,7 +519,10 @@ NativeWindowMac::~NativeWindowMac() { [NSEvent removeMonitor:wheel_event_monitor_]; } -void NativeWindowMac::RepositionTrafficLights() { +void NativeWindowMac::RedrawTrafficLights() { + // Ensure maximizable options retain pre-existing state. + SetMaximizable(maximizable_); + if (!traffic_light_position_.x() && !traffic_light_position_.y()) { return; } @@ -708,7 +711,7 @@ void NativeWindowMac::SetExitingFullScreen(bool flag) { void NativeWindowMac::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) { base::PostTask(FROM_HERE, {content::BrowserThread::UI}, - base::BindOnce(&NativeWindowMac::RepositionTrafficLights, + base::BindOnce(&NativeWindowMac::RedrawTrafficLights, base::Unretained(this))); } @@ -930,6 +933,7 @@ bool NativeWindowMac::IsMinimizable() { } void NativeWindowMac::SetMaximizable(bool maximizable) { + maximizable_ = maximizable; [[window_ standardWindowButton:NSWindowZoomButton] setEnabled:maximizable]; } @@ -1009,8 +1013,7 @@ void NativeWindowMac::SetWindowLevel(int unbounded_level) { // Set level will make the zoom button revert to default, probably // a bug of Cocoa or macOS. - [[window_ standardWindowButton:NSWindowZoomButton] - setEnabled:was_maximizable_]; + SetMaximizable(was_maximizable_); // This must be notified at the very end or IsAlwaysOnTop // will not yet have been updated to reflect the new status @@ -1034,7 +1037,7 @@ void NativeWindowMac::Invalidate() { void NativeWindowMac::SetTitle(const std::string& title) { [window_ setTitle:base::SysUTF8ToNSString(title)]; if (title_bar_style_ == TitleBarStyle::HIDDEN) { - RepositionTrafficLights(); + RedrawTrafficLights(); } } @@ -1554,7 +1557,7 @@ void NativeWindowMac::SetVibrancy(const std::string& type) { void NativeWindowMac::SetTrafficLightPosition(const gfx::Point& position) { traffic_light_position_ = position; - RepositionTrafficLights(); + RedrawTrafficLights(); } gfx::Point NativeWindowMac::GetTrafficLightPosition() const { @@ -1683,7 +1686,7 @@ void NativeWindowMac::AddContentViewLayers(bool minimizable, bool closable) { // Some third-party macOS utilities check the zoom button's enabled state to // determine whether to show custom UI on hover, so we disable it here to // prevent them from doing so in a frameless app window. - [[window_ standardWindowButton:NSWindowZoomButton] setEnabled:NO]; + SetMaximizable(false); } } @@ -1722,7 +1725,7 @@ void NativeWindowMac::OverrideNSWindowContentView() { // content view with a simple NSView. if (has_frame()) { container_view_.reset( - [[ElectronAdapatedContentView alloc] initWithShell:this]); + [[ElectronAdaptedContentView alloc] initWithShell:this]); } else { container_view_.reset([[FullSizeContentView alloc] init]); [container_view_ setFrame:[[[window_ contentView] superview] bounds]]; diff --git a/shell/browser/ui/cocoa/electron_ns_window_delegate.mm b/shell/browser/ui/cocoa/electron_ns_window_delegate.mm index d5d9fcc2ea5b..9ec36c71407c 100644 --- a/shell/browser/ui/cocoa/electron_ns_window_delegate.mm +++ b/shell/browser/ui/cocoa/electron_ns_window_delegate.mm @@ -137,7 +137,7 @@ using TitleBarStyle = electron::NativeWindowMac::TitleBarStyle; [super windowDidResize:notification]; shell_->NotifyWindowResize(); if (shell_->title_bar_style() == TitleBarStyle::HIDDEN) { - shell_->RepositionTrafficLights(); + shell_->RedrawTrafficLights(); } } @@ -254,7 +254,7 @@ using TitleBarStyle = electron::NativeWindowMac::TitleBarStyle; } shell_->SetExitingFullScreen(true); if (shell_->title_bar_style() == TitleBarStyle::HIDDEN) { - shell_->RepositionTrafficLights(); + shell_->RedrawTrafficLights(); } } @@ -263,7 +263,7 @@ using TitleBarStyle = electron::NativeWindowMac::TitleBarStyle; shell_->NotifyWindowLeaveFullScreen(); shell_->SetExitingFullScreen(false); if (shell_->title_bar_style() == TitleBarStyle::HIDDEN) { - shell_->RepositionTrafficLights(); + shell_->RedrawTrafficLights(); } }