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

Side by Side Diff: ash/devtools/view_element.cc

Issue 2776543002: Create a unified UIElement interface for Widget, View and Window. (Closed)
Patch Set: nits Created 3 years, 7 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
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ash/devtools/view_element.h"
6
7 #include "ash/devtools/ui_element_delegate.h"
8 #include "ui/views/widget/widget.h"
9
10 namespace ash {
11 namespace devtools {
12 namespace {
13
14 int FindSibling(views::View* view) {
sadrul 2017/05/09 04:58:07 Call this FindIndexInParent() or something like th
thanhph 2017/05/09 20:52:46 Done.
15 views::View* parent = view->parent();
16 int view_index = -1;
17 for (int i = 0, count = parent->child_count(); i < count; i++) {
18 if (view == parent->child_at(i)) {
19 view_index = i;
20 break;
21 }
22 }
23 DCHECK_GE(view_index, 0);
sadrul 2017/05/09 04:58:07 Use View::GetIndexOf() instead.
thanhph 2017/05/09 20:52:47 cool, I removed FindSibling().
24 return view_index;
25 }
26
27 } // namespace
28
29 ViewElement::ViewElement(views::View* view,
30 UIElementDelegate* ui_element_delegate,
31 UIElement* parent)
32 : UIElement(UIElementType::VIEW, ui_element_delegate, parent), view_(view) {
33 view_->AddObserver(this);
34 }
35
36 ViewElement::~ViewElement() {
37 view_->RemoveObserver(this);
38 }
39
40 void ViewElement::OnChildViewRemoved(views::View* parent, views::View* view) {
41 DCHECK(parent == view_);
42 for (auto* child_element : children()) {
43 if (UIElement::GetBackingElement<views::View, ViewElement>(child_element) ==
44 view &&
45 delegate()->OnUIElementRemoved(child_element)) {
46 RemoveChild(child_element);
47 child_element->Destroy();
48 }
49 }
sadrul 2017/05/09 04:58:07 You can avoid the for-loop here: auto iter = st
thanhph 2017/05/09 20:52:47 Nice, done!
50 }
51
52 void ViewElement::OnChildViewAdded(views::View* parent, views::View* view) {
53 DCHECK(parent == view_);
sadrul 2017/05/09 04:58:07 DCHECK_EQ (here and elsewhere)
thanhph 2017/05/09 20:52:47 Done.
54 AddChild(new ViewElement(view, delegate(), this),
55 children().empty() ? nullptr : children().back());
56 }
57
58 void ViewElement::OnChildViewReordered(views::View* parent, views::View* view) {
59 DCHECK(view == view_);
sadrul 2017/05/09 04:58:07 This is wrong, right? Because according to the com
thanhph 2017/05/09 20:52:47 Good catch, unittests havent covered this so I add
60 this->parent()->RemoveChild(this);
61 this->parent()->ReorderChild(this, FindSibling(view));
62 delegate()->OnUIElementReordered(this->parent(), this);
63 }
64
65 void ViewElement::OnViewBoundsChanged(views::View* view) {
66 DCHECK(view_ == view);
sadrul 2017/05/09 04:58:07 Use DCHECK_EQ
thanhph 2017/05/09 20:52:46 Done.
67 delegate()->OnUIElementBoundsChanged(this);
68 }
69
70 void ViewElement::Destroy() {
71 delete this;
72 }
73
74 void ViewElement::GetBounds(gfx::Rect* bounds) const {
75 *bounds = view_->bounds();
76 }
77
78 void ViewElement::SetBounds(const gfx::Rect& bounds) {
79 view_->SetBoundsRect(bounds);
80 }
81
82 void ViewElement::GetVisible(bool* visible) const {
83 *visible = view_->visible();
84 }
85
86 void ViewElement::SetVisible(bool visible) {
87 if (visible != view_->visible())
88 view_->SetVisible(visible);
89 }
90
91 std::pair<aura::Window*, gfx::Rect> ViewElement::GetNodeWindowAndBounds()
92 const {
93 return std::make_pair(view_->GetWidget()->GetNativeWindow(), view_->bounds());
94 }
95
96 // static
97 views::View* ViewElement::From(UIElement* element) {
98 DCHECK_EQ(UIElementType::VIEW, element->type());
99 return static_cast<ViewElement*>(element)->view_;
100 }
101
102 } // namespace devtools
103 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698