chrome_render_widget_host_view_mac_history_swiper.h revision e5d81f57cb97b3b6b7fccc9c5610d21eb81db09d
15b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey// Copyright 2013 The Chromium Authors. All rights reserved.
25b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey// Use of this source code is governed by a BSD-style license that can be
35b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey// found in the LICENSE file.
45b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey
55b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey#ifndef CHROME_BROWSER_RENDERER_HOST_CHROME_RENDER_WIDGET_HOST_VIEW_MAC_HISTORY_SWIPER_
65b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey#define CHROME_BROWSER_RENDERER_HOST_CHROME_RENDER_WIDGET_HOST_VIEW_MAC_HISTORY_SWIPER_
75b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey
85b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey#import <Cocoa/Cocoa.h>
95b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey
105b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey@class HistorySwiper;
115b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey@protocol HistorySwiperDelegate
125b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey// Return NO from this method is the view/render_widget_host should not
135b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey// allow history swiping.
145b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey- (BOOL)shouldAllowHistorySwiping;
155b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey// The history overlay is added to the view returning from this method.
165b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey- (NSView*)viewThatWantsHistoryOverlay;
175b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey@end
185b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey
195b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkeynamespace history_swiper {
205b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkeyenum NavigationDirection {
215b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  kBackwards = 0,
225b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  kForwards,
235b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey};
245b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey} // history_swiper
255b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey
265b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey// Responsible for maintaining state for 2-finger swipe history navigation.
275b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey// Relevant blink/NSWindow touch events must be passed to this class.
285b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey// We want to be able to cancel history swipes if the user's swipe has a lot of
295b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey// vertical motion. The API [NSEvent trackSwipeEventWithOptions] doesn't give
3073fbfc38792bd96137d5b6ae3016dfc4d9805d46Jeff Sharkey// vertical swipe distance, and it swallows the touch events so that we can't
3173fbfc38792bd96137d5b6ae3016dfc4d9805d46Jeff Sharkey// independently gather them either. Instead of using that api, we manually
3273fbfc38792bd96137d5b6ae3016dfc4d9805d46Jeff Sharkey// track all touch events using the low level APIs touches*WithEvent:
3373fbfc38792bd96137d5b6ae3016dfc4d9805d46Jeff Sharkey@class HistoryOverlayController;
3473fbfc38792bd96137d5b6ae3016dfc4d9805d46Jeff Sharkey@interface HistorySwiper : NSObject {
355b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey @private
365b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // If the viewport is scrolled all the way to the left or right.
375b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // Used for history swiping.
385b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  BOOL isPinnedLeft_;
395b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  BOOL isPinnedRight_;
405b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey
415b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // If the main frame has a horizontal scrollbar.
425b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // Used for history swiping.
435b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  BOOL hasHorizontalScrollbar_;
445b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey
455b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // If a scroll event came back unhandled from the renderer. Set to |NO| at
465b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // the start of a scroll gesture, and then to |YES| if a scroll event comes
475b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // back unhandled from the renderer.
485b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // Used for history swiping.
495b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  BOOL gotUnhandledWheelEvent_;
505b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey
515b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // This controller will exist if and only if the UI is in history swipe mode.
525b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  HistoryOverlayController* historyOverlay_;
535b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // Each gesture received by the window is given a unique id.
545b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // The id is monotonically increasing.
555b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  int currentGestureId_;
565b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // The location of the fingers when the gesture started.
575b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  NSPoint gestureStartPoint_;
585b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // The current location of the fingers in the gesture.
595b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  NSPoint gestureCurrentPoint_;
605b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // A flag that indicates that there is an ongoing gesture.
615b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // The method [NSEvent touchesMatchingPhase:inView:] is only valid for events
625b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // that are part of a gesture.
635b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  BOOL inGesture_;
645b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // Each time a new gesture begins, we must get a new start point.
655b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // This ivar determines whether the start point is valid.
665b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  int gestureStartPointValid_;
675b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // The id of the last gesture that we processed as a history swipe.
685b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  int lastProcessedGestureId_;
695b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // A flag that indicates that we cancelled the history swipe for the current
705b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // gesture.
715b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  BOOL historySwipeCancelled_;
725b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // A flag that indicates the user's intended direction with the history swipe.
735b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  history_swiper::NavigationDirection historySwipeDirection_;
745b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // A flag that indicates whether the gesture has its direction inverted.
755b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  BOOL historySwipeDirectionInverted_;
765b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey
775b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  id<HistorySwiperDelegate> delegate_;
785b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey
795b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // Magic mouse and touchpad swipe events are identical except magic mouse
805b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // events do not generate NSTouch callbacks. Since we rely on NSTouch
815b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // callbacks to determine vertical scrolling, magic mouse swipe events use an
825b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // entirely different set of logic.
835b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  //
845b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // The two event types do not play well together. Just calling the API
855b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // `[NSEvent trackSwipeEventWithOptions:]` will block touches{Began, Moved, *}
865b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // callbacks for a non-deterministic period of time (even after the swipe has
875b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // completed).
885b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  BOOL receivedTouch_;
895b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // Cumulative scroll delta since scroll gesture start. Only valid during
905b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  // scroll gesture handling. Used for history swiping.
915b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey  NSSize mouseScrollDelta_;
925b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey}
935b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey
945b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey// Many event types are passed in, but the only one we care about is
9573fbfc38792bd96137d5b6ae3016dfc4d9805d46Jeff Sharkey// NSScrollWheel. We look at the phase to determine whether to trigger history
9673fbfc38792bd96137d5b6ae3016dfc4d9805d46Jeff Sharkey// swiping
975b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey- (BOOL)handleEvent:(NSEvent*)event;
985b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey- (void)gotUnhandledWheelEvent;
9973fbfc38792bd96137d5b6ae3016dfc4d9805d46Jeff Sharkey- (void)scrollOffsetPinnedToLeft:(BOOL)left toRight:(BOOL)right;
1005b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey- (void)setHasHorizontalScrollbar:(BOOL)hasHorizontalScrollbar;
1015b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey
1025b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey// The event passed in is a gesture event, and has touch data associated with
1035b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey// the trackpad.
1045b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey- (void)touchesBeganWithEvent:(NSEvent*)event;
1055b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey- (void)touchesMovedWithEvent:(NSEvent*)event;
1065b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey- (void)touchesCancelledWithEvent:(NSEvent*)event;
1075b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey- (void)touchesEndedWithEvent:(NSEvent*)event;
1085b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey- (void)beginGestureWithEvent:(NSEvent*)event;
1095b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey- (void)endGestureWithEvent:(NSEvent*)event;
1105b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey
1115b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey// These methods control whether a given view is allowed to rubberband in the
1125b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey// given direction. This is inversely related to whether the view is allowed to
1135b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey// 2-finger history swipe in the given direction.
1145b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey- (BOOL)canRubberbandLeft:(NSView*)view;
1155b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey- (BOOL)canRubberbandRight:(NSView*)view;
1165b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey
1175b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey// Designated initializer.
1185b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey- (id)initWithDelegate:(id<HistorySwiperDelegate>)delegate;
1195b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey
1205b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey@property (nonatomic, assign) id<HistorySwiperDelegate> delegate;
1215b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey
1225b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey@end
1235b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey
1245b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey// Exposed only for unit testing, do not call directly.
1255b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey@interface HistorySwiper (PrivateExposedForTesting)
1265b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey+ (void)resetMagicMouseState;
1275b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey@end
1285b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey
1295b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey#endif // CHROME_BROWSER_RENDERER_HOST_CHROME_RENDER_WIDGET_HOST_VIEW_MAC_HISTORY_SWIPER_
1305b78a3aa7741c3f44b676ccffa765cecee1cbd4cJeff Sharkey