Simplify the setStyleMask calls

This commit is contained in:
Cheng Zhao 2016-01-23 03:12:19 -08:00
parent 239bfe970c
commit 010cc3276a
2 changed files with 39 additions and 53 deletions

View file

@ -120,6 +120,10 @@ class NativeWindowMac : public NativeWindow {
// whehter we can drag. // whehter we can drag.
void UpdateDraggableRegionViews(const std::vector<DraggableRegion>& regions); void UpdateDraggableRegionViews(const std::vector<DraggableRegion>& regions);
// Set the attribute of NSWindow while work around a bug of zo0m button.
void SetStyleMask(bool on, NSUInteger flag);
void SetCollectionBehavior(bool on, NSUInteger flag);
base::scoped_nsobject<AtomNSWindow> window_; base::scoped_nsobject<AtomNSWindow> window_;
base::scoped_nsobject<AtomNSWindowDelegate> window_delegate_; base::scoped_nsobject<AtomNSWindowDelegate> window_delegate_;

View file

@ -639,18 +639,10 @@ void NativeWindowMac::SetContentSizeConstraints(
} }
void NativeWindowMac::SetResizable(bool resizable) { void NativeWindowMac::SetResizable(bool resizable) {
bool maximizable = IsMaximizable();
// Change styleMask for frameless causes the window to change size, so we have // Change styleMask for frameless causes the window to change size, so we have
// to explicitly disables that. // to explicitly disables that.
ScopedDisableResize disable_resize; ScopedDisableResize disable_resize;
if (resizable) { SetStyleMask(resizable, NSResizableWindowMask);
[window_ setStyleMask:[window_ styleMask] | NSResizableWindowMask];
} else {
[window_ setStyleMask:[window_ styleMask] & (~NSResizableWindowMask)];
}
if (!maximizable) {
SetMaximizable(false);
}
} }
bool NativeWindowMac::IsResizable() { bool NativeWindowMac::IsResizable() {
@ -666,20 +658,7 @@ bool NativeWindowMac::IsMovable() {
} }
void NativeWindowMac::SetMinimizable(bool minimizable) { void NativeWindowMac::SetMinimizable(bool minimizable) {
bool maximizable = IsMaximizable(); SetStyleMask(minimizable, NSMiniaturizableWindowMask);
if (minimizable) {
[window_ setStyleMask:[window_ styleMask] | NSMiniaturizableWindowMask];
} else {
[window_ setStyleMask:[window_ styleMask] & (~NSMiniaturizableWindowMask)];
}
// If fullscreen has not been disabled via `fullscreenable: false` (i.e. when
// collectionBehavior has NSWindowCollectionBehaviorFullScreenPrimary mask),
// zoom button is reset to it's default (enabled) state when window's
// styleMask has been changed. So if the button was disabled, we have to
// disable it again. I think it's a bug in Cocoa.
if (!maximizable) {
SetMaximizable(false);
}
} }
bool NativeWindowMac::IsMinimizable() { bool NativeWindowMac::IsMinimizable() {
@ -695,35 +674,20 @@ bool NativeWindowMac::IsMaximizable() {
} }
void NativeWindowMac::SetFullScreenable(bool fullscreenable) { void NativeWindowMac::SetFullScreenable(bool fullscreenable) {
bool maximizable = IsMaximizable(); SetCollectionBehavior(
NSUInteger collectionBehavior = [window_ collectionBehavior]; fullscreenable, NSWindowCollectionBehaviorFullScreenPrimary);
if (fullscreenable) { // On EL Capitan this flag is required to hide fullscreen button.
collectionBehavior |= NSWindowCollectionBehaviorFullScreenPrimary; SetCollectionBehavior(
} else { !fullscreenable, NSWindowCollectionBehaviorFullScreenAuxiliary);
collectionBehavior &= (~NSWindowCollectionBehaviorFullScreenPrimary);
// On EL Capitan this flag is required to hide fullscreen button.
collectionBehavior |= NSWindowCollectionBehaviorFullScreenAuxiliary;
}
[window_ setCollectionBehavior:collectionBehavior];
if (!maximizable) {
SetMaximizable(false);
}
} }
bool NativeWindowMac::IsFullScreenable() { bool NativeWindowMac::IsFullScreenable() {
return [window_ collectionBehavior] & NSWindowCollectionBehaviorFullScreenPrimary; NSUInteger collectionBehavior = [window_ collectionBehavior];
return collectionBehavior & NSWindowCollectionBehaviorFullScreenPrimary;
} }
void NativeWindowMac::SetClosable(bool closable) { void NativeWindowMac::SetClosable(bool closable) {
bool maximizable = IsMaximizable(); SetStyleMask(closable, NSClosableWindowMask);
if (closable) {
[window_ setStyleMask:[window_ styleMask] | NSClosableWindowMask];
} else {
[window_ setStyleMask:[window_ styleMask] & (~NSClosableWindowMask)];
}
if (!maximizable) {
SetMaximizable(false);
}
} }
bool NativeWindowMac::IsClosable() { bool NativeWindowMac::IsClosable() {
@ -890,13 +854,7 @@ void NativeWindowMac::ShowDefinitionForSelection() {
} }
void NativeWindowMac::SetVisibleOnAllWorkspaces(bool visible) { void NativeWindowMac::SetVisibleOnAllWorkspaces(bool visible) {
NSUInteger collectionBehavior = [window_ collectionBehavior]; SetCollectionBehavior(visible, NSWindowCollectionBehaviorCanJoinAllSpaces);
if (visible) {
collectionBehavior |= NSWindowCollectionBehaviorCanJoinAllSpaces;
} else {
collectionBehavior &= ~NSWindowCollectionBehaviorCanJoinAllSpaces;
}
[window_ setCollectionBehavior:collectionBehavior];
} }
bool NativeWindowMac::IsVisibleOnAllWorkspaces() { bool NativeWindowMac::IsVisibleOnAllWorkspaces() {
@ -1053,6 +1011,30 @@ void NativeWindowMac::UpdateDraggableRegionViews(
[window_ setMovableByWindowBackground:YES]; [window_ setMovableByWindowBackground:YES];
} }
void NativeWindowMac::SetStyleMask(bool on, NSUInteger flag) {
bool zoom_button_enabled = IsMaximizable();
if (on)
[window_ setStyleMask:[window_ styleMask] | flag];
else
[window_ setStyleMask:[window_ styleMask] & (~flag)];
// Change style mask will make the zoom button revert to default, probably
// a bug of Cocoa or OS X.
if (!zoom_button_enabled)
SetMaximizable(false);
}
void NativeWindowMac::SetCollectionBehavior(bool on, NSUInteger flag) {
bool zoom_button_enabled = IsMaximizable();
if (on)
[window_ setCollectionBehavior:[window_ collectionBehavior] | flag];
else
[window_ setCollectionBehavior:[window_ collectionBehavior] & (~flag)];
// Change collectionBehavior will make the zoom button revert to default,
// probably a bug of Cocoa or OS X.
if (!zoom_button_enabled)
SetMaximizable(false);
}
// static // static
NativeWindow* NativeWindow::Create( NativeWindow* NativeWindow::Create(
brightray::InspectableWebContents* inspectable_web_contents, brightray::InspectableWebContents* inspectable_web_contents,