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 "ScrollingTreeFixedNode.h"
28
29#if ENABLE(ASYNC_SCROLLING)
30
31#include "ScrollingStateFixedNode.h"
32#include "ScrollingTree.h"
33#include <QuartzCore/CALayer.h>
34
35namespace WebCore {
36
37PassRefPtr<ScrollingTreeFixedNode> ScrollingTreeFixedNode::create(ScrollingTree& scrollingTree, ScrollingNodeID nodeID)
38{
39    return adoptRef(new ScrollingTreeFixedNode(scrollingTree, nodeID));
40}
41
42ScrollingTreeFixedNode::ScrollingTreeFixedNode(ScrollingTree& scrollingTree, ScrollingNodeID nodeID)
43    : ScrollingTreeNode(scrollingTree, FixedNode, nodeID)
44{
45    scrollingTree.fixedOrStickyNodeAdded();
46}
47
48ScrollingTreeFixedNode::~ScrollingTreeFixedNode()
49{
50    scrollingTree().fixedOrStickyNodeRemoved();
51}
52
53void ScrollingTreeFixedNode::updateBeforeChildren(const ScrollingStateNode& stateNode)
54{
55    const ScrollingStateFixedNode& fixedStateNode = toScrollingStateFixedNode(stateNode);
56
57    if (fixedStateNode.hasChangedProperty(ScrollingStateNode::ScrollLayer))
58        m_layer = fixedStateNode.layer();
59
60    if (stateNode.hasChangedProperty(ScrollingStateFixedNode::ViewportConstraints))
61        m_constraints = fixedStateNode.viewportConstraints();
62}
63
64static inline CGPoint operator*(CGPoint& a, const CGSize& b)
65{
66    return CGPointMake(a.x * b.width, a.y * b.height);
67}
68
69void ScrollingTreeFixedNode::updateLayersAfterAncestorChange(const ScrollingTreeNode& changedNode, const FloatRect& fixedPositionRect, const FloatSize& cumulativeDelta)
70{
71    FloatPoint layerPosition = m_constraints.layerPositionForViewportRect(fixedPositionRect);
72    layerPosition -= cumulativeDelta;
73
74    CGRect layerBounds = [m_layer bounds];
75    CGPoint anchorPoint = [m_layer anchorPoint];
76    CGPoint newPosition = layerPosition - m_constraints.alignmentOffset() + anchorPoint * layerBounds.size;
77
78    [m_layer setPosition:newPosition];
79
80    if (!m_children)
81        return;
82
83    FloatSize newDelta = layerPosition - m_constraints.layerPositionAtLastLayout() + cumulativeDelta;
84
85    for (auto& child : *m_children)
86        child->updateLayersAfterAncestorChange(changedNode, fixedPositionRect, newDelta);
87}
88
89} // namespace WebCore
90
91#endif // ENABLE(ASYNC_SCROLLING)
92