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

Side by Side Diff: ash/devtools/window_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
« ash/devtools/ui_element.h ('K') | « ash/devtools/window_element.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/window_element.h"
6
7 #include "ash/devtools/ui_element_delegate.h"
8 #include "ui/aura/window.h"
9
10 namespace ash {
11 namespace devtools {
12
13 namespace {
14
15 int FindSibling(aura::Window* window) {
16 const aura::Window::Windows& siblings = window->parent()->children();
17 auto it = std::find(siblings.begin(), siblings.end(), window);
18 DCHECK(it != siblings.end());
19 return std::distance(siblings.begin(), it);
20 }
21
22 } // namespace
23
24 WindowElement::WindowElement(aura::Window* window,
25 UIElementDelegate* ui_element_delegate,
26 UIElement* parent)
27 : UIElement(UIElementType::WINDOW, ui_element_delegate, parent),
28 window_(window) {
29 if (window_ && !window_->HasObserver(this))
30 window_->AddObserver(this);
31 }
32
33 WindowElement::~WindowElement() {
34 if (window_ && window_->HasObserver(this))
35 window_->RemoveObserver(this);
36 }
37
38 aura::Window* WindowElement::window() {
39 return window_;
40 }
41
42 // Handles removing window_.
43 void WindowElement::OnWindowHierarchyChanging(
44 const aura::WindowObserver::HierarchyChangeParams& params) {
45 if (params.target == window_ && delegate()->OnUIElementRemoved(GetNodeId())) {
46 RemoveChildFromParent();
47 Destroy();
48 }
49 }
50
51 // Handles adding window_.
52 void WindowElement::OnWindowHierarchyChanged(
53 const aura::WindowObserver::HierarchyChangeParams& params) {
54 if (window_ == params.new_parent && params.receiver == params.new_parent) {
55 delegate()->OnUIElementAdded(
56 this, new WindowElement(params.target, delegate(), this),
57 LastChildIterator() - 1);
58 }
59 }
60
61 void WindowElement::OnWindowStackingChanged(aura::Window* window) {
62 if (window_ == window) {
63 if (delegate()->IsHighlightingWindow(window))
64 return;
65 RemoveChildFromParent();
66
67 int i = FindSibling(window);
68 std::vector<UIElement*>::iterator new_sibling_position =
69 GetParent()->BeginChildIterator();
70 while (i-- > 0)
71 new_sibling_position++;
72
73 GetParent()->AddChild(new_sibling_position, this);
74 }
75 }
76
77 void WindowElement::OnWindowBoundsChanged(aura::Window* window,
78 const gfx::Rect& old_bounds,
79 const gfx::Rect& new_bounds) {
80 if (window_ == window)
81 delegate()->OnUIElementBoundsChanged(GetNodeId());
82 }
83
84 void WindowElement::Destroy() {
85 delete this;
86 }
87
88 bool WindowElement::GetBounds(gfx::Rect* bounds) {
89 if (window_) {
90 *bounds = window_->bounds();
91 return true;
92 }
93 return false;
94 }
95
96 bool WindowElement::SetBounds(const gfx::Rect& bounds) {
97 if (window_) {
98 window_->SetBounds(bounds);
99 return true;
100 }
101 return false;
102 }
103
104 bool WindowElement::GetVisible(bool* visible) {
105 if (window_) {
106 *visible = window_->IsVisible();
107 return true;
108 }
109 return false;
110 }
111
112 bool WindowElement::SetVisible(bool visible) {
113 if (window_) {
114 if (visible != window_->IsVisible()) {
115 if (visible)
116 window_->Show();
117 else
118 window_->Hide();
119 }
120 return true;
121 }
122 return false;
123 }
124
125 std::pair<aura::Window*, gfx::Rect> WindowElement::GetNodeWindowAndBounds() {
126 if (window_)
127 return std::make_pair(window_, window_->GetBoundsInScreen());
128 return std::make_pair(nullptr, gfx::Rect());
129 }
130
131 // static
132 aura::Window* WindowElement::From(UIElement* element) {
133 DCHECK_EQ(UIElementType::WINDOW, element->GetType());
134 return static_cast<WindowElement*>(element)->window_;
135 }
136
137 } // namespace devtools
138 } // namespace ash
OLDNEW
« ash/devtools/ui_element.h ('K') | « ash/devtools/window_element.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698