Browse Source

Add events to manage sheets of macOS BrowserWindow

Yuya Ochiai 8 years ago
parent
commit
75184046f6

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

@@ -298,6 +298,16 @@ void Window::OnWindowMessage(UINT message, WPARAM w_param, LPARAM l_param) {
 }
 #endif
 
+#if defined(OS_MACOSX)
+void Window::OnWindowSheetBegin() {
+  Emit("sheet-begin");
+}
+
+void Window::OnWindowSheetEnd() {
+  Emit("sheet-end");
+}
+#endif
+
 // static
 mate::WrappableBase* Window::New(mate::Arguments* args) {
   if (!Browser::Get()->is_ready()) {

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

@@ -93,6 +93,11 @@ class Window : public mate::TrackableObject<Window>,
   void OnWindowMessage(UINT message, WPARAM w_param, LPARAM l_param) override;
   #endif
 
+  #if defined(OS_MACOSX)
+  void OnWindowSheetBegin() override;
+  void OnWindowSheetEnd() override;
+  #endif
+
  private:
   void Init(v8::Isolate* isolate,
             v8::Local<v8::Object> wrapper,

+ 12 - 0
atom/browser/native_window.cc

@@ -590,6 +590,18 @@ void NativeWindow::NotifyWindowMessage(
 }
 #endif
 
+#if defined(OS_MACOSX)
+void NativeWindow::NotifyWindowSheetBegin() {
+  for (NativeWindowObserver& observer : observers_)
+    observer.OnWindowSheetBegin();
+}
+
+void NativeWindow::NotifyWindowSheetEnd() {
+  for (NativeWindowObserver& observer : observers_)
+    observer.OnWindowSheetEnd();
+}
+#endif
+
 std::unique_ptr<SkRegion> NativeWindow::DraggableRegionsToSkRegion(
     const std::vector<DraggableRegion>& regions) {
   std::unique_ptr<SkRegion> sk_region(new SkRegion);

+ 5 - 0
atom/browser/native_window.h

@@ -245,6 +245,11 @@ class NativeWindow : public base::SupportsUserData,
   void NotifyWindowMessage(UINT message, WPARAM w_param, LPARAM l_param);
   #endif
 
+  #if defined(OS_MACOSX)
+  void NotifyWindowSheetBegin();
+  void NotifyWindowSheetEnd();
+  #endif
+
   void AddObserver(NativeWindowObserver* obs) {
     observers_.AddObserver(obs);
   }

+ 8 - 0
atom/browser/native_window_mac.mm

@@ -313,6 +313,14 @@ bool ScopedDisableResize::disable_resize_ = false;
   return rect;
 }
 
+- (void)windowWillBeginSheet:(NSNotification *)notification {
+  shell_->NotifyWindowSheetBegin();
+}
+
+- (void)windowDidEndSheet:(NSNotification *)notification {
+  shell_->NotifyWindowSheetEnd();
+}
+
 @end
 
 @interface AtomPreviewItem : NSObject <QLPreviewItem>

+ 5 - 0
atom/browser/native_window_observer.h

@@ -79,6 +79,11 @@ class NativeWindowObserver {
   virtual void OnWindowMessage(UINT message, WPARAM w_param, LPARAM l_param) {}
   #endif
 
+  #if defined(OS_MACOSX)
+  virtual void OnWindowSheetBegin() {}
+  virtual void OnWindowSheetEnd() {}
+  #endif
+
   // Called when renderer is hung.
   virtual void OnRendererUnresponsive() {}
 

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

@@ -498,6 +498,14 @@ Returns:
 
 Emitted on 3-finger swipe. Possible directions are `up`, `right`, `down`, `left`.
 
+#### Event: 'sheet-begin' _macOS_
+
+Emitted when the window opens a sheet.
+
+#### Event: 'sheet-end' _macOS_
+
+Emitted when the window has closed a sheet.
+
 ### Static Methods
 
 The `BrowserWindow` class has the following static methods:

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

@@ -1191,6 +1191,54 @@ describe('BrowserWindow module', function () {
     })
   })
 
+  describe('sheet-begin event', function () {
+    if (process.platform !== 'darwin') {
+      return
+    }
+
+    let sheet = null
+
+    afterEach(function () {
+      return closeWindow(sheet, {assertSingleWindow: false}).then(function () { sheet = null })
+    })
+
+    it('emits when window opens a sheet', function (done) {
+      w.show()
+      w.once('sheet-begin', function () {
+        sheet.close()
+        done()
+      })
+      sheet = new BrowserWindow({
+        modal: true,
+        parent: w
+      })
+    })
+  })
+
+  describe('sheet-end event', function () {
+    if (process.platform !== 'darwin') {
+      return
+    }
+
+    let sheet = null
+
+    afterEach(function () {
+      return closeWindow(sheet, {assertSingleWindow: false}).then(function () { sheet = null })
+    })
+
+    it('emits when window has closed a sheet', function (done) {
+      w.show()
+      sheet = new BrowserWindow({
+        modal: true,
+        parent: w
+      })
+      w.once('sheet-end', function () {
+        done()
+      })
+      sheet.close()
+    })
+  })
+
   describe('beginFrameSubscription method', function () {
     // This test is too slow, only test it on CI.
     if (!isCI) return