Merge pull request #9687 from electron/fix-custom-window-button-view

Simplify customButtonsOnHover implementation
This commit is contained in:
Kevin Sawicki 2017-06-06 18:54:04 -07:00 committed by GitHub
commit 7a3a092c72

View file

@ -46,84 +46,79 @@ bool ScopedDisableResize::disable_resize_ = false;
} // namespace } // namespace
// This view encapsuates the Quit, Minimize and Full Screen buttons. It is being // Custom Quit, Minimize and Full Screen button container for frameless
// used for frameless windows. // windows.
@interface SemaphoreView : NSView { @interface CustomWindowButtonView : NSView {
@private @private
BOOL mouse_inside_; BOOL mouse_inside_;
} }
@end @end
@implementation SemaphoreView @implementation CustomWindowButtonView
- (id)initWithFrame:(NSRect)frame { - (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame]; self = [super initWithFrame:frame];
if (self) { NSButton* close_button = [NSWindow standardWindowButton:NSWindowCloseButton
mouse_inside_ = NO; forStyleMask:NSTitledWindowMask];
NSButton* miniaturize_button =
[NSWindow standardWindowButton:NSWindowMiniaturizeButton
forStyleMask:NSTitledWindowMask];
NSButton* zoom_button = [NSWindow standardWindowButton:NSWindowZoomButton
forStyleMask:NSTitledWindowMask];
// create buttons CGFloat x = 0;
NSButton* closeButton = [NSWindow standardWindowButton:NSWindowCloseButton const CGFloat space_between = 20;
forStyleMask:NSTitledWindowMask];
NSButton* minitButton = [NSWindow standardWindowButton:NSWindowMiniaturizeButton
forStyleMask:NSTitledWindowMask];
NSButton* fullScreenButton = [NSWindow standardWindowButton:NSWindowZoomButton
forStyleMask:NSTitledWindowMask];
// size view for buttons [close_button setFrameOrigin:NSMakePoint(x, 0)];
const int top = 3; x += space_between;
const int bottom = 3; [self addSubview:close_button];
const int left = 7;
const int between = 6;
const int right = 6;
auto buttonsSize = NSMakeRect(0, [miniaturize_button setFrameOrigin:NSMakePoint(x, 0)];
0, x += space_between;
left + closeButton.frame.size.width + between + minitButton.frame.size.width + between + fullScreenButton.frame.size.width + right, [self addSubview:miniaturize_button];
top + closeButton.frame.size.height + bottom);
[self setFrame:buttonsSize];
//set their location [zoom_button setFrameOrigin:NSMakePoint(x, 0)];
[closeButton setFrame:NSMakeRect(left, x += space_between;
buttonsSize.size.height - closeButton.frame.size.height - top, [self addSubview:zoom_button];
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 const auto last_button_frame = zoom_button.frame;
[self addSubview:closeButton]; [self setFrameSize:NSMakeSize(last_button_frame.origin.x +
[self addSubview:minitButton]; last_button_frame.size.width,
[self addSubview:fullScreenButton]; last_button_frame.size.height)];
// stay in upper left corner mouse_inside_ = NO;
[self setAutoresizingMask: NSViewMaxXMargin | NSViewMinYMargin]; [self setNeedsDisplayForButtons];
// refresh for initial conditions
[self setNeedsDisplayForButtons];
}
return self; 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 { - (BOOL)_mouseInGroup:(NSButton*)button {
return mouse_inside_; return mouse_inside_;
} }
- (void)updateTrackingAreas { - (void)updateTrackingAreas {
auto trackingArea = [[[NSTrackingArea alloc] initWithRect:NSZeroRect auto tracking_area = [[[NSTrackingArea alloc]
options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect initWithRect:NSZeroRect
owner:self options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways |
userInfo:nil] autorelease]; NSTrackingInVisibleRect
[self addTrackingArea:trackingArea]; owner:self
userInfo:nil] autorelease];
[self addTrackingArea:tracking_area];
} }
- (void)mouseEntered:(NSEvent*)event { - (void)mouseEntered:(NSEvent*)event {
@ -1691,13 +1686,9 @@ void NativeWindowMac::InstallView() {
[[window_ standardWindowButton:NSWindowFullScreenButton] setHidden:YES]; [[window_ standardWindowButton:NSWindowFullScreenButton] setHidden:YES];
if (title_bar_style_ == CUSTOM_BUTTONS_ON_HOVER) { if (title_bar_style_ == CUSTOM_BUTTONS_ON_HOVER) {
NSView* buttons = NSView* window_button_view = [[[CustomWindowButtonView alloc]
[[[SemaphoreView alloc] initWithFrame:NSZeroRect] autorelease]; initWithFrame:NSZeroRect] autorelease];
buttons.frame = CGRectMake(0, [content_view_ addSubview:window_button_view];
[content_view_ bounds].size.height - buttons.frame.size.height,
buttons.frame.size.width,
buttons.frame.size.height);
[content_view_ addSubview:buttons];
} else { } else {
if (title_bar_style_ != NORMAL) { if (title_bar_style_ != NORMAL) {
if (base::mac::IsOS10_9()) { if (base::mac::IsOS10_9()) {