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

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

Issue 2959263002: Show corresponding window/widget/view in UIElement tree when clicking on a UI element. (Closed)
Patch Set: add domain overlay Created 3 years, 5 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 "components/ui_devtools/views/ui_devtools_dom_agent.h" 5 #include "components/ui_devtools/views/ui_devtools_dom_agent.h"
6 6
7 #include "components/ui_devtools/devtools_server.h" 7 #include "components/ui_devtools/devtools_server.h"
8 #include "components/ui_devtools/views/ui_devtools_overlay_agent.h"
8 #include "components/ui_devtools/views/ui_element.h" 9 #include "components/ui_devtools/views/ui_element.h"
9 #include "components/ui_devtools/views/view_element.h" 10 #include "components/ui_devtools/views/view_element.h"
10 #include "components/ui_devtools/views/widget_element.h" 11 #include "components/ui_devtools/views/widget_element.h"
11 #include "components/ui_devtools/views/window_element.h" 12 #include "components/ui_devtools/views/window_element.h"
12 #include "third_party/skia/include/core/SkColor.h" 13 #include "third_party/skia/include/core/SkColor.h"
13 #include "ui/aura/client/screen_position_client.h" 14 #include "ui/aura/client/screen_position_client.h"
14 #include "ui/aura/env.h" 15 #include "ui/aura/env.h"
15 #include "ui/aura/window.h" 16 #include "ui/aura/window.h"
16 #include "ui/aura/window_tree_host.h" 17 #include "ui/aura/window_tree_host.h"
17 #include "ui/display/display.h" 18 #include "ui/display/display.h"
18 #include "ui/display/screen.h" 19 #include "ui/display/screen.h"
19 #include "ui/views/background.h" 20 #include "ui/views/background.h"
20 #include "ui/views/border.h" 21 #include "ui/views/border.h"
22 #include "ui/views/pointer_watcher.h"
21 #include "ui/views/view.h" 23 #include "ui/views/view.h"
22 #include "ui/views/widget/widget.h" 24 #include "ui/views/widget/widget.h"
23 #include "ui/wm/core/window_util.h" 25 #include "ui/wm/core/window_util.h"
24 26
25 namespace ui_devtools { 27 namespace ui_devtools {
26 namespace { 28 namespace {
27 29
28 using namespace ui_devtools::protocol; 30 using namespace ui_devtools::protocol;
29 // TODO(mhashmi): Make ids reusable 31 // TODO(mhashmi): Make ids reusable
30 32
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 .setNodeId(root->node_id()) 109 .setNodeId(root->node_id())
108 .setNodeName(root->GetTypeName()) 110 .setNodeName(root->GetTypeName())
109 .setNodeType(kDomElementNodeType) 111 .setNodeType(kDomElementNodeType)
110 .setAttributes(GetAttributes(root)) 112 .setAttributes(GetAttributes(root))
111 .build(); 113 .build();
112 node->setChildNodeCount(static_cast<int>(children->length())); 114 node->setChildNodeCount(static_cast<int>(children->length()));
113 node->setChildren(std::move(children)); 115 node->setChildren(std::move(children));
114 return node; 116 return node;
115 } 117 }
116 118
119 void FindElement(UIElement* root,
120 const int x,
121 const int y,
sadrul 2017/07/13 20:17:20 Take a const gfx::Point& as the location input.
thanhph 2017/07/14 16:59:46 Done.
122 int* min_bounds,
123 int* element_id) {
124 LOG(ERROR) << "FindElement root->node_id(): " << root->node_id();
125 gfx::Rect bounds;
126 for (auto* child : root->children()) {
127 child->GetBounds(&bounds);
128 if (bounds.x() <= x && bounds.y() <= y && bounds.right() >= x &&
129 bounds.bottom() >= y &&
sadrul 2017/07/13 20:17:20 Use gfx::Rect::Contains(gfx::Point)
thanhph 2017/07/14 16:59:46 Done.
130 (*min_bounds) > bounds.width() + bounds.height()) {
131 *element_id = child->node_id();
132 *min_bounds = bounds.width() + bounds.height();
sadrul 2017/07/13 20:17:20 I don't understand why you need |min_bounds| here.
thanhph 2017/07/14 16:59:46 Acknowledged. We just select the deepest element i
133 LOG(ERROR) << "*min_bounds: " << *min_bounds;
134 LOG(ERROR) << "*element_id: " << *element_id;
135 }
136 FindElement(child, x, y, min_bounds, element_id);
137 }
138 }
139
117 } // namespace 140 } // namespace
118 141
119 UIDevToolsDOMAgent::UIDevToolsDOMAgent() : is_building_tree_(false) { 142 UIDevToolsDOMAgent::UIDevToolsDOMAgent(UIDevToolsOverlayAgent* overlay_agent)
143 : overlay_agent_(overlay_agent), is_building_tree_(false) {
120 aura::Env::GetInstance()->AddObserver(this); 144 aura::Env::GetInstance()->AddObserver(this);
121 } 145 }
122 146
123 UIDevToolsDOMAgent::~UIDevToolsDOMAgent() { 147 UIDevToolsDOMAgent::~UIDevToolsDOMAgent() {
124 aura::Env::GetInstance()->RemoveObserver(this); 148 aura::Env::GetInstance()->RemoveObserver(this);
125 Reset(); 149 Reset();
126 } 150 }
127 151
128 ui_devtools::protocol::Response UIDevToolsDOMAgent::disable() { 152 ui_devtools::protocol::Response UIDevToolsDOMAgent::disable() {
129 Reset(); 153 Reset();
130 return ui_devtools::protocol::Response::OK(); 154 return ui_devtools::protocol::Response::OK();
131 } 155 }
132 156
133 ui_devtools::protocol::Response UIDevToolsDOMAgent::getDocument( 157 ui_devtools::protocol::Response UIDevToolsDOMAgent::getDocument(
134 std::unique_ptr<ui_devtools::protocol::DOM::Node>* out_root) { 158 std::unique_ptr<ui_devtools::protocol::DOM::Node>* out_root) {
135 *out_root = BuildInitialTree(); 159 *out_root = BuildInitialTree();
136 return ui_devtools::protocol::Response::OK(); 160 return ui_devtools::protocol::Response::OK();
137 } 161 }
138 162
139 ui_devtools::protocol::Response UIDevToolsDOMAgent::highlightNode( 163 ui_devtools::protocol::Response UIDevToolsDOMAgent::highlightNode(
140 std::unique_ptr<ui_devtools::protocol::DOM::HighlightConfig> 164 std::unique_ptr<ui_devtools::protocol::DOM::HighlightConfig>
141 highlight_config, 165 highlight_config,
142 ui_devtools::protocol::Maybe<int> node_id) { 166 ui_devtools::protocol::Maybe<int> node_id) {
143 return HighlightNode(std::move(highlight_config), node_id.fromJust()); 167 return HighlightNode(std::move(highlight_config), node_id.fromJust());
144 } 168 }
145 169
170 ui_devtools::protocol::Response UIDevToolsDOMAgent::setInspectMode(
171 ui_devtools::protocol::Maybe<String> mode,
172 std::unique_ptr<ui_devtools::protocol::DOM::HighlightConfig>
173 highlight_config,
174 ui_devtools::protocol::Maybe<int> node_id) {
175 LOG(ERROR) << __PRETTY_FUNCTION__;
176 if (mode.fromJust().compare("searchForUAShadowDOM") == 0)
177 aura::Env::GetInstance()->PrependPreTargetHandler(this);
178 else if (mode.fromJust().compare("none") == 0)
179 aura::Env::GetInstance()->RemovePreTargetHandler(this);
180
181 return ui_devtools::protocol::Response::OK();
182 }
183
146 ui_devtools::protocol::Response UIDevToolsDOMAgent::hideHighlight() { 184 ui_devtools::protocol::Response UIDevToolsDOMAgent::hideHighlight() {
147 if (widget_for_highlighting_ && widget_for_highlighting_->IsVisible()) 185 if (widget_for_highlighting_ && widget_for_highlighting_->IsVisible())
148 widget_for_highlighting_->Hide(); 186 widget_for_highlighting_->Hide();
149 return ui_devtools::protocol::Response::OK(); 187 return ui_devtools::protocol::Response::OK();
150 } 188 }
151 189
152 void UIDevToolsDOMAgent::OnUIElementAdded(UIElement* parent, UIElement* child) { 190 void UIDevToolsDOMAgent::OnUIElementAdded(UIElement* parent, UIElement* child) {
153 // When parent is null, only need to update |node_id_to_ui_element_|. 191 // When parent is null, only need to update |node_id_to_ui_element_|.
154 if (!parent) { 192 if (!parent) {
155 node_id_to_ui_element_[child->node_id()] = child; 193 node_id_to_ui_element_[child->node_id()] = child;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 void UIDevToolsDOMAgent::OnUIElementBoundsChanged(UIElement* ui_element) { 229 void UIDevToolsDOMAgent::OnUIElementBoundsChanged(UIElement* ui_element) {
192 for (auto& observer : observers_) 230 for (auto& observer : observers_)
193 observer.OnNodeBoundsChanged(ui_element->node_id()); 231 observer.OnNodeBoundsChanged(ui_element->node_id());
194 } 232 }
195 233
196 bool UIDevToolsDOMAgent::IsHighlightingWindow(aura::Window* window) { 234 bool UIDevToolsDOMAgent::IsHighlightingWindow(aura::Window* window) {
197 return widget_for_highlighting_ && 235 return widget_for_highlighting_ &&
198 GetWidgetFromWindow(window) == widget_for_highlighting_.get(); 236 GetWidgetFromWindow(window) == widget_for_highlighting_.get();
199 } 237 }
200 238
239 void UIDevToolsDOMAgent::OnMouseEvent(ui::MouseEvent* event) {
240 if (!window_element_root_)
241 return;
242
243 if (event->type() == ui::ET_MOUSE_PRESSED) {
244 // TODO(thanhph): needs to disable the highlighting inspection arrow.
245 frontend()->setInspectMode(Maybe<ui_devtools::String>("none"));
246 overlay_agent_->setInspectMode(
247 "none", protocol::Maybe<protocol::Overlay::HighlightConfig>());
248 aura::Env::GetInstance()->RemovePreTargetHandler(this);
249 return;
250 }
251 if (event->type() != ui::ET_MOUSEWHEEL)
252 return;
253
254 // maxmium of width + height of a screen.
255 int min_bounds = 1000000;
256
257 // Node id of the element whose bounds contain the mouse pointer location.
258 int element_id = 1;
259 FindElement(window_element_root_.get(), event->location().x(),
sadrul 2017/07/13 20:17:20 event->location() is in the coordinate space of |e
thanhph 2017/07/14 16:59:46 I picked the 1st approach.
260 event->location().y(), &min_bounds, &element_id);
261 LOG(ERROR) << "Find - event->location().x():" << event->location().x()
262 << "event->location().y(): " << event->location().y();
263 gfx::Rect bounds1;
264 node_id_to_ui_element_[element_id]->GetBounds(&bounds1);
265 LOG(ERROR) << "bounds1 node_id_to_ui_element_[element_id]: " << bounds1.x()
266 << " - " << bounds1.y() << " - " << bounds1.right() << " - "
267 << bounds1.bottom();
268 frontend()->nodeHighlightRequested(element_id);
269 }
270
201 void UIDevToolsDOMAgent::AddObserver(UIDevToolsDOMAgentObserver* observer) { 271 void UIDevToolsDOMAgent::AddObserver(UIDevToolsDOMAgentObserver* observer) {
202 observers_.AddObserver(observer); 272 observers_.AddObserver(observer);
203 } 273 }
204 274
205 void UIDevToolsDOMAgent::RemoveObserver(UIDevToolsDOMAgentObserver* observer) { 275 void UIDevToolsDOMAgent::RemoveObserver(UIDevToolsDOMAgentObserver* observer) {
206 observers_.RemoveObserver(observer); 276 observers_.RemoveObserver(observer);
207 } 277 }
208 278
209 UIElement* UIDevToolsDOMAgent::GetElementFromNodeId(int node_id) { 279 UIElement* UIDevToolsDOMAgent::GetElementFromNodeId(int node_id) {
210 return node_id_to_ui_element_[node_id]; 280 return node_id_to_ui_element_[node_id];
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 RGBAToSkColor(highlight_config->getContentColor(nullptr)); 465 RGBAToSkColor(highlight_config->getContentColor(nullptr));
396 UpdateHighlight(window_and_bounds, content_color, border_color); 466 UpdateHighlight(window_and_bounds, content_color, border_color);
397 467
398 if (!widget_for_highlighting_->IsVisible()) 468 if (!widget_for_highlighting_->IsVisible())
399 widget_for_highlighting_->Show(); 469 widget_for_highlighting_->Show();
400 470
401 return ui_devtools::protocol::Response::OK(); 471 return ui_devtools::protocol::Response::OK();
402 } 472 }
403 473
404 } // namespace ui_devtools 474 } // namespace ui_devtools
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