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 "ScrollingTreeNode.h"
28
29#if ENABLE(ASYNC_SCROLLING)
30
31#include "ScrollingStateTree.h"
32#include "ScrollingTree.h"
33#include "ScrollingTreeFrameScrollingNode.h"
34#include <wtf/text/TextStream.h>
35
36namespace WebCore {
37
38ScrollingTreeNode::ScrollingTreeNode(ScrollingTree& scrollingTree, ScrollingNodeType nodeType, ScrollingNodeID nodeID)
39 : m_scrollingTree(scrollingTree)
40 , m_nodeType(nodeType)
41 , m_nodeID(nodeID)
42 , m_parent(nullptr)
43{
44}
45
46ScrollingTreeNode::~ScrollingTreeNode() = default;
47
48void ScrollingTreeNode::appendChild(Ref<ScrollingTreeNode>&& childNode)
49{
50 childNode->setParent(this);
51
52 if (!m_children)
53 m_children = std::make_unique<Vector<RefPtr<ScrollingTreeNode>>>();
54 m_children->append(WTFMove(childNode));
55}
56
57void ScrollingTreeNode::removeChild(ScrollingTreeNode& node)
58{
59 if (!m_children)
60 return;
61
62 size_t index = m_children->find(&node);
63
64 // The index will be notFound if the node to remove is a deeper-than-1-level descendant or
65 // if node is the root state node.
66 if (index != notFound) {
67 m_children->remove(index);
68 return;
69 }
70
71 for (auto& child : *m_children)
72 child->removeChild(node);
73}
74
75bool ScrollingTreeNode::isRootNode() const
76{
77 return m_scrollingTree.rootNode() == this;
78}
79
80void ScrollingTreeNode::relatedNodeScrollPositionDidChange(const ScrollingTreeScrollingNode&, const FloatRect& layoutViewport, FloatSize& cumulativeDelta)
81{
82 applyLayerPositions(layoutViewport, cumulativeDelta);
83}
84
85void ScrollingTreeNode::dumpProperties(TextStream& ts, ScrollingStateTreeAsTextBehavior behavior) const
86{
87 if (behavior & ScrollingStateTreeAsTextBehaviorIncludeNodeIDs)
88 ts.dumpProperty("nodeID", scrollingNodeID());
89}
90
91ScrollingTreeFrameScrollingNode* ScrollingTreeNode::enclosingFrameNodeIncludingSelf()
92{
93 auto* node = this;
94 while (node && !node->isFrameScrollingNode())
95 node = node->parent();
96
97 return downcast<ScrollingTreeFrameScrollingNode>(node);
98}
99
100ScrollingTreeScrollingNode* ScrollingTreeNode::enclosingScrollingNodeIncludingSelf()
101{
102 auto* node = this;
103 while (node && !node->isScrollingNode())
104 node = node->parent();
105
106 return downcast<ScrollingTreeScrollingNode>(node);
107}
108
109void ScrollingTreeNode::dump(TextStream& ts, ScrollingStateTreeAsTextBehavior behavior) const
110{
111 dumpProperties(ts, behavior);
112
113 if (m_children) {
114 for (auto& child : *m_children) {
115 TextStream::GroupScope scope(ts);
116 child->dump(ts, behavior);
117 }
118 }
119}
120
121ScrollingTreeScrollingNode* ScrollingTreeNode::scrollingNodeForPoint(LayoutPoint parentPoint) const
122{
123 LayoutPoint localPoint = parentToLocalPoint(parentPoint);
124 LayoutPoint contentsPoint = localToContentsPoint(localPoint);
125
126 if (children()) {
127 for (auto iterator = children()->rbegin(), end = children()->rend(); iterator != end; iterator++) {
128 if (auto node = (**iterator).scrollingNodeForPoint(contentsPoint))
129 return node;
130 }
131 }
132
133 return nullptr;
134}
135
136} // namespace WebCore
137
138#endif // ENABLE(ASYNC_SCROLLING)
139