Make custom window buttons an option

This commit is contained in:
Kevin Sawicki 2017-06-05 12:50:18 -07:00
parent 2880cfdc9c
commit 1493d6763c
4 changed files with 61 additions and 22 deletions

View file

@ -132,6 +132,8 @@ class NativeWindowMac : public NativeWindow,
bool zoom_to_page_width() const { return zoom_to_page_width_; } bool zoom_to_page_width() const { return zoom_to_page_width_; }
bool custom_window_buttons() const { return custom_window_buttons_; }
protected: protected:
// Return a vector of non-draggable regions that fill a window of size // 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. // |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 zoom_to_page_width_;
bool custom_window_buttons_;
NSInteger attention_request_id_; // identifier from requestUserAttention NSInteger attention_request_id_; // identifier from requestUserAttention
// The presentation options before entering kiosk mode. // The presentation options before entering kiosk mode.

View file

@ -398,7 +398,7 @@ BOOL mouseInside = NO;
- (BOOL)windowShouldClose:(id)window { - (BOOL)windowShouldClose:(id)window {
// When user tries to close the window by clicking the close button, we do // 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 // 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(); shell_->RequestToClosePage();
return NO; return NO;
} }
@ -697,25 +697,22 @@ enum {
// Custom window button methods // 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 { - (void)performClose:(id)sender {
if ([[self delegate] respondsToSelector:@selector(windowShouldClose:)]) { if (shell_->custom_window_buttons()) {
if (![[self delegate] windowShouldClose:self]) return; [[self delegate] windowShouldClose:self];
} else if ([self respondsToSelector:@selector(windowShouldClose:)]) { return;
if (![self windowShouldClose:self]) return;
} }
[self close];
[super performClose:sender];
} }
- (void)performMiniaturize:(id)sender { - (void)performMiniaturize:(id)sender {
[self miniaturize:self]; if (shell_->custom_window_buttons()) {
[self miniaturize:self];
return;
}
[super performMiniaturize:sender];
} }
@end @end
@ -806,6 +803,7 @@ NativeWindowMac::NativeWindowMac(
is_kiosk_(false), is_kiosk_(false),
was_fullscreen_(false), was_fullscreen_(false),
zoom_to_page_width_(false), zoom_to_page_width_(false),
custom_window_buttons_(false),
attention_request_id_(0), attention_request_id_(0),
title_bar_style_(NORMAL) { title_bar_style_(NORMAL) {
int width = 800, height = 600; int width = 800, height = 600;
@ -847,7 +845,15 @@ NativeWindowMac::NativeWindowMac(
useStandardWindow = false; 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) { if (minimizable) {
styleMask |= NSMiniaturizableWindowMask; styleMask |= NSMiniaturizableWindowMask;
} }
@ -902,7 +908,9 @@ NativeWindowMac::NativeWindowMac(
if (transparent() || !has_frame()) { if (transparent() || !has_frame()) {
if (base::mac::IsAtLeastOS10_10()) { if (base::mac::IsAtLeastOS10_10()) {
// Don't show title bar. // Don't show title bar.
[window_ setTitlebarAppearsTransparent:YES]; if (custom_window_buttons_) {
[window_ setTitlebarAppearsTransparent:YES];
}
[window_ setTitleVisibility:NSWindowTitleHidden]; [window_ setTitleVisibility:NSWindowTitleHidden];
} }
// Remove non-transparent corners, see http://git.io/vfonD. // Remove non-transparent corners, see http://git.io/vfonD.
@ -1653,6 +1661,11 @@ void NativeWindowMac::UpdateDraggableRegions(
UpdateDraggableRegionViews(regions); UpdateDraggableRegionViews(regions);
} }
void NativeWindowMac::ShowWindowButton(NSWindowButton button) {
auto view = [window_ standardWindowButton:button];
[view.superview addSubview:view positioned:NSWindowAbove relativeTo:nil];
}
void NativeWindowMac::InstallView() { void NativeWindowMac::InstallView() {
// Make sure the bottom corner is rounded for non-modal windows: http://crbug.com/396264. // 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 // 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]]; [view setFrame:[content_view_ bounds]];
[content_view_ addSubview:view]; [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. // The fullscreen button should always be hidden for frameless window.
[[window_ standardWindowButton:NSWindowFullScreenButton] setHidden:YES]; [[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 // 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 // determine whether to show custom UI on hover, so we disable it here to
// prevent them from doing so in a frameless app window. // prevent them from doing so in a frameless app window.

View file

@ -90,6 +90,9 @@ const char kWebPreferences[] = "webPreferences";
// Add a vibrancy effect to the browser window // Add a vibrancy effect to the browser window
const char kVibrancyType[] = "vibrancy"; 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. // The factor of which page should be zoomed.
const char kZoomFactor[] = "zoomFactor"; const char kZoomFactor[] = "zoomFactor";

View file

@ -49,6 +49,7 @@ extern const char kHasShadow[];
extern const char kFocusable[]; extern const char kFocusable[];
extern const char kWebPreferences[]; extern const char kWebPreferences[];
extern const char kVibrancyType[]; extern const char kVibrancyType[];
extern const char kCustomWindowButtons[];
// WebPreferences. // WebPreferences.
extern const char kZoomFactor[]; extern const char kZoomFactor[];