Skip to content

Commit

Permalink
Extend NativeWindow to track touch-based scroll events on OS X
Browse files Browse the repository at this point in the history
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: electron#1486, electron#2683, nylas/nylas-mail#541
  • Loading branch information
bengotow committed Jan 21, 2016
1 parent fda480d commit bd2252e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions atom/browser/api/atom_api_window.cc
Expand Up @@ -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");
}
Expand Down
2 changes: 2 additions & 0 deletions atom/browser/api/atom_api_window.h
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions atom/browser/native_window.cc
Expand Up @@ -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());
Expand Down
2 changes: 2 additions & 0 deletions atom/browser/native_window.h
Expand Up @@ -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();
Expand Down
19 changes: 19 additions & 0 deletions atom/browser/native_window_mac.mm
Expand Up @@ -490,6 +490,25 @@ - (void)drawRect:(NSRect)dirtyRect {
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();
}

Expand Down
2 changes: 2 additions & 0 deletions atom/browser/native_window_observer.h
Expand Up @@ -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() {}
Expand Down

0 comments on commit bd2252e

Please sign in to comment.