]> khamidou — GitHub mirrors - electron.git/commitdiff
Simplify the setStyleMask calls
authorCheng Zhao <zcbenz@gmail.com>
Sat, 23 Jan 2016 11:12:19 +0000 (03:12 -0800)
committerCheng Zhao <zcbenz@gmail.com>
Sun, 24 Jan 2016 06:31:37 +0000 (14:31 +0800)
atom/browser/native_window_mac.h
atom/browser/native_window_mac.mm

index 83c2dffe66f0aae56966fe9d0aaa7cd6da064345..964126593ae0f1a27b7f8ad3f4d2c4ef1a4b295d 100644 (file)
@@ -120,6 +120,10 @@ class NativeWindowMac : public NativeWindow {
   // whehter we can drag.
   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<AtomNSWindowDelegate> window_delegate_;
 
index a5dd2842b65c78b3723f7d8d9afbed3f62d35d51..bd47fef914b781a815de6517c9b0135b218f4ad7 100644 (file)
@@ -639,18 +639,10 @@ void NativeWindowMac::SetContentSizeConstraints(
 }
 
 void NativeWindowMac::SetResizable(bool resizable) {
-  bool maximizable = IsMaximizable();
   // Change styleMask for frameless causes the window to change size, so we have
   // to explicitly disables that.
   ScopedDisableResize disable_resize;
-  if (resizable) {
-    [window_ setStyleMask:[window_ styleMask] | NSResizableWindowMask];
-  } else {
-    [window_ setStyleMask:[window_ styleMask] & (~NSResizableWindowMask)];
-  }
-  if (!maximizable) {
-    SetMaximizable(false);
-  }
+  SetStyleMask(resizable, NSResizableWindowMask);
 }
 
 bool NativeWindowMac::IsResizable() {
@@ -666,20 +658,7 @@ bool NativeWindowMac::IsMovable() {
 }
 
 void NativeWindowMac::SetMinimizable(bool minimizable) {
-  bool maximizable = IsMaximizable();
-  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);
-  }
+  SetStyleMask(minimizable, NSMiniaturizableWindowMask);
 }
 
 bool NativeWindowMac::IsMinimizable() {
@@ -695,35 +674,20 @@ bool NativeWindowMac::IsMaximizable() {
 }
 
 void NativeWindowMac::SetFullScreenable(bool fullscreenable) {
-  bool maximizable = IsMaximizable();
-  NSUInteger collectionBehavior = [window_ collectionBehavior];
-  if (fullscreenable) {
-    collectionBehavior |= NSWindowCollectionBehaviorFullScreenPrimary;
-  } else {
-    collectionBehavior &= (~NSWindowCollectionBehaviorFullScreenPrimary);
-    // On EL Capitan this flag is required to hide fullscreen button.
-    collectionBehavior |= NSWindowCollectionBehaviorFullScreenAuxiliary;
-  }
-  [window_ setCollectionBehavior:collectionBehavior];
-  if (!maximizable) {
-    SetMaximizable(false);
-  }
+  SetCollectionBehavior(
+      fullscreenable, NSWindowCollectionBehaviorFullScreenPrimary);
+  // On EL Capitan this flag is required to hide fullscreen button.
+  SetCollectionBehavior(
+      !fullscreenable, NSWindowCollectionBehaviorFullScreenAuxiliary);
 }
 
 bool NativeWindowMac::IsFullScreenable() {
-  return [window_ collectionBehavior] & NSWindowCollectionBehaviorFullScreenPrimary;
+  NSUInteger collectionBehavior = [window_ collectionBehavior];
+  return collectionBehavior & NSWindowCollectionBehaviorFullScreenPrimary;
 }
 
 void NativeWindowMac::SetClosable(bool closable) {
-  bool maximizable = IsMaximizable();
-  if (closable) {
-    [window_ setStyleMask:[window_ styleMask] | NSClosableWindowMask];
-  } else {
-    [window_ setStyleMask:[window_ styleMask] & (~NSClosableWindowMask)];
-  }
-  if (!maximizable) {
-    SetMaximizable(false);
-  }
+  SetStyleMask(closable, NSClosableWindowMask);
 }
 
 bool NativeWindowMac::IsClosable() {
@@ -890,13 +854,7 @@ void NativeWindowMac::ShowDefinitionForSelection() {
 }
 
 void NativeWindowMac::SetVisibleOnAllWorkspaces(bool visible) {
-  NSUInteger collectionBehavior = [window_ collectionBehavior];
-  if (visible) {
-    collectionBehavior |= NSWindowCollectionBehaviorCanJoinAllSpaces;
-  } else {
-    collectionBehavior &= ~NSWindowCollectionBehaviorCanJoinAllSpaces;
-  }
-  [window_ setCollectionBehavior:collectionBehavior];
+  SetCollectionBehavior(visible, NSWindowCollectionBehaviorCanJoinAllSpaces);
 }
 
 bool NativeWindowMac::IsVisibleOnAllWorkspaces() {
@@ -1053,6 +1011,30 @@ void NativeWindowMac::UpdateDraggableRegionViews(
   [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
 NativeWindow* NativeWindow::Create(
     brightray::InspectableWebContents* inspectable_web_contents,