Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: mojo/services/public/cpp/view_manager/lib/view_tree_node.cc

Issue 327073003: Reorder (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/public/cpp/view_manager/view_tree_node.h" 5 #include "mojo/services/public/cpp/view_manager/view_tree_node.h"
6 6
7 #include "mojo/services/public/cpp/view_manager/lib/view_manager_synchronizer.h" 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" 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" 9 #include "mojo/services/public/cpp/view_manager/lib/view_tree_node_private.h"
10 #include "mojo/services/public/cpp/view_manager/view.h" 10 #include "mojo/services/public/cpp/view_manager/view.h"
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 73
74 void RemoveChildImpl(ViewTreeNode* child, ViewTreeNode::Children* children) { 74 void RemoveChildImpl(ViewTreeNode* child, ViewTreeNode::Children* children) {
75 ViewTreeNode::Children::iterator it = 75 ViewTreeNode::Children::iterator it =
76 std::find(children->begin(), children->end(), child); 76 std::find(children->begin(), children->end(), child);
77 if (it != children->end()) { 77 if (it != children->end()) {
78 children->erase(it); 78 children->erase(it);
79 ViewTreeNodePrivate(child).ClearParent(); 79 ViewTreeNodePrivate(child).ClearParent();
80 } 80 }
81 } 81 }
82 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)) {
sky 2014/06/11 19:36:44 nit: '+1' -> '+ 1'
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
83 class ScopedSetActiveViewNotifier { 149 class ScopedSetActiveViewNotifier {
84 public: 150 public:
85 ScopedSetActiveViewNotifier(ViewTreeNode* node, 151 ScopedSetActiveViewNotifier(ViewTreeNode* node,
86 View* old_view, 152 View* old_view,
87 View* new_view) 153 View* new_view)
88 : node_(node), 154 : node_(node),
89 old_view_(old_view), 155 old_view_(old_view),
90 new_view_(new_view) { 156 new_view_(new_view) {
91 FOR_EACH_OBSERVER( 157 FOR_EACH_OBSERVER(
92 ViewTreeNodeObserver, 158 ViewTreeNodeObserver,
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 // embeddee in an embedder-embeddee relationship. 297 // embeddee in an embedder-embeddee relationship.
232 if (manager_) 298 if (manager_)
233 CHECK_EQ(ViewTreeNodePrivate(child).view_manager(), manager_); 299 CHECK_EQ(ViewTreeNodePrivate(child).view_manager(), manager_);
234 LocalRemoveChild(child); 300 LocalRemoveChild(child);
235 if (manager_) { 301 if (manager_) {
236 static_cast<ViewManagerSynchronizer*>(manager_)->RemoveChild(child->id(), 302 static_cast<ViewManagerSynchronizer*>(manager_)->RemoveChild(child->id(),
237 id_); 303 id_);
238 } 304 }
239 } 305 }
240 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
241 bool ViewTreeNode::Contains(ViewTreeNode* child) const { 325 bool ViewTreeNode::Contains(ViewTreeNode* child) const {
242 if (manager_) 326 if (manager_)
243 CHECK_EQ(ViewTreeNodePrivate(child).view_manager(), manager_); 327 CHECK_EQ(ViewTreeNodePrivate(child).view_manager(), manager_);
244 for (ViewTreeNode* p = child->parent(); p; p = p->parent()) { 328 for (ViewTreeNode* p = child->parent(); p; p = p->parent()) {
245 if (p == this) 329 if (p == this)
246 return true; 330 return true;
247 } 331 }
248 return false; 332 return false;
249 } 333 }
250 334
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 children_.push_back(child); 401 children_.push_back(child);
318 child->parent_ = this; 402 child->parent_ = this;
319 } 403 }
320 404
321 void ViewTreeNode::LocalRemoveChild(ViewTreeNode* child) { 405 void ViewTreeNode::LocalRemoveChild(ViewTreeNode* child) {
322 DCHECK_EQ(this, child->parent()); 406 DCHECK_EQ(this, child->parent());
323 ScopedTreeNotifier notifier(child, this, NULL); 407 ScopedTreeNotifier notifier(child, this, NULL);
324 RemoveChildImpl(child, &children_); 408 RemoveChildImpl(child, &children_);
325 } 409 }
326 410
411 bool ViewTreeNode::LocalReorder(ViewTreeNode* relative,
412 OrderDirection direction) {
413 return ReorderImpl(&parent_->children_, this, relative, direction);
414 }
415
327 void ViewTreeNode::LocalSetActiveView(View* view) { 416 void ViewTreeNode::LocalSetActiveView(View* view) {
328 ScopedSetActiveViewNotifier notifier(this, active_view_, view); 417 ScopedSetActiveViewNotifier notifier(this, active_view_, view);
329 if (active_view_) 418 if (active_view_)
330 ViewPrivate(active_view_).set_node(NULL); 419 ViewPrivate(active_view_).set_node(NULL);
331 active_view_ = view; 420 active_view_ = view;
332 if (active_view_) 421 if (active_view_)
333 ViewPrivate(active_view_).set_node(this); 422 ViewPrivate(active_view_).set_node(this);
334 } 423 }
335 424
336 void ViewTreeNode::LocalSetBounds(const gfx::Rect& old_bounds, 425 void ViewTreeNode::LocalSetBounds(const gfx::Rect& old_bounds,
337 const gfx::Rect& new_bounds) { 426 const gfx::Rect& new_bounds) {
338 DCHECK(old_bounds == bounds_); 427 DCHECK(old_bounds == bounds_);
339 ScopedSetBoundsNotifier notifier(this, old_bounds, new_bounds); 428 ScopedSetBoundsNotifier notifier(this, old_bounds, new_bounds);
340 bounds_ = new_bounds; 429 bounds_ = new_bounds;
341 } 430 }
342 431
343 } // namespace view_manager 432 } // namespace view_manager
344 } // namespace mojo 433 } // namespace mojo
345 434
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698