Home | History | Annotate | Download | only in mac
      1 /*
      2  * Copyright (C) 2011 Apple Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
     14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
     17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     23  * THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #ifndef ScrollElasticityController_h
     27 #define ScrollElasticityController_h
     28 
     29 #if USE(RUBBER_BANDING)
     30 
     31 #include "platform/PlatformExport.h"
     32 #include "platform/geometry/FloatPoint.h"
     33 #include "platform/geometry/FloatSize.h"
     34 #include "platform/scroll/ScrollTypes.h"
     35 #include "wtf/Noncopyable.h"
     36 
     37 namespace WebCore {
     38 
     39 class PlatformWheelEvent;
     40 
     41 class ScrollElasticityControllerClient {
     42 protected:
     43     virtual ~ScrollElasticityControllerClient() { }
     44 
     45 public:
     46     virtual bool allowsHorizontalStretching() = 0;
     47     virtual bool allowsVerticalStretching() = 0;
     48     // The amount that the view is stretched past the normal allowable bounds.
     49     // The "overhang" amount.
     50     virtual IntSize stretchAmount() = 0;
     51     virtual bool pinnedInDirection(const FloatSize&) = 0;
     52     virtual bool canScrollHorizontally() = 0;
     53     virtual bool canScrollVertically() = 0;
     54 
     55     // Return the absolute scroll position, not relative to the scroll origin.
     56     virtual WebCore::IntPoint absoluteScrollPosition() = 0;
     57 
     58     virtual void immediateScrollBy(const FloatSize&) = 0;
     59     virtual void immediateScrollByWithoutContentEdgeConstraints(const FloatSize&) = 0;
     60     virtual void startSnapRubberbandTimer() = 0;
     61     virtual void stopSnapRubberbandTimer() = 0;
     62 
     63     // If the current scroll position is within the overhang area, this function will cause
     64     // the page to scroll to the nearest boundary point.
     65     virtual void adjustScrollPositionToBoundsIfNecessary() = 0;
     66 };
     67 
     68 class PLATFORM_EXPORT ScrollElasticityController {
     69     WTF_MAKE_NONCOPYABLE(ScrollElasticityController);
     70 
     71 public:
     72     explicit ScrollElasticityController(ScrollElasticityControllerClient*);
     73 
     74     // This method is responsible for both scrolling and rubber-banding.
     75     //
     76     // Events are passed by IPC from the embedder. Events on Mac are grouped
     77     // into "gestures". If this method returns 'true', then this object has
     78     // handled the event. It expects the embedder to continue to forward events
     79     // from the gesture.
     80     //
     81     // This method makes the assumption that there is only 1 input device being
     82     // used at a time. If the user simultaneously uses multiple input devices,
     83     // Cocoa does not correctly pass all the gestureBegin/End events. The state
     84     // of this class is guaranteed to become eventually consistent, once the
     85     // user stops using multiple input devices.
     86     bool handleWheelEvent(const PlatformWheelEvent&);
     87     void snapRubberBandTimerFired();
     88 
     89     bool isRubberBandInProgress() const;
     90 
     91 private:
     92     void stopSnapRubberbandTimer();
     93     void snapRubberBand();
     94 
     95     // This method determines whether a given event should be handled. The
     96     // logic for control events of gestures (PhaseBegan, PhaseEnded) is handled
     97     // elsewhere.
     98     //
     99     // This class handles almost all wheel events. All of the following
    100     // conditions must be met for this class to ignore an event:
    101     // + No previous events in this gesture have caused any scrolling or rubber
    102     // banding.
    103     // + The event contains a horizontal component.
    104     // + The client's view is pinned in the horizontal direction of the event.
    105     // + The wheel event disallows rubber banding in the horizontal direction
    106     // of the event.
    107     bool shouldHandleEvent(const PlatformWheelEvent&);
    108 
    109     ScrollElasticityControllerClient* m_client;
    110 
    111     // There is an active scroll gesture event. This parameter only gets set to
    112     // false after the rubber band has been snapped, and before a new gesture
    113     // has begun. A careful audit of the code may deprecate the need for this
    114     // parameter.
    115     bool m_inScrollGesture;
    116     // At least one event in the current gesture has been consumed and has
    117     // caused the view to scroll or rubber band. All future events in this
    118     // gesture will be consumed and overscrolls will cause rubberbanding.
    119     bool m_hasScrolled;
    120     bool m_momentumScrollInProgress;
    121     bool m_ignoreMomentumScrolls;
    122 
    123     CFTimeInterval m_lastMomentumScrollTimestamp;
    124     FloatSize m_overflowScrollDelta;
    125     FloatSize m_stretchScrollForce;
    126     FloatSize m_momentumVelocity;
    127 
    128     // Rubber band state.
    129     CFTimeInterval m_startTime;
    130     FloatSize m_startStretch;
    131     FloatPoint m_origOrigin;
    132     FloatSize m_origVelocity;
    133 
    134     bool m_snapRubberbandTimerIsActive;
    135 };
    136 
    137 } // namespace WebCore
    138 
    139 #endif // USE(RUBBER_BANDING)
    140 
    141 #endif // ScrollElasticityController_h
    142