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#include "config.h"
27#include "ScrollingTreeStickyNode.h"
28
29#if ENABLE(ASYNC_SCROLLING)
30
31#include "ScrollingStateStickyNode.h"
32#include "ScrollingTree.h"
33#include "ScrollingTreeOverflowScrollingNode.h"
34#include <QuartzCore/CALayer.h>
35
36namespace WebCore {
37
38PassRefPtr<ScrollingTreeStickyNode> ScrollingTreeStickyNode::create(ScrollingTree& scrollingTree, ScrollingNodeID nodeID)
39{
40    return adoptRef(new ScrollingTreeStickyNode(scrollingTree, nodeID));
41}
42
43ScrollingTreeStickyNode::ScrollingTreeStickyNode(ScrollingTree& scrollingTree, ScrollingNodeID nodeID)
44    : ScrollingTreeNode(scrollingTree, StickyNode, nodeID)
45{
46    scrollingTree.fixedOrStickyNodeAdded();
47}
48
49ScrollingTreeStickyNode::~ScrollingTreeStickyNode()
50{
51    scrollingTree().fixedOrStickyNodeRemoved();
52}
53
54void ScrollingTreeStickyNode::updateBeforeChildren(const ScrollingStateNode& stateNode)
55{
56    const ScrollingStateStickyNode& stickyStateNode = toScrollingStateStickyNode(stateNode);
57
58    if (stickyStateNode.hasChangedProperty(ScrollingStateNode::ScrollLayer))
59        m_layer = stickyStateNode.layer();
60
61    if (stateNode.hasChangedProperty(ScrollingStateStickyNode::ViewportConstraints))
62        m_constraints = stickyStateNode.viewportConstraints();
63}
64
65static inline CGPoint operator*(CGPoint& a, const CGSize& b)
66{
67    return CGPointMake(a.x * b.width, a.y * b.height);
68}
69
70void ScrollingTreeStickyNode::updateLayersAfterAncestorChange(const ScrollingTreeNode& changedNode, const FloatRect& fixedPositionRect, const FloatSize& cumulativeDelta)
71{
72    bool adjustStickyLayer = false;
73    FloatRect constrainingRect;
74
75    if (parent()->isOverflowScrollingNode()) {
76        constrainingRect = FloatRect(toScrollingTreeOverflowScrollingNode(parent())->scrollPosition(), m_constraints.constrainingRectAtLastLayout().size());
77        adjustStickyLayer = true;
78    } else if (parent()->isFrameScrollingNode()) {
79        constrainingRect = fixedPositionRect;
80        adjustStickyLayer = true;
81    }
82
83    FloatSize deltaForDescendants = cumulativeDelta;
84
85    if (adjustStickyLayer) {
86        FloatPoint layerPosition = m_constraints.layerPositionForConstrainingRect(constrainingRect);
87
88        CGRect layerBounds = [m_layer bounds];
89        CGPoint anchorPoint = [m_layer anchorPoint];
90        CGPoint newPosition = layerPosition - m_constraints.alignmentOffset() + anchorPoint * layerBounds.size;
91        [m_layer setPosition:newPosition];
92
93        deltaForDescendants = layerPosition - m_constraints.layerPositionAtLastLayout() + cumulativeDelta;
94    }
95
96    if (!m_children)
97        return;
98
99    for (auto& child : *m_children)
100        child->updateLayersAfterAncestorChange(changedNode, fixedPositionRect, deltaForDescendants);
101}
102
103} // namespace WebCore
104
105#endif // ENABLE(ASYNC_SCROLLING)
106