]> khamidou — GitHub mirrors - electron.git/commitdiff
Extend NativeWindow to track touch-based scroll events on OS X
authorBen Gotow <bengotow@gmail.com>
Thu, 21 Jan 2016 17:40:21 +0000 (09:40 -0800)
committerBen Gotow <bengotow@gmail.com>
Thu, 21 Jan 2016 17:40:21 +0000 (09:40 -0800)
In N1, we want to implement the famous "swipe to archive" action on threads in the user's inbox. Chrome exposes `scroll` and `wheel` events, but these aren't sufficient to implement the interaction because the element needs to "snap" when the user lifts their fingers from the trackpad, not when they / we stop receiving `wheel` / `scroll` events. These events may stop before the user lifts their fingers, or continue after the user has lifted their fingers if they had enough momentum for the gesture to continue.

This exposes BrowserWindow `scroll-touch-down` and `scroll-touch-up`, which fire immeditaely when the user touches two fingers to the trackpad, and again when the user lifts their fingers. Combined with the existing wheel event should allow for "swipe-to-archive" and other similar interactions.

Note: This is only implemented on Mac OS X and the events don't fire unless you're using a trackpad!

Related: #1486, #2683, https://github.com/nylas/N1/issues/541

atom/browser/api/atom_api_window.cc
atom/browser/api/atom_api_window.h
atom/browser/native_window.cc
atom/browser/native_window.h
atom/browser/native_window_mac.mm
atom/browser/native_window_observer.h

index 09d88edb9b7b1b7b507c0b0aa31df72c2ecc0fb1..ef38490d8f7b200353532e03ae506bd0149d9b4b 100644 (file)
@@ -227,6 +227,14 @@ void Window::OnWindowLeaveFullScreen() {
   Emit("leave-full-screen");
 }
 
+void Window::OnWindowScrollTouchUp() {
+  Emit("scroll-touch-up");
+}
+
+void Window::OnWindowScrollTouchDown() {
+  Emit("scroll-touch-down");
+}
+
 void Window::OnWindowEnterHtmlFullScreen() {
   Emit("enter-html-full-screen");
 }
index d8e25965d89877a8a0e9937e367e59d08db38150..12eec04680f59e4f9c054296b75af360fc1c588b 100644 (file)
@@ -65,6 +65,8 @@ class Window : public mate::TrackableObject<Window>,
   void OnWindowResize() override;
   void OnWindowMove() override;
   void OnWindowMoved() override;
+  void OnWindowScrollTouchUp() override;
+  void OnWindowScrollTouchDown() override;
   void OnWindowEnterFullScreen() override;
   void OnWindowLeaveFullScreen() override;
   void OnWindowEnterHtmlFullScreen() override;
index 4df4b49169635a73dab05c8c12e8af8506ddd732..89354381beade9757c98f80c1fce30715f640dfc 100644 (file)
@@ -442,6 +442,16 @@ void NativeWindow::NotifyWindowEnterFullScreen() {
                     OnWindowEnterFullScreen());
 }
 
+void NativeWindow::NotifyWindowScrollTouchUp() {
+  FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
+                    OnWindowScrollTouchUp());
+}
+
+void NativeWindow::NotifyWindowScrollTouchDown() {
+  FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
+                    OnWindowScrollTouchDown());
+}
+
 void NativeWindow::NotifyWindowLeaveFullScreen() {
   FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
                     OnWindowLeaveFullScreen());
index 0ccf3dfbc2ec4d601bb3001641d296a1bce5e3d8..f1dac26650c745eed3ba09f867bdc7b579f3a39a 100644 (file)
@@ -205,6 +205,8 @@ class NativeWindow : public base::SupportsUserData,
   void NotifyWindowMove();
   void NotifyWindowResize();
   void NotifyWindowMoved();
+  void NotifyWindowScrollTouchUp();
+  void NotifyWindowScrollTouchDown();
   void NotifyWindowEnterFullScreen();
   void NotifyWindowLeaveFullScreen();
   void NotifyWindowEnterHtmlFullScreen();
index f128f3b2cb718730ea499a07f5e64da05e65aab7..198a78e3f59489649c6de582dc2a4f61ec6bffed 100644 (file)
@@ -490,6 +490,25 @@ NativeWindowMac::NativeWindowMac(
   NSView* view = inspectable_web_contents()->GetView()->GetNativeView();
   [view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
 
+  BOOL __block down = NO;
+  [NSEvent addLocalMonitorForEventsMatchingMask:NSScrollWheelMask handler:^NSEvent * _Nullable(NSEvent * event) {
+    if (![window_ isKeyWindow])
+      return event;
+
+    if (!web_contents)
+      return event;
+
+    if (!down && (([event phase] == NSEventPhaseMayBegin) || ([event phase] == NSEventPhaseBegan))) {
+      this->NotifyWindowScrollTouchDown();
+      down = YES;
+    }
+    if (down && (([event phase] == NSEventPhaseEnded) || ([event phase] == NSEventPhaseCancelled))) {
+      this->NotifyWindowScrollTouchUp();
+      down = NO;
+    }
+    return event;
+  }];
+
   InstallView();
 }
 
index ce2d41c6fad6f5d759fb9768bf708318bd6c3ea0..b7ba9d756f11893d57fc13c05077fdd6dd1fd1cf 100644 (file)
@@ -50,6 +50,8 @@ class NativeWindowObserver {
   virtual void OnWindowResize() {}
   virtual void OnWindowMove() {}
   virtual void OnWindowMoved() {}
+  virtual void OnWindowScrollTouchUp() {}
+  virtual void OnWindowScrollTouchDown() {}
   virtual void OnWindowEnterFullScreen() {}
   virtual void OnWindowLeaveFullScreen() {}
   virtual void OnWindowEnterHtmlFullScreen() {}