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

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

Issue 2776543002: Create a unified UIElement interface for Widget, View and Window. (Closed)
Patch Set: . 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 "ash/wm_window.h"
9 #include "ui/views/widget/widget.h"
10
11 namespace ash {
12
13 namespace devtools {
14
15 namespace {
16
17 int FindSibling(views::View* view) {
18 views::View* parent = view->parent();
19 int view_index = -1;
20 for (int i = 0, count = parent->child_count(); i < count; i++) {
21 if (view == parent->child_at(i)) {
22 view_index = i;
23 break;
24 }
25 }
26 DCHECK_GE(view_index, 0);
27 return view_index;
28 }
29
30 } // namespace
31
32 ViewElement::ViewElement(views::View* view,
33 UIElementDelegate* ui_element_delegate,
34 UIElement* parent)
35 : UIElement(UIElementType::VIEW, ui_element_delegate, parent), view_(view) {
36 if (!view_->HasObserver(this))
37 view_->AddObserver(this);
38 }
39
40 ViewElement::~ViewElement() {
41 if (view_ && view_->HasObserver(this))
42 view_->RemoveObserver(this);
43 }
44
45 views::View* ViewElement::view() {
46 return view_;
47 }
48
49 void ViewElement::OnChildViewRemoved(views::View* parent, views::View* view) {
50 if (parent == view_) {
51 for (auto* child_element : GetChildren()) {
52 if (UIElement::GetBackingElement<views::View, ViewElement>(
53 child_element) == view &&
54 delegate()->OnUIElementRemoved(child_element->GetNodeId())) {
55 child_element->RemoveChildFromParent();
56 child_element->Destroy();
57 }
58 }
59 }
60 }
61
62 void ViewElement::OnChildViewAdded(views::View* parent, views::View* view) {
63 if (parent == view_) {
64 delegate()->OnUIElementAdded(this, new ViewElement(view, delegate(), this),
65 LastChildIterator() - 1);
66 }
67 }
68
69 void ViewElement::OnChildViewReordered(views::View* parent, views::View* view) {
70 if (view == view_) {
71 RemoveChildFromParent();
72
73 int i = FindSibling(view);
74 std::vector<UIElement*>::iterator new_sibling_position =
75 GetParent()->BeginChildIterator();
76 while (i-- > 0)
77 new_sibling_position++;
78
79 GetParent()->AddChild(new_sibling_position, this);
80 }
81 }
82
83 void ViewElement::OnViewBoundsChanged(views::View* view) {
84 if (view_ == view)
85 delegate()->OnUIElementBoundsChanged(GetNodeId());
86 }
87
88 void ViewElement::Destroy() {
89 delete this;
90 }
91
92 bool ViewElement::GetBounds(gfx::Rect* bounds) {
93 if (view_) {
94 *bounds = view_->bounds();
95 return true;
96 }
97 return false;
98 }
99
100 bool ViewElement::SetBounds(const gfx::Rect& bounds) {
101 if (view_) {
102 view_->SetBoundsRect(bounds);
103 return true;
104 }
105 return false;
106 }
107
108 bool ViewElement::GetVisible(bool* visible) {
109 if (view_) {
110 *visible = view_->visible();
111 return true;
112 }
113 return false;
114 }
115
116 bool ViewElement::SetVisible(bool visible) {
117 if (view_) {
118 if (visible != view_->visible())
119 view_->SetVisible(visible);
120 return true;
121 }
122 return false;
123 }
124
125 std::pair<aura::Window*, gfx::Rect> ViewElement::GetNodeWindowAndBounds() {
126 if (view_)
127 return std::make_pair(view_->GetWidget()->GetNativeWindow(),
128 view_->bounds());
129 return std::make_pair(nullptr, gfx::Rect());
130 }
131
132 // static
133 views::View* ViewElement::From(UIElement* element) {
134 DCHECK_EQ(UIElementType::VIEW, element->GetType());
135 return static_cast<ViewElement*>(element)->view_;
136 }
137
138 } // namespace devtools
139 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698