Chromium Code Reviews| 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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 82 const int kDefaultVerticalDragThreshold = 8; | 83 const int kDefaultVerticalDragThreshold = 8; |
| 83 | 84 |
| 84 // Returns the top view in |view|'s hierarchy. | 85 // Returns the top view in |view|'s hierarchy. |
| 85 const View* GetHierarchyRoot(const View* view) { | 86 const View* GetHierarchyRoot(const View* view) { |
| 86 const View* root = view; | 87 const View* root = view; |
| 87 while (root && root->parent()) | 88 while (root && root->parent()) |
| 88 root = root->parent(); | 89 root = root->parent(); |
| 89 return root; | 90 return root; |
| 90 } | 91 } |
| 91 | 92 |
| 93 #if DCHECK_IS_ON() | |
| 94 class ScopedChildrenLock { | |
| 95 public: | |
| 96 explicit ScopedChildrenLock(const View* view) : view_(view) { | |
| 97 DCHECK(!view->iterating_); | |
| 98 view->iterating_ = true; | |
|
Daniele Castagna
2016/12/23 23:45:14
|iterating_| is private and this code doesn't comp
sadrul
2016/12/24 00:13:11
|iterating_| should remain private. But I think Sc
Daniele Castagna
2016/12/24 00:48:01
I'm not sure why we're not catching that on trybot
| |
| 99 } | |
| 100 | |
| 101 ~ScopedChildrenLock() { view_->iterating_ = false; } | |
| 102 | |
| 103 private: | |
| 104 const View* view_; | |
| 105 DISALLOW_COPY_AND_ASSIGN(ScopedChildrenLock); | |
| 106 }; | |
| 107 #else | |
| 108 class ScopedChildrenLock { | |
| 109 public: | |
| 110 explicit ScopedChildrenLock(const View* view) {} | |
| 111 ~ScopedChildrenLock() {} | |
| 112 }; | |
| 113 #endif | |
| 114 | |
| 92 } // namespace | 115 } // namespace |
| 93 | 116 |
| 94 // static | 117 // static |
| 95 const char View::kViewClassName[] = "View"; | 118 const char View::kViewClassName[] = "View"; |
| 96 | 119 |
| 97 //////////////////////////////////////////////////////////////////////////////// | 120 //////////////////////////////////////////////////////////////////////////////// |
| 98 // View, public: | 121 // View, public: |
| 99 | 122 |
| 100 // Creation and lifetime ------------------------------------------------------- | 123 // Creation and lifetime ------------------------------------------------------- |
| 101 | 124 |
| 102 View::View() | 125 View::View() |
| 103 : owned_by_client_(false), | 126 : owned_by_client_(false), |
| 104 id_(0), | 127 id_(0), |
| 105 group_(-1), | 128 group_(-1), |
| 106 parent_(NULL), | 129 parent_(NULL), |
| 130 #if DCHECK_IS_ON() | |
| 131 iterating_(false), | |
| 132 #endif | |
| 107 visible_(true), | 133 visible_(true), |
| 108 enabled_(true), | 134 enabled_(true), |
| 109 notify_enter_exit_on_child_(false), | 135 notify_enter_exit_on_child_(false), |
| 110 registered_for_visible_bounds_notification_(false), | 136 registered_for_visible_bounds_notification_(false), |
| 111 needs_layout_(true), | 137 needs_layout_(true), |
| 112 snap_layer_to_pixel_boundary_(false), | 138 snap_layer_to_pixel_boundary_(false), |
| 113 flip_canvas_on_paint_for_rtl_ui_(false), | 139 flip_canvas_on_paint_for_rtl_ui_(false), |
| 114 paint_to_layer_(false), | 140 paint_to_layer_(false), |
| 115 accelerator_focus_manager_(NULL), | 141 accelerator_focus_manager_(NULL), |
| 116 registered_accelerator_count_(0), | 142 registered_accelerator_count_(0), |
| 117 next_focusable_view_(NULL), | 143 next_focusable_view_(NULL), |
| 118 previous_focusable_view_(NULL), | 144 previous_focusable_view_(NULL), |
| 119 focus_behavior_(FocusBehavior::NEVER), | 145 focus_behavior_(FocusBehavior::NEVER), |
| 120 context_menu_controller_(NULL), | 146 context_menu_controller_(NULL), |
| 121 drag_controller_(NULL), | 147 drag_controller_(NULL), |
| 122 native_view_accessibility_(NULL) { | 148 native_view_accessibility_(NULL) { |
| 123 SetTargetHandler(this); | 149 SetTargetHandler(this); |
| 124 } | 150 } |
| 125 | 151 |
| 126 View::~View() { | 152 View::~View() { |
| 127 if (parent_) | 153 if (parent_) |
| 128 parent_->RemoveChildView(this); | 154 parent_->RemoveChildView(this); |
| 129 | 155 |
| 130 ViewStorage::GetInstance()->ViewRemoved(this); | 156 ViewStorage::GetInstance()->ViewRemoved(this); |
| 131 | 157 |
| 132 for (Views::const_iterator i(children_.begin()); i != children_.end(); ++i) { | 158 { |
| 133 (*i)->parent_ = NULL; | 159 ScopedChildrenLock(this); |
| 134 if (!(*i)->owned_by_client_) | 160 for (auto* child : children_) { |
| 135 delete *i; | 161 child->parent_ = NULL; |
| 162 if (!child->owned_by_client_) | |
| 163 delete child; | |
| 164 } | |
| 136 } | 165 } |
| 137 | 166 |
| 138 // Release ownership of the native accessibility object, but it's | 167 // Release ownership of the native accessibility object, but it's |
| 139 // reference-counted on some platforms, so it may not be deleted right away. | 168 // reference-counted on some platforms, so it may not be deleted right away. |
| 140 if (native_view_accessibility_) | 169 if (native_view_accessibility_) |
| 141 native_view_accessibility_->Destroy(); | 170 native_view_accessibility_->Destroy(); |
| 142 } | 171 } |
| 143 | 172 |
| 144 // Tree operations ------------------------------------------------------------- | 173 // Tree operations ------------------------------------------------------------- |
| 145 | 174 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 172 ReorderChildView(view, index); | 201 ReorderChildView(view, index); |
| 173 return; | 202 return; |
| 174 } | 203 } |
| 175 parent->DoRemoveChildView(view, true, true, false, this); | 204 parent->DoRemoveChildView(view, true, true, false, this); |
| 176 } | 205 } |
| 177 | 206 |
| 178 // Sets the prev/next focus views. | 207 // Sets the prev/next focus views. |
| 179 InitFocusSiblings(view, index); | 208 InitFocusSiblings(view, index); |
| 180 | 209 |
| 181 view->parent_ = this; | 210 view->parent_ = this; |
| 211 #if DCHECK_IS_ON() | |
| 212 DCHECK(!iterating_); | |
| 213 #endif | |
| 182 children_.insert(children_.begin() + index, view); | 214 children_.insert(children_.begin() + index, view); |
| 183 | 215 |
| 184 // Ensure the layer tree matches the view tree before calling to any client | 216 // 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 | 217 // code. This way if client code further modifies the view tree we are in a |
| 186 // sane state. | 218 // sane state. |
| 187 const bool did_reparent_any_layers = view->UpdateParentLayers(); | 219 const bool did_reparent_any_layers = view->UpdateParentLayers(); |
| 188 Widget* widget = GetWidget(); | 220 Widget* widget = GetWidget(); |
| 189 if (did_reparent_any_layers && widget) | 221 if (did_reparent_any_layers && widget) |
| 190 widget->UpdateRootLayers(); | 222 widget->UpdateRootLayers(); |
| 191 | 223 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 230 DCHECK_EQ(view->parent_, this); | 262 DCHECK_EQ(view->parent_, this); |
| 231 if (index < 0) | 263 if (index < 0) |
| 232 index = child_count() - 1; | 264 index = child_count() - 1; |
| 233 else if (index >= child_count()) | 265 else if (index >= child_count()) |
| 234 return; | 266 return; |
| 235 if (children_[index] == view) | 267 if (children_[index] == view) |
| 236 return; | 268 return; |
| 237 | 269 |
| 238 const Views::iterator i(std::find(children_.begin(), children_.end(), view)); | 270 const Views::iterator i(std::find(children_.begin(), children_.end(), view)); |
| 239 DCHECK(i != children_.end()); | 271 DCHECK(i != children_.end()); |
| 272 #if DCHECK_IS_ON() | |
| 273 DCHECK(!iterating_); | |
| 274 #endif | |
| 240 children_.erase(i); | 275 children_.erase(i); |
| 241 | 276 |
| 242 // Unlink the view first | 277 // Unlink the view first |
| 243 View* next_focusable = view->next_focusable_view_; | 278 View* next_focusable = view->next_focusable_view_; |
| 244 View* prev_focusable = view->previous_focusable_view_; | 279 View* prev_focusable = view->previous_focusable_view_; |
| 245 if (prev_focusable) | 280 if (prev_focusable) |
| 246 prev_focusable->next_focusable_view_ = next_focusable; | 281 prev_focusable->next_focusable_view_ = next_focusable; |
| 247 if (next_focusable) | 282 if (next_focusable) |
| 248 next_focusable->previous_focusable_view_ = prev_focusable; | 283 next_focusable->previous_focusable_view_ = prev_focusable; |
| 249 | 284 |
| (...skipping 297 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. | 582 // If we have a layout manager, let it handle the layout for us. |
| 548 if (layout_manager_.get()) | 583 if (layout_manager_.get()) |
| 549 layout_manager_->Layout(this); | 584 layout_manager_->Layout(this); |
| 550 | 585 |
| 551 // Make sure to propagate the Layout() call to any children that haven't | 586 // 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 | 587 // 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 | 588 // 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 | 589 // 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 | 590 // just propagate the Layout() call down the hierarchy, so whoever receives |
| 556 // the call can take appropriate action. | 591 // the call can take appropriate action. |
| 557 for (int i = 0, count = child_count(); i < count; ++i) { | 592 ScopedChildrenLock(this); |
| 558 View* child = child_at(i); | 593 for (auto* child : children_) { |
| 559 if (child->needs_layout_ || !layout_manager_.get()) { | 594 if (child->needs_layout_ || !layout_manager_.get()) { |
| 560 TRACE_EVENT1("views", "View::Layout", "class", child->GetClassName()); | 595 TRACE_EVENT1("views", "View::Layout", "class", child->GetClassName()); |
| 561 child->needs_layout_ = false; | 596 child->needs_layout_ = false; |
| 562 child->Layout(); | 597 child->Layout(); |
| 563 } | 598 } |
| 564 } | 599 } |
| 565 } | 600 } |
| 566 | 601 |
| 567 void View::InvalidateLayout() { | 602 void View::InvalidateLayout() { |
| 568 // Always invalidate up. This is needed to handle the case of us already being | 603 // 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 | 649 |
| 615 View* View::GetAncestorWithClassName(const std::string& name) { | 650 View* View::GetAncestorWithClassName(const std::string& name) { |
| 616 return const_cast<View*>(const_cast<const View*>(this)-> | 651 return const_cast<View*>(const_cast<const View*>(this)-> |
| 617 GetAncestorWithClassName(name)); | 652 GetAncestorWithClassName(name)); |
| 618 } | 653 } |
| 619 | 654 |
| 620 const View* View::GetViewByID(int id) const { | 655 const View* View::GetViewByID(int id) const { |
| 621 if (id == id_) | 656 if (id == id_) |
| 622 return const_cast<View*>(this); | 657 return const_cast<View*>(this); |
| 623 | 658 |
| 624 for (int i = 0, count = child_count(); i < count; ++i) { | 659 ScopedChildrenLock(this); |
| 625 const View* view = child_at(i)->GetViewByID(id); | 660 for (auto* child : children_) { |
| 661 const View* view = child->GetViewByID(id); | |
| 626 if (view) | 662 if (view) |
| 627 return view; | 663 return view; |
| 628 } | 664 } |
| 629 return NULL; | 665 return NULL; |
| 630 } | 666 } |
| 631 | 667 |
| 632 View* View::GetViewByID(int id) { | 668 View* View::GetViewByID(int id) { |
| 633 return const_cast<View*>(const_cast<const View*>(this)->GetViewByID(id)); | 669 return const_cast<View*>(const_cast<const View*>(this)->GetViewByID(id)); |
| 634 } | 670 } |
| 635 | 671 |
| 636 void View::SetGroup(int gid) { | 672 void View::SetGroup(int gid) { |
| 637 // Don't change the group id once it's set. | 673 // Don't change the group id once it's set. |
| 638 DCHECK(group_ == -1 || group_ == gid); | 674 DCHECK(group_ == -1 || group_ == gid); |
| 639 group_ = gid; | 675 group_ = gid; |
| 640 } | 676 } |
| 641 | 677 |
| 642 int View::GetGroup() const { | 678 int View::GetGroup() const { |
| 643 return group_; | 679 return group_; |
| 644 } | 680 } |
| 645 | 681 |
| 646 bool View::IsGroupFocusTraversable() const { | 682 bool View::IsGroupFocusTraversable() const { |
| 647 return true; | 683 return true; |
| 648 } | 684 } |
| 649 | 685 |
| 650 void View::GetViewsInGroup(int group, Views* views) { | 686 void View::GetViewsInGroup(int group, Views* views) { |
| 651 if (group_ == group) | 687 if (group_ == group) |
| 652 views->push_back(this); | 688 views->push_back(this); |
| 653 | 689 |
| 654 for (int i = 0, count = child_count(); i < count; ++i) | 690 ScopedChildrenLock(this); |
| 655 child_at(i)->GetViewsInGroup(group, views); | 691 for (auto* child : children_) |
| 692 child->GetViewsInGroup(group, views); | |
| 656 } | 693 } |
| 657 | 694 |
| 658 View* View::GetSelectedViewForGroup(int group) { | 695 View* View::GetSelectedViewForGroup(int group) { |
| 659 Views views; | 696 Views views; |
| 660 GetWidget()->GetRootView()->GetViewsInGroup(group, &views); | 697 GetWidget()->GetRootView()->GetViewsInGroup(group, &views); |
| 661 return views.empty() ? NULL : views[0]; | 698 return views.empty() ? NULL : views[0]; |
| 662 } | 699 } |
| 663 | 700 |
| 664 // Coordinate conversion ------------------------------------------------------- | 701 // Coordinate conversion ------------------------------------------------------- |
| 665 | 702 |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 910 return true; | 947 return true; |
| 911 } | 948 } |
| 912 | 949 |
| 913 View* View::GetTooltipHandlerForPoint(const gfx::Point& point) { | 950 View* View::GetTooltipHandlerForPoint(const gfx::Point& point) { |
| 914 // TODO(tdanderson): Move this implementation into ViewTargetDelegate. | 951 // TODO(tdanderson): Move this implementation into ViewTargetDelegate. |
| 915 if (!HitTestPoint(point) || !CanProcessEventsWithinSubtree()) | 952 if (!HitTestPoint(point) || !CanProcessEventsWithinSubtree()) |
| 916 return NULL; | 953 return NULL; |
| 917 | 954 |
| 918 // Walk the child Views recursively looking for the View that most | 955 // Walk the child Views recursively looking for the View that most |
| 919 // tightly encloses the specified point. | 956 // tightly encloses the specified point. |
| 920 for (int i = child_count() - 1; i >= 0; --i) { | 957 for (auto* child : base::Reversed(children_)) { |
| 921 View* child = child_at(i); | |
| 922 if (!child->visible()) | 958 if (!child->visible()) |
| 923 continue; | 959 continue; |
| 924 | 960 |
| 925 gfx::Point point_in_child_coords(point); | 961 gfx::Point point_in_child_coords(point); |
| 926 ConvertPointToTarget(this, child, &point_in_child_coords); | 962 ConvertPointToTarget(this, child, &point_in_child_coords); |
| 927 View* handler = child->GetTooltipHandlerForPoint(point_in_child_coords); | 963 View* handler = child->GetTooltipHandlerForPoint(point_in_child_coords); |
| 928 if (handler) | 964 if (handler) |
| 929 return handler; | 965 return handler; |
| 930 } | 966 } |
| 931 return this; | 967 return this; |
| (...skipping 498 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1430 | 1466 |
| 1431 if (focus_manager) | 1467 if (focus_manager) |
| 1432 RegisterPendingAccelerators(); | 1468 RegisterPendingAccelerators(); |
| 1433 } | 1469 } |
| 1434 } | 1470 } |
| 1435 | 1471 |
| 1436 // Painting -------------------------------------------------------------------- | 1472 // Painting -------------------------------------------------------------------- |
| 1437 | 1473 |
| 1438 void View::PaintChildren(const ui::PaintContext& context) { | 1474 void View::PaintChildren(const ui::PaintContext& context) { |
| 1439 TRACE_EVENT1("views", "View::PaintChildren", "class", GetClassName()); | 1475 TRACE_EVENT1("views", "View::PaintChildren", "class", GetClassName()); |
| 1440 for (int i = 0, count = child_count(); i < count; ++i) | 1476 ScopedChildrenLock(this); |
| 1441 if (!child_at(i)->layer()) | 1477 for (auto* child : children_) { |
| 1442 child_at(i)->Paint(context); | 1478 if (!child->layer()) |
| 1479 child->Paint(context); | |
| 1480 } | |
| 1443 } | 1481 } |
| 1444 | 1482 |
| 1445 void View::OnPaint(gfx::Canvas* canvas) { | 1483 void View::OnPaint(gfx::Canvas* canvas) { |
| 1446 TRACE_EVENT1("views", "View::OnPaint", "class", GetClassName()); | 1484 TRACE_EVENT1("views", "View::OnPaint", "class", GetClassName()); |
| 1447 OnPaintBackground(canvas); | 1485 OnPaintBackground(canvas); |
| 1448 OnPaintBorder(canvas); | 1486 OnPaintBorder(canvas); |
| 1449 } | 1487 } |
| 1450 | 1488 |
| 1451 void View::OnPaintBackground(gfx::Canvas* canvas) { | 1489 void View::OnPaintBackground(gfx::Canvas* canvas) { |
| 1452 if (background_.get()) { | 1490 if (background_.get()) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1498 void View::MoveLayerToParent(ui::Layer* parent_layer, | 1536 void View::MoveLayerToParent(ui::Layer* parent_layer, |
| 1499 const gfx::Point& point) { | 1537 const gfx::Point& point) { |
| 1500 gfx::Point local_point(point); | 1538 gfx::Point local_point(point); |
| 1501 if (parent_layer != layer()) | 1539 if (parent_layer != layer()) |
| 1502 local_point.Offset(GetMirroredX(), y()); | 1540 local_point.Offset(GetMirroredX(), y()); |
| 1503 if (layer() && parent_layer != layer()) { | 1541 if (layer() && parent_layer != layer()) { |
| 1504 parent_layer->Add(layer()); | 1542 parent_layer->Add(layer()); |
| 1505 SetLayerBounds(gfx::Rect(local_point.x(), local_point.y(), | 1543 SetLayerBounds(gfx::Rect(local_point.x(), local_point.y(), |
| 1506 width(), height())); | 1544 width(), height())); |
| 1507 } else { | 1545 } else { |
| 1508 for (int i = 0, count = child_count(); i < count; ++i) | 1546 ScopedChildrenLock(this); |
| 1509 child_at(i)->MoveLayerToParent(parent_layer, local_point); | 1547 for (auto* child : children_) |
| 1548 child->MoveLayerToParent(parent_layer, local_point); | |
| 1510 } | 1549 } |
| 1511 } | 1550 } |
| 1512 | 1551 |
| 1513 void View::UpdateLayerVisibility() { | 1552 void View::UpdateLayerVisibility() { |
| 1514 bool visible = visible_; | 1553 bool visible = visible_; |
| 1515 for (const View* v = parent_; visible && v && !v->layer(); v = v->parent_) | 1554 for (const View* v = parent_; visible && v && !v->layer(); v = v->parent_) |
| 1516 visible = v->visible(); | 1555 visible = v->visible(); |
| 1517 | 1556 |
| 1518 UpdateChildLayerVisibility(visible); | 1557 UpdateChildLayerVisibility(visible); |
| 1519 } | 1558 } |
| 1520 | 1559 |
| 1521 void View::UpdateChildLayerVisibility(bool ancestor_visible) { | 1560 void View::UpdateChildLayerVisibility(bool ancestor_visible) { |
| 1522 if (layer()) { | 1561 if (layer()) { |
| 1523 layer()->SetVisible(ancestor_visible && visible_); | 1562 layer()->SetVisible(ancestor_visible && visible_); |
| 1524 } else { | 1563 } else { |
| 1525 for (int i = 0, count = child_count(); i < count; ++i) | 1564 ScopedChildrenLock(this); |
| 1526 child_at(i)->UpdateChildLayerVisibility(ancestor_visible && visible_); | 1565 for (auto* child : children_) |
| 1566 child->UpdateChildLayerVisibility(ancestor_visible && visible_); | |
| 1527 } | 1567 } |
| 1528 } | 1568 } |
| 1529 | 1569 |
| 1530 void View::UpdateChildLayerBounds(const gfx::Vector2d& offset) { | 1570 void View::UpdateChildLayerBounds(const gfx::Vector2d& offset) { |
| 1531 if (layer()) { | 1571 if (layer()) { |
| 1532 SetLayerBounds(GetLocalBounds() + offset); | 1572 SetLayerBounds(GetLocalBounds() + offset); |
| 1533 } else { | 1573 } else { |
| 1534 for (int i = 0, count = child_count(); i < count; ++i) { | 1574 ScopedChildrenLock(this); |
| 1535 View* child = child_at(i); | 1575 for (auto* child : children_) { |
| 1536 child->UpdateChildLayerBounds( | 1576 child->UpdateChildLayerBounds( |
| 1537 offset + gfx::Vector2d(child->GetMirroredX(), child->y())); | 1577 offset + gfx::Vector2d(child->GetMirroredX(), child->y())); |
| 1538 } | 1578 } |
| 1539 } | 1579 } |
| 1540 } | 1580 } |
| 1541 | 1581 |
| 1542 void View::OnPaintLayer(const ui::PaintContext& context) { | 1582 void View::OnPaintLayer(const ui::PaintContext& context) { |
| 1543 Paint(context); | 1583 Paint(context); |
| 1544 } | 1584 } |
| 1545 | 1585 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1581 } | 1621 } |
| 1582 | 1622 |
| 1583 void View::ReorderChildLayers(ui::Layer* parent_layer) { | 1623 void View::ReorderChildLayers(ui::Layer* parent_layer) { |
| 1584 if (layer() && layer() != parent_layer) { | 1624 if (layer() && layer() != parent_layer) { |
| 1585 DCHECK_EQ(parent_layer, layer()->parent()); | 1625 DCHECK_EQ(parent_layer, layer()->parent()); |
| 1586 parent_layer->StackAtBottom(layer()); | 1626 parent_layer->StackAtBottom(layer()); |
| 1587 } else { | 1627 } else { |
| 1588 // Iterate backwards through the children so that a child with a layer | 1628 // 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 | 1629 // which is further to the back is stacked above one which is further to |
| 1590 // the front. | 1630 // the front. |
| 1591 for (Views::reverse_iterator it(children_.rbegin()); | 1631 ScopedChildrenLock(this); |
| 1592 it != children_.rend(); ++it) { | 1632 for (auto* child : base::Reversed(children_)) { |
| 1593 (*it)->ReorderChildLayers(parent_layer); | 1633 child->ReorderChildLayers(parent_layer); |
| 1594 } | 1634 } |
| 1595 } | 1635 } |
| 1596 } | 1636 } |
| 1597 | 1637 |
| 1598 // Input ----------------------------------------------------------------------- | 1638 // Input ----------------------------------------------------------------------- |
| 1599 | 1639 |
| 1600 View::DragInfo* View::GetDragInfo() { | 1640 View::DragInfo* View::GetDragInfo() { |
| 1601 return parent_ ? parent_->GetDragInfo() : NULL; | 1641 return parent_ ? parent_->GetDragInfo() : NULL; |
| 1602 } | 1642 } |
| 1603 | 1643 |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1758 | 1798 |
| 1759 base::snprintf(pp, kMaxPointerStringLength, "%p", parent_); | 1799 base::snprintf(pp, kMaxPointerStringLength, "%p", parent_); |
| 1760 result.append(" N"); | 1800 result.append(" N"); |
| 1761 result.append(pp + 2); | 1801 result.append(pp + 2); |
| 1762 result.append(" -> N"); | 1802 result.append(" -> N"); |
| 1763 result.append(p + 2); | 1803 result.append(p + 2); |
| 1764 result.append("\n"); | 1804 result.append("\n"); |
| 1765 } | 1805 } |
| 1766 | 1806 |
| 1767 // Children. | 1807 // Children. |
| 1768 for (int i = 0, count = view_with_children->child_count(); i < count; ++i) | 1808 for (auto* child : view_with_children->children_) |
| 1769 result.append(view_with_children->child_at(i)->PrintViewGraph(false)); | 1809 result.append(child->PrintViewGraph(false)); |
| 1770 | 1810 |
| 1771 if (first) | 1811 if (first) |
| 1772 result.append("}\n"); | 1812 result.append("}\n"); |
| 1773 | 1813 |
| 1774 return result; | 1814 return result; |
| 1775 } | 1815 } |
| 1776 #endif | 1816 #endif |
| 1777 | 1817 |
| 1778 //////////////////////////////////////////////////////////////////////////////// | 1818 //////////////////////////////////////////////////////////////////////////////// |
| 1779 // View, private: | 1819 // View, private: |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1855 view->OrphanLayers(); | 1895 view->OrphanLayers(); |
| 1856 if (widget) | 1896 if (widget) |
| 1857 widget->UpdateRootLayers(); | 1897 widget->UpdateRootLayers(); |
| 1858 | 1898 |
| 1859 view->PropagateRemoveNotifications(this, new_parent); | 1899 view->PropagateRemoveNotifications(this, new_parent); |
| 1860 view->parent_ = nullptr; | 1900 view->parent_ = nullptr; |
| 1861 | 1901 |
| 1862 if (delete_removed_view && !view->owned_by_client_) | 1902 if (delete_removed_view && !view->owned_by_client_) |
| 1863 view_to_be_deleted.reset(view); | 1903 view_to_be_deleted.reset(view); |
| 1864 | 1904 |
| 1905 #if DCHECK_IS_ON() | |
| 1906 DCHECK(!iterating_); | |
| 1907 #endif | |
| 1865 children_.erase(i); | 1908 children_.erase(i); |
| 1866 | 1909 |
| 1867 if (update_tool_tip) | 1910 if (update_tool_tip) |
| 1868 UpdateTooltip(); | 1911 UpdateTooltip(); |
| 1869 | 1912 |
| 1870 if (layout_manager_) | 1913 if (layout_manager_) |
| 1871 layout_manager_->ViewRemoved(this, view); | 1914 layout_manager_->ViewRemoved(this, view); |
| 1872 | 1915 |
| 1873 for (ViewObserver& observer : observers_) | 1916 for (ViewObserver& observer : observers_) |
| 1874 observer.OnChildViewRemoved(view, this); | 1917 observer.OnChildViewRemoved(view, this); |
| 1875 } | 1918 } |
| 1876 | 1919 |
| 1877 void View::PropagateRemoveNotifications(View* old_parent, View* new_parent) { | 1920 void View::PropagateRemoveNotifications(View* old_parent, View* new_parent) { |
| 1878 for (int i = 0, count = child_count(); i < count; ++i) | 1921 { |
| 1879 child_at(i)->PropagateRemoveNotifications(old_parent, new_parent); | 1922 ScopedChildrenLock(this); |
| 1923 for (auto* child : children_) | |
| 1924 child->PropagateRemoveNotifications(old_parent, new_parent); | |
| 1925 } | |
| 1880 | 1926 |
| 1881 ViewHierarchyChangedDetails details(false, old_parent, this, new_parent); | 1927 ViewHierarchyChangedDetails details(false, old_parent, this, new_parent); |
| 1882 for (View* v = this; v; v = v->parent_) | 1928 for (View* v = this; v; v = v->parent_) |
| 1883 v->ViewHierarchyChangedImpl(true, details); | 1929 v->ViewHierarchyChangedImpl(true, details); |
| 1884 } | 1930 } |
| 1885 | 1931 |
| 1886 void View::PropagateAddNotifications( | 1932 void View::PropagateAddNotifications( |
| 1887 const ViewHierarchyChangedDetails& details) { | 1933 const ViewHierarchyChangedDetails& details) { |
| 1888 for (int i = 0, count = child_count(); i < count; ++i) | 1934 { |
| 1889 child_at(i)->PropagateAddNotifications(details); | 1935 ScopedChildrenLock(this); |
| 1936 for (auto* child : children_) | |
| 1937 child->PropagateAddNotifications(details); | |
| 1938 } | |
| 1890 ViewHierarchyChangedImpl(true, details); | 1939 ViewHierarchyChangedImpl(true, details); |
| 1891 } | 1940 } |
| 1892 | 1941 |
| 1893 void View::PropagateNativeViewHierarchyChanged() { | 1942 void View::PropagateNativeViewHierarchyChanged() { |
| 1894 for (int i = 0, count = child_count(); i < count; ++i) | 1943 { |
| 1895 child_at(i)->PropagateNativeViewHierarchyChanged(); | 1944 ScopedChildrenLock(this); |
| 1945 for (auto* child : children_) | |
| 1946 child->PropagateNativeViewHierarchyChanged(); | |
| 1947 } | |
| 1896 NativeViewHierarchyChanged(); | 1948 NativeViewHierarchyChanged(); |
| 1897 } | 1949 } |
| 1898 | 1950 |
| 1899 void View::ViewHierarchyChangedImpl( | 1951 void View::ViewHierarchyChangedImpl( |
| 1900 bool register_accelerators, | 1952 bool register_accelerators, |
| 1901 const ViewHierarchyChangedDetails& details) { | 1953 const ViewHierarchyChangedDetails& details) { |
| 1902 if (register_accelerators) { | 1954 if (register_accelerators) { |
| 1903 if (details.is_add) { | 1955 if (details.is_add) { |
| 1904 // If you get this registration, you are part of a subtree that has been | 1956 // If you get this registration, you are part of a subtree that has been |
| 1905 // added to the view hierarchy. | 1957 // added to the view hierarchy. |
| 1906 if (GetFocusManager()) | 1958 if (GetFocusManager()) |
| 1907 RegisterPendingAccelerators(); | 1959 RegisterPendingAccelerators(); |
| 1908 } else { | 1960 } else { |
| 1909 if (details.child == this) | 1961 if (details.child == this) |
| 1910 UnregisterAccelerators(true); | 1962 UnregisterAccelerators(true); |
| 1911 } | 1963 } |
| 1912 } | 1964 } |
| 1913 | 1965 |
| 1914 ViewHierarchyChanged(details); | 1966 ViewHierarchyChanged(details); |
| 1915 details.parent->needs_layout_ = true; | 1967 details.parent->needs_layout_ = true; |
| 1916 } | 1968 } |
| 1917 | 1969 |
| 1918 void View::PropagateNativeThemeChanged(const ui::NativeTheme* theme) { | 1970 void View::PropagateNativeThemeChanged(const ui::NativeTheme* theme) { |
| 1919 for (int i = 0, count = child_count(); i < count; ++i) | 1971 { |
| 1920 child_at(i)->PropagateNativeThemeChanged(theme); | 1972 ScopedChildrenLock(this); |
| 1973 for (auto* child : children_) | |
| 1974 child->PropagateNativeThemeChanged(theme); | |
| 1975 } | |
| 1921 OnNativeThemeChanged(theme); | 1976 OnNativeThemeChanged(theme); |
| 1922 } | 1977 } |
| 1923 | 1978 |
| 1924 // Size and disposition -------------------------------------------------------- | 1979 // Size and disposition -------------------------------------------------------- |
| 1925 | 1980 |
| 1926 void View::PropagateVisibilityNotifications(View* start, bool is_visible) { | 1981 void View::PropagateVisibilityNotifications(View* start, bool is_visible) { |
| 1927 for (int i = 0, count = child_count(); i < count; ++i) | 1982 { |
| 1928 child_at(i)->PropagateVisibilityNotifications(start, is_visible); | 1983 ScopedChildrenLock(this); |
| 1984 for (auto* child : children_) | |
| 1985 child->PropagateVisibilityNotifications(start, is_visible); | |
| 1986 } | |
| 1929 VisibilityChangedImpl(start, is_visible); | 1987 VisibilityChangedImpl(start, is_visible); |
| 1930 } | 1988 } |
| 1931 | 1989 |
| 1932 void View::VisibilityChangedImpl(View* starting_from, bool is_visible) { | 1990 void View::VisibilityChangedImpl(View* starting_from, bool is_visible) { |
| 1933 VisibilityChanged(starting_from, is_visible); | 1991 VisibilityChanged(starting_from, is_visible); |
| 1934 } | 1992 } |
| 1935 | 1993 |
| 1936 void View::BoundsChanged(const gfx::Rect& previous_bounds) { | 1994 void View::BoundsChanged(const gfx::Rect& previous_bounds) { |
| 1937 if (visible_) { | 1995 if (visible_) { |
| 1938 // Paint the new bounds. | 1996 // Paint the new bounds. |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2099 bool result = GetTransformRelativeTo(ancestor, &trans); | 2157 bool result = GetTransformRelativeTo(ancestor, &trans); |
| 2100 trans.TransformRectReverse(rect); | 2158 trans.TransformRectReverse(rect); |
| 2101 return result; | 2159 return result; |
| 2102 } | 2160 } |
| 2103 | 2161 |
| 2104 // Accelerated painting -------------------------------------------------------- | 2162 // Accelerated painting -------------------------------------------------------- |
| 2105 | 2163 |
| 2106 void View::CreateLayer() { | 2164 void View::CreateLayer() { |
| 2107 // A new layer is being created for the view. So all the layers of the | 2165 // 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. | 2166 // sub-tree can inherit the visibility of the corresponding view. |
| 2109 for (int i = 0, count = child_count(); i < count; ++i) | 2167 { |
| 2110 child_at(i)->UpdateChildLayerVisibility(true); | 2168 ScopedChildrenLock(this); |
| 2169 for (auto* child : children_) | |
| 2170 child->UpdateChildLayerVisibility(true); | |
| 2171 } | |
| 2111 | 2172 |
| 2112 SetLayer(base::MakeUnique<ui::Layer>()); | 2173 SetLayer(base::MakeUnique<ui::Layer>()); |
| 2113 layer()->set_delegate(this); | 2174 layer()->set_delegate(this); |
| 2114 layer()->set_name(GetClassName()); | 2175 layer()->set_name(GetClassName()); |
| 2115 | 2176 |
| 2116 UpdateParentLayers(); | 2177 UpdateParentLayers(); |
| 2117 UpdateLayerVisibility(); | 2178 UpdateLayerVisibility(); |
| 2118 | 2179 |
| 2119 // The new layer needs to be ordered in the layer tree according | 2180 // 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 | 2181 // to the view tree. Children of this layer were added in order |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 2138 if (layer()) { | 2199 if (layer()) { |
| 2139 if (!layer()->parent()) { | 2200 if (!layer()->parent()) { |
| 2140 UpdateParentLayer(); | 2201 UpdateParentLayer(); |
| 2141 return true; | 2202 return true; |
| 2142 } | 2203 } |
| 2143 // The layers of any child views are already in place, so we can stop | 2204 // The layers of any child views are already in place, so we can stop |
| 2144 // iterating here. | 2205 // iterating here. |
| 2145 return false; | 2206 return false; |
| 2146 } | 2207 } |
| 2147 bool result = false; | 2208 bool result = false; |
| 2148 for (int i = 0, count = child_count(); i < count; ++i) { | 2209 ScopedChildrenLock(this); |
| 2149 if (child_at(i)->UpdateParentLayers()) | 2210 for (auto* child : children_) { |
| 2211 if (child->UpdateParentLayers()) | |
| 2150 result = true; | 2212 result = true; |
| 2151 } | 2213 } |
| 2152 return result; | 2214 return result; |
| 2153 } | 2215 } |
| 2154 | 2216 |
| 2155 void View::OrphanLayers() { | 2217 void View::OrphanLayers() { |
| 2156 if (layer()) { | 2218 if (layer()) { |
| 2157 if (layer()->parent()) | 2219 if (layer()->parent()) |
| 2158 layer()->parent()->Remove(layer()); | 2220 layer()->parent()->Remove(layer()); |
| 2159 | 2221 |
| 2160 // The layer belonging to this View has already been orphaned. It is not | 2222 // The layer belonging to this View has already been orphaned. It is not |
| 2161 // necessary to orphan the child layers. | 2223 // necessary to orphan the child layers. |
| 2162 return; | 2224 return; |
| 2163 } | 2225 } |
| 2164 for (int i = 0, count = child_count(); i < count; ++i) | 2226 ScopedChildrenLock(this); |
| 2165 child_at(i)->OrphanLayers(); | 2227 for (auto* child : children_) |
| 2228 child->OrphanLayers(); | |
| 2166 } | 2229 } |
| 2167 | 2230 |
| 2168 void View::ReparentLayer(const gfx::Vector2d& offset, ui::Layer* parent_layer) { | 2231 void View::ReparentLayer(const gfx::Vector2d& offset, ui::Layer* parent_layer) { |
| 2169 layer()->SetBounds(GetLocalBounds() + offset); | 2232 layer()->SetBounds(GetLocalBounds() + offset); |
| 2170 DCHECK_NE(layer(), parent_layer); | 2233 DCHECK_NE(layer(), parent_layer); |
| 2171 if (parent_layer) | 2234 if (parent_layer) |
| 2172 parent_layer->Add(layer()); | 2235 parent_layer->Add(layer()); |
| 2173 layer()->SchedulePaint(GetLocalBounds()); | 2236 layer()->SchedulePaint(GetLocalBounds()); |
| 2174 MoveLayerToParent(layer(), gfx::Point()); | 2237 MoveLayerToParent(layer(), gfx::Point()); |
| 2175 } | 2238 } |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2338 | 2401 |
| 2339 if (count == 0) { | 2402 if (count == 0) { |
| 2340 v->next_focusable_view_ = NULL; | 2403 v->next_focusable_view_ = NULL; |
| 2341 v->previous_focusable_view_ = NULL; | 2404 v->previous_focusable_view_ = NULL; |
| 2342 } else { | 2405 } else { |
| 2343 if (index == count) { | 2406 if (index == count) { |
| 2344 // We are inserting at the end, but the end of the child list may not be | 2407 // 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 | 2408 // the last focusable element. Let's try to find an element with no next |
| 2346 // focusable element to link to. | 2409 // focusable element to link to. |
| 2347 View* last_focusable_view = NULL; | 2410 View* last_focusable_view = NULL; |
| 2348 for (Views::iterator i(children_.begin()); i != children_.end(); ++i) { | 2411 { |
| 2349 if (!(*i)->next_focusable_view_) { | 2412 ScopedChildrenLock(this); |
| 2350 last_focusable_view = *i; | 2413 for (auto* child : children_) { |
| 2414 if (!child->next_focusable_view_) { | |
| 2415 last_focusable_view = child; | |
| 2351 break; | 2416 break; |
| 2352 } | 2417 } |
| 2418 } | |
| 2353 } | 2419 } |
| 2354 if (last_focusable_view == NULL) { | 2420 if (last_focusable_view == NULL) { |
| 2355 // Hum... there is a cycle in the focus list. Let's just insert ourself | 2421 // Hum... there is a cycle in the focus list. Let's just insert ourself |
| 2356 // after the last child. | 2422 // after the last child. |
| 2357 View* prev = children_[index - 1]; | 2423 View* prev = children_[index - 1]; |
| 2358 v->previous_focusable_view_ = prev; | 2424 v->previous_focusable_view_ = prev; |
| 2359 v->next_focusable_view_ = prev->next_focusable_view_; | 2425 v->next_focusable_view_ = prev->next_focusable_view_; |
| 2360 prev->next_focusable_view_->previous_focusable_view_ = v; | 2426 prev->next_focusable_view_->previous_focusable_view_ = v; |
| 2361 prev->next_focusable_view_ = v; | 2427 prev->next_focusable_view_ = v; |
| 2362 } else { | 2428 } else { |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 2384 return; | 2450 return; |
| 2385 | 2451 |
| 2386 FocusManager* focus_manager = GetFocusManager(); | 2452 FocusManager* focus_manager = GetFocusManager(); |
| 2387 if (focus_manager) | 2453 if (focus_manager) |
| 2388 focus_manager->AdvanceFocusIfNecessary(); | 2454 focus_manager->AdvanceFocusIfNecessary(); |
| 2389 } | 2455 } |
| 2390 | 2456 |
| 2391 // System events --------------------------------------------------------------- | 2457 // System events --------------------------------------------------------------- |
| 2392 | 2458 |
| 2393 void View::PropagateThemeChanged() { | 2459 void View::PropagateThemeChanged() { |
| 2394 for (int i = child_count() - 1; i >= 0; --i) | 2460 { |
| 2395 child_at(i)->PropagateThemeChanged(); | 2461 ScopedChildrenLock(this); |
| 2462 for (auto* child : base::Reversed(children_)) | |
| 2463 child->PropagateThemeChanged(); | |
| 2464 } | |
| 2396 OnThemeChanged(); | 2465 OnThemeChanged(); |
| 2397 } | 2466 } |
| 2398 | 2467 |
| 2399 void View::PropagateLocaleChanged() { | 2468 void View::PropagateLocaleChanged() { |
| 2400 for (int i = child_count() - 1; i >= 0; --i) | 2469 { |
| 2401 child_at(i)->PropagateLocaleChanged(); | 2470 ScopedChildrenLock(this); |
| 2471 for (auto* child : base::Reversed(children_)) | |
| 2472 child->PropagateLocaleChanged(); | |
| 2473 } | |
| 2402 OnLocaleChanged(); | 2474 OnLocaleChanged(); |
| 2403 } | 2475 } |
| 2404 | 2476 |
| 2405 void View::PropagateDeviceScaleFactorChanged(float device_scale_factor) { | 2477 void View::PropagateDeviceScaleFactorChanged(float device_scale_factor) { |
| 2406 for (int i = child_count() - 1; i >= 0; --i) | 2478 { |
| 2407 child_at(i)->PropagateDeviceScaleFactorChanged(device_scale_factor); | 2479 ScopedChildrenLock(this); |
| 2480 for (auto* child : base::Reversed(children_)) | |
| 2481 child->PropagateDeviceScaleFactorChanged(device_scale_factor); | |
| 2482 } | |
| 2408 | 2483 |
| 2409 // If the view is drawing to the layer, OnDeviceScaleFactorChanged() is called | 2484 // If the view is drawing to the layer, OnDeviceScaleFactorChanged() is called |
| 2410 // through LayerDelegate callback. | 2485 // through LayerDelegate callback. |
| 2411 if (!layer()) | 2486 if (!layer()) |
| 2412 OnDeviceScaleFactorChanged(device_scale_factor); | 2487 OnDeviceScaleFactorChanged(device_scale_factor); |
| 2413 } | 2488 } |
| 2414 | 2489 |
| 2415 // Tooltips -------------------------------------------------------------------- | 2490 // Tooltips -------------------------------------------------------------------- |
| 2416 | 2491 |
| 2417 void View::UpdateTooltip() { | 2492 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 | 2523 // 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. | 2524 // the RootView can detect it and avoid calling us back. |
| 2450 gfx::Point widget_location(event.location()); | 2525 gfx::Point widget_location(event.location()); |
| 2451 ConvertPointToWidget(this, &widget_location); | 2526 ConvertPointToWidget(this, &widget_location); |
| 2452 widget->RunShellDrag(this, data, widget_location, drag_operations, source); | 2527 widget->RunShellDrag(this, data, widget_location, drag_operations, source); |
| 2453 // WARNING: we may have been deleted. | 2528 // WARNING: we may have been deleted. |
| 2454 return true; | 2529 return true; |
| 2455 } | 2530 } |
| 2456 | 2531 |
| 2457 } // namespace views | 2532 } // namespace views |
| OLD | NEW |