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

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 "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 view_->AddObserver(this);
37 }
38
39 ViewElement::~ViewElement() {
40 view_->RemoveObserver(this);
41 }
42
43 views::View* ViewElement::view() const {
44 return view_;
45 }
46
47 void ViewElement::OnChildViewRemoved(views::View* parent, views::View* view) {
48 DCHECK(parent == view_);
49 for (auto* child_element : GetChildren()) {
50 if (UIElement::GetBackingElement<views::View, ViewElement>(child_element) ==
51 view &&
52 delegate()->OnUIElementRemoved(child_element)) {
53 RemoveChild(child_element);
54 child_element->Destroy();
55 }
56 }
57 }
58
59 void ViewElement::OnChildViewAdded(views::View* parent, views::View* view) {
60 DCHECK(parent == view_);
61 delegate()->OnUIElementAdded(
62 this, new ViewElement(view, delegate(), this),
63 GetChildren().empty() ? nullptr : GetChildren().back());
64 }
65
66 void ViewElement::OnChildViewReordered(views::View* parent, views::View* view) {
67 DCHECK(view == view_);
68 GetParent()->RemoveChild(this);
69 UIElement* prev_sibling = GetParent()->ReorderChild(this, FindSibling(view));
70 delegate()->OnUIElementReordered(GetParent(), this, prev_sibling);
71 }
72
73 void ViewElement::OnViewBoundsChanged(views::View* view) {
74 DCHECK(view_ == view);
75 delegate()->OnUIElementBoundsChanged(this);
76 }
77
78 void ViewElement::Destroy() {
79 delete this;
80 }
81
82 void ViewElement::GetBounds(gfx::Rect* bounds) const {
83 *bounds = view_->bounds();
84 }
85
86 void ViewElement::SetBounds(const gfx::Rect& bounds) {
87 view_->SetBoundsRect(bounds);
88 }
89
90 void ViewElement::GetVisible(bool* visible) const {
91 *visible = view_->visible();
92 }
93
94 void ViewElement::SetVisible(bool visible) {
95 if (visible != view_->visible())
96 view_->SetVisible(visible);
97 }
98
99 std::pair<aura::Window*, gfx::Rect> ViewElement::GetNodeWindowAndBounds()
100 const {
101 return std::make_pair(view_->GetWidget()->GetNativeWindow(), view_->bounds());
102 }
103
104 // static
105 views::View* ViewElement::From(UIElement* element) {
106 DCHECK_EQ(UIElementType::VIEW, element->GetType());
107 return static_cast<ViewElement*>(element)->view_;
108 }
109
110 } // namespace devtools
111 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698