From 1493d6763c2d0522c79344f5279e12196e5a9bf0 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 5 Jun 2017 12:50:18 -0700 Subject: [PATCH] Make custom window buttons an option --- atom/browser/native_window_mac.h | 4 ++ atom/browser/native_window_mac.mm | 75 ++++++++++++++++++++++--------- atom/common/options_switches.cc | 3 ++ atom/common/options_switches.h | 1 + 4 files changed, 61 insertions(+), 22 deletions(-) diff --git a/atom/browser/native_window_mac.h b/atom/browser/native_window_mac.h index b5271e4d436..161ba5bbc0a 100644 --- a/atom/browser/native_window_mac.h +++ b/atom/browser/native_window_mac.h @@ -132,6 +132,8 @@ class NativeWindowMac : public NativeWindow, bool zoom_to_page_width() const { return zoom_to_page_width_; } + bool custom_window_buttons() const { return custom_window_buttons_; } + protected: // Return a vector of non-draggable regions that fill a window of size // |width| by |height|, but leave gaps where the window should be draggable. @@ -176,6 +178,8 @@ class NativeWindowMac : public NativeWindow, bool zoom_to_page_width_; + bool custom_window_buttons_; + NSInteger attention_request_id_; // identifier from requestUserAttention // The presentation options before entering kiosk mode. diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index 940ce89fb01..1ffe708a8a5 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -398,7 +398,7 @@ BOOL mouseInside = NO; - (BOOL)windowShouldClose:(id)window { // When user tries to close the window by clicking the close button, we do // not close the window immediately, instead we try to close the web page - // fisrt, and when the web page is closed the window will also be closed. + // first, and when the web page is closed the window will also be closed. shell_->RequestToClosePage(); return NO; } @@ -697,25 +697,22 @@ enum { // Custom window button methods -- (BOOL)validateMenuItem:(NSMenuItem*)menuItem { - return ([menuItem action] == @selector(performClose:) || [menuItem action] == @selector(performMiniaturize:)) ? YES : [super validateMenuItem:menuItem]; -} - -- (BOOL)windowShouldClose:(id)sender { - return YES; -} - - (void)performClose:(id)sender { - if ([[self delegate] respondsToSelector:@selector(windowShouldClose:)]) { - if (![[self delegate] windowShouldClose:self]) return; - } else if ([self respondsToSelector:@selector(windowShouldClose:)]) { - if (![self windowShouldClose:self]) return; + if (shell_->custom_window_buttons()) { + [[self delegate] windowShouldClose:self]; + return; } - [self close]; + + [super performClose:sender]; } - (void)performMiniaturize:(id)sender { - [self miniaturize:self]; + if (shell_->custom_window_buttons()) { + [self miniaturize:self]; + return; + } + + [super performMiniaturize:sender]; } @end @@ -806,6 +803,7 @@ NativeWindowMac::NativeWindowMac( is_kiosk_(false), was_fullscreen_(false), zoom_to_page_width_(false), + custom_window_buttons_(false), attention_request_id_(0), title_bar_style_(NORMAL) { int width = 800, height = 600; @@ -847,7 +845,15 @@ NativeWindowMac::NativeWindowMac( useStandardWindow = false; } - NSUInteger styleMask = (base::mac::IsAtLeastOS10_10() && (!useStandardWindow || transparent() || !has_frame()))? NSFullSizeContentViewWindowMask : NSTitledWindowMask; + options.Get(options::kCustomWindowButtons, &custom_window_buttons_); + + NSUInteger styleMask = NSTitledWindowMask; + + if (custom_window_buttons_ && + base::mac::IsAtLeastOS10_10() && + (!useStandardWindow || transparent() || !has_frame())) { + styleMask = NSFullSizeContentViewWindowMask; + } if (minimizable) { styleMask |= NSMiniaturizableWindowMask; } @@ -902,7 +908,9 @@ NativeWindowMac::NativeWindowMac( if (transparent() || !has_frame()) { if (base::mac::IsAtLeastOS10_10()) { // Don't show title bar. - [window_ setTitlebarAppearsTransparent:YES]; + if (custom_window_buttons_) { + [window_ setTitlebarAppearsTransparent:YES]; + } [window_ setTitleVisibility:NSWindowTitleHidden]; } // Remove non-transparent corners, see http://git.io/vfonD. @@ -1653,6 +1661,11 @@ void NativeWindowMac::UpdateDraggableRegions( UpdateDraggableRegionViews(regions); } +void NativeWindowMac::ShowWindowButton(NSWindowButton button) { + auto view = [window_ standardWindowButton:button]; + [view.superview addSubview:view positioned:NSWindowAbove relativeTo:nil]; +} + void NativeWindowMac::InstallView() { // Make sure the bottom corner is rounded for non-modal windows: http://crbug.com/396264. // But do not enable it on OS X 10.9 for transparent window, otherwise a @@ -1678,14 +1691,32 @@ void NativeWindowMac::InstallView() { [view setFrame:[content_view_ bounds]]; [content_view_ addSubview:view]; - // add semaphore - NSView* buttons = [[SemaphoreView alloc] initWithFrame:NSZeroRect]; - buttons.frame = CGRectMake(0, [content_view_ bounds].size.height - buttons.frame.size.height, buttons.frame.size.width, buttons.frame.size.height); - [content_view_ addSubview:buttons]; - // The fullscreen button should always be hidden for frameless window. [[window_ standardWindowButton:NSWindowFullScreenButton] setHidden:YES]; + if (custom_window_buttons_) { + NSView* buttons = [[SemaphoreView alloc] initWithFrame:NSZeroRect]; + buttons.frame = CGRectMake(0, + [content_view_ bounds].size.height - buttons.frame.size.height, + buttons.frame.size.width, + buttons.frame.size.height); + [content_view_ addSubview:buttons]; + } else { + if (title_bar_style_ != NORMAL) { + if (base::mac::IsOS10_9()) { + ShowWindowButton(NSWindowZoomButton); + ShowWindowButton(NSWindowMiniaturizeButton); + ShowWindowButton(NSWindowCloseButton); + } + return; + } + + // Hide the window buttons. + [[window_ standardWindowButton:NSWindowZoomButton] setHidden:YES]; + [[window_ standardWindowButton:NSWindowMiniaturizeButton] setHidden:YES]; + [[window_ standardWindowButton:NSWindowCloseButton] setHidden:YES]; + } + // 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. diff --git a/atom/common/options_switches.cc b/atom/common/options_switches.cc index ce63fc716a3..30593cee24f 100644 --- a/atom/common/options_switches.cc +++ b/atom/common/options_switches.cc @@ -90,6 +90,9 @@ const char kWebPreferences[] = "webPreferences"; // Add a vibrancy effect to the browser window const char kVibrancyType[] = "vibrancy"; +// Use custom buttons for window close, minimize, maximize on macOS +const char kCustomWindowButtons[] = "customWindowButtons"; + // The factor of which page should be zoomed. const char kZoomFactor[] = "zoomFactor"; diff --git a/atom/common/options_switches.h b/atom/common/options_switches.h index 6fda408ee5c..33db0077205 100644 --- a/atom/common/options_switches.h +++ b/atom/common/options_switches.h @@ -49,6 +49,7 @@ extern const char kHasShadow[]; extern const char kFocusable[]; extern const char kWebPreferences[]; extern const char kVibrancyType[]; +extern const char kCustomWindowButtons[]; // WebPreferences. extern const char kZoomFactor[];