From 31064785106cda5fb984a0af6bd1c8ddbf68e74e Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Wed, 7 Jun 2017 01:18:22 +0300 Subject: [PATCH 1/3] Rename SemaphoreView to CustomWindowButtonView --- atom/browser/native_window_mac.mm | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index e4efaa477b30..5a8527e48938 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -46,15 +46,15 @@ bool ScopedDisableResize::disable_resize_ = false; } // namespace -// This view encapsuates the Quit, Minimize and Full Screen buttons. It is being -// used for frameless windows. -@interface SemaphoreView : NSView { +// Custom Quit, Minimize and Full Screen button container for frameless +// windows. +@interface CustomWindowButtonView : NSView { @private BOOL mouse_inside_; } @end -@implementation SemaphoreView +@implementation CustomWindowButtonView - (id)initWithFrame:(NSRect)frame { self = [super initWithFrame:frame]; @@ -1691,13 +1691,15 @@ void NativeWindowMac::InstallView() { [[window_ standardWindowButton:NSWindowFullScreenButton] setHidden:YES]; if (title_bar_style_ == CUSTOM_BUTTONS_ON_HOVER) { - NSView* buttons = - [[[SemaphoreView alloc] initWithFrame:NSZeroRect] autorelease]; - 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]; + NSView* window_button_view = [[[CustomWindowButtonView alloc] + initWithFrame:NSZeroRect] autorelease]; + window_button_view.frame = + CGRectMake(0, + [content_view_ bounds].size.height - + window_button_view.frame.size.height, + window_button_view.frame.size.width, + window_button_view.frame.size.height); + [content_view_ addSubview:window_button_view]; } else { if (title_bar_style_ != NORMAL) { if (base::mac::IsOS10_9()) { From f60315141bca327323cc19ca619074f7520f5a84 Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Wed, 7 Jun 2017 01:19:53 +0300 Subject: [PATCH 2/3] Remove unnecessary if check --- atom/browser/native_window_mac.mm | 85 +++++++++++++++---------------- 1 file changed, 41 insertions(+), 44 deletions(-) diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index 5a8527e48938..a1a60d2a1014 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -59,57 +59,54 @@ bool ScopedDisableResize::disable_resize_ = false; - (id)initWithFrame:(NSRect)frame { self = [super initWithFrame:frame]; - if (self) { - mouse_inside_ = NO; + mouse_inside_ = NO; - // create buttons - NSButton* closeButton = [NSWindow standardWindowButton:NSWindowCloseButton - forStyleMask:NSTitledWindowMask]; - NSButton* minitButton = [NSWindow standardWindowButton:NSWindowMiniaturizeButton - forStyleMask:NSTitledWindowMask]; - NSButton* fullScreenButton = [NSWindow standardWindowButton:NSWindowZoomButton - forStyleMask:NSTitledWindowMask]; + NSButton* closeButton = [NSWindow standardWindowButton:NSWindowCloseButton + forStyleMask:NSTitledWindowMask]; + NSButton* minitButton = [NSWindow standardWindowButton:NSWindowMiniaturizeButton + forStyleMask:NSTitledWindowMask]; + NSButton* fullScreenButton = [NSWindow standardWindowButton:NSWindowZoomButton + forStyleMask:NSTitledWindowMask]; - // size view for buttons - const int top = 3; - const int bottom = 3; - const int left = 7; - const int between = 6; - const int right = 6; + // size view for buttons + const int top = 3; + const int bottom = 3; + const int left = 7; + const int between = 6; + const int right = 6; - auto buttonsSize = NSMakeRect(0, - 0, - left + closeButton.frame.size.width + between + minitButton.frame.size.width + between + fullScreenButton.frame.size.width + right, - top + closeButton.frame.size.height + bottom); - [self setFrame:buttonsSize]; + auto buttonsSize = NSMakeRect(0, + 0, + left + closeButton.frame.size.width + between + minitButton.frame.size.width + between + fullScreenButton.frame.size.width + right, + top + closeButton.frame.size.height + bottom); + [self setFrame:buttonsSize]; - //set their location - [closeButton setFrame:NSMakeRect(left, - buttonsSize.size.height - closeButton.frame.size.height - top, - closeButton.frame.size.width, - closeButton.frame.size.height)]; - [minitButton setFrame:NSMakeRect( - left + closeButton.frame.size.width + between, - buttonsSize.size.height - minitButton.frame.size.height - top, - minitButton.frame.size.width, - minitButton.frame.size.height)]; - [fullScreenButton setFrame:NSMakeRect( - left + closeButton.frame.size.width + between + minitButton.frame.size.width + between, - buttonsSize.size.height - fullScreenButton.frame.size.height - top, - fullScreenButton.frame.size.width, - fullScreenButton.frame.size.height)]; + //set their location + [closeButton setFrame:NSMakeRect(left, + buttonsSize.size.height - closeButton.frame.size.height - top, + closeButton.frame.size.width, + closeButton.frame.size.height)]; + [minitButton setFrame:NSMakeRect( + left + closeButton.frame.size.width + between, + buttonsSize.size.height - minitButton.frame.size.height - top, + minitButton.frame.size.width, + minitButton.frame.size.height)]; + [fullScreenButton setFrame:NSMakeRect( + left + closeButton.frame.size.width + between + minitButton.frame.size.width + between, + buttonsSize.size.height - fullScreenButton.frame.size.height - top, + fullScreenButton.frame.size.width, + fullScreenButton.frame.size.height)]; - //add buttons to the window - [self addSubview:closeButton]; - [self addSubview:minitButton]; - [self addSubview:fullScreenButton]; + //add buttons to the window + [self addSubview:closeButton]; + [self addSubview:minitButton]; + [self addSubview:fullScreenButton]; - // stay in upper left corner - [self setAutoresizingMask: NSViewMaxXMargin | NSViewMinYMargin]; + // stay in upper left corner + [self setAutoresizingMask: NSViewMaxXMargin | NSViewMinYMargin]; - // refresh for initial conditions - [self setNeedsDisplayForButtons]; - } + // refresh for initial conditions + [self setNeedsDisplayForButtons]; return self; } From 391c07b80ac838d20ada53ad0a7f19c56cfbb44d Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Wed, 7 Jun 2017 01:22:15 +0300 Subject: [PATCH 3/3] Simplify custom window button positioning --- atom/browser/native_window_mac.mm | 106 ++++++++++++++---------------- 1 file changed, 49 insertions(+), 57 deletions(-) diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index a1a60d2a1014..f46400ec0b02 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -59,68 +59,66 @@ bool ScopedDisableResize::disable_resize_ = false; - (id)initWithFrame:(NSRect)frame { self = [super initWithFrame:frame]; + NSButton* close_button = [NSWindow standardWindowButton:NSWindowCloseButton + forStyleMask:NSTitledWindowMask]; + NSButton* miniaturize_button = + [NSWindow standardWindowButton:NSWindowMiniaturizeButton + forStyleMask:NSTitledWindowMask]; + NSButton* zoom_button = [NSWindow standardWindowButton:NSWindowZoomButton + forStyleMask:NSTitledWindowMask]; + + CGFloat x = 0; + const CGFloat space_between = 20; + + [close_button setFrameOrigin:NSMakePoint(x, 0)]; + x += space_between; + [self addSubview:close_button]; + + [miniaturize_button setFrameOrigin:NSMakePoint(x, 0)]; + x += space_between; + [self addSubview:miniaturize_button]; + + [zoom_button setFrameOrigin:NSMakePoint(x, 0)]; + x += space_between; + [self addSubview:zoom_button]; + + const auto last_button_frame = zoom_button.frame; + [self setFrameSize:NSMakeSize(last_button_frame.origin.x + + last_button_frame.size.width, + last_button_frame.size.height)]; + mouse_inside_ = NO; - - NSButton* closeButton = [NSWindow standardWindowButton:NSWindowCloseButton - forStyleMask:NSTitledWindowMask]; - NSButton* minitButton = [NSWindow standardWindowButton:NSWindowMiniaturizeButton - forStyleMask:NSTitledWindowMask]; - NSButton* fullScreenButton = [NSWindow standardWindowButton:NSWindowZoomButton - forStyleMask:NSTitledWindowMask]; - - // size view for buttons - const int top = 3; - const int bottom = 3; - const int left = 7; - const int between = 6; - const int right = 6; - - auto buttonsSize = NSMakeRect(0, - 0, - left + closeButton.frame.size.width + between + minitButton.frame.size.width + between + fullScreenButton.frame.size.width + right, - top + closeButton.frame.size.height + bottom); - [self setFrame:buttonsSize]; - - //set their location - [closeButton setFrame:NSMakeRect(left, - buttonsSize.size.height - closeButton.frame.size.height - top, - closeButton.frame.size.width, - closeButton.frame.size.height)]; - [minitButton setFrame:NSMakeRect( - left + closeButton.frame.size.width + between, - buttonsSize.size.height - minitButton.frame.size.height - top, - minitButton.frame.size.width, - minitButton.frame.size.height)]; - [fullScreenButton setFrame:NSMakeRect( - left + closeButton.frame.size.width + between + minitButton.frame.size.width + between, - buttonsSize.size.height - fullScreenButton.frame.size.height - top, - fullScreenButton.frame.size.width, - fullScreenButton.frame.size.height)]; - - //add buttons to the window - [self addSubview:closeButton]; - [self addSubview:minitButton]; - [self addSubview:fullScreenButton]; - - // stay in upper left corner - [self setAutoresizingMask: NSViewMaxXMargin | NSViewMinYMargin]; - - // refresh for initial conditions [self setNeedsDisplayForButtons]; return self; } +- (void)viewDidMoveToWindow { + if (!self.window) { + return; + } + + // Stay in upper left corner. + const CGFloat top_margin = 3; + const CGFloat left_margin = 7; + [self setAutoresizingMask:NSViewMaxXMargin | NSViewMinYMargin]; + [self setFrameOrigin:NSMakePoint(left_margin, self.window.frame.size.height - + self.frame.size.height - + top_margin)]; +} + - (BOOL)_mouseInGroup:(NSButton*)button { return mouse_inside_; } - (void)updateTrackingAreas { - auto trackingArea = [[[NSTrackingArea alloc] initWithRect:NSZeroRect - options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect - owner:self - userInfo:nil] autorelease]; - [self addTrackingArea:trackingArea]; + auto tracking_area = [[[NSTrackingArea alloc] + initWithRect:NSZeroRect + options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | + NSTrackingInVisibleRect + owner:self + userInfo:nil] autorelease]; + [self addTrackingArea:tracking_area]; } - (void)mouseEntered:(NSEvent*)event { @@ -1690,12 +1688,6 @@ void NativeWindowMac::InstallView() { if (title_bar_style_ == CUSTOM_BUTTONS_ON_HOVER) { NSView* window_button_view = [[[CustomWindowButtonView alloc] initWithFrame:NSZeroRect] autorelease]; - window_button_view.frame = - CGRectMake(0, - [content_view_ bounds].size.height - - window_button_view.frame.size.height, - window_button_view.frame.size.width, - window_button_view.frame.size.height); [content_view_ addSubview:window_button_view]; } else { if (title_bar_style_ != NORMAL) {