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

Side by Side Diff: components/ui_devtools/views/ui_devtools_dom_agent.cc

Issue 2899783002: Move DevTools out of ash and turn it to a component. (Closed)
Patch Set: add README.md Created 3 years, 6 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ash/devtools/ash_devtools_dom_agent.h" 5 #include "components/ui_devtools/views/ui_devtools_dom_agent.h"
6 6
7 #include "ash/devtools/ui_element.h"
8 #include "ash/devtools/view_element.h"
9 #include "ash/devtools/widget_element.h"
10 #include "ash/devtools/window_element.h"
11 #include "ash/public/cpp/shell_window_ids.h"
12 #include "components/ui_devtools/devtools_server.h" 7 #include "components/ui_devtools/devtools_server.h"
8 #include "components/ui_devtools/views/ui_element.h"
9 #include "components/ui_devtools/views/view_element.h"
10 #include "components/ui_devtools/views/widget_element.h"
11 #include "components/ui_devtools/views/window_element.h"
13 #include "third_party/skia/include/core/SkColor.h" 12 #include "third_party/skia/include/core/SkColor.h"
14 #include "ui/aura/client/screen_position_client.h" 13 #include "ui/aura/client/screen_position_client.h"
15 #include "ui/aura/env.h" 14 #include "ui/aura/env.h"
16 #include "ui/aura/window.h" 15 #include "ui/aura/window.h"
17 #include "ui/aura/window_tree_host.h" 16 #include "ui/aura/window_tree_host.h"
18 #include "ui/display/display.h" 17 #include "ui/display/display.h"
19 #include "ui/display/screen.h" 18 #include "ui/display/screen.h"
20 #include "ui/views/background.h" 19 #include "ui/views/background.h"
21 #include "ui/views/border.h" 20 #include "ui/views/border.h"
22 #include "ui/views/view.h" 21 #include "ui/views/view.h"
23 #include "ui/views/widget/widget.h" 22 #include "ui/views/widget/widget.h"
24 #include "ui/wm/core/window_util.h" 23 #include "ui/wm/core/window_util.h"
25 24
26 namespace ash { 25 namespace ui_devtools {
27 namespace devtools {
28 namespace { 26 namespace {
29 27
30 using namespace ui::devtools::protocol; 28 using namespace ui_devtools::protocol;
31 // TODO(mhashmi): Make ids reusable 29 // TODO(mhashmi): Make ids reusable
32 30
33 std::unique_ptr<DOM::Node> BuildNode( 31 std::unique_ptr<DOM::Node> BuildNode(
34 const std::string& name, 32 const std::string& name,
35 std::unique_ptr<Array<std::string>> attributes, 33 std::unique_ptr<Array<std::string>> attributes,
36 std::unique_ptr<Array<DOM::Node>> children, 34 std::unique_ptr<Array<DOM::Node>> children,
37 int node_ids) { 35 int node_ids) {
38 constexpr int kDomElementNodeType = 1; 36 constexpr int kDomElementNodeType = 1;
39 std::unique_ptr<DOM::Node> node = DOM::Node::create() 37 std::unique_ptr<DOM::Node> node = DOM::Node::create()
40 .setNodeId(node_ids) 38 .setNodeId(node_ids)
41 .setNodeName(name) 39 .setNodeName(name)
42 .setNodeType(kDomElementNodeType) 40 .setNodeType(kDomElementNodeType)
43 .setAttributes(std::move(attributes)) 41 .setAttributes(std::move(attributes))
44 .build(); 42 .build();
45 node->setChildNodeCount(children->length()); 43 node->setChildNodeCount(static_cast<int>(children->length()));
46 node->setChildren(std::move(children)); 44 node->setChildren(std::move(children));
47 return node; 45 return node;
48 } 46 }
49 47
50 // TODO(thanhph): Move this function to UIElement::GetAttributes(). 48 // TODO(thanhph): Move this function to UIElement::GetAttributes().
51 std::unique_ptr<Array<std::string>> GetAttributes(UIElement* ui_element) { 49 std::unique_ptr<Array<std::string>> GetAttributes(UIElement* ui_element) {
52 std::unique_ptr<Array<std::string>> attributes = Array<std::string>::create(); 50 std::unique_ptr<Array<std::string>> attributes = Array<std::string>::create();
53 attributes->addItem("name"); 51 attributes->addItem("name");
54 switch (ui_element->type()) { 52 switch (ui_element->type()) {
55 case UIElementType::WINDOW: { 53 case UIElementType::WINDOW: {
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 for (auto* it : root->children()) 102 for (auto* it : root->children())
105 children->addItem(BuildDomNodeFromUIElement(it)); 103 children->addItem(BuildDomNodeFromUIElement(it));
106 104
107 constexpr int kDomElementNodeType = 1; 105 constexpr int kDomElementNodeType = 1;
108 std::unique_ptr<DOM::Node> node = DOM::Node::create() 106 std::unique_ptr<DOM::Node> node = DOM::Node::create()
109 .setNodeId(root->node_id()) 107 .setNodeId(root->node_id())
110 .setNodeName(root->GetTypeName()) 108 .setNodeName(root->GetTypeName())
111 .setNodeType(kDomElementNodeType) 109 .setNodeType(kDomElementNodeType)
112 .setAttributes(GetAttributes(root)) 110 .setAttributes(GetAttributes(root))
113 .build(); 111 .build();
114 node->setChildNodeCount(children->length()); 112 node->setChildNodeCount(static_cast<int>(children->length()));
115 node->setChildren(std::move(children)); 113 node->setChildren(std::move(children));
116 return node; 114 return node;
117 } 115 }
118 116
119 } // namespace 117 } // namespace
120 118
121 AshDevToolsDOMAgent::AshDevToolsDOMAgent() : is_building_tree_(false) { 119 UIDevToolsDOMAgent::UIDevToolsDOMAgent() : is_building_tree_(false) {
122 aura::Env::GetInstance()->AddObserver(this); 120 aura::Env::GetInstance()->AddObserver(this);
123 } 121 }
124 122
125 AshDevToolsDOMAgent::~AshDevToolsDOMAgent() { 123 UIDevToolsDOMAgent::~UIDevToolsDOMAgent() {
126 aura::Env::GetInstance()->RemoveObserver(this); 124 aura::Env::GetInstance()->RemoveObserver(this);
127 Reset(); 125 Reset();
128 } 126 }
129 127
130 ui::devtools::protocol::Response AshDevToolsDOMAgent::disable() { 128 ui_devtools::protocol::Response UIDevToolsDOMAgent::disable() {
131 Reset(); 129 Reset();
132 return ui::devtools::protocol::Response::OK(); 130 return ui_devtools::protocol::Response::OK();
133 } 131 }
134 132
135 ui::devtools::protocol::Response AshDevToolsDOMAgent::getDocument( 133 ui_devtools::protocol::Response UIDevToolsDOMAgent::getDocument(
136 std::unique_ptr<ui::devtools::protocol::DOM::Node>* out_root) { 134 std::unique_ptr<ui_devtools::protocol::DOM::Node>* out_root) {
137 *out_root = BuildInitialTree(); 135 *out_root = BuildInitialTree();
138 return ui::devtools::protocol::Response::OK(); 136 return ui_devtools::protocol::Response::OK();
139 } 137 }
140 138
141 ui::devtools::protocol::Response AshDevToolsDOMAgent::highlightNode( 139 ui_devtools::protocol::Response UIDevToolsDOMAgent::highlightNode(
142 std::unique_ptr<ui::devtools::protocol::DOM::HighlightConfig> 140 std::unique_ptr<ui_devtools::protocol::DOM::HighlightConfig>
143 highlight_config, 141 highlight_config,
144 ui::devtools::protocol::Maybe<int> node_id) { 142 ui_devtools::protocol::Maybe<int> node_id) {
145 return HighlightNode(std::move(highlight_config), node_id.fromJust()); 143 return HighlightNode(std::move(highlight_config), node_id.fromJust());
146 } 144 }
147 145
148 ui::devtools::protocol::Response AshDevToolsDOMAgent::hideHighlight() { 146 ui_devtools::protocol::Response UIDevToolsDOMAgent::hideHighlight() {
149 if (widget_for_highlighting_ && widget_for_highlighting_->IsVisible()) 147 if (widget_for_highlighting_ && widget_for_highlighting_->IsVisible())
150 widget_for_highlighting_->Hide(); 148 widget_for_highlighting_->Hide();
151 return ui::devtools::protocol::Response::OK(); 149 return ui_devtools::protocol::Response::OK();
152 } 150 }
153 151
154 void AshDevToolsDOMAgent::OnUIElementAdded(UIElement* parent, 152 void UIDevToolsDOMAgent::OnUIElementAdded(UIElement* parent, UIElement* child) {
155 UIElement* child) {
156 // When parent is null, only need to update |node_id_to_ui_element_|. 153 // When parent is null, only need to update |node_id_to_ui_element_|.
157 if (!parent) { 154 if (!parent) {
158 node_id_to_ui_element_[child->node_id()] = child; 155 node_id_to_ui_element_[child->node_id()] = child;
159 return; 156 return;
160 } 157 }
161 // If tree is being built, don't add child to dom tree again. 158 // If tree is being built, don't add child to dom tree again.
162 if (is_building_tree_) 159 if (is_building_tree_)
163 return; 160 return;
164 DCHECK(node_id_to_ui_element_.count(parent->node_id())); 161 DCHECK(node_id_to_ui_element_.count(parent->node_id()));
165 162
166 const auto& children = parent->children(); 163 const auto& children = parent->children();
167 auto iter = std::find(children.begin(), children.end(), child); 164 auto iter = std::find(children.begin(), children.end(), child);
168 int prev_node_id = 165 int prev_node_id =
169 (iter == children.end() - 1) ? 0 : (*std::next(iter))->node_id(); 166 (iter == children.end() - 1) ? 0 : (*std::next(iter))->node_id();
170 frontend()->childNodeInserted(parent->node_id(), prev_node_id, 167 frontend()->childNodeInserted(parent->node_id(), prev_node_id,
171 BuildTreeForUIElement(child)); 168 BuildTreeForUIElement(child));
172 } 169 }
173 170
174 void AshDevToolsDOMAgent::OnUIElementReordered(UIElement* parent, 171 void UIDevToolsDOMAgent::OnUIElementReordered(UIElement* parent,
175 UIElement* child) { 172 UIElement* child) {
176 DCHECK(node_id_to_ui_element_.count(parent->node_id())); 173 DCHECK(node_id_to_ui_element_.count(parent->node_id()));
177 174
178 const auto& children = parent->children(); 175 const auto& children = parent->children();
179 auto iter = std::find(children.begin(), children.end(), child); 176 auto iter = std::find(children.begin(), children.end(), child);
180 int prev_node_id = 177 int prev_node_id =
181 (iter == children.begin()) ? 0 : (*std::prev(iter))->node_id(); 178 (iter == children.begin()) ? 0 : (*std::prev(iter))->node_id();
182 RemoveDomNode(child); 179 RemoveDomNode(child);
183 frontend()->childNodeInserted(parent->node_id(), prev_node_id, 180 frontend()->childNodeInserted(parent->node_id(), prev_node_id,
184 BuildDomNodeFromUIElement(child)); 181 BuildDomNodeFromUIElement(child));
185 } 182 }
186 183
187 void AshDevToolsDOMAgent::OnUIElementRemoved(UIElement* ui_element) { 184 void UIDevToolsDOMAgent::OnUIElementRemoved(UIElement* ui_element) {
188 DCHECK(node_id_to_ui_element_.count(ui_element->node_id())); 185 DCHECK(node_id_to_ui_element_.count(ui_element->node_id()));
189 186
190 RemoveDomNode(ui_element); 187 RemoveDomNode(ui_element);
191 node_id_to_ui_element_.erase(ui_element->node_id()); 188 node_id_to_ui_element_.erase(ui_element->node_id());
192 } 189 }
193 190
194 void AshDevToolsDOMAgent::OnUIElementBoundsChanged(UIElement* ui_element) { 191 void UIDevToolsDOMAgent::OnUIElementBoundsChanged(UIElement* ui_element) {
195 for (auto& observer : observers_) 192 for (auto& observer : observers_)
196 observer.OnNodeBoundsChanged(ui_element->node_id()); 193 observer.OnNodeBoundsChanged(ui_element->node_id());
197 } 194 }
198 195
199 bool AshDevToolsDOMAgent::IsHighlightingWindow(aura::Window* window) { 196 bool UIDevToolsDOMAgent::IsHighlightingWindow(aura::Window* window) {
200 return widget_for_highlighting_ && 197 return widget_for_highlighting_ &&
201 GetWidgetFromWindow(window) == widget_for_highlighting_.get(); 198 GetWidgetFromWindow(window) == widget_for_highlighting_.get();
202 } 199 }
203 200
204 void AshDevToolsDOMAgent::AddObserver(AshDevToolsDOMAgentObserver* observer) { 201 void UIDevToolsDOMAgent::AddObserver(UIDevToolsDOMAgentObserver* observer) {
205 observers_.AddObserver(observer); 202 observers_.AddObserver(observer);
206 } 203 }
207 204
208 void AshDevToolsDOMAgent::RemoveObserver( 205 void UIDevToolsDOMAgent::RemoveObserver(UIDevToolsDOMAgentObserver* observer) {
209 AshDevToolsDOMAgentObserver* observer) {
210 observers_.RemoveObserver(observer); 206 observers_.RemoveObserver(observer);
211 } 207 }
212 208
213 UIElement* AshDevToolsDOMAgent::GetElementFromNodeId(int node_id) { 209 UIElement* UIDevToolsDOMAgent::GetElementFromNodeId(int node_id) {
214 return node_id_to_ui_element_[node_id]; 210 return node_id_to_ui_element_[node_id];
215 } 211 }
216 212
217 void AshDevToolsDOMAgent::OnHostInitialized(aura::WindowTreeHost* host) { 213 void UIDevToolsDOMAgent::OnHostInitialized(aura::WindowTreeHost* host) {
218 root_windows_.push_back(host->window()); 214 root_windows_.push_back(host->window());
219 } 215 }
220 216
221 void AshDevToolsDOMAgent::OnNodeBoundsChanged(int node_id) { 217 void UIDevToolsDOMAgent::OnNodeBoundsChanged(int node_id) {
222 for (auto& observer : observers_) 218 for (auto& observer : observers_)
223 observer.OnNodeBoundsChanged(node_id); 219 observer.OnNodeBoundsChanged(node_id);
224 } 220 }
225 221
226 std::unique_ptr<ui::devtools::protocol::DOM::Node> 222 std::unique_ptr<ui_devtools::protocol::DOM::Node>
227 AshDevToolsDOMAgent::BuildInitialTree() { 223 UIDevToolsDOMAgent::BuildInitialTree() {
228 is_building_tree_ = true; 224 is_building_tree_ = true;
229 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create(); 225 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create();
230 226
231 // TODO(thanhph): Root of UIElement tree shoudn't be WindowElement 227 // TODO(thanhph): Root of UIElement tree shoudn't be WindowElement
232 // but maybe a new different element type. 228 // but maybe a new different element type.
233 window_element_root_ = 229 window_element_root_ =
234 base::MakeUnique<WindowElement>(nullptr, this, nullptr); 230 base::MakeUnique<WindowElement>(nullptr, this, nullptr);
235 231
236 for (aura::Window* window : root_windows()) { 232 for (aura::Window* window : root_windows()) {
237 UIElement* window_element = 233 UIElement* window_element =
238 new WindowElement(window, this, window_element_root_.get()); 234 new WindowElement(window, this, window_element_root_.get());
239 235
240 children->addItem(BuildTreeForUIElement(window_element)); 236 children->addItem(BuildTreeForUIElement(window_element));
241 window_element_root_->AddChild(window_element); 237 window_element_root_->AddChild(window_element);
242 } 238 }
243 std::unique_ptr<ui::devtools::protocol::DOM::Node> root_node = BuildNode( 239 std::unique_ptr<ui_devtools::protocol::DOM::Node> root_node = BuildNode(
244 "root", nullptr, std::move(children), window_element_root_->node_id()); 240 "root", nullptr, std::move(children), window_element_root_->node_id());
245 is_building_tree_ = false; 241 is_building_tree_ = false;
246 return root_node; 242 return root_node;
247 } 243 }
248 244
249 std::unique_ptr<ui::devtools::protocol::DOM::Node> 245 std::unique_ptr<ui_devtools::protocol::DOM::Node>
250 AshDevToolsDOMAgent::BuildTreeForUIElement(UIElement* ui_element) { 246 UIDevToolsDOMAgent::BuildTreeForUIElement(UIElement* ui_element) {
251 if (ui_element->type() == UIElementType::WINDOW) { 247 if (ui_element->type() == UIElementType::WINDOW) {
252 return BuildTreeForWindow( 248 return BuildTreeForWindow(
253 ui_element, 249 ui_element,
254 UIElement::GetBackingElement<aura::Window, WindowElement>(ui_element)); 250 UIElement::GetBackingElement<aura::Window, WindowElement>(ui_element));
255 } else if (ui_element->type() == UIElementType::WIDGET) { 251 } else if (ui_element->type() == UIElementType::WIDGET) {
256 return BuildTreeForRootWidget( 252 return BuildTreeForRootWidget(
257 ui_element, 253 ui_element,
258 UIElement::GetBackingElement<views::Widget, WidgetElement>(ui_element)); 254 UIElement::GetBackingElement<views::Widget, WidgetElement>(ui_element));
259 } else if (ui_element->type() == UIElementType::VIEW) { 255 } else if (ui_element->type() == UIElementType::VIEW) {
260 return BuildTreeForView( 256 return BuildTreeForView(
261 ui_element, 257 ui_element,
262 UIElement::GetBackingElement<views::View, ViewElement>(ui_element)); 258 UIElement::GetBackingElement<views::View, ViewElement>(ui_element));
263 } 259 }
264 return nullptr; 260 return nullptr;
265 } 261 }
266 262
267 std::unique_ptr<DOM::Node> AshDevToolsDOMAgent::BuildTreeForWindow( 263 std::unique_ptr<DOM::Node> UIDevToolsDOMAgent::BuildTreeForWindow(
268 UIElement* window_element_root, 264 UIElement* window_element_root,
269 aura::Window* window) { 265 aura::Window* window) {
270 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create(); 266 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create();
271 views::Widget* widget = GetWidgetFromWindow(window); 267 views::Widget* widget = GetWidgetFromWindow(window);
272 if (widget) { 268 if (widget) {
273 UIElement* widget_element = 269 UIElement* widget_element =
274 new WidgetElement(widget, this, window_element_root); 270 new WidgetElement(widget, this, window_element_root);
275 271
276 children->addItem(BuildTreeForRootWidget(widget_element, widget)); 272 children->addItem(BuildTreeForRootWidget(widget_element, widget));
277 window_element_root->AddChild(widget_element); 273 window_element_root->AddChild(widget_element);
278 } 274 }
279 for (aura::Window* child : window->children()) { 275 for (aura::Window* child : window->children()) {
280 UIElement* window_element = 276 UIElement* window_element =
281 new WindowElement(child, this, window_element_root); 277 new WindowElement(child, this, window_element_root);
282 278
283 children->addItem(BuildTreeForWindow(window_element, child)); 279 children->addItem(BuildTreeForWindow(window_element, child));
284 window_element_root->AddChild(window_element); 280 window_element_root->AddChild(window_element);
285 } 281 }
286 std::unique_ptr<ui::devtools::protocol::DOM::Node> node = 282 std::unique_ptr<ui_devtools::protocol::DOM::Node> node =
287 BuildNode("Window", GetAttributes(window_element_root), 283 BuildNode("Window", GetAttributes(window_element_root),
288 std::move(children), window_element_root->node_id()); 284 std::move(children), window_element_root->node_id());
289 return node; 285 return node;
290 } 286 }
291 287
292 std::unique_ptr<DOM::Node> AshDevToolsDOMAgent::BuildTreeForRootWidget( 288 std::unique_ptr<DOM::Node> UIDevToolsDOMAgent::BuildTreeForRootWidget(
293 UIElement* widget_element, 289 UIElement* widget_element,
294 views::Widget* widget) { 290 views::Widget* widget) {
295 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create(); 291 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create();
296 292
297 UIElement* view_element = 293 UIElement* view_element =
298 new ViewElement(widget->GetRootView(), this, widget_element); 294 new ViewElement(widget->GetRootView(), this, widget_element);
299 295
300 children->addItem(BuildTreeForView(view_element, widget->GetRootView())); 296 children->addItem(BuildTreeForView(view_element, widget->GetRootView()));
301 widget_element->AddChild(view_element); 297 widget_element->AddChild(view_element);
302 298
303 std::unique_ptr<ui::devtools::protocol::DOM::Node> node = 299 std::unique_ptr<ui_devtools::protocol::DOM::Node> node =
304 BuildNode("Widget", GetAttributes(widget_element), std::move(children), 300 BuildNode("Widget", GetAttributes(widget_element), std::move(children),
305 widget_element->node_id()); 301 widget_element->node_id());
306 return node; 302 return node;
307 } 303 }
308 304
309 std::unique_ptr<DOM::Node> AshDevToolsDOMAgent::BuildTreeForView( 305 std::unique_ptr<DOM::Node> UIDevToolsDOMAgent::BuildTreeForView(
310 UIElement* view_element, 306 UIElement* view_element,
311 views::View* view) { 307 views::View* view) {
312 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create(); 308 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create();
313 309
314 for (auto* child : view->GetChildrenInZOrder()) { 310 for (auto* child : view->GetChildrenInZOrder()) {
315 UIElement* view_element_child = new ViewElement(child, this, view_element); 311 UIElement* view_element_child = new ViewElement(child, this, view_element);
316 312
317 children->addItem(BuildTreeForView(view_element_child, child)); 313 children->addItem(BuildTreeForView(view_element_child, child));
318 view_element->AddChild(view_element_child); 314 view_element->AddChild(view_element_child);
319 } 315 }
320 std::unique_ptr<ui::devtools::protocol::DOM::Node> node = 316 std::unique_ptr<ui_devtools::protocol::DOM::Node> node =
321 BuildNode("View", GetAttributes(view_element), std::move(children), 317 BuildNode("View", GetAttributes(view_element), std::move(children),
322 view_element->node_id()); 318 view_element->node_id());
323 return node; 319 return node;
324 } 320 }
325 321
326 void AshDevToolsDOMAgent::RemoveDomNode(UIElement* ui_element) { 322 void UIDevToolsDOMAgent::RemoveDomNode(UIElement* ui_element) {
327 for (auto* child_element : ui_element->children()) 323 for (auto* child_element : ui_element->children())
328 RemoveDomNode(child_element); 324 RemoveDomNode(child_element);
329 frontend()->childNodeRemoved(ui_element->parent()->node_id(), 325 frontend()->childNodeRemoved(ui_element->parent()->node_id(),
330 ui_element->node_id()); 326 ui_element->node_id());
331 } 327 }
332 328
333 void AshDevToolsDOMAgent::Reset() { 329 void UIDevToolsDOMAgent::Reset() {
334 is_building_tree_ = false; 330 is_building_tree_ = false;
335 widget_for_highlighting_.reset(); 331 widget_for_highlighting_.reset();
336 window_element_root_.reset(); 332 window_element_root_.reset();
337 node_id_to_ui_element_.clear(); 333 node_id_to_ui_element_.clear();
338 observers_.Clear(); 334 observers_.Clear();
339 } 335 }
340 336
341 void AshDevToolsDOMAgent::InitializeHighlightingWidget() { 337 void UIDevToolsDOMAgent::InitializeHighlightingWidget() {
342 DCHECK(!widget_for_highlighting_); 338 DCHECK(!widget_for_highlighting_);
343 widget_for_highlighting_.reset(new views::Widget); 339 widget_for_highlighting_.reset(new views::Widget);
344 views::Widget::InitParams params; 340 views::Widget::InitParams params;
345 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS; 341 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS;
346 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO; 342 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO;
347 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 343 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
348 params.opacity = views::Widget::InitParams::WindowOpacity::TRANSLUCENT_WINDOW; 344 params.opacity = views::Widget::InitParams::WindowOpacity::TRANSLUCENT_WINDOW;
349 params.name = "HighlightingWidget"; 345 params.name = "HighlightingWidget";
346 params.parent = root_windows()[0];
350 params.keep_on_top = true; 347 params.keep_on_top = true;
351 params.accept_events = false; 348 params.accept_events = false;
352 widget_for_highlighting_->Init(params); 349 widget_for_highlighting_->Init(params);
353 } 350 }
354 351
355 void AshDevToolsDOMAgent::UpdateHighlight( 352 void UIDevToolsDOMAgent::UpdateHighlight(
356 const std::pair<aura::Window*, gfx::Rect>& window_and_bounds, 353 const std::pair<aura::Window*, gfx::Rect>& window_and_bounds,
357 SkColor background, 354 SkColor background,
358 SkColor border) { 355 SkColor border) {
359 constexpr int kBorderThickness = 1; 356 constexpr int kBorderThickness = 1;
360 views::View* root_view = widget_for_highlighting_->GetRootView(); 357 views::View* root_view = widget_for_highlighting_->GetRootView();
361 root_view->SetBorder(views::CreateSolidBorder(kBorderThickness, border)); 358 root_view->SetBorder(views::CreateSolidBorder(kBorderThickness, border));
362 root_view->SetBackground(views::CreateSolidBackground(background)); 359 root_view->SetBackground(views::CreateSolidBackground(background));
363 display::Display display = 360 display::Display display =
364 display::Screen::GetScreen()->GetDisplayNearestWindow( 361 display::Screen::GetScreen()->GetDisplayNearestWindow(
365 window_and_bounds.first); 362 window_and_bounds.first);
366 aura::Window* root = window_and_bounds.first->GetRootWindow(); 363 aura::Window* root = window_and_bounds.first->GetRootWindow();
367 if (root != widget_for_highlighting_->GetNativeWindow()->GetRootWindow()) 364 if (root != widget_for_highlighting_->GetNativeWindow()->GetRootWindow())
368 root->AddChild(widget_for_highlighting_->GetNativeWindow()); 365 root->AddChild(widget_for_highlighting_->GetNativeWindow());
369 366
370 aura::client::ScreenPositionClient* screen_position_client = 367 aura::client::ScreenPositionClient* screen_position_client =
371 aura::client::GetScreenPositionClient(root); 368 aura::client::GetScreenPositionClient(root);
372 369
373 gfx::Rect bounds(window_and_bounds.second); 370 gfx::Rect bounds(window_and_bounds.second);
374 gfx::Point origin = bounds.origin(); 371 gfx::Point origin = bounds.origin();
375 screen_position_client->ConvertPointFromScreen(root, &origin); 372 screen_position_client->ConvertPointFromScreen(root, &origin);
376 bounds.set_origin(origin); 373 bounds.set_origin(origin);
377 widget_for_highlighting_->GetNativeWindow()->SetBounds(bounds); 374 widget_for_highlighting_->GetNativeWindow()->SetBounds(bounds);
378 } 375 }
379 376
380 ui::devtools::protocol::Response AshDevToolsDOMAgent::HighlightNode( 377 ui_devtools::protocol::Response UIDevToolsDOMAgent::HighlightNode(
381 std::unique_ptr<ui::devtools::protocol::DOM::HighlightConfig> 378 std::unique_ptr<ui_devtools::protocol::DOM::HighlightConfig>
382 highlight_config, 379 highlight_config,
383 int node_id) { 380 int node_id) {
384 if (!widget_for_highlighting_) 381 if (!widget_for_highlighting_)
385 InitializeHighlightingWidget(); 382 InitializeHighlightingWidget();
386 383
387 std::pair<aura::Window*, gfx::Rect> window_and_bounds = 384 std::pair<aura::Window*, gfx::Rect> window_and_bounds =
388 node_id_to_ui_element_.count(node_id) 385 node_id_to_ui_element_.count(node_id)
389 ? node_id_to_ui_element_[node_id]->GetNodeWindowAndBounds() 386 ? node_id_to_ui_element_[node_id]->GetNodeWindowAndBounds()
390 : std::make_pair<aura::Window*, gfx::Rect>(nullptr, gfx::Rect()); 387 : std::make_pair<aura::Window*, gfx::Rect>(nullptr, gfx::Rect());
391 388
392 if (!window_and_bounds.first) { 389 if (!window_and_bounds.first) {
393 return ui::devtools::protocol::Response::Error( 390 return ui_devtools::protocol::Response::Error("No node found with that id");
394 "No node found with that id");
395 } 391 }
396 SkColor border_color = 392 SkColor border_color =
397 RGBAToSkColor(highlight_config->getBorderColor(nullptr)); 393 RGBAToSkColor(highlight_config->getBorderColor(nullptr));
398 SkColor content_color = 394 SkColor content_color =
399 RGBAToSkColor(highlight_config->getContentColor(nullptr)); 395 RGBAToSkColor(highlight_config->getContentColor(nullptr));
400 UpdateHighlight(window_and_bounds, content_color, border_color); 396 UpdateHighlight(window_and_bounds, content_color, border_color);
401 397
402 if (!widget_for_highlighting_->IsVisible()) 398 if (!widget_for_highlighting_->IsVisible())
403 widget_for_highlighting_->Show(); 399 widget_for_highlighting_->Show();
404 400
405 return ui::devtools::protocol::Response::OK(); 401 return ui_devtools::protocol::Response::OK();
406 } 402 }
407 403
408 } // namespace devtools 404 } // namespace ui_devtools
409 } // namespace ash
OLDNEW
« no previous file with comments | « components/ui_devtools/views/ui_devtools_dom_agent.h ('k') | components/ui_devtools/views/ui_devtools_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698