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

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, 8 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 namespace devtools {
13
14 ViewElement::ViewElement(views::View* view,
15 UIElementDelegate* ui_element_delegate,
16 UIElement* parent)
17 : UIElement(UIElementType::VIEW, ui_element_delegate, parent), view_(view) {
18 if (!view_->HasObserver(this))
19 view_->AddObserver(this);
20 }
21
22 ViewElement::~ViewElement() {
23 if (view_ && view_->HasObserver(this))
24 view_->RemoveObserver(this);
25 }
26
27 views::View* ViewElement::view() {
28 return view_;
29 }
30
31 void ViewElement::OnChildViewRemoved(views::View* parent, views::View* view) {
32 if (parent == view_) {
33 for (auto* child_element : GetChildren()) {
34 if (UIElement::GetBackingElement<views::View, ViewElement>(
35 child_element) == view &&
36 GetUIElementDelegate()->OnUIElementRemoved(
37 child_element->GetNodeId())) {
38 child_element->RemoveChildFromParent();
39 child_element->Destroy();
40 }
41 }
42 }
43 }
44
45 void ViewElement::OnChildViewAdded(views::View* parent, views::View* view) {
46 if (parent == view_) {
47 GetUIElementDelegate()->OnUIElementAdded(
48 GetNodeId(), new ViewElement(view, GetUIElementDelegate(), this),
49 GetChildren().end() - 1);
50 }
51 }
52
53 void ViewElement::OnChildViewReordered(views::View* parent, views::View* view) {
54 if (view == view_) {
55 std::vector<UIElement*>::iterator prev_sibling_position;
56 if (GetUIElementDelegate()->OnUIElementRemoved(GetNodeId()))
57 prev_sibling_position = RemoveChildFromParent();
58 else
59 prev_sibling_position = GetParent()->GetChildren().end();
60
61 GetUIElementDelegate()->OnUIElementAdded(
62 GetParent()->GetNodeId(),
63 new ViewElement(view, GetUIElementDelegate(), GetParent()),
64 prev_sibling_position);
65 Destroy();
66 }
67 }
68
69 void ViewElement::OnViewBoundsChanged(views::View* view) {
70 if (view_ == view)
71 GetUIElementDelegate()->OnUIElementBoundsChanged(GetNodeId());
72 }
73
74 void ViewElement::Destroy() {
75 delete this;
76 }
77
78 bool ViewElement::GetBounds(gfx::Rect* bounds) {
79 if (view_) {
80 *bounds = view_->bounds();
81 return true;
82 }
83 return false;
84 }
85
86 bool ViewElement::SetBounds(const gfx::Rect& bounds) {
87 if (view_) {
88 view_->SetBoundsRect(bounds);
89 return true;
90 }
91 return false;
92 }
93
94 bool ViewElement::GetVisible(bool* visible) {
95 if (view_) {
96 *visible = view_->visible();
97 return true;
98 }
99 return false;
100 }
101
102 bool ViewElement::SetVisible(bool visible) {
103 if (view_) {
104 if (visible != view_->visible())
105 view_->SetVisible(visible);
106 return true;
107 }
108 return false;
109 }
110
111 std::pair<aura::Window*, gfx::Rect> ViewElement::GetNodeWindowAndBounds() {
112 if (view_)
113 return std::make_pair(view_->GetWidget()->GetNativeWindow(),
114 view_->bounds());
115 return std::make_pair(nullptr, gfx::Rect());
116 }
117
118 // static
119 views::View* ViewElement::From(UIElement* element) {
120 DCHECK_EQ(UIElementType::VIEW, element->GetType());
121 return static_cast<ViewElement*>(element)->view_;
122 }
123
124 } // namespace devtools
125 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698