2 * Copyright (C) 2012 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef ScrollingConstraints_h
27 #define ScrollingConstraints_h
29 #include "platform/geometry/FloatRect.h"
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 {
37 // FIXME: Simplify this code now that position: sticky doesn't exist.
39 FixedPositionConstaint,
42 enum AnchorEdgeFlags {
43 AnchorEdgeLeft = 1 << 0,
44 AnchorEdgeRight = 1 << 1,
45 AnchorEdgeTop = 1 << 2,
46 AnchorEdgeBottom = 1 << 3
48 typedef unsigned AnchorEdges;
50 ViewportConstraints(const ViewportConstraints& other)
51 : m_alignmentOffset(other.m_alignmentOffset)
52 , m_anchorEdges(other.m_anchorEdges)
55 virtual ~ViewportConstraints() { }
57 virtual ConstraintType constraintType() const = 0;
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; }
64 FloatSize alignmentOffset() const { return m_alignmentOffset; }
65 void setAlignmentOffset(const FloatSize& offset) { m_alignmentOffset = offset; }
72 FloatSize m_alignmentOffset;
73 AnchorEdges m_anchorEdges;
76 class FixedPositionViewportConstraints final : public ViewportConstraints {
78 FixedPositionViewportConstraints()
79 : ViewportConstraints()
82 FixedPositionViewportConstraints(const FixedPositionViewportConstraints& other)
83 : ViewportConstraints(other)
84 , m_viewportRectAtLastLayout(other.m_viewportRectAtLastLayout)
85 , m_layerPositionAtLastLayout(other.m_layerPositionAtLastLayout)
88 FloatPoint layerPositionForViewportRect(const FloatRect& viewportRect) const;
90 const FloatRect& viewportRectAtLastLayout() const { return m_viewportRectAtLastLayout; }
91 void setViewportRectAtLastLayout(const FloatRect& rect) { m_viewportRectAtLastLayout = rect; }
93 const FloatPoint& layerPositionAtLastLayout() const { return m_layerPositionAtLastLayout; }
94 void setLayerPositionAtLastLayout(const FloatPoint& point) { m_layerPositionAtLastLayout = point; }
96 bool operator==(const FixedPositionViewportConstraints& other) const
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;
104 bool operator!=(const FixedPositionViewportConstraints& other) const { return !(*this == other); }
107 virtual ConstraintType constraintType() const override { return FixedPositionConstaint; }
109 FloatRect m_viewportRectAtLastLayout;
110 FloatPoint m_layerPositionAtLastLayout;
115 #endif // ScrollingConstraints_h