OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #define _USE_MATH_DEFINES // For VC++ to get M_PI. This has to be first. | 5 #define _USE_MATH_DEFINES // For VC++ to get M_PI. This has to be first. |
6 | 6 |
7 #include "ui/views/view.h" | 7 #include "ui/views/view.h" |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <cmath> | 10 #include <cmath> |
11 #include <memory> | 11 #include <memory> |
12 #include <utility> | 12 #include <utility> |
13 | 13 |
14 #include "base/containers/adapters.h" | |
14 #include "base/logging.h" | 15 #include "base/logging.h" |
15 #include "base/macros.h" | 16 #include "base/macros.h" |
16 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
17 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
18 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
19 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
20 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
21 #include "base/trace_event/trace_event.h" | 22 #include "base/trace_event/trace_event.h" |
22 #include "build/build_config.h" | 23 #include "build/build_config.h" |
23 #include "third_party/skia/include/core/SkRect.h" | 24 #include "third_party/skia/include/core/SkRect.h" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
97 //////////////////////////////////////////////////////////////////////////////// | 98 //////////////////////////////////////////////////////////////////////////////// |
98 // View, public: | 99 // View, public: |
99 | 100 |
100 // Creation and lifetime ------------------------------------------------------- | 101 // Creation and lifetime ------------------------------------------------------- |
101 | 102 |
102 View::View() | 103 View::View() |
103 : owned_by_client_(false), | 104 : owned_by_client_(false), |
104 id_(0), | 105 id_(0), |
105 group_(-1), | 106 group_(-1), |
106 parent_(NULL), | 107 parent_(NULL), |
108 #if DCHECK_IS_ON() | |
109 iterating_(false), | |
110 #endif | |
107 visible_(true), | 111 visible_(true), |
108 enabled_(true), | 112 enabled_(true), |
109 notify_enter_exit_on_child_(false), | 113 notify_enter_exit_on_child_(false), |
110 registered_for_visible_bounds_notification_(false), | 114 registered_for_visible_bounds_notification_(false), |
111 needs_layout_(true), | 115 needs_layout_(true), |
112 snap_layer_to_pixel_boundary_(false), | 116 snap_layer_to_pixel_boundary_(false), |
113 flip_canvas_on_paint_for_rtl_ui_(false), | 117 flip_canvas_on_paint_for_rtl_ui_(false), |
114 paint_to_layer_(false), | 118 paint_to_layer_(false), |
115 accelerator_focus_manager_(NULL), | 119 accelerator_focus_manager_(NULL), |
116 registered_accelerator_count_(0), | 120 registered_accelerator_count_(0), |
117 next_focusable_view_(NULL), | 121 next_focusable_view_(NULL), |
118 previous_focusable_view_(NULL), | 122 previous_focusable_view_(NULL), |
119 focus_behavior_(FocusBehavior::NEVER), | 123 focus_behavior_(FocusBehavior::NEVER), |
120 context_menu_controller_(NULL), | 124 context_menu_controller_(NULL), |
121 drag_controller_(NULL), | 125 drag_controller_(NULL), |
122 native_view_accessibility_(NULL) { | 126 native_view_accessibility_(NULL) { |
123 SetTargetHandler(this); | 127 SetTargetHandler(this); |
124 } | 128 } |
125 | 129 |
126 View::~View() { | 130 View::~View() { |
127 if (parent_) | 131 if (parent_) |
128 parent_->RemoveChildView(this); | 132 parent_->RemoveChildView(this); |
129 | 133 |
130 ViewStorage::GetInstance()->ViewRemoved(this); | 134 ViewStorage::GetInstance()->ViewRemoved(this); |
131 | 135 |
132 for (Views::const_iterator i(children_.begin()); i != children_.end(); ++i) { | 136 { |
133 (*i)->parent_ = NULL; | 137 #if DCHECK_IS_ON() |
134 if (!(*i)->owned_by_client_) | 138 base::AutoReset<bool> iterating(&iterating_, true); |
135 delete *i; | 139 #endif |
140 for (auto* child : children_) { | |
141 child->parent_ = NULL; | |
142 if (!child->owned_by_client_) | |
143 delete child; | |
144 } | |
136 } | 145 } |
137 | 146 |
138 // Release ownership of the native accessibility object, but it's | 147 // Release ownership of the native accessibility object, but it's |
139 // reference-counted on some platforms, so it may not be deleted right away. | 148 // reference-counted on some platforms, so it may not be deleted right away. |
140 if (native_view_accessibility_) | 149 if (native_view_accessibility_) |
141 native_view_accessibility_->Destroy(); | 150 native_view_accessibility_->Destroy(); |
142 } | 151 } |
143 | 152 |
144 // Tree operations ------------------------------------------------------------- | 153 // Tree operations ------------------------------------------------------------- |
145 | 154 |
(...skipping 26 matching lines...) Expand all Loading... | |
172 ReorderChildView(view, index); | 181 ReorderChildView(view, index); |
173 return; | 182 return; |
174 } | 183 } |
175 parent->DoRemoveChildView(view, true, true, false, this); | 184 parent->DoRemoveChildView(view, true, true, false, this); |
176 } | 185 } |
177 | 186 |
178 // Sets the prev/next focus views. | 187 // Sets the prev/next focus views. |
179 InitFocusSiblings(view, index); | 188 InitFocusSiblings(view, index); |
180 | 189 |
181 view->parent_ = this; | 190 view->parent_ = this; |
191 #if DCHECK_IS_ON() | |
192 DCHECK(!iterating_); | |
sadrul
2016/12/22 21:37:52
Since this is in a DCHECK, do you need the DCHECK_
varkha
2016/12/22 22:36:02
Yes, I get compiler "error: use of undeclared iden
| |
193 #endif | |
182 children_.insert(children_.begin() + index, view); | 194 children_.insert(children_.begin() + index, view); |
183 | 195 |
184 // Ensure the layer tree matches the view tree before calling to any client | 196 // Ensure the layer tree matches the view tree before calling to any client |
185 // code. This way if client code further modifies the view tree we are in a | 197 // code. This way if client code further modifies the view tree we are in a |
186 // sane state. | 198 // sane state. |
187 const bool did_reparent_any_layers = view->UpdateParentLayers(); | 199 const bool did_reparent_any_layers = view->UpdateParentLayers(); |
188 Widget* widget = GetWidget(); | 200 Widget* widget = GetWidget(); |
189 if (did_reparent_any_layers && widget) | 201 if (did_reparent_any_layers && widget) |
190 widget->UpdateRootLayers(); | 202 widget->UpdateRootLayers(); |
191 | 203 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
230 DCHECK_EQ(view->parent_, this); | 242 DCHECK_EQ(view->parent_, this); |
231 if (index < 0) | 243 if (index < 0) |
232 index = child_count() - 1; | 244 index = child_count() - 1; |
233 else if (index >= child_count()) | 245 else if (index >= child_count()) |
234 return; | 246 return; |
235 if (children_[index] == view) | 247 if (children_[index] == view) |
236 return; | 248 return; |
237 | 249 |
238 const Views::iterator i(std::find(children_.begin(), children_.end(), view)); | 250 const Views::iterator i(std::find(children_.begin(), children_.end(), view)); |
239 DCHECK(i != children_.end()); | 251 DCHECK(i != children_.end()); |
252 #if DCHECK_IS_ON() | |
253 DCHECK(!iterating_); | |
254 #endif | |
240 children_.erase(i); | 255 children_.erase(i); |
241 | 256 |
242 // Unlink the view first | 257 // Unlink the view first |
243 View* next_focusable = view->next_focusable_view_; | 258 View* next_focusable = view->next_focusable_view_; |
244 View* prev_focusable = view->previous_focusable_view_; | 259 View* prev_focusable = view->previous_focusable_view_; |
245 if (prev_focusable) | 260 if (prev_focusable) |
246 prev_focusable->next_focusable_view_ = next_focusable; | 261 prev_focusable->next_focusable_view_ = next_focusable; |
247 if (next_focusable) | 262 if (next_focusable) |
248 next_focusable->previous_focusable_view_ = prev_focusable; | 263 next_focusable->previous_focusable_view_ = prev_focusable; |
249 | 264 |
250 // Add it in the specified index now. | 265 // Add it in the specified index now. |
251 InitFocusSiblings(view, index); | 266 InitFocusSiblings(view, index); |
267 #if DCHECK_IS_ON() | |
268 DCHECK(!iterating_); | |
269 #endif | |
252 children_.insert(children_.begin() + index, view); | 270 children_.insert(children_.begin() + index, view); |
sadrul
2016/12/22 21:37:52
Don't need both this and in 252:254?
varkha
2016/12/22 22:36:01
Done.
| |
253 | 271 |
254 for (ViewObserver& observer : observers_) | 272 for (ViewObserver& observer : observers_) |
255 observer.OnChildViewReordered(view); | 273 observer.OnChildViewReordered(view); |
256 | 274 |
257 ReorderLayers(); | 275 ReorderLayers(); |
258 } | 276 } |
259 | 277 |
260 void View::RemoveChildView(View* view) { | 278 void View::RemoveChildView(View* view) { |
261 DoRemoveChildView(view, true, true, false, NULL); | 279 DoRemoveChildView(view, true, true, false, NULL); |
262 } | 280 } |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
547 // If we have a layout manager, let it handle the layout for us. | 565 // If we have a layout manager, let it handle the layout for us. |
548 if (layout_manager_.get()) | 566 if (layout_manager_.get()) |
549 layout_manager_->Layout(this); | 567 layout_manager_->Layout(this); |
550 | 568 |
551 // Make sure to propagate the Layout() call to any children that haven't | 569 // Make sure to propagate the Layout() call to any children that haven't |
552 // received it yet through the layout manager and need to be laid out. This | 570 // received it yet through the layout manager and need to be laid out. This |
553 // is needed for the case when the child requires a layout but its bounds | 571 // is needed for the case when the child requires a layout but its bounds |
554 // weren't changed by the layout manager. If there is no layout manager, we | 572 // weren't changed by the layout manager. If there is no layout manager, we |
555 // just propagate the Layout() call down the hierarchy, so whoever receives | 573 // just propagate the Layout() call down the hierarchy, so whoever receives |
556 // the call can take appropriate action. | 574 // the call can take appropriate action. |
557 for (int i = 0, count = child_count(); i < count; ++i) { | 575 #if DCHECK_IS_ON() |
558 View* child = child_at(i); | 576 base::AutoReset<bool> iterating(&iterating_, true); |
577 #endif | |
578 for (auto* child : children_) { | |
559 if (child->needs_layout_ || !layout_manager_.get()) { | 579 if (child->needs_layout_ || !layout_manager_.get()) { |
560 TRACE_EVENT1("views", "View::Layout", "class", child->GetClassName()); | 580 TRACE_EVENT1("views", "View::Layout", "class", child->GetClassName()); |
561 child->needs_layout_ = false; | 581 child->needs_layout_ = false; |
562 child->Layout(); | 582 child->Layout(); |
563 } | 583 } |
564 } | 584 } |
565 } | 585 } |
566 | 586 |
567 void View::InvalidateLayout() { | 587 void View::InvalidateLayout() { |
568 // Always invalidate up. This is needed to handle the case of us already being | 588 // Always invalidate up. This is needed to handle the case of us already being |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
614 | 634 |
615 View* View::GetAncestorWithClassName(const std::string& name) { | 635 View* View::GetAncestorWithClassName(const std::string& name) { |
616 return const_cast<View*>(const_cast<const View*>(this)-> | 636 return const_cast<View*>(const_cast<const View*>(this)-> |
617 GetAncestorWithClassName(name)); | 637 GetAncestorWithClassName(name)); |
618 } | 638 } |
619 | 639 |
620 const View* View::GetViewByID(int id) const { | 640 const View* View::GetViewByID(int id) const { |
621 if (id == id_) | 641 if (id == id_) |
622 return const_cast<View*>(this); | 642 return const_cast<View*>(this); |
623 | 643 |
624 for (int i = 0, count = child_count(); i < count; ++i) { | 644 #if DCHECK_IS_ON() |
625 const View* view = child_at(i)->GetViewByID(id); | 645 base::AutoReset<bool> iterating(&iterating_, true); |
646 #endif | |
647 for (auto* child : children_) { | |
648 const View* view = child->GetViewByID(id); | |
626 if (view) | 649 if (view) |
627 return view; | 650 return view; |
628 } | 651 } |
629 return NULL; | 652 return NULL; |
630 } | 653 } |
631 | 654 |
632 View* View::GetViewByID(int id) { | 655 View* View::GetViewByID(int id) { |
633 return const_cast<View*>(const_cast<const View*>(this)->GetViewByID(id)); | 656 return const_cast<View*>(const_cast<const View*>(this)->GetViewByID(id)); |
634 } | 657 } |
635 | 658 |
636 void View::SetGroup(int gid) { | 659 void View::SetGroup(int gid) { |
637 // Don't change the group id once it's set. | 660 // Don't change the group id once it's set. |
638 DCHECK(group_ == -1 || group_ == gid); | 661 DCHECK(group_ == -1 || group_ == gid); |
639 group_ = gid; | 662 group_ = gid; |
640 } | 663 } |
641 | 664 |
642 int View::GetGroup() const { | 665 int View::GetGroup() const { |
643 return group_; | 666 return group_; |
644 } | 667 } |
645 | 668 |
646 bool View::IsGroupFocusTraversable() const { | 669 bool View::IsGroupFocusTraversable() const { |
647 return true; | 670 return true; |
648 } | 671 } |
649 | 672 |
650 void View::GetViewsInGroup(int group, Views* views) { | 673 void View::GetViewsInGroup(int group, Views* views) { |
651 if (group_ == group) | 674 if (group_ == group) |
652 views->push_back(this); | 675 views->push_back(this); |
653 | 676 |
654 for (int i = 0, count = child_count(); i < count; ++i) | 677 #if DCHECK_IS_ON() |
655 child_at(i)->GetViewsInGroup(group, views); | 678 base::AutoReset<bool> iterating(&iterating_, true); |
679 #endif | |
680 for (auto* child : children_) | |
681 child->GetViewsInGroup(group, views); | |
656 } | 682 } |
657 | 683 |
658 View* View::GetSelectedViewForGroup(int group) { | 684 View* View::GetSelectedViewForGroup(int group) { |
659 Views views; | 685 Views views; |
660 GetWidget()->GetRootView()->GetViewsInGroup(group, &views); | 686 GetWidget()->GetRootView()->GetViewsInGroup(group, &views); |
661 return views.empty() ? NULL : views[0]; | 687 return views.empty() ? NULL : views[0]; |
662 } | 688 } |
663 | 689 |
664 // Coordinate conversion ------------------------------------------------------- | 690 // Coordinate conversion ------------------------------------------------------- |
665 | 691 |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
910 return true; | 936 return true; |
911 } | 937 } |
912 | 938 |
913 View* View::GetTooltipHandlerForPoint(const gfx::Point& point) { | 939 View* View::GetTooltipHandlerForPoint(const gfx::Point& point) { |
914 // TODO(tdanderson): Move this implementation into ViewTargetDelegate. | 940 // TODO(tdanderson): Move this implementation into ViewTargetDelegate. |
915 if (!HitTestPoint(point) || !CanProcessEventsWithinSubtree()) | 941 if (!HitTestPoint(point) || !CanProcessEventsWithinSubtree()) |
916 return NULL; | 942 return NULL; |
917 | 943 |
918 // Walk the child Views recursively looking for the View that most | 944 // Walk the child Views recursively looking for the View that most |
919 // tightly encloses the specified point. | 945 // tightly encloses the specified point. |
920 for (int i = child_count() - 1; i >= 0; --i) { | 946 for (auto* child : base::Reversed(children_)) { |
921 View* child = child_at(i); | |
922 if (!child->visible()) | 947 if (!child->visible()) |
923 continue; | 948 continue; |
924 | 949 |
925 gfx::Point point_in_child_coords(point); | 950 gfx::Point point_in_child_coords(point); |
926 ConvertPointToTarget(this, child, &point_in_child_coords); | 951 ConvertPointToTarget(this, child, &point_in_child_coords); |
927 View* handler = child->GetTooltipHandlerForPoint(point_in_child_coords); | 952 View* handler = child->GetTooltipHandlerForPoint(point_in_child_coords); |
928 if (handler) | 953 if (handler) |
929 return handler; | 954 return handler; |
930 } | 955 } |
931 return this; | 956 return this; |
(...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1430 | 1455 |
1431 if (focus_manager) | 1456 if (focus_manager) |
1432 RegisterPendingAccelerators(); | 1457 RegisterPendingAccelerators(); |
1433 } | 1458 } |
1434 } | 1459 } |
1435 | 1460 |
1436 // Painting -------------------------------------------------------------------- | 1461 // Painting -------------------------------------------------------------------- |
1437 | 1462 |
1438 void View::PaintChildren(const ui::PaintContext& context) { | 1463 void View::PaintChildren(const ui::PaintContext& context) { |
1439 TRACE_EVENT1("views", "View::PaintChildren", "class", GetClassName()); | 1464 TRACE_EVENT1("views", "View::PaintChildren", "class", GetClassName()); |
1440 for (int i = 0, count = child_count(); i < count; ++i) | 1465 #if DCHECK_IS_ON() |
1441 if (!child_at(i)->layer()) | 1466 base::AutoReset<bool> iterating(&iterating_, true); |
1442 child_at(i)->Paint(context); | 1467 #endif |
1468 for (auto* child : children_) { | |
1469 if (!child->layer()) | |
1470 child->Paint(context); | |
1471 } | |
1443 } | 1472 } |
1444 | 1473 |
1445 void View::OnPaint(gfx::Canvas* canvas) { | 1474 void View::OnPaint(gfx::Canvas* canvas) { |
1446 TRACE_EVENT1("views", "View::OnPaint", "class", GetClassName()); | 1475 TRACE_EVENT1("views", "View::OnPaint", "class", GetClassName()); |
1447 OnPaintBackground(canvas); | 1476 OnPaintBackground(canvas); |
1448 OnPaintBorder(canvas); | 1477 OnPaintBorder(canvas); |
1449 } | 1478 } |
1450 | 1479 |
1451 void View::OnPaintBackground(gfx::Canvas* canvas) { | 1480 void View::OnPaintBackground(gfx::Canvas* canvas) { |
1452 if (background_.get()) { | 1481 if (background_.get()) { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1498 void View::MoveLayerToParent(ui::Layer* parent_layer, | 1527 void View::MoveLayerToParent(ui::Layer* parent_layer, |
1499 const gfx::Point& point) { | 1528 const gfx::Point& point) { |
1500 gfx::Point local_point(point); | 1529 gfx::Point local_point(point); |
1501 if (parent_layer != layer()) | 1530 if (parent_layer != layer()) |
1502 local_point.Offset(GetMirroredX(), y()); | 1531 local_point.Offset(GetMirroredX(), y()); |
1503 if (layer() && parent_layer != layer()) { | 1532 if (layer() && parent_layer != layer()) { |
1504 parent_layer->Add(layer()); | 1533 parent_layer->Add(layer()); |
1505 SetLayerBounds(gfx::Rect(local_point.x(), local_point.y(), | 1534 SetLayerBounds(gfx::Rect(local_point.x(), local_point.y(), |
1506 width(), height())); | 1535 width(), height())); |
1507 } else { | 1536 } else { |
1508 for (int i = 0, count = child_count(); i < count; ++i) | 1537 #if DCHECK_IS_ON() |
1509 child_at(i)->MoveLayerToParent(parent_layer, local_point); | 1538 base::AutoReset<bool> iterating(&iterating_, true); |
1539 #endif | |
1540 for (auto* child : children_) | |
1541 child->MoveLayerToParent(parent_layer, local_point); | |
1510 } | 1542 } |
1511 } | 1543 } |
1512 | 1544 |
1513 void View::UpdateLayerVisibility() { | 1545 void View::UpdateLayerVisibility() { |
1514 bool visible = visible_; | 1546 bool visible = visible_; |
1515 for (const View* v = parent_; visible && v && !v->layer(); v = v->parent_) | 1547 for (const View* v = parent_; visible && v && !v->layer(); v = v->parent_) |
1516 visible = v->visible(); | 1548 visible = v->visible(); |
1517 | 1549 |
1518 UpdateChildLayerVisibility(visible); | 1550 UpdateChildLayerVisibility(visible); |
1519 } | 1551 } |
1520 | 1552 |
1521 void View::UpdateChildLayerVisibility(bool ancestor_visible) { | 1553 void View::UpdateChildLayerVisibility(bool ancestor_visible) { |
1522 if (layer()) { | 1554 if (layer()) { |
1523 layer()->SetVisible(ancestor_visible && visible_); | 1555 layer()->SetVisible(ancestor_visible && visible_); |
1524 } else { | 1556 } else { |
1525 for (int i = 0, count = child_count(); i < count; ++i) | 1557 #if DCHECK_IS_ON() |
1526 child_at(i)->UpdateChildLayerVisibility(ancestor_visible && visible_); | 1558 base::AutoReset<bool> iterating(&iterating_, true); |
1559 #endif | |
1560 for (auto* child : children_) | |
1561 child->UpdateChildLayerVisibility(ancestor_visible && visible_); | |
1527 } | 1562 } |
1528 } | 1563 } |
1529 | 1564 |
1530 void View::UpdateChildLayerBounds(const gfx::Vector2d& offset) { | 1565 void View::UpdateChildLayerBounds(const gfx::Vector2d& offset) { |
1531 if (layer()) { | 1566 if (layer()) { |
1532 SetLayerBounds(GetLocalBounds() + offset); | 1567 SetLayerBounds(GetLocalBounds() + offset); |
1533 } else { | 1568 } else { |
1534 for (int i = 0, count = child_count(); i < count; ++i) { | 1569 #if DCHECK_IS_ON() |
1535 View* child = child_at(i); | 1570 base::AutoReset<bool> iterating(&iterating_, true); |
1571 #endif | |
1572 for (auto* child : children_) { | |
1536 child->UpdateChildLayerBounds( | 1573 child->UpdateChildLayerBounds( |
1537 offset + gfx::Vector2d(child->GetMirroredX(), child->y())); | 1574 offset + gfx::Vector2d(child->GetMirroredX(), child->y())); |
1538 } | 1575 } |
1539 } | 1576 } |
1540 } | 1577 } |
1541 | 1578 |
1542 void View::OnPaintLayer(const ui::PaintContext& context) { | 1579 void View::OnPaintLayer(const ui::PaintContext& context) { |
1543 Paint(context); | 1580 Paint(context); |
1544 } | 1581 } |
1545 | 1582 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1581 } | 1618 } |
1582 | 1619 |
1583 void View::ReorderChildLayers(ui::Layer* parent_layer) { | 1620 void View::ReorderChildLayers(ui::Layer* parent_layer) { |
1584 if (layer() && layer() != parent_layer) { | 1621 if (layer() && layer() != parent_layer) { |
1585 DCHECK_EQ(parent_layer, layer()->parent()); | 1622 DCHECK_EQ(parent_layer, layer()->parent()); |
1586 parent_layer->StackAtBottom(layer()); | 1623 parent_layer->StackAtBottom(layer()); |
1587 } else { | 1624 } else { |
1588 // Iterate backwards through the children so that a child with a layer | 1625 // Iterate backwards through the children so that a child with a layer |
1589 // which is further to the back is stacked above one which is further to | 1626 // which is further to the back is stacked above one which is further to |
1590 // the front. | 1627 // the front. |
1591 for (Views::reverse_iterator it(children_.rbegin()); | 1628 #if DCHECK_IS_ON() |
1592 it != children_.rend(); ++it) { | 1629 base::AutoReset<bool> iterating(&iterating_, true); |
1593 (*it)->ReorderChildLayers(parent_layer); | 1630 #endif |
1631 for (auto* child : base::Reversed(children_)) { | |
1632 child->ReorderChildLayers(parent_layer); | |
1594 } | 1633 } |
1595 } | 1634 } |
1596 } | 1635 } |
1597 | 1636 |
1598 // Input ----------------------------------------------------------------------- | 1637 // Input ----------------------------------------------------------------------- |
1599 | 1638 |
1600 View::DragInfo* View::GetDragInfo() { | 1639 View::DragInfo* View::GetDragInfo() { |
1601 return parent_ ? parent_->GetDragInfo() : NULL; | 1640 return parent_ ? parent_->GetDragInfo() : NULL; |
1602 } | 1641 } |
1603 | 1642 |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1758 | 1797 |
1759 base::snprintf(pp, kMaxPointerStringLength, "%p", parent_); | 1798 base::snprintf(pp, kMaxPointerStringLength, "%p", parent_); |
1760 result.append(" N"); | 1799 result.append(" N"); |
1761 result.append(pp + 2); | 1800 result.append(pp + 2); |
1762 result.append(" -> N"); | 1801 result.append(" -> N"); |
1763 result.append(p + 2); | 1802 result.append(p + 2); |
1764 result.append("\n"); | 1803 result.append("\n"); |
1765 } | 1804 } |
1766 | 1805 |
1767 // Children. | 1806 // Children. |
1768 for (int i = 0, count = view_with_children->child_count(); i < count; ++i) | 1807 for (auto* child : view_with_children->children_) |
1769 result.append(view_with_children->child_at(i)->PrintViewGraph(false)); | 1808 result.append(child->PrintViewGraph(false)); |
1770 | 1809 |
1771 if (first) | 1810 if (first) |
1772 result.append("}\n"); | 1811 result.append("}\n"); |
1773 | 1812 |
1774 return result; | 1813 return result; |
1775 } | 1814 } |
1776 #endif | 1815 #endif |
1777 | 1816 |
1778 //////////////////////////////////////////////////////////////////////////////// | 1817 //////////////////////////////////////////////////////////////////////////////// |
1779 // View, private: | 1818 // View, private: |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1855 view->OrphanLayers(); | 1894 view->OrphanLayers(); |
1856 if (widget) | 1895 if (widget) |
1857 widget->UpdateRootLayers(); | 1896 widget->UpdateRootLayers(); |
1858 | 1897 |
1859 view->PropagateRemoveNotifications(this, new_parent); | 1898 view->PropagateRemoveNotifications(this, new_parent); |
1860 view->parent_ = nullptr; | 1899 view->parent_ = nullptr; |
1861 | 1900 |
1862 if (delete_removed_view && !view->owned_by_client_) | 1901 if (delete_removed_view && !view->owned_by_client_) |
1863 view_to_be_deleted.reset(view); | 1902 view_to_be_deleted.reset(view); |
1864 | 1903 |
1904 #if DCHECK_IS_ON() | |
1905 DCHECK(!iterating_); | |
1906 #endif | |
1865 children_.erase(i); | 1907 children_.erase(i); |
1866 | 1908 |
1867 if (update_tool_tip) | 1909 if (update_tool_tip) |
1868 UpdateTooltip(); | 1910 UpdateTooltip(); |
1869 | 1911 |
1870 if (layout_manager_) | 1912 if (layout_manager_) |
1871 layout_manager_->ViewRemoved(this, view); | 1913 layout_manager_->ViewRemoved(this, view); |
1872 | 1914 |
1873 for (ViewObserver& observer : observers_) | 1915 for (ViewObserver& observer : observers_) |
1874 observer.OnChildViewRemoved(view, this); | 1916 observer.OnChildViewRemoved(view, this); |
1875 } | 1917 } |
1876 | 1918 |
1877 void View::PropagateRemoveNotifications(View* old_parent, View* new_parent) { | 1919 void View::PropagateRemoveNotifications(View* old_parent, View* new_parent) { |
1878 for (int i = 0, count = child_count(); i < count; ++i) | 1920 { |
1879 child_at(i)->PropagateRemoveNotifications(old_parent, new_parent); | 1921 #if DCHECK_IS_ON() |
1922 base::AutoReset<bool> iterating(&iterating_, true); | |
1923 #endif | |
1924 for (auto* child : children_) | |
1925 child->PropagateRemoveNotifications(old_parent, new_parent); | |
1926 } | |
1880 | 1927 |
1881 ViewHierarchyChangedDetails details(false, old_parent, this, new_parent); | 1928 ViewHierarchyChangedDetails details(false, old_parent, this, new_parent); |
1882 for (View* v = this; v; v = v->parent_) | 1929 for (View* v = this; v; v = v->parent_) |
1883 v->ViewHierarchyChangedImpl(true, details); | 1930 v->ViewHierarchyChangedImpl(true, details); |
1884 } | 1931 } |
1885 | 1932 |
1886 void View::PropagateAddNotifications( | 1933 void View::PropagateAddNotifications( |
1887 const ViewHierarchyChangedDetails& details) { | 1934 const ViewHierarchyChangedDetails& details) { |
1888 for (int i = 0, count = child_count(); i < count; ++i) | 1935 { |
1889 child_at(i)->PropagateAddNotifications(details); | 1936 #if DCHECK_IS_ON() |
1937 base::AutoReset<bool> iterating(&iterating_, true); | |
1938 #endif | |
1939 for (auto* child : children_) | |
1940 child->PropagateAddNotifications(details); | |
1941 } | |
1890 ViewHierarchyChangedImpl(true, details); | 1942 ViewHierarchyChangedImpl(true, details); |
1891 } | 1943 } |
1892 | 1944 |
1893 void View::PropagateNativeViewHierarchyChanged() { | 1945 void View::PropagateNativeViewHierarchyChanged() { |
1894 for (int i = 0, count = child_count(); i < count; ++i) | 1946 { |
1895 child_at(i)->PropagateNativeViewHierarchyChanged(); | 1947 #if DCHECK_IS_ON() |
1948 base::AutoReset<bool> iterating(&iterating_, true); | |
1949 #endif | |
1950 for (auto* child : children_) | |
1951 child->PropagateNativeViewHierarchyChanged(); | |
1952 } | |
1896 NativeViewHierarchyChanged(); | 1953 NativeViewHierarchyChanged(); |
1897 } | 1954 } |
1898 | 1955 |
1899 void View::ViewHierarchyChangedImpl( | 1956 void View::ViewHierarchyChangedImpl( |
1900 bool register_accelerators, | 1957 bool register_accelerators, |
1901 const ViewHierarchyChangedDetails& details) { | 1958 const ViewHierarchyChangedDetails& details) { |
1902 if (register_accelerators) { | 1959 if (register_accelerators) { |
1903 if (details.is_add) { | 1960 if (details.is_add) { |
1904 // If you get this registration, you are part of a subtree that has been | 1961 // If you get this registration, you are part of a subtree that has been |
1905 // added to the view hierarchy. | 1962 // added to the view hierarchy. |
1906 if (GetFocusManager()) | 1963 if (GetFocusManager()) |
1907 RegisterPendingAccelerators(); | 1964 RegisterPendingAccelerators(); |
1908 } else { | 1965 } else { |
1909 if (details.child == this) | 1966 if (details.child == this) |
1910 UnregisterAccelerators(true); | 1967 UnregisterAccelerators(true); |
1911 } | 1968 } |
1912 } | 1969 } |
1913 | 1970 |
1914 ViewHierarchyChanged(details); | 1971 ViewHierarchyChanged(details); |
1915 details.parent->needs_layout_ = true; | 1972 details.parent->needs_layout_ = true; |
1916 } | 1973 } |
1917 | 1974 |
1918 void View::PropagateNativeThemeChanged(const ui::NativeTheme* theme) { | 1975 void View::PropagateNativeThemeChanged(const ui::NativeTheme* theme) { |
1919 for (int i = 0, count = child_count(); i < count; ++i) | 1976 { |
1920 child_at(i)->PropagateNativeThemeChanged(theme); | 1977 #if DCHECK_IS_ON() |
1978 base::AutoReset<bool> iterating(&iterating_, true); | |
1979 #endif | |
1980 for (auto* child : children_) | |
1981 child->PropagateNativeThemeChanged(theme); | |
1982 } | |
1921 OnNativeThemeChanged(theme); | 1983 OnNativeThemeChanged(theme); |
1922 } | 1984 } |
1923 | 1985 |
1924 // Size and disposition -------------------------------------------------------- | 1986 // Size and disposition -------------------------------------------------------- |
1925 | 1987 |
1926 void View::PropagateVisibilityNotifications(View* start, bool is_visible) { | 1988 void View::PropagateVisibilityNotifications(View* start, bool is_visible) { |
1927 for (int i = 0, count = child_count(); i < count; ++i) | 1989 { |
1928 child_at(i)->PropagateVisibilityNotifications(start, is_visible); | 1990 #if DCHECK_IS_ON() |
1991 base::AutoReset<bool> iterating(&iterating_, true); | |
1992 #endif | |
1993 for (auto* child : children_) | |
1994 child->PropagateVisibilityNotifications(start, is_visible); | |
1995 } | |
1929 VisibilityChangedImpl(start, is_visible); | 1996 VisibilityChangedImpl(start, is_visible); |
1930 } | 1997 } |
1931 | 1998 |
1932 void View::VisibilityChangedImpl(View* starting_from, bool is_visible) { | 1999 void View::VisibilityChangedImpl(View* starting_from, bool is_visible) { |
1933 VisibilityChanged(starting_from, is_visible); | 2000 VisibilityChanged(starting_from, is_visible); |
1934 } | 2001 } |
1935 | 2002 |
1936 void View::BoundsChanged(const gfx::Rect& previous_bounds) { | 2003 void View::BoundsChanged(const gfx::Rect& previous_bounds) { |
1937 if (visible_) { | 2004 if (visible_) { |
1938 // Paint the new bounds. | 2005 // Paint the new bounds. |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2099 bool result = GetTransformRelativeTo(ancestor, &trans); | 2166 bool result = GetTransformRelativeTo(ancestor, &trans); |
2100 trans.TransformRectReverse(rect); | 2167 trans.TransformRectReverse(rect); |
2101 return result; | 2168 return result; |
2102 } | 2169 } |
2103 | 2170 |
2104 // Accelerated painting -------------------------------------------------------- | 2171 // Accelerated painting -------------------------------------------------------- |
2105 | 2172 |
2106 void View::CreateLayer() { | 2173 void View::CreateLayer() { |
2107 // A new layer is being created for the view. So all the layers of the | 2174 // A new layer is being created for the view. So all the layers of the |
2108 // sub-tree can inherit the visibility of the corresponding view. | 2175 // sub-tree can inherit the visibility of the corresponding view. |
2109 for (int i = 0, count = child_count(); i < count; ++i) | 2176 { |
2110 child_at(i)->UpdateChildLayerVisibility(true); | 2177 #if DCHECK_IS_ON() |
2178 base::AutoReset<bool> iterating(&iterating_, true); | |
2179 #endif | |
2180 for (auto* child : children_) | |
2181 child->UpdateChildLayerVisibility(true); | |
2182 } | |
2111 | 2183 |
2112 SetLayer(base::MakeUnique<ui::Layer>()); | 2184 SetLayer(base::MakeUnique<ui::Layer>()); |
2113 layer()->set_delegate(this); | 2185 layer()->set_delegate(this); |
2114 layer()->set_name(GetClassName()); | 2186 layer()->set_name(GetClassName()); |
2115 | 2187 |
2116 UpdateParentLayers(); | 2188 UpdateParentLayers(); |
2117 UpdateLayerVisibility(); | 2189 UpdateLayerVisibility(); |
2118 | 2190 |
2119 // The new layer needs to be ordered in the layer tree according | 2191 // The new layer needs to be ordered in the layer tree according |
2120 // to the view tree. Children of this layer were added in order | 2192 // to the view tree. Children of this layer were added in order |
(...skipping 17 matching lines...) Expand all Loading... | |
2138 if (layer()) { | 2210 if (layer()) { |
2139 if (!layer()->parent()) { | 2211 if (!layer()->parent()) { |
2140 UpdateParentLayer(); | 2212 UpdateParentLayer(); |
2141 return true; | 2213 return true; |
2142 } | 2214 } |
2143 // The layers of any child views are already in place, so we can stop | 2215 // The layers of any child views are already in place, so we can stop |
2144 // iterating here. | 2216 // iterating here. |
2145 return false; | 2217 return false; |
2146 } | 2218 } |
2147 bool result = false; | 2219 bool result = false; |
2148 for (int i = 0, count = child_count(); i < count; ++i) { | 2220 #if DCHECK_IS_ON() |
2149 if (child_at(i)->UpdateParentLayers()) | 2221 base::AutoReset<bool> iterating(&iterating_, true); |
2222 #endif | |
2223 for (auto* child : children_) { | |
2224 if (child->UpdateParentLayers()) | |
2150 result = true; | 2225 result = true; |
2151 } | 2226 } |
2152 return result; | 2227 return result; |
2153 } | 2228 } |
2154 | 2229 |
2155 void View::OrphanLayers() { | 2230 void View::OrphanLayers() { |
2156 if (layer()) { | 2231 if (layer()) { |
2157 if (layer()->parent()) | 2232 if (layer()->parent()) |
2158 layer()->parent()->Remove(layer()); | 2233 layer()->parent()->Remove(layer()); |
2159 | 2234 |
2160 // The layer belonging to this View has already been orphaned. It is not | 2235 // The layer belonging to this View has already been orphaned. It is not |
2161 // necessary to orphan the child layers. | 2236 // necessary to orphan the child layers. |
2162 return; | 2237 return; |
2163 } | 2238 } |
2164 for (int i = 0, count = child_count(); i < count; ++i) | 2239 #if DCHECK_IS_ON() |
2165 child_at(i)->OrphanLayers(); | 2240 base::AutoReset<bool> iterating(&iterating_, true); |
2241 #endif | |
2242 for (auto* child : children_) | |
2243 child->OrphanLayers(); | |
2166 } | 2244 } |
2167 | 2245 |
2168 void View::ReparentLayer(const gfx::Vector2d& offset, ui::Layer* parent_layer) { | 2246 void View::ReparentLayer(const gfx::Vector2d& offset, ui::Layer* parent_layer) { |
2169 layer()->SetBounds(GetLocalBounds() + offset); | 2247 layer()->SetBounds(GetLocalBounds() + offset); |
2170 DCHECK_NE(layer(), parent_layer); | 2248 DCHECK_NE(layer(), parent_layer); |
2171 if (parent_layer) | 2249 if (parent_layer) |
2172 parent_layer->Add(layer()); | 2250 parent_layer->Add(layer()); |
2173 layer()->SchedulePaint(GetLocalBounds()); | 2251 layer()->SchedulePaint(GetLocalBounds()); |
2174 MoveLayerToParent(layer(), gfx::Point()); | 2252 MoveLayerToParent(layer(), gfx::Point()); |
2175 } | 2253 } |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2338 | 2416 |
2339 if (count == 0) { | 2417 if (count == 0) { |
2340 v->next_focusable_view_ = NULL; | 2418 v->next_focusable_view_ = NULL; |
2341 v->previous_focusable_view_ = NULL; | 2419 v->previous_focusable_view_ = NULL; |
2342 } else { | 2420 } else { |
2343 if (index == count) { | 2421 if (index == count) { |
2344 // We are inserting at the end, but the end of the child list may not be | 2422 // We are inserting at the end, but the end of the child list may not be |
2345 // the last focusable element. Let's try to find an element with no next | 2423 // the last focusable element. Let's try to find an element with no next |
2346 // focusable element to link to. | 2424 // focusable element to link to. |
2347 View* last_focusable_view = NULL; | 2425 View* last_focusable_view = NULL; |
2348 for (Views::iterator i(children_.begin()); i != children_.end(); ++i) { | 2426 { |
2349 if (!(*i)->next_focusable_view_) { | 2427 #if DCHECK_IS_ON() |
2350 last_focusable_view = *i; | 2428 base::AutoReset<bool> iterating(&iterating_, true); |
2429 #endif | |
2430 for (auto* child : children_) { | |
2431 if (!child->next_focusable_view_) { | |
2432 last_focusable_view = child; | |
2351 break; | 2433 break; |
2352 } | 2434 } |
2435 } | |
2353 } | 2436 } |
2354 if (last_focusable_view == NULL) { | 2437 if (last_focusable_view == NULL) { |
2355 // Hum... there is a cycle in the focus list. Let's just insert ourself | 2438 // Hum... there is a cycle in the focus list. Let's just insert ourself |
2356 // after the last child. | 2439 // after the last child. |
2357 View* prev = children_[index - 1]; | 2440 View* prev = children_[index - 1]; |
2358 v->previous_focusable_view_ = prev; | 2441 v->previous_focusable_view_ = prev; |
2359 v->next_focusable_view_ = prev->next_focusable_view_; | 2442 v->next_focusable_view_ = prev->next_focusable_view_; |
2360 prev->next_focusable_view_->previous_focusable_view_ = v; | 2443 prev->next_focusable_view_->previous_focusable_view_ = v; |
2361 prev->next_focusable_view_ = v; | 2444 prev->next_focusable_view_ = v; |
2362 } else { | 2445 } else { |
(...skipping 21 matching lines...) Expand all Loading... | |
2384 return; | 2467 return; |
2385 | 2468 |
2386 FocusManager* focus_manager = GetFocusManager(); | 2469 FocusManager* focus_manager = GetFocusManager(); |
2387 if (focus_manager) | 2470 if (focus_manager) |
2388 focus_manager->AdvanceFocusIfNecessary(); | 2471 focus_manager->AdvanceFocusIfNecessary(); |
2389 } | 2472 } |
2390 | 2473 |
2391 // System events --------------------------------------------------------------- | 2474 // System events --------------------------------------------------------------- |
2392 | 2475 |
2393 void View::PropagateThemeChanged() { | 2476 void View::PropagateThemeChanged() { |
2394 for (int i = child_count() - 1; i >= 0; --i) | 2477 { |
2395 child_at(i)->PropagateThemeChanged(); | 2478 #if DCHECK_IS_ON() |
2479 base::AutoReset<bool> iterating(&iterating_, true); | |
2480 #endif | |
2481 for (auto* child : base::Reversed(children_)) | |
2482 child->PropagateThemeChanged(); | |
2483 } | |
2396 OnThemeChanged(); | 2484 OnThemeChanged(); |
2397 } | 2485 } |
2398 | 2486 |
2399 void View::PropagateLocaleChanged() { | 2487 void View::PropagateLocaleChanged() { |
2400 for (int i = child_count() - 1; i >= 0; --i) | 2488 { |
2401 child_at(i)->PropagateLocaleChanged(); | 2489 #if DCHECK_IS_ON() |
2490 base::AutoReset<bool> iterating(&iterating_, true); | |
2491 #endif | |
2492 for (auto* child : base::Reversed(children_)) | |
2493 child->PropagateLocaleChanged(); | |
2494 } | |
2402 OnLocaleChanged(); | 2495 OnLocaleChanged(); |
2403 } | 2496 } |
2404 | 2497 |
2405 void View::PropagateDeviceScaleFactorChanged(float device_scale_factor) { | 2498 void View::PropagateDeviceScaleFactorChanged(float device_scale_factor) { |
2406 for (int i = child_count() - 1; i >= 0; --i) | 2499 { |
2407 child_at(i)->PropagateDeviceScaleFactorChanged(device_scale_factor); | 2500 #if DCHECK_IS_ON() |
2501 base::AutoReset<bool> iterating(&iterating_, true); | |
2502 #endif | |
2503 for (auto* child : base::Reversed(children_)) | |
2504 child->PropagateDeviceScaleFactorChanged(device_scale_factor); | |
2505 } | |
sadrul
2016/12/22 21:37:52
An alternative to do this is something like this:
varkha
2016/12/22 22:36:01
Done.
| |
2408 | 2506 |
2409 // If the view is drawing to the layer, OnDeviceScaleFactorChanged() is called | 2507 // If the view is drawing to the layer, OnDeviceScaleFactorChanged() is called |
2410 // through LayerDelegate callback. | 2508 // through LayerDelegate callback. |
2411 if (!layer()) | 2509 if (!layer()) |
2412 OnDeviceScaleFactorChanged(device_scale_factor); | 2510 OnDeviceScaleFactorChanged(device_scale_factor); |
2413 } | 2511 } |
2414 | 2512 |
2415 // Tooltips -------------------------------------------------------------------- | 2513 // Tooltips -------------------------------------------------------------------- |
2416 | 2514 |
2417 void View::UpdateTooltip() { | 2515 void View::UpdateTooltip() { |
(...skipping 30 matching lines...) Expand all Loading... | |
2448 // Message the RootView to do the drag and drop. That way if we're removed | 2546 // Message the RootView to do the drag and drop. That way if we're removed |
2449 // the RootView can detect it and avoid calling us back. | 2547 // the RootView can detect it and avoid calling us back. |
2450 gfx::Point widget_location(event.location()); | 2548 gfx::Point widget_location(event.location()); |
2451 ConvertPointToWidget(this, &widget_location); | 2549 ConvertPointToWidget(this, &widget_location); |
2452 widget->RunShellDrag(this, data, widget_location, drag_operations, source); | 2550 widget->RunShellDrag(this, data, widget_location, drag_operations, source); |
2453 // WARNING: we may have been deleted. | 2551 // WARNING: we may have been deleted. |
2454 return true; | 2552 return true; |
2455 } | 2553 } |
2456 | 2554 |
2457 } // namespace views | 2555 } // namespace views |
OLD | NEW |