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 WebCore { 32 33 // ViewportConstraints classes encapsulate data and logic required to reposition elements whose layout 34 // depends on the viewport rect (positions fixed and sticky), when scrolling and zooming. 35 class ViewportConstraints { 36 public: 37 enum ConstraintType { 38 FixedPositionConstaint, 39 StickyPositionConstraint 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 : 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 class StickyPositionViewportConstraints : public ViewportConstraints { 114 public: 115 StickyPositionViewportConstraints() 116 : m_leftOffset(0) 117 , m_rightOffset(0) 118 , m_topOffset(0) 119 , m_bottomOffset(0) 120 { } 121 122 StickyPositionViewportConstraints(const StickyPositionViewportConstraints& other) 123 : ViewportConstraints(other) 124 , m_leftOffset(other.m_leftOffset) 125 , m_rightOffset(other.m_rightOffset) 126 , m_topOffset(other.m_topOffset) 127 , m_bottomOffset(other.m_bottomOffset) 128 , m_absoluteContainingBlockRect(other.m_absoluteContainingBlockRect) 129 , m_absoluteStickyBoxRect(other.m_absoluteStickyBoxRect) 130 , m_stickyOffsetAtLastLayout(other.m_stickyOffsetAtLastLayout) 131 , m_layerPositionAtLastLayout(other.m_layerPositionAtLastLayout) 132 { } 133 134 FloatSize computeStickyOffset(const FloatRect& viewportRect) const; 135 136 const FloatSize stickyOffsetAtLastLayout() const { return m_stickyOffsetAtLastLayout; } 137 void setStickyOffsetAtLastLayout(const FloatSize& offset) { m_stickyOffsetAtLastLayout = offset; } 138 139 FloatPoint layerPositionForViewportRect(const FloatRect& viewportRect) const; 140 141 const FloatPoint& layerPositionAtLastLayout() const { return m_layerPositionAtLastLayout; } 142 void setLayerPositionAtLastLayout(const FloatPoint& point) { m_layerPositionAtLastLayout = point; } 143 144 float leftOffset() const { return m_leftOffset; } 145 float rightOffset() const { return m_rightOffset; } 146 float topOffset() const { return m_topOffset; } 147 float bottomOffset() const { return m_bottomOffset; } 148 149 void setLeftOffset(float offset) { m_leftOffset = offset; } 150 void setRightOffset(float offset) { m_rightOffset = offset; } 151 void setTopOffset(float offset) { m_topOffset = offset; } 152 void setBottomOffset(float offset) { m_bottomOffset = offset; } 153 154 FloatRect absoluteContainingBlockRect() const { return m_absoluteContainingBlockRect; } 155 void setAbsoluteContainingBlockRect(const FloatRect& rect) { m_absoluteContainingBlockRect = rect; } 156 157 FloatRect absoluteStickyBoxRect() const { return m_absoluteStickyBoxRect; } 158 void setAbsoluteStickyBoxRect(const FloatRect& rect) { m_absoluteStickyBoxRect = rect; } 159 160 bool operator==(const StickyPositionViewportConstraints& other) const 161 { 162 return m_leftOffset == other.m_leftOffset 163 && m_rightOffset == other.m_rightOffset 164 && m_topOffset == other.m_topOffset 165 && m_bottomOffset == other.m_bottomOffset 166 && m_absoluteContainingBlockRect == other.m_absoluteContainingBlockRect 167 && m_absoluteStickyBoxRect == other.m_absoluteStickyBoxRect 168 && m_stickyOffsetAtLastLayout == other.m_stickyOffsetAtLastLayout 169 && m_layerPositionAtLastLayout == other.m_layerPositionAtLastLayout; 170 } 171 172 bool operator!=(const StickyPositionViewportConstraints& other) const { return !(*this == other); } 173 174 private: 175 virtual ConstraintType constraintType() const OVERRIDE { return StickyPositionConstraint; }; 176 177 float m_leftOffset; 178 float m_rightOffset; 179 float m_topOffset; 180 float m_bottomOffset; 181 FloatRect m_absoluteContainingBlockRect; 182 FloatRect m_absoluteStickyBoxRect; 183 FloatSize m_stickyOffsetAtLastLayout; 184 FloatPoint m_layerPositionAtLastLayout; 185 }; 186 187 } // namespace WebCore 188 189 #endif // ScrollingConstraints_h 190