OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/services/view_manager/node.h" | 5 #include "mojo/services/view_manager/server_view.h" |
6 | 6 |
7 #include "mojo/services/view_manager/node_delegate.h" | 7 #include "mojo/services/view_manager/server_view_delegate.h" |
8 | 8 |
9 namespace mojo { | 9 namespace mojo { |
10 namespace service { | 10 namespace service { |
11 | 11 |
12 Node::Node(NodeDelegate* delegate, const NodeId& id) | 12 ServerView::ServerView(ServerViewDelegate* delegate, const ViewId& id) |
13 : delegate_(delegate), | 13 : delegate_(delegate), id_(id), parent_(NULL), visible_(true) { |
14 id_(id), | |
15 parent_(NULL), | |
16 visible_(true) { | |
17 DCHECK(delegate); // Must provide a delegate. | 14 DCHECK(delegate); // Must provide a delegate. |
18 } | 15 } |
19 | 16 |
20 Node::~Node() { | 17 ServerView::~ServerView() { |
21 while (!children_.empty()) | 18 while (!children_.empty()) |
22 children_.front()->parent()->Remove(children_.front()); | 19 children_.front()->parent()->Remove(children_.front()); |
23 | 20 |
24 if (parent_) | 21 if (parent_) |
25 parent_->Remove(this); | 22 parent_->Remove(this); |
26 | 23 |
27 delegate_->OnNodeDestroyed(this); | 24 delegate_->OnViewDestroyed(this); |
28 } | 25 } |
29 | 26 |
30 void Node::Add(Node* child) { | 27 void ServerView::Add(ServerView* child) { |
31 // We assume validation checks happened already. | 28 // We assume validation checks happened already. |
32 DCHECK(child); | 29 DCHECK(child); |
33 DCHECK(child != this); | 30 DCHECK(child != this); |
34 DCHECK(!child->Contains(this)); | 31 DCHECK(!child->Contains(this)); |
35 if (child->parent() == this) { | 32 if (child->parent() == this) { |
36 if (children_.size() == 1) | 33 if (children_.size() == 1) |
37 return; // Already in the right position. | 34 return; // Already in the right position. |
38 Reorder(child, children_.back(), ORDER_DIRECTION_ABOVE); | 35 Reorder(child, children_.back(), ORDER_DIRECTION_ABOVE); |
39 return; | 36 return; |
40 } | 37 } |
41 | 38 |
42 const Node* old_parent = child->parent(); | 39 const ServerView* old_parent = child->parent(); |
43 if (child->parent()) | 40 if (child->parent()) |
44 child->parent()->RemoveImpl(child); | 41 child->parent()->RemoveImpl(child); |
45 | 42 |
46 child->parent_ = this; | 43 child->parent_ = this; |
47 children_.push_back(child); | 44 children_.push_back(child); |
48 child->delegate_->OnNodeHierarchyChanged(child, this, old_parent); | 45 child->delegate_->OnViewHierarchyChanged(child, this, old_parent); |
49 } | 46 } |
50 | 47 |
51 void Node::Remove(Node* child) { | 48 void ServerView::Remove(ServerView* child) { |
52 // We assume validation checks happened else where. | 49 // We assume validation checks happened else where. |
53 DCHECK(child); | 50 DCHECK(child); |
54 DCHECK(child != this); | 51 DCHECK(child != this); |
55 DCHECK(child->parent() == this); | 52 DCHECK(child->parent() == this); |
56 | 53 |
57 RemoveImpl(child); | 54 RemoveImpl(child); |
58 child->delegate_->OnNodeHierarchyChanged(child, NULL, this); | 55 child->delegate_->OnViewHierarchyChanged(child, NULL, this); |
59 } | 56 } |
60 | 57 |
61 void Node::Reorder(Node* child, Node* relative, OrderDirection direction) { | 58 void ServerView::Reorder(ServerView* child, |
| 59 ServerView* relative, |
| 60 OrderDirection direction) { |
62 // We assume validation checks happened else where. | 61 // We assume validation checks happened else where. |
63 DCHECK(child); | 62 DCHECK(child); |
64 DCHECK(child->parent() == this); | 63 DCHECK(child->parent() == this); |
65 DCHECK_GT(children_.size(), 1u); | 64 DCHECK_GT(children_.size(), 1u); |
66 children_.erase(std::find(children_.begin(), children_.end(), child)); | 65 children_.erase(std::find(children_.begin(), children_.end(), child)); |
67 Nodes::iterator i = std::find(children_.begin(), children_.end(), relative); | 66 Views::iterator i = std::find(children_.begin(), children_.end(), relative); |
68 if (direction == ORDER_DIRECTION_ABOVE) { | 67 if (direction == ORDER_DIRECTION_ABOVE) { |
69 DCHECK(i != children_.end()); | 68 DCHECK(i != children_.end()); |
70 children_.insert(++i, child); | 69 children_.insert(++i, child); |
71 } else if (direction == ORDER_DIRECTION_BELOW) { | 70 } else if (direction == ORDER_DIRECTION_BELOW) { |
72 DCHECK(i != children_.end()); | 71 DCHECK(i != children_.end()); |
73 children_.insert(i, child); | 72 children_.insert(i, child); |
74 } | 73 } |
75 } | 74 } |
76 | 75 |
77 void Node::SetBounds(const gfx::Rect& bounds) { | 76 void ServerView::SetBounds(const gfx::Rect& bounds) { |
78 if (bounds_ == bounds) | 77 if (bounds_ == bounds) |
79 return; | 78 return; |
80 | 79 |
81 const gfx::Rect old_bounds = bounds_; | 80 const gfx::Rect old_bounds = bounds_; |
82 bounds_ = bounds; | 81 bounds_ = bounds; |
83 delegate_->OnNodeBoundsChanged(this, old_bounds, bounds); | 82 delegate_->OnViewBoundsChanged(this, old_bounds, bounds); |
84 } | 83 } |
85 | 84 |
86 const Node* Node::GetRoot() const { | 85 const ServerView* ServerView::GetRoot() const { |
87 const Node* node = this; | 86 const ServerView* view = this; |
88 while (node && node->parent()) | 87 while (view && view->parent()) |
89 node = node->parent(); | 88 view = view->parent(); |
90 return node; | 89 return view; |
91 } | 90 } |
92 | 91 |
93 std::vector<const Node*> Node::GetChildren() const { | 92 std::vector<const ServerView*> ServerView::GetChildren() const { |
94 std::vector<const Node*> children; | 93 std::vector<const ServerView*> children; |
95 children.reserve(children_.size()); | 94 children.reserve(children_.size()); |
96 for (size_t i = 0; i < children_.size(); ++i) | 95 for (size_t i = 0; i < children_.size(); ++i) |
97 children.push_back(children_[i]); | 96 children.push_back(children_[i]); |
98 return children; | 97 return children; |
99 } | 98 } |
100 | 99 |
101 std::vector<Node*> Node::GetChildren() { | 100 std::vector<ServerView*> ServerView::GetChildren() { |
102 // TODO(sky): rename to children() and fix return type. | 101 // TODO(sky): rename to children() and fix return type. |
103 return children_; | 102 return children_; |
104 } | 103 } |
105 | 104 |
106 bool Node::Contains(const Node* node) const { | 105 bool ServerView::Contains(const ServerView* view) const { |
107 for (const Node* parent = node; parent; parent = parent->parent_) { | 106 for (const ServerView* parent = view; parent; parent = parent->parent_) { |
108 if (parent == this) | 107 if (parent == this) |
109 return true; | 108 return true; |
110 } | 109 } |
111 return false; | 110 return false; |
112 } | 111 } |
113 | 112 |
114 void Node::SetVisible(bool value) { | 113 void ServerView::SetVisible(bool value) { |
115 if (visible_ == value) | 114 if (visible_ == value) |
116 return; | 115 return; |
117 | 116 |
118 visible_ = value; | 117 visible_ = value; |
119 // TODO(sky): notification, including repaint. | 118 // TODO(sky): notification, including repaint. |
120 } | 119 } |
121 | 120 |
122 void Node::SetBitmap(const SkBitmap& bitmap) { | 121 void ServerView::SetBitmap(const SkBitmap& bitmap) { |
123 bitmap_ = bitmap; | 122 bitmap_ = bitmap; |
124 delegate_->OnNodeBitmapChanged(this); | 123 delegate_->OnViewBitmapChanged(this); |
125 } | 124 } |
126 | 125 |
127 void Node::RemoveImpl(Node* node) { | 126 void ServerView::RemoveImpl(ServerView* view) { |
128 node->parent_ = NULL; | 127 view->parent_ = NULL; |
129 children_.erase(std::find(children_.begin(), children_.end(), node)); | 128 children_.erase(std::find(children_.begin(), children_.end(), view)); |
130 } | 129 } |
131 | 130 |
132 } // namespace service | 131 } // namespace service |
133 } // namespace mojo | 132 } // namespace mojo |
OLD | NEW |