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::dumpProperties(TextStream& ts, ScrollingStateTreeAsTextBehavior behavior) const
81{
82 if (behavior & ScrollingStateTreeAsTextBehaviorIncludeNodeIDs)
83 ts.dumpProperty("nodeID", scrollingNodeID());
84}
85
86ScrollingTreeFrameScrollingNode* ScrollingTreeNode::enclosingFrameNodeIncludingSelf()
87{
88 auto* node = this;
89 while (node && !node->isFrameScrollingNode())
90 node = node->parent();
91
92 return downcast<ScrollingTreeFrameScrollingNode>(node);
93}
94
95ScrollingTreeScrollingNode* ScrollingTreeNode::enclosingScrollingNodeIncludingSelf()
96{
97 auto* node = this;
98 while (node && !node->isScrollingNode())
99 node = node->parent();
100
101 return downcast<ScrollingTreeScrollingNode>(node);
102}
103
104void ScrollingTreeNode::dump(TextStream& ts, ScrollingStateTreeAsTextBehavior behavior) const
105{
106 dumpProperties(ts, behavior);
107
108 if (m_children) {
109 for (auto& child : *m_children) {
110 TextStream::GroupScope scope(ts);
111 child->dump(ts, behavior);
112 }
113 }
114}
115
116ScrollingTreeScrollingNode* ScrollingTreeNode::scrollingNodeForPoint(LayoutPoint parentPoint) const
117{
118 LayoutPoint localPoint = parentToLocalPoint(parentPoint);
119 LayoutPoint contentsPoint = localToContentsPoint(localPoint);
120
121 if (children()) {
122 for (auto iterator = children()->rbegin(), end = children()->rend(); iterator != end; iterator++) {
123 if (auto node = (**iterator).scrollingNodeForPoint(contentsPoint))
124 return node;
125 }
126 }
127
128 return nullptr;
129}
130
131} // namespace WebCore
132
133#endif // ENABLE(ASYNC_SCROLLING)
134