Browse Source

Add BrowserWindow.setAutoHideCursor for macOS

The `disableAutoHideCursor` BrowserWindow option can be used to control
auto-hiding behavior when the window is created. This new API is needed
to dynamically change the behavior after the fact.
Birunthan Mohanathas 8 years ago
parent
commit
18c49285a8

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

@@ -811,6 +811,10 @@ bool Window::IsVisibleOnAllWorkspaces() {
   return window_->IsVisibleOnAllWorkspaces();
 }
 
+void Window::SetAutoHideCursor(bool auto_hide) {
+  window_->SetAutoHideCursor(auto_hide);
+}
+
 void Window::SetVibrancy(mate::Arguments* args) {
   std::string type;
 
@@ -934,6 +938,9 @@ void Window::BuildPrototype(v8::Isolate* isolate,
                  &Window::SetVisibleOnAllWorkspaces)
       .SetMethod("isVisibleOnAllWorkspaces",
                  &Window::IsVisibleOnAllWorkspaces)
+#if defined(OS_MACOSX)
+      .SetMethod("setAutoHideCursor", &Window::SetAutoHideCursor)
+#endif
       .SetMethod("setVibrancy", &Window::SetVibrancy)
 #if defined(OS_WIN)
       .SetMethod("hookWindowMessage", &Window::HookWindowMessage)

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

@@ -198,6 +198,8 @@ class Window : public mate::TrackableObject<Window>,
   void SetVisibleOnAllWorkspaces(bool visible);
   bool IsVisibleOnAllWorkspaces();
 
+  void SetAutoHideCursor(bool auto_hide);
+
   void SetVibrancy(mate::Arguments* args);
 
   int32_t ID() const;

+ 3 - 0
atom/browser/native_window.cc

@@ -333,6 +333,9 @@ void NativeWindow::SetParentWindow(NativeWindow* parent) {
   parent_ = parent;
 }
 
+void NativeWindow::SetAutoHideCursor(bool auto_hide) {
+}
+
 void NativeWindow::SetVibrancy(const std::string& filename) {
 }
 

+ 2 - 0
atom/browser/native_window.h

@@ -161,6 +161,8 @@ class NativeWindow : public base::SupportsUserData,
   virtual void SetVisibleOnAllWorkspaces(bool visible) = 0;
   virtual bool IsVisibleOnAllWorkspaces() = 0;
 
+  virtual void SetAutoHideCursor(bool auto_hide);
+
   // Vibrancy API
   virtual void SetVibrancy(const std::string& type);
 

+ 4 - 0
atom/browser/native_window_mac.h

@@ -91,8 +91,12 @@ class NativeWindowMac : public NativeWindow,
   void SetProgressBar(double progress, const ProgressState state) override;
   void SetOverlayIcon(const gfx::Image& overlay,
                       const std::string& description) override;
+
   void SetVisibleOnAllWorkspaces(bool visible) override;
   bool IsVisibleOnAllWorkspaces() override;
+
+  void SetAutoHideCursor(bool auto_hide);
+
   void SetVibrancy(const std::string& type) override;
 
   // content::RenderWidgetHost::InputEventObserver:

+ 4 - 0
atom/browser/native_window_mac.mm

@@ -1255,6 +1255,10 @@ bool NativeWindowMac::IsVisibleOnAllWorkspaces() {
   return collectionBehavior & NSWindowCollectionBehaviorCanJoinAllSpaces;
 }
 
+void NativeWindowMac::SetAutoHideCursor(bool auto_hide) {
+  [window_ setDisableAutoHideCursor:!auto_hide];
+}
+
 void NativeWindowMac::SetVibrancy(const std::string& type) {
   if (!base::mac::IsOSYosemiteOrLater()) return;
 

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

@@ -1218,6 +1218,12 @@ Returns `BrowserWindow` - The parent window.
 
 Returns `BrowserWindow[]` - All child windows.
 
+#### `win.setAutoHideCursor(autoHide)` _macOS_
+
+* `autoHide` Boolean
+
+Controls whether to hide cursor when typing.
+
 #### `win.setVibrancy(type)` _macOS_
 
 * `type` String - Can be `appearance-based`, `light`, `dark`, `titlebar`,

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

@@ -506,6 +506,23 @@ describe('browser-window module', function () {
     })
   })
 
+  describe('BrowserWindow.setAutoHideCursor(autoHide)', () => {
+    if (process.platform !== 'darwin') {
+      it('is not available on non-macOS platforms', () => {
+        assert.ok(!w.setAutoHideCursor)
+      })
+
+      return
+    }
+
+    it('allows changing cursor auto-hiding', () => {
+      assert.doesNotThrow(() => {
+        w.setAutoHideCursor(false)
+        w.setAutoHideCursor(true)
+      })
+    })
+  })
+
   describe('BrowserWindow.setVibrancy(type)', function () {
     it('allows setting, changing, and removing the vibrancy', function () {
       assert.doesNotThrow(function () {