Browse Source

adds vibrancy effect for macos

gellert 8 years ago
parent
commit
8ad50d1e35

+ 10 - 0
atom/browser/api/atom_api_window.cc

@@ -786,6 +786,14 @@ bool Window::IsVisibleOnAllWorkspaces() {
   return window_->IsVisibleOnAllWorkspaces();
 }
 
+void Window::SetVibrancy(const std::string& type) {
+  window_->SetVibrancy(type);
+}
+
+void Window::RemoveVibrancy() {
+  window_->RemoveVibrancy();
+}
+
 int32_t Window::ID() const {
   return weak_map_id();
 }
@@ -901,6 +909,8 @@ void Window::BuildPrototype(v8::Isolate* isolate,
                  &Window::SetVisibleOnAllWorkspaces)
       .SetMethod("isVisibleOnAllWorkspaces",
                  &Window::IsVisibleOnAllWorkspaces)
+      .SetMethod("setVibrancy", &Window::SetVibrancy)
+      .SetMethod("removeVibrancy", &Window::RemoveVibrancy)
 #if defined(OS_WIN)
       .SetMethod("hookWindowMessage", &Window::HookWindowMessage)
       .SetMethod("isWindowMessageHooked", &Window::IsWindowMessageHooked)

+ 3 - 0
atom/browser/api/atom_api_window.h

@@ -196,6 +196,9 @@ class Window : public mate::TrackableObject<Window>,
   void SetVisibleOnAllWorkspaces(bool visible);
   bool IsVisibleOnAllWorkspaces();
 
+  void SetVibrancy(const std::string& type);
+  void RemoveVibrancy();
+
   int32_t ID() const;
   v8::Local<v8::Value> WebContents(v8::Isolate* isolate);
 

+ 7 - 0
atom/browser/native_window.cc

@@ -201,6 +201,13 @@ void NativeWindow::InitFromOptions(const mate::Dictionary& options) {
   options.Get(options::kShow, &show);
   if (show)
     Show();
+
+#if defined(OS_MACOSX)
+  std::string type;
+  if (options.Get(options::kVibrancyType, &type)) {
+    SetVibrancy(type);
+  }
+#endif
 }
 
 void NativeWindow::SetSize(const gfx::Size& size, bool animate) {

+ 4 - 0
atom/browser/native_window.h

@@ -161,6 +161,10 @@ class NativeWindow : public base::SupportsUserData,
   virtual void SetVisibleOnAllWorkspaces(bool visible) = 0;
   virtual bool IsVisibleOnAllWorkspaces() = 0;
 
+  // Vibrancy API
+  virtual void SetVibrancy(const std::string& type) = 0;
+  virtual void RemoveVibrancy() = 0;
+
   // Webview APIs.
   virtual void FocusOnWebView();
   virtual void BlurWebView();

+ 5 - 0
atom/browser/native_window_mac.h

@@ -92,6 +92,8 @@ class NativeWindowMac : public NativeWindow,
                       const std::string& description) override;
   void SetVisibleOnAllWorkspaces(bool visible) override;
   bool IsVisibleOnAllWorkspaces() override;
+  void SetVibrancy(const std::string& type) override;
+  void RemoveVibrancy() override;
 
   // content::RenderWidgetHost::InputEventObserver:
   void OnInputEvent(const blink::WebInputEvent& event) override;
@@ -162,6 +164,9 @@ class NativeWindowMac : public NativeWindow,
   // The "titleBarStyle" option.
   TitleBarStyle title_bar_style_;
 
+  // Vibrancy view
+  NSView* vibrant_view_;
+
   DISALLOW_COPY_AND_ASSIGN(NativeWindowMac);
 };
 

+ 57 - 0
atom/browser/native_window_mac.mm

@@ -1205,6 +1205,63 @@ bool NativeWindowMac::IsVisibleOnAllWorkspaces() {
   return collectionBehavior & NSWindowCollectionBehaviorCanJoinAllSpaces;
 }
 
+void NativeWindowMac::SetVibrancy(const std::string& type) {
+  if (!(base::mac::IsOSMavericks() || base::mac::IsOSYosemiteOrLater())) return;
+
+  NSVisualEffectView *vview = (NSVisualEffectView *)vibrant_view_;
+  if (vview == nil) {
+    vview = [[NSVisualEffectView alloc] initWithFrame:
+      [[window_ contentView] bounds]];
+    vibrant_view_ = (NSView *)vview;
+
+    [vview setAutoresizingMask:
+      NSViewWidthSizable | NSViewHeightSizable];
+
+    [vview setBlendingMode:NSVisualEffectBlendingModeBehindWindow];
+    [vview setState:NSVisualEffectStateActive];
+    [[window_ contentView] addSubview:vview
+                           positioned:NSWindowBelow
+                           relativeTo:nil];
+  }
+
+  NSVisualEffectMaterial vibrancyType = NSVisualEffectMaterialLight;
+
+  if (type == "appearance-based") {
+    vibrancyType = NSVisualEffectMaterialAppearanceBased;
+  } else if (type == "light") {
+    vibrancyType = NSVisualEffectMaterialLight;
+  } else if (type == "dark") {
+    vibrancyType = NSVisualEffectMaterialDark;
+  } else if (type == "titlebar") {
+    vibrancyType = NSVisualEffectMaterialTitlebar;
+  }
+
+  if (base::mac::IsOSYosemiteOrLater()) {
+    if (type == "selection") {
+      vibrancyType = NSVisualEffectMaterialSelection;
+    } else if (type == "menu") {
+      vibrancyType = NSVisualEffectMaterialMenu;
+    } else if (type == "popover") {
+      vibrancyType = NSVisualEffectMaterialPopover;
+    } else if (type == "sidebar") {
+      vibrancyType = NSVisualEffectMaterialSidebar;
+    } else if (type == "medium-light") {
+      vibrancyType = NSVisualEffectMaterialMediumLight;
+    } else if (type == "ultra-dark") {
+      vibrancyType = NSVisualEffectMaterialUltraDark;
+    }
+  }
+
+  [vview setMaterial:vibrancyType];
+}
+
+void NativeWindowMac::RemoveVibrancy() {
+  if (vibrant_view_ == nil) return;
+
+  [vibrant_view_ removeFromSuperview];
+  vibrant_view_ = nil;
+}
+
 void NativeWindowMac::OnInputEvent(const blink::WebInputEvent& event) {
   switch (event.type) {
     case blink::WebInputEvent::GestureScrollBegin:

+ 3 - 0
atom/common/options_switches.cc

@@ -81,6 +81,9 @@ const char kFocusable[] = "focusable";
 // The WebPreferences.
 const char kWebPreferences[] = "webPreferences";
 
+// Add a vibrancy effect to the browser window
+const char kVibrancyType[] = "vibrancy";
+
 // The factor of which page should be zoomed.
 const char kZoomFactor[] = "zoomFactor";
 

+ 1 - 0
atom/common/options_switches.h

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

+ 15 - 0
docs/api/browser-window.md

@@ -265,6 +265,8 @@ It creates a new `BrowserWindow` with native properties as set by the `options`.
     * `offscreen` Boolean - Whether to enable offscreen rendering for the browser
       window. Defaults to `false`.
     * `sandbox` Boolean - Whether to enable Chromium OS-level sandbox.
+    * `vibrancy` String - Add a type of vibrancy effect to the window, only on
+      macOS. See supported types at the `setVibrancy` method.
 
 When setting minimum or maximum window size with `minWidth`/`maxWidth`/
 `minHeight`/`maxHeight`, it only constrains the users. It won't prevent you from
@@ -1188,3 +1190,16 @@ Returns `BrowserWindow[]` - All child windows.
 
 [window-levels]: https://developer.apple.com/reference/appkit/nswindow/1664726-window_levels
 [quick-look]: https://en.wikipedia.org/wiki/Quick_Look
+
+#### `win.setVibrancy(type)` _macOS_
+
+* `type` String - Adds a vibrancy effect to the browser window. Values include
+  `appearance-based`, `light`, `dark`, `titlebar`, `selection`, `menu`,
+  `popover`, `sidebar`, `medium-light`, `ultra-dark`. See the
+  [macOS documentation][vibrancy-docs] for more details.
+
+[vibrancy-docs]: https://developer.apple.com/reference/appkit/nsvisualeffectview?language=objc
+
+#### `win.removeVibrancy()` _macOS_
+
+Removes the vibrancy effect on the window.