Browse Source

Add BrowserWindow.getOpacity for consistency

Taeho Kim 7 years ago
parent
commit
7570ec9d39

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

@@ -635,6 +635,10 @@ void Window::SetOpacity(const double opacity) {
   window_->SetOpacity(opacity);
 }
 
+double Window::GetOpacity() {
+  return window_->GetOpacity();
+}
+
 void Window::FocusOnWebView() {
   window_->FocusOnWebView();
 }
@@ -1061,6 +1065,7 @@ void Window::BuildPrototype(v8::Isolate* isolate,
       .SetMethod("setHasShadow", &Window::SetHasShadow)
       .SetMethod("hasShadow", &Window::HasShadow)
       .SetMethod("setOpacity", &Window::SetOpacity)
+      .SetMethod("getOpacity", &Window::GetOpacity)
       .SetMethod("setRepresentedFilename", &Window::SetRepresentedFilename)
       .SetMethod("getRepresentedFilename", &Window::GetRepresentedFilename)
       .SetMethod("setDocumentEdited", &Window::SetDocumentEdited)

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

@@ -162,6 +162,7 @@ class Window : public mate::TrackableObject<Window>,
   void SetHasShadow(bool has_shadow);
   bool HasShadow();
   void SetOpacity(const double opacity);
+  double GetOpacity();
   void FocusOnWebView();
   void BlurWebView();
   bool IsWebViewFocused();

+ 1 - 0
atom/browser/native_window.h

@@ -142,6 +142,7 @@ class NativeWindow : public base::SupportsUserData,
   virtual void SetHasShadow(bool has_shadow) = 0;
   virtual bool HasShadow() = 0;
   virtual void SetOpacity(const double opacity) = 0;
+  virtual double GetOpacity() = 0;
   virtual void SetRepresentedFilename(const std::string& filename);
   virtual std::string GetRepresentedFilename();
   virtual void SetDocumentEdited(bool edited);

+ 1 - 0
atom/browser/native_window_mac.h

@@ -84,6 +84,7 @@ class NativeWindowMac : public NativeWindow,
   void SetHasShadow(bool has_shadow) override;
   bool HasShadow() override;
   void SetOpacity(const double opacity) override;
+  double GetOpacity() override;
   void SetRepresentedFilename(const std::string& filename) override;
   std::string GetRepresentedFilename() override;
   void SetDocumentEdited(bool edited) override;

+ 4 - 0
atom/browser/native_window_mac.mm

@@ -1507,6 +1507,10 @@ void NativeWindowMac::SetOpacity(const double opacity) {
   [window_ setAlphaValue:opacity];
 }
 
+double NativeWindowMac::GetOpacity() {
+  return [window_ alphaValue];
+}
+
 void NativeWindowMac::SetRepresentedFilename(const std::string& filename) {
   [window_ setRepresentedFilename:base::SysUTF8ToNSString(filename)];
 }

+ 5 - 0
atom/browser/native_window_views.cc

@@ -825,6 +825,11 @@ void NativeWindowViews::SetOpacity(const double opacity) {
   }
   ::SetLayeredWindowAttributes(hwnd, 0, opacity * 255, LWA_ALPHA);
 #endif
+  opacity_ = opacity;
+}
+
+double NativeWindowViews::GetOpacity() {
+  return opacity_;
 }
 
 void NativeWindowViews::SetIgnoreMouseEvents(bool ignore, bool forward) {

+ 2 - 0
atom/browser/native_window_views.h

@@ -105,6 +105,7 @@ class NativeWindowViews : public NativeWindow,
   void SetHasShadow(bool has_shadow) override;
   bool HasShadow() override;
   void SetOpacity(const double opacity) override;
+  double GetOpacity() override;
   void SetIgnoreMouseEvents(bool ignore, bool forward) override;
   void SetContentProtection(bool enable) override;
   void SetFocusable(bool focusable) override;
@@ -295,6 +296,7 @@ class NativeWindowViews : public NativeWindow,
   bool fullscreenable_;
   std::string title_;
   gfx::Size widget_size_;
+  double opacity_ = 1.0;
 
   DISALLOW_COPY_AND_ASSIGN(NativeWindowViews);
 };

+ 6 - 2
docs/api/browser-window.md

@@ -205,7 +205,7 @@ It creates a new `BrowserWindow` with native properties as set by the `options`.
     `#FFF` (white).
   * `hasShadow` Boolean (optional) - Whether window should have a shadow. This is only
     implemented on macOS. Default is `true`.
-  * `opacity` Double (optional) - Set the initial opacity of the window, between 0.0 (fully
+  * `opacity` Number (optional) - Set the initial opacity of the window, between 0.0 (fully
     transparent) and 1.0 (fully opaque). This is only implemented on Windows and macOS.
   * `darkTheme` Boolean (optional) - Forces using dark theme for the window, only works on
     some GTK+3 desktop environments. Default is `false`.
@@ -1210,10 +1210,14 @@ On Windows and Linux always returns
 
 #### `win.setOpacity(opacity)` _Windows_ _macOS_
 
-* `opacity` Double - between 0.0 (fully transparent) and 1.0 (fully opaque)
+* `opacity` Number - between 0.0 (fully transparent) and 1.0 (fully opaque)
 
 Sets the opacity of the window. On Linux does nothing.
 
+#### `win.getOpacity()` _Windows_ _macOS_
+
+Returns `Number` - between 0.0 (fully transparent) and 1.0 (fully opaque)
+
 #### `win.setThumbarButtons(buttons)` _Windows_
 
 * `buttons` [ThumbarButton[]](structures/thumbar-button.md)

+ 4 - 0
spec/api-browser-window-spec.js

@@ -804,13 +804,17 @@ describe('BrowserWindow module', function () {
         height: 400,
         opacity: 0.5
       })
+      assert.equal(w.getOpacity(), 0.5)
     })
 
     it('allows setting the opacity', function () {
       assert.doesNotThrow(function () {
         w.setOpacity(0.0)
+        assert.equal(w.getOpacity(), 0.0)
         w.setOpacity(0.5)
+        assert.equal(w.getOpacity(), 0.5)
         w.setOpacity(1.0)
+        assert.equal(w.getOpacity(), 1.0)
       })
     })
   })