From d7e54f88ea247e7f68cbe8759b245074a46f7beb Mon Sep 17 00:00:00 2001 From: Allan Odgaard Date: Fri, 26 Apr 2013 20:44:43 +0700 Subject: [PATCH] =?UTF-8?q?Use=20trackSwipeEventWithOptions:=E2=80=A6=20fo?= =?UTF-8?q?r=20swipe=20gestures?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HTMLOutput/src/browser/HOBrowserView.mm | 76 ++++++------------- .../OakFileBrowser/src/OakFileBrowser.mm | 51 ++++++------- 2 files changed, 48 insertions(+), 79 deletions(-) diff --git a/Frameworks/HTMLOutput/src/browser/HOBrowserView.mm b/Frameworks/HTMLOutput/src/browser/HOBrowserView.mm index 316b3fa1..4e6c35e7 100644 --- a/Frameworks/HTMLOutput/src/browser/HOBrowserView.mm +++ b/Frameworks/HTMLOutput/src/browser/HOBrowserView.mm @@ -8,8 +8,6 @@ @property (nonatomic, readwrite) WebView* webView; @property (nonatomic, readwrite) HOStatusBar* statusBar; @property (nonatomic, retain) HOWebViewDelegateHelper* webViewDelegateHelper; -@property (nonatomic, copy) NSEvent* gestureBeginEvent; -@property (nonatomic) NSRect visibleRectBeforeGesture; @end @implementation HOBrowserView @@ -93,63 +91,37 @@ // = Swipe = // ========= -- (void)swipeWithEvent:(NSEvent*)anEvent +- (BOOL)wantsScrollEventsForSwipeTrackingOnAxis:(NSEventGestureAxis)axis { - if([anEvent deltaX] == +1 && _webView.canGoBack) - [_webView goBack:self]; - else if([anEvent deltaX] == -1 && _webView.canGoForward) - [_webView goForward:self]; + return axis == NSEventGestureAxisHorizontal; } -- (NSView*)scrollableViewForPoint:(NSPoint)aPoint inWebFrame:(WebFrame*)parentFrame +- (void)scrollWheel:(NSEvent*)anEvent { - for(WebFrame* webFrame in [parentFrame childFrames]) - { - if(NSView* res = [self scrollableViewForPoint:aPoint inWebFrame:webFrame]) - return res; - } - - NSView * documentView = [[parentFrame frameView] documentView]; - NSRect visible = [documentView convertRect:[documentView visibleRect] toView:nil]; - NSRect actual = [documentView convertRect:[documentView bounds] toView:nil]; - if(NSMouseInRect(aPoint, visible, [documentView isFlipped]) && !NSEqualRects(visible, actual)) - return documentView; - - return nil; -} - -- (void)beginGestureWithEvent:(NSEvent*)anEvent -{ - self.gestureBeginEvent = anEvent; - - NSView* view = [self scrollableViewForPoint:[anEvent locationInWindow] inWebFrame:[_webView mainFrame]]; - self.visibleRectBeforeGesture = view ? [view visibleRect] : NSZeroRect; -} - -- (void)endGestureWithEvent:(NSEvent*)anEvent -{ - NSTimeInterval duration = [anEvent timestamp] - [self.gestureBeginEvent timestamp]; - NSMutableDictionary* map = [NSMutableDictionary dictionary]; - for(NSTouch* touch in [self.gestureBeginEvent touchesMatchingPhase:NSTouchPhaseBegan inView:nil]) - map[touch.identity] = touch; - self.gestureBeginEvent = nil; - - NSView* view = [self scrollableViewForPoint:[anEvent locationInWindow] inWebFrame:[_webView mainFrame]]; - if(duration > 0.2 || view && !NSEqualRects(self.visibleRectBeforeGesture, [view visibleRect])) + if(![NSEvent isSwipeTrackingFromScrollEventsEnabled] || [anEvent phase] == NSEventPhaseNone || fabsf([anEvent scrollingDeltaX]) <= fabsf([anEvent scrollingDeltaY])) return; - NSInteger direction = 0; - for(NSTouch* touch in [anEvent touchesMatchingPhase:NSTouchPhaseAny inView:nil]) - { - NSTouch* initialTouch = map[touch.identity]; - CGFloat distance = touch.normalizedPosition.x - initialTouch.normalizedPosition.x; - direction += distance <= -0.1 ? +1 : (distance >= 0.1 ? -1 : 0); - } + [anEvent trackSwipeEventWithOptions:0 dampenAmountThresholdMin:(_webView.canGoForward ? -1 : 0) max:(_webView.canGoBack ? +1 : 0) usingHandler:^(CGFloat gestureAmount, NSEventPhase phase, BOOL isComplete, BOOL* stop) { + if(phase == NSEventPhaseBegan) + { + // Setup animation overlay layers + } - if(direction == -2 && _webView.canGoBack) - [_webView goBack:self]; - else if(direction == +2 && _webView.canGoForward) - [_webView goForward:self]; + // Update animation overlay to match gestureAmount + + if(phase == NSEventPhaseEnded) + { + if(gestureAmount > 0 && _webView.canGoBack) + [_webView goBack:self]; + else if(gestureAmount < 0 && _webView.canGoForward) + [_webView goForward:self]; + } + + if(isComplete) + { + // Tear down animation overlay here + } + }]; } // ======================= diff --git a/Frameworks/OakFileBrowser/src/OakFileBrowser.mm b/Frameworks/OakFileBrowser/src/OakFileBrowser.mm index b8ee3686..563eecc8 100644 --- a/Frameworks/OakFileBrowser/src/OakFileBrowser.mm +++ b/Frameworks/OakFileBrowser/src/OakFileBrowser.mm @@ -114,8 +114,6 @@ static NSImage* IconImage (NSURL* url, NSSize size = NSMakeSize(16, 16)) @property (nonatomic) NSMutableArray* history; @property (nonatomic) NSUInteger historyIndex; - -@property (nonatomic, copy) NSEvent* gestureBeginEvent; @end static bool is_binary (std::string const& path) @@ -1152,38 +1150,37 @@ static NSMutableSet* SymmetricDifference (NSMutableSet* aSet, NSMutableSet* anot // = Swipe = // ========= -- (void)swipeWithEvent:(NSEvent*)anEvent +- (BOOL)wantsScrollEventsForSwipeTrackingOnAxis:(NSEventGestureAxis)axis { - if([anEvent deltaX] == +1 && self.canGoBack) - [self goBack:self]; - else if([anEvent deltaX] == -1 && self.canGoForward) - [self goForward:self]; + return axis == NSEventGestureAxisHorizontal; } -- (void)beginGestureWithEvent:(NSEvent*)anEvent +- (void)scrollWheel:(NSEvent*)anEvent { - self.gestureBeginEvent = anEvent; -} + if(![NSEvent isSwipeTrackingFromScrollEventsEnabled] || [anEvent phase] == NSEventPhaseNone || fabsf([anEvent scrollingDeltaX]) <= fabsf([anEvent scrollingDeltaY])) + return; -- (void)endGestureWithEvent:(NSEvent*)anEvent -{ - NSMutableDictionary* map = [NSMutableDictionary dictionary]; - for(NSTouch* touch in [self.gestureBeginEvent touchesMatchingPhase:NSTouchPhaseBegan inView:nil]) - map[touch.identity] = touch; - self.gestureBeginEvent = nil; + [anEvent trackSwipeEventWithOptions:0 dampenAmountThresholdMin:(self.canGoForward ? -1 : 0) max:(self.canGoBack ? +1 : 0) usingHandler:^(CGFloat gestureAmount, NSEventPhase phase, BOOL isComplete, BOOL* stop) { + if(phase == NSEventPhaseBegan) + { + // Setup animation overlay layers + } - NSInteger direction = 0; - for(NSTouch* touch in [anEvent touchesMatchingPhase:NSTouchPhaseAny inView:nil]) - { - NSTouch* initialTouch = map[touch.identity]; - CGFloat distance = touch.normalizedPosition.x - initialTouch.normalizedPosition.x; - direction += distance < -0.1 ? +1 : (distance > 0.1 ? -1 : 0); - } + // Update animation overlay to match gestureAmount - if(direction == -2 && self.canGoBack) - [self goBack:self]; - else if(direction == +2 && self.canGoForward) - [self goForward:self]; + if(phase == NSEventPhaseEnded) + { + if(gestureAmount > 0 && self.canGoBack) + [self goBack:self]; + else if(gestureAmount < 0 && self.canGoForward) + [self goForward:self]; + } + + if(isComplete) + { + // Tear down animation overlay here + } + }]; } // ===================