Home | History | Annotate | Download | only in scrolling
      1 /*
      2  * Copyright (C) 2012 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 ScrollingConstraints_h
     27 #define ScrollingConstraints_h
     28 
     29 #include "platform/geometry/FloatRect.h"
     30 
     31 namespace blink {
     32 
     33 // ViewportConstraints classes encapsulate data and logic required to reposition elements whose layout
     34 // depends on the viewport rect (i.e., position fixed), when scrolling and zooming.
     35 class ViewportConstraints {
     36 public:
     37     // FIXME: Simplify this code now that position: sticky doesn't exist.
     38     enum ConstraintType {
     39         FixedPositionConstaint,
     40     };
     41 
     42     enum AnchorEdgeFlags {
     43         AnchorEdgeLeft = 1 << 0,
     44         AnchorEdgeRight = 1 << 1,
     45         AnchorEdgeTop = 1 << 2,
     46         AnchorEdgeBottom = 1 << 3
     47     };
     48     typedef unsigned AnchorEdges;
     49 
     50     ViewportConstraints(const ViewportConstraints& other)
     51         : m_alignmentOffset(other.m_alignmentOffset)
     52         , m_anchorEdges(other.m_anchorEdges)
     53     { }
     54 
     55     virtual ~ViewportConstraints() { }
     56 
     57     virtual ConstraintType constraintType() const = 0;
     58 
     59     AnchorEdges anchorEdges() const { return m_anchorEdges; }
     60     bool hasAnchorEdge(AnchorEdgeFlags flag) const { return m_anchorEdges & flag; }
     61     void addAnchorEdge(AnchorEdgeFlags edgeFlag) { m_anchorEdges |= edgeFlag; }
     62     void setAnchorEdges(AnchorEdges edges) { m_anchorEdges = edges; }
     63 
     64     FloatSize alignmentOffset() const { return m_alignmentOffset; }
     65     void setAlignmentOffset(const FloatSize& offset) { m_alignmentOffset = offset; }
     66 
     67 protected:
     68     ViewportConstraints()
     69         : m_anchorEdges(0)
     70     { }
     71 
     72     FloatSize m_alignmentOffset;
     73     AnchorEdges m_anchorEdges;
     74 };
     75 
     76 class FixedPositionViewportConstraints FINAL : public ViewportConstraints {
     77 public:
     78     FixedPositionViewportConstraints()
     79         : ViewportConstraints()
     80     { }
     81 
     82     FixedPositionViewportConstraints(const FixedPositionViewportConstraints& other)
     83         : ViewportConstraints(other)
     84         , m_viewportRectAtLastLayout(other.m_viewportRectAtLastLayout)
     85         , m_layerPositionAtLastLayout(other.m_layerPositionAtLastLayout)
     86     { }
     87 
     88     FloatPoint layerPositionForViewportRect(const FloatRect& viewportRect) const;
     89 
     90     const FloatRect& viewportRectAtLastLayout() const { return m_viewportRectAtLastLayout; }
     91     void setViewportRectAtLastLayout(const FloatRect& rect) { m_viewportRectAtLastLayout = rect; }
     92 
     93     const FloatPoint& layerPositionAtLastLayout() const { return m_layerPositionAtLastLayout; }
     94     void setLayerPositionAtLastLayout(const FloatPoint& point) { m_layerPositionAtLastLayout = point; }
     95 
     96     bool operator==(const FixedPositionViewportConstraints& other) const
     97     {
     98         return m_alignmentOffset == other.m_alignmentOffset
     99             && m_anchorEdges == other.m_anchorEdges
    100             && m_viewportRectAtLastLayout == other.m_viewportRectAtLastLayout
    101             && m_layerPositionAtLastLayout == other.m_layerPositionAtLastLayout;
    102     }
    103 
    104     bool operator!=(const FixedPositionViewportConstraints& other) const { return !(*this == other); }
    105 
    106 private:
    107     virtual ConstraintType constraintType() const OVERRIDE { return FixedPositionConstaint; }
    108 
    109     FloatRect m_viewportRectAtLastLayout;
    110     FloatPoint m_layerPositionAtLastLayout;
    111 };
    112 
    113 } // namespace blink
    114 
    115 #endif // ScrollingConstraints_h
    116