OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/services/public/cpp/view_manager/view_tree_node.h" | |
6 | |
7 #include "mojo/services/public/cpp/view_manager/lib/view_manager_synchronizer.h" | |
8 #include "mojo/services/public/cpp/view_manager/lib/view_private.h" | |
9 #include "mojo/services/public/cpp/view_manager/lib/view_tree_node_private.h" | |
10 #include "mojo/services/public/cpp/view_manager/view.h" | |
11 #include "mojo/services/public/cpp/view_manager/view_tree_node_observer.h" | |
12 | |
13 namespace mojo { | |
14 namespace view_manager { | |
15 | |
16 namespace { | |
17 | |
18 void NotifyViewTreeChangeAtReceiver( | |
19 ViewTreeNode* receiver, | |
20 const ViewTreeNodeObserver::TreeChangeParams& params) { | |
21 ViewTreeNodeObserver::TreeChangeParams local_params = params; | |
22 local_params.receiver = receiver; | |
23 FOR_EACH_OBSERVER(ViewTreeNodeObserver, | |
24 *ViewTreeNodePrivate(receiver).observers(), | |
25 OnTreeChange(local_params)); | |
26 } | |
27 | |
28 void NotifyViewTreeChangeUp( | |
29 ViewTreeNode* start_at, | |
30 const ViewTreeNodeObserver::TreeChangeParams& params) { | |
31 for (ViewTreeNode* current = start_at; current; current = current->parent()) | |
32 NotifyViewTreeChangeAtReceiver(current, params); | |
33 } | |
34 | |
35 void NotifyViewTreeChangeDown( | |
36 ViewTreeNode* start_at, | |
37 const ViewTreeNodeObserver::TreeChangeParams& params) { | |
38 NotifyViewTreeChangeAtReceiver(start_at, params); | |
39 ViewTreeNode::Children::const_iterator it = start_at->children().begin(); | |
40 for (; it != start_at->children().end(); ++it) | |
41 NotifyViewTreeChangeDown(*it, params); | |
42 } | |
43 | |
44 void NotifyViewTreeChange( | |
45 const ViewTreeNodeObserver::TreeChangeParams& params) { | |
46 NotifyViewTreeChangeDown(params.target, params); | |
47 if (params.old_parent) | |
48 NotifyViewTreeChangeUp(params.old_parent, params); | |
49 if (params.new_parent) | |
50 NotifyViewTreeChangeUp(params.new_parent, params); | |
51 } | |
52 | |
53 class ScopedTreeNotifier { | |
54 public: | |
55 ScopedTreeNotifier(ViewTreeNode* target, | |
56 ViewTreeNode* old_parent, | |
57 ViewTreeNode* new_parent) { | |
58 params_.target = target; | |
59 params_.old_parent = old_parent; | |
60 params_.new_parent = new_parent; | |
61 NotifyViewTreeChange(params_); | |
62 } | |
63 ~ScopedTreeNotifier() { | |
64 params_.phase = ViewTreeNodeObserver::DISPOSITION_CHANGED; | |
65 NotifyViewTreeChange(params_); | |
66 } | |
67 | |
68 private: | |
69 ViewTreeNodeObserver::TreeChangeParams params_; | |
70 | |
71 DISALLOW_COPY_AND_ASSIGN(ScopedTreeNotifier); | |
72 }; | |
73 | |
74 void RemoveChildImpl(ViewTreeNode* child, ViewTreeNode::Children* children) { | |
75 ViewTreeNode::Children::iterator it = | |
76 std::find(children->begin(), children->end(), child); | |
77 if (it != children->end()) { | |
78 children->erase(it); | |
79 ViewTreeNodePrivate(child).ClearParent(); | |
80 } | |
81 } | |
82 | |
83 class ScopedOrderChangedNotifier { | |
84 public: | |
85 ScopedOrderChangedNotifier(ViewTreeNode* node, | |
86 ViewTreeNode* relative_node, | |
87 OrderDirection direction) | |
88 : node_(node), | |
89 relative_node_(relative_node), | |
90 direction_(direction) { | |
91 FOR_EACH_OBSERVER( | |
92 ViewTreeNodeObserver, | |
93 *ViewTreeNodePrivate(node_).observers(), | |
94 OnNodeReordered(node_, | |
95 relative_node_, | |
96 direction_, | |
97 ViewTreeNodeObserver::DISPOSITION_CHANGING)); | |
98 | |
99 } | |
100 ~ScopedOrderChangedNotifier() { | |
101 FOR_EACH_OBSERVER( | |
102 ViewTreeNodeObserver, | |
103 *ViewTreeNodePrivate(node_).observers(), | |
104 OnNodeReordered(node_, | |
105 relative_node_, | |
106 direction_, | |
107 ViewTreeNodeObserver::DISPOSITION_CHANGED)); | |
108 } | |
109 | |
110 private: | |
111 ViewTreeNode* node_; | |
112 ViewTreeNode* relative_node_; | |
113 OrderDirection direction_; | |
114 | |
115 DISALLOW_COPY_AND_ASSIGN(ScopedOrderChangedNotifier); | |
116 }; | |
117 | |
118 // Returns true if the order actually changed. | |
119 bool ReorderImpl(ViewTreeNode::Children* children, | |
120 ViewTreeNode* node, | |
121 ViewTreeNode* relative, | |
122 OrderDirection direction) { | |
123 DCHECK(relative); | |
124 DCHECK_NE(node, relative); | |
125 DCHECK_EQ(node->parent(), relative->parent()); | |
126 | |
127 const size_t child_i = | |
128 std::find(children->begin(), children->end(), node) - children->begin(); | |
129 const size_t target_i = | |
130 std::find(children->begin(), children->end(), relative) - | |
131 children->begin(); | |
132 if ((direction == ORDER_ABOVE && child_i == target_i + 1) || | |
133 (direction == ORDER_BELOW && child_i + 1 == target_i)) { | |
134 return false; | |
135 } | |
136 | |
137 ScopedOrderChangedNotifier notifier(node, relative, direction); | |
138 | |
139 const size_t dest_i = | |
140 direction == ORDER_ABOVE ? | |
141 (child_i < target_i ? target_i : target_i + 1) : | |
142 (child_i < target_i ? target_i - 1 : target_i); | |
143 children->erase(children->begin() + child_i); | |
144 children->insert(children->begin() + dest_i, node); | |
145 | |
146 return true; | |
147 } | |
148 | |
149 class ScopedSetActiveViewNotifier { | |
150 public: | |
151 ScopedSetActiveViewNotifier(ViewTreeNode* node, | |
152 View* old_view, | |
153 View* new_view) | |
154 : node_(node), | |
155 old_view_(old_view), | |
156 new_view_(new_view) { | |
157 FOR_EACH_OBSERVER( | |
158 ViewTreeNodeObserver, | |
159 *ViewTreeNodePrivate(node).observers(), | |
160 OnNodeActiveViewChange(node_, | |
161 old_view_, | |
162 new_view_, | |
163 ViewTreeNodeObserver::DISPOSITION_CHANGING)); | |
164 } | |
165 ~ScopedSetActiveViewNotifier() { | |
166 FOR_EACH_OBSERVER( | |
167 ViewTreeNodeObserver, | |
168 *ViewTreeNodePrivate(node_).observers(), | |
169 OnNodeActiveViewChange(node_, | |
170 old_view_, | |
171 new_view_, | |
172 ViewTreeNodeObserver::DISPOSITION_CHANGED)); | |
173 } | |
174 | |
175 private: | |
176 ViewTreeNode* node_; | |
177 View* old_view_; | |
178 View* new_view_; | |
179 | |
180 DISALLOW_COPY_AND_ASSIGN(ScopedSetActiveViewNotifier); | |
181 }; | |
182 | |
183 class ScopedSetBoundsNotifier { | |
184 public: | |
185 ScopedSetBoundsNotifier(ViewTreeNode* node, | |
186 const gfx::Rect& old_bounds, | |
187 const gfx::Rect& new_bounds) | |
188 : node_(node), | |
189 old_bounds_(old_bounds), | |
190 new_bounds_(new_bounds) { | |
191 FOR_EACH_OBSERVER( | |
192 ViewTreeNodeObserver, | |
193 *ViewTreeNodePrivate(node_).observers(), | |
194 OnNodeBoundsChange(node_, | |
195 old_bounds_, | |
196 new_bounds_, | |
197 ViewTreeNodeObserver::DISPOSITION_CHANGING)); | |
198 } | |
199 ~ScopedSetBoundsNotifier() { | |
200 FOR_EACH_OBSERVER( | |
201 ViewTreeNodeObserver, | |
202 *ViewTreeNodePrivate(node_).observers(), | |
203 OnNodeBoundsChange(node_, | |
204 old_bounds_, | |
205 new_bounds_, | |
206 ViewTreeNodeObserver::DISPOSITION_CHANGED)); | |
207 } | |
208 | |
209 private: | |
210 ViewTreeNode* node_; | |
211 const gfx::Rect old_bounds_; | |
212 const gfx::Rect new_bounds_; | |
213 | |
214 DISALLOW_COPY_AND_ASSIGN(ScopedSetBoundsNotifier); | |
215 }; | |
216 | |
217 class ScopedDestructionNotifier { | |
218 public: | |
219 explicit ScopedDestructionNotifier(ViewTreeNode* node) | |
220 : node_(node) { | |
221 FOR_EACH_OBSERVER( | |
222 ViewTreeNodeObserver, | |
223 *ViewTreeNodePrivate(node_).observers(), | |
224 OnNodeDestroy(node_, ViewTreeNodeObserver::DISPOSITION_CHANGING)); | |
225 } | |
226 ~ScopedDestructionNotifier() { | |
227 FOR_EACH_OBSERVER( | |
228 ViewTreeNodeObserver, | |
229 *ViewTreeNodePrivate(node_).observers(), | |
230 OnNodeDestroy(node_, ViewTreeNodeObserver::DISPOSITION_CHANGED)); | |
231 } | |
232 | |
233 private: | |
234 ViewTreeNode* node_; | |
235 | |
236 DISALLOW_COPY_AND_ASSIGN(ScopedDestructionNotifier); | |
237 }; | |
238 | |
239 // Some operations are only permitted in the connection that created the node. | |
240 bool OwnsNode(ViewManager* manager, ViewTreeNode* node) { | |
241 return !manager || | |
242 static_cast<ViewManagerSynchronizer*>(manager)->OwnsNode(node->id()); | |
243 } | |
244 | |
245 } // namespace | |
246 | |
247 //////////////////////////////////////////////////////////////////////////////// | |
248 // ViewTreeNode, public: | |
249 | |
250 // static | |
251 ViewTreeNode* ViewTreeNode::Create(ViewManager* view_manager) { | |
252 ViewTreeNode* node = new ViewTreeNode(view_manager); | |
253 static_cast<ViewManagerSynchronizer*>(view_manager)->AddNode(node); | |
254 return node; | |
255 } | |
256 | |
257 void ViewTreeNode::Destroy() { | |
258 if (!OwnsNode(manager_, this)) | |
259 return; | |
260 | |
261 if (manager_) | |
262 static_cast<ViewManagerSynchronizer*>(manager_)->DestroyViewTreeNode(id_); | |
263 while (!children_.empty()) | |
264 children_.front()->Destroy(); | |
265 LocalDestroy(); | |
266 } | |
267 | |
268 void ViewTreeNode::SetBounds(const gfx::Rect& bounds) { | |
269 if (!OwnsNode(manager_, this)) | |
270 return; | |
271 | |
272 if (manager_) | |
273 static_cast<ViewManagerSynchronizer*>(manager_)->SetBounds(id_, bounds); | |
274 LocalSetBounds(bounds_, bounds); | |
275 } | |
276 | |
277 void ViewTreeNode::AddObserver(ViewTreeNodeObserver* observer) { | |
278 observers_.AddObserver(observer); | |
279 } | |
280 | |
281 void ViewTreeNode::RemoveObserver(ViewTreeNodeObserver* observer) { | |
282 observers_.RemoveObserver(observer); | |
283 } | |
284 | |
285 void ViewTreeNode::AddChild(ViewTreeNode* child) { | |
286 // TODO(beng): not necessarily valid to all connections, but possibly to the | |
287 // embeddee in an embedder-embeddee relationship. | |
288 if (manager_) | |
289 CHECK_EQ(ViewTreeNodePrivate(child).view_manager(), manager_); | |
290 LocalAddChild(child); | |
291 if (manager_) | |
292 static_cast<ViewManagerSynchronizer*>(manager_)->AddChild(child->id(), id_); | |
293 } | |
294 | |
295 void ViewTreeNode::RemoveChild(ViewTreeNode* child) { | |
296 // TODO(beng): not necessarily valid to all connections, but possibly to the | |
297 // embeddee in an embedder-embeddee relationship. | |
298 if (manager_) | |
299 CHECK_EQ(ViewTreeNodePrivate(child).view_manager(), manager_); | |
300 LocalRemoveChild(child); | |
301 if (manager_) { | |
302 static_cast<ViewManagerSynchronizer*>(manager_)->RemoveChild(child->id(), | |
303 id_); | |
304 } | |
305 } | |
306 | |
307 void ViewTreeNode::MoveToFront() { | |
308 Reorder(parent_->children_.back(), ORDER_ABOVE); | |
309 } | |
310 | |
311 void ViewTreeNode::MoveToBack() { | |
312 Reorder(parent_->children_.front(), ORDER_BELOW); | |
313 } | |
314 | |
315 void ViewTreeNode::Reorder(ViewTreeNode* relative, OrderDirection direction) { | |
316 if (!LocalReorder(relative, direction)) | |
317 return; | |
318 if (manager_) { | |
319 static_cast<ViewManagerSynchronizer*>(manager_)->Reorder(id_, | |
320 relative->id(), | |
321 direction); | |
322 } | |
323 } | |
324 | |
325 bool ViewTreeNode::Contains(ViewTreeNode* child) const { | |
326 if (manager_) | |
327 CHECK_EQ(ViewTreeNodePrivate(child).view_manager(), manager_); | |
328 for (ViewTreeNode* p = child->parent(); p; p = p->parent()) { | |
329 if (p == this) | |
330 return true; | |
331 } | |
332 return false; | |
333 } | |
334 | |
335 ViewTreeNode* ViewTreeNode::GetChildById(Id id) { | |
336 if (id == id_) | |
337 return this; | |
338 // TODO(beng): this could be improved depending on how we decide to own nodes. | |
339 Children::const_iterator it = children_.begin(); | |
340 for (; it != children_.end(); ++it) { | |
341 ViewTreeNode* node = (*it)->GetChildById(id); | |
342 if (node) | |
343 return node; | |
344 } | |
345 return NULL; | |
346 } | |
347 | |
348 void ViewTreeNode::SetActiveView(View* view) { | |
349 // TODO(beng): not necessarily valid to all connections, but possibly to the | |
350 // embeddee in an embedder-embeddee relationship. | |
351 if (manager_) | |
352 CHECK_EQ(ViewPrivate(view).view_manager(), manager_); | |
353 LocalSetActiveView(view); | |
354 if (manager_) { | |
355 static_cast<ViewManagerSynchronizer*>(manager_)->SetActiveView( | |
356 id_, active_view_->id()); | |
357 } | |
358 } | |
359 | |
360 void ViewTreeNode::SetFocus() { | |
361 if (manager_) | |
362 static_cast<ViewManagerSynchronizer*>(manager_)->SetFocus(id_); | |
363 } | |
364 | |
365 void ViewTreeNode::Embed(const String& url) { | |
366 static_cast<ViewManagerSynchronizer*>(manager_)->Embed(url, id_); | |
367 } | |
368 | |
369 //////////////////////////////////////////////////////////////////////////////// | |
370 // ViewTreeNode, protected: | |
371 | |
372 ViewTreeNode::ViewTreeNode() | |
373 : manager_(NULL), | |
374 id_(-1), | |
375 parent_(NULL), | |
376 active_view_(NULL) {} | |
377 | |
378 ViewTreeNode::~ViewTreeNode() { | |
379 ScopedDestructionNotifier notifier(this); | |
380 if (parent_) | |
381 parent_->LocalRemoveChild(this); | |
382 // TODO(beng): It'd be better to do this via a destruction observer in the | |
383 // synchronizer. | |
384 if (manager_) | |
385 static_cast<ViewManagerSynchronizer*>(manager_)->RemoveNode(id_); | |
386 } | |
387 | |
388 //////////////////////////////////////////////////////////////////////////////// | |
389 // ViewTreeNode, private: | |
390 | |
391 ViewTreeNode::ViewTreeNode(ViewManager* manager) | |
392 : manager_(manager), | |
393 id_(static_cast<ViewManagerSynchronizer*>( | |
394 manager_)->CreateViewTreeNode()), | |
395 parent_(NULL), | |
396 active_view_(NULL) {} | |
397 | |
398 void ViewTreeNode::LocalDestroy() { | |
399 delete this; | |
400 } | |
401 | |
402 void ViewTreeNode::LocalAddChild(ViewTreeNode* child) { | |
403 ScopedTreeNotifier notifier(child, child->parent(), this); | |
404 if (child->parent()) | |
405 RemoveChildImpl(child, &child->parent_->children_); | |
406 children_.push_back(child); | |
407 child->parent_ = this; | |
408 } | |
409 | |
410 void ViewTreeNode::LocalRemoveChild(ViewTreeNode* child) { | |
411 DCHECK_EQ(this, child->parent()); | |
412 ScopedTreeNotifier notifier(child, this, NULL); | |
413 RemoveChildImpl(child, &children_); | |
414 } | |
415 | |
416 bool ViewTreeNode::LocalReorder(ViewTreeNode* relative, | |
417 OrderDirection direction) { | |
418 return ReorderImpl(&parent_->children_, this, relative, direction); | |
419 } | |
420 | |
421 void ViewTreeNode::LocalSetActiveView(View* view) { | |
422 ScopedSetActiveViewNotifier notifier(this, active_view_, view); | |
423 if (active_view_) | |
424 ViewPrivate(active_view_).set_node(NULL); | |
425 active_view_ = view; | |
426 if (active_view_) | |
427 ViewPrivate(active_view_).set_node(this); | |
428 } | |
429 | |
430 void ViewTreeNode::LocalSetBounds(const gfx::Rect& old_bounds, | |
431 const gfx::Rect& new_bounds) { | |
432 DCHECK(old_bounds == bounds_); | |
433 ScopedSetBoundsNotifier notifier(this, old_bounds, new_bounds); | |
434 bounds_ = new_bounds; | |
435 } | |
436 | |
437 } // namespace view_manager | |
438 } // namespace mojo | |
OLD | NEW |