Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "ash/devtools/ash_devtools_dom_agent.h" |
| 6 | 6 |
| 7 #include "ash/devtools/ui_element.h" | 7 #include "ash/devtools/ui_element.h" |
| 8 #include "ash/devtools/view_element.h" | 8 #include "ash/devtools/view_element.h" |
| 9 #include "ash/devtools/widget_element.h" | 9 #include "ash/devtools/widget_element.h" |
| 10 #include "ash/devtools/window_element.h" | 10 #include "ash/devtools/window_element.h" |
| 11 #include "ash/public/cpp/shell_window_ids.h" | 11 #include "ash/public/cpp/shell_window_ids.h" |
| 12 #include "ash/root_window_controller.h" | |
| 13 #include "ash/shell.h" | |
| 14 #include "components/ui_devtools/devtools_server.h" | 12 #include "components/ui_devtools/devtools_server.h" |
| 15 #include "third_party/skia/include/core/SkColor.h" | 13 #include "third_party/skia/include/core/SkColor.h" |
| 14 #include "ui/aura/client/screen_position_client.h" | |
| 15 #include "ui/aura/env.h" | |
| 16 #include "ui/aura/window.h" | 16 #include "ui/aura/window.h" |
| 17 #include "ui/aura/window_tree_host.h" | |
| 17 #include "ui/display/display.h" | 18 #include "ui/display/display.h" |
| 19 #include "ui/display/screen.h" | |
| 18 #include "ui/views/background.h" | 20 #include "ui/views/background.h" |
| 19 #include "ui/views/border.h" | 21 #include "ui/views/border.h" |
| 20 #include "ui/views/view.h" | 22 #include "ui/views/view.h" |
| 21 #include "ui/views/widget/widget.h" | 23 #include "ui/views/widget/widget.h" |
| 22 #include "ui/wm/core/window_util.h" | 24 #include "ui/wm/core/window_util.h" |
| 23 | 25 |
| 24 namespace ash { | 26 namespace ash { |
| 25 namespace devtools { | 27 namespace devtools { |
| 26 namespace { | 28 namespace { |
| 27 | 29 |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 109 .setNodeType(kDomElementNodeType) | 111 .setNodeType(kDomElementNodeType) |
| 110 .setAttributes(GetAttributes(root)) | 112 .setAttributes(GetAttributes(root)) |
| 111 .build(); | 113 .build(); |
| 112 node->setChildNodeCount(children->length()); | 114 node->setChildNodeCount(children->length()); |
| 113 node->setChildren(std::move(children)); | 115 node->setChildren(std::move(children)); |
| 114 return node; | 116 return node; |
| 115 } | 117 } |
| 116 | 118 |
| 117 } // namespace | 119 } // namespace |
| 118 | 120 |
| 119 AshDevToolsDOMAgent::AshDevToolsDOMAgent() : is_building_tree_(false) {} | 121 AshDevToolsDOMAgent::AshDevToolsDOMAgent() : is_building_tree_(false) { |
| 122 aura::Env::GetInstance()->AddObserver(this); | |
| 123 } | |
| 120 | 124 |
| 121 AshDevToolsDOMAgent::~AshDevToolsDOMAgent() { | 125 AshDevToolsDOMAgent::~AshDevToolsDOMAgent() { |
| 126 aura::Env::GetInstance()->RemoveObserver(this); | |
| 122 Reset(); | 127 Reset(); |
| 123 } | 128 } |
| 124 | 129 |
| 125 ui::devtools::protocol::Response AshDevToolsDOMAgent::disable() { | 130 ui::devtools::protocol::Response AshDevToolsDOMAgent::disable() { |
| 126 Reset(); | 131 Reset(); |
| 127 return ui::devtools::protocol::Response::OK(); | 132 return ui::devtools::protocol::Response::OK(); |
| 128 } | 133 } |
| 129 | 134 |
| 130 ui::devtools::protocol::Response AshDevToolsDOMAgent::getDocument( | 135 ui::devtools::protocol::Response AshDevToolsDOMAgent::getDocument( |
| 131 std::unique_ptr<ui::devtools::protocol::DOM::Node>* out_root) { | 136 std::unique_ptr<ui::devtools::protocol::DOM::Node>* out_root) { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 202 | 207 |
| 203 void AshDevToolsDOMAgent::RemoveObserver( | 208 void AshDevToolsDOMAgent::RemoveObserver( |
| 204 AshDevToolsDOMAgentObserver* observer) { | 209 AshDevToolsDOMAgentObserver* observer) { |
| 205 observers_.RemoveObserver(observer); | 210 observers_.RemoveObserver(observer); |
| 206 } | 211 } |
| 207 | 212 |
| 208 UIElement* AshDevToolsDOMAgent::GetElementFromNodeId(int node_id) { | 213 UIElement* AshDevToolsDOMAgent::GetElementFromNodeId(int node_id) { |
| 209 return node_id_to_ui_element_[node_id]; | 214 return node_id_to_ui_element_[node_id]; |
| 210 } | 215 } |
| 211 | 216 |
| 217 void AshDevToolsDOMAgent::OnHostInitialized(aura::WindowTreeHost* host) { | |
| 218 root_windows_.push_back(host->window()); | |
| 219 LOG(ERROR) << "host->window()->GetBoundsInScreen():" | |
| 220 << host->window()->GetBoundsInScreen().ToString(); | |
| 221 } | |
| 222 | |
| 212 void AshDevToolsDOMAgent::OnNodeBoundsChanged(int node_id) { | 223 void AshDevToolsDOMAgent::OnNodeBoundsChanged(int node_id) { |
| 213 for (auto& observer : observers_) | 224 for (auto& observer : observers_) |
| 214 observer.OnNodeBoundsChanged(node_id); | 225 observer.OnNodeBoundsChanged(node_id); |
| 215 } | 226 } |
| 216 | 227 |
| 217 std::unique_ptr<ui::devtools::protocol::DOM::Node> | 228 std::unique_ptr<ui::devtools::protocol::DOM::Node> |
| 218 AshDevToolsDOMAgent::BuildInitialTree() { | 229 AshDevToolsDOMAgent::BuildInitialTree() { |
| 219 is_building_tree_ = true; | 230 is_building_tree_ = true; |
| 220 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create(); | 231 std::unique_ptr<Array<DOM::Node>> children = Array<DOM::Node>::create(); |
| 221 | 232 |
| 222 // TODO(thanhph): Root of UIElement tree shoudn't be WindowElement | 233 // TODO(thanhph): Root of UIElement tree shoudn't be WindowElement |
| 223 // but maybe a new different element type. | 234 // but maybe a new different element type. |
| 224 window_element_root_ = new WindowElement(nullptr, this, nullptr); | 235 window_element_root_ = new WindowElement(nullptr, this, nullptr); |
| 225 | 236 |
| 226 for (aura::Window* window : Shell::GetAllRootWindows()) { | 237 for (aura::Window* window : root_windows()) { |
| 227 UIElement* window_element = | 238 UIElement* window_element = |
| 228 new WindowElement(window, this, window_element_root_); | 239 new WindowElement(window, this, window_element_root_); |
| 229 | 240 |
| 230 children->addItem(BuildTreeForUIElement(window_element)); | 241 children->addItem(BuildTreeForUIElement(window_element)); |
| 231 window_element_root_->AddChild(window_element); | 242 window_element_root_->AddChild(window_element); |
| 232 } | 243 } |
| 233 std::unique_ptr<ui::devtools::protocol::DOM::Node> root_node = BuildNode( | 244 std::unique_ptr<ui::devtools::protocol::DOM::Node> root_node = BuildNode( |
| 234 "root", nullptr, std::move(children), window_element_root_->node_id()); | 245 "root", nullptr, std::move(children), window_element_root_->node_id()); |
| 235 is_building_tree_ = false; | 246 is_building_tree_ = false; |
| 236 return root_node; | 247 return root_node; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 321 } | 332 } |
| 322 | 333 |
| 323 void AshDevToolsDOMAgent::Reset() { | 334 void AshDevToolsDOMAgent::Reset() { |
| 324 is_building_tree_ = false; | 335 is_building_tree_ = false; |
| 325 widget_for_highlighting_.reset(); | 336 widget_for_highlighting_.reset(); |
| 326 delete window_element_root_; | 337 delete window_element_root_; |
| 327 node_id_to_ui_element_.clear(); | 338 node_id_to_ui_element_.clear(); |
| 328 observers_.Clear(); | 339 observers_.Clear(); |
| 329 } | 340 } |
| 330 | 341 |
| 331 void AshDevToolsDOMAgent::InitializeHighlightingWidget() { | 342 void AshDevToolsDOMAgent::InitializeHighlightingWidget(int root_window_index) { |
| 332 DCHECK(!widget_for_highlighting_); | 343 DCHECK(!widget_for_highlighting_); |
| 333 widget_for_highlighting_.reset(new views::Widget); | 344 widget_for_highlighting_.reset(new views::Widget); |
| 334 views::Widget::InitParams params; | 345 views::Widget::InitParams params; |
| 335 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS; | 346 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS; |
| 336 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO; | 347 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO; |
| 337 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 348 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 338 params.opacity = views::Widget::InitParams::WindowOpacity::TRANSLUCENT_WINDOW; | 349 params.opacity = views::Widget::InitParams::WindowOpacity::TRANSLUCENT_WINDOW; |
| 350 LOG(ERROR) << "root_window_index: " << root_window_index; | |
| 339 params.name = "HighlightingWidget"; | 351 params.name = "HighlightingWidget"; |
| 340 Shell::GetPrimaryRootWindowController() | 352 params.parent = root_windows()[0]; |
| 341 ->ConfigureWidgetInitParamsForContainer(widget_for_highlighting_.get(), | 353 DCHECK(root_windows().size()); |
| 342 kShellWindowId_OverlayContainer, | 354 // params.parent = |
| 343 ¶ms); | 355 // root_windows()[root_window_index]->GetChildById(kShellWindowId_OverlayConta iner);; |
| 356 // params.parent = root_windows()[root_window_index]; | |
| 344 params.keep_on_top = true; | 357 params.keep_on_top = true; |
| 345 params.accept_events = false; | 358 params.accept_events = false; |
| 346 widget_for_highlighting_->Init(params); | 359 widget_for_highlighting_->Init(params); |
| 347 } | 360 } |
| 348 | 361 |
| 349 void AshDevToolsDOMAgent::UpdateHighlight( | 362 void AshDevToolsDOMAgent::UpdateHighlight( |
| 350 const std::pair<aura::Window*, gfx::Rect>& window_and_bounds, | 363 const std::pair<aura::Window*, gfx::Rect>& window_and_bounds, |
| 351 SkColor background, | 364 SkColor background, |
| 352 SkColor border) { | 365 SkColor border) { |
| 353 constexpr int kBorderThickness = 1; | 366 constexpr int kBorderThickness = 1; |
| 354 views::View* root_view = widget_for_highlighting_->GetRootView(); | 367 views::View* root_view = widget_for_highlighting_->GetRootView(); |
| 355 root_view->SetBorder(views::CreateSolidBorder(kBorderThickness, border)); | 368 root_view->SetBorder(views::CreateSolidBorder(kBorderThickness, border)); |
| 356 root_view->set_background( | 369 root_view->set_background( |
| 357 views::Background::CreateSolidBackground(background)); | 370 views::Background::CreateSolidBackground(background)); |
| 358 display::Display display = | 371 display::Display display = |
| 359 display::Screen::GetScreen()->GetDisplayNearestWindow( | 372 display::Screen::GetScreen()->GetDisplayNearestWindow( |
| 360 window_and_bounds.first); | 373 window_and_bounds.first); |
| 361 widget_for_highlighting_->GetNativeWindow()->SetBoundsInScreen( | 374 aura::Window* root = window_and_bounds.first->GetRootWindow(); |
| 362 window_and_bounds.second, display); | 375 if (root != widget_for_highlighting_->GetNativeWindow()->GetRootWindow()) |
| 376 root->AddChild(widget_for_highlighting_->GetNativeWindow()); | |
| 377 | |
| 378 aura::client::ScreenPositionClient* screen_position_client = | |
| 379 aura::client::GetScreenPositionClient( | |
| 380 window_and_bounds.first->GetRootWindow()); | |
| 381 gfx::Point origin_in_screen = window_and_bounds.first->bounds().origin(); | |
| 382 screen_position_client->ConvertPointFromScreen(root, &origin_in_screen); | |
| 383 | |
| 384 gfx::Rect origin_in_window(origin_in_screen, | |
| 385 window_and_bounds.first->bounds().size()); | |
| 386 widget_for_highlighting_->GetNativeWindow()->SetBounds(origin_in_window); | |
|
sadrul
2017/05/26 20:09:56
gfx::Rect bounds = window_and_bounds.second;
gfx::
thanhph
2017/05/28 00:04:25
Done. I use gfx::Rect bounds(window_and_bounds.sec
| |
| 387 | |
| 388 LOG(ERROR) << "name: widget_for_highlighting_->GetNativeWindow(): " | |
| 389 << widget_for_highlighting_->GetNativeWindow() | |
| 390 ->GetRootWindow() | |
| 391 ->GetDebugInfo(); | |
| 392 LOG(ERROR) << "widget_for_highlighting_: " | |
| 393 << widget_for_highlighting_->GetNativeWindow() | |
| 394 ->GetBoundsInScreen() | |
| 395 .ToString(); | |
| 363 } | 396 } |
| 364 | 397 |
| 365 ui::devtools::protocol::Response AshDevToolsDOMAgent::HighlightNode( | 398 ui::devtools::protocol::Response AshDevToolsDOMAgent::HighlightNode( |
| 366 std::unique_ptr<ui::devtools::protocol::DOM::HighlightConfig> | 399 std::unique_ptr<ui::devtools::protocol::DOM::HighlightConfig> |
| 367 highlight_config, | 400 highlight_config, |
| 368 int node_id) { | 401 int node_id) { |
| 369 if (!widget_for_highlighting_) | 402 if (!widget_for_highlighting_) { |
| 370 InitializeHighlightingWidget(); | 403 LOG(ERROR) << "AshDevToolsDOMAgent::HighlightNode - root_window().size(): " |
| 371 | 404 << root_windows().size(); |
| 405 UIElement* ui_element = GetElementFromNodeId(node_id); | |
| 406 UIElement* child_element = nullptr; | |
| 407 while (ui_element->parent()) { | |
| 408 child_element = ui_element; | |
| 409 ui_element = ui_element->parent(); | |
| 410 } | |
| 411 DCHECK_EQ(UIElementType::WINDOW, child_element->type()); | |
| 412 aura::Window* window = | |
| 413 UIElement::GetBackingElement<aura::Window, WindowElement>( | |
| 414 child_element); | |
| 415 auto iter = std::find(root_windows().begin(), root_windows().end(), window); | |
| 416 DCHECK(iter != root_windows().end()); | |
| 417 InitializeHighlightingWidget(std::distance(root_windows().begin(), iter)); | |
| 418 } | |
| 372 std::pair<aura::Window*, gfx::Rect> window_and_bounds = | 419 std::pair<aura::Window*, gfx::Rect> window_and_bounds = |
| 373 node_id_to_ui_element_.count(node_id) | 420 node_id_to_ui_element_.count(node_id) |
| 374 ? node_id_to_ui_element_[node_id]->GetNodeWindowAndBounds() | 421 ? node_id_to_ui_element_[node_id]->GetNodeWindowAndBounds() |
| 375 : std::make_pair<aura::Window*, gfx::Rect>(nullptr, gfx::Rect()); | 422 : std::make_pair<aura::Window*, gfx::Rect>(nullptr, gfx::Rect()); |
| 376 | 423 LOG(ERROR) << "bounds: " << window_and_bounds.second.ToString(); |
| 424 LOG(ERROR) << "widget_for_highlighting_: " | |
| 425 << widget_for_highlighting_->GetWindowBoundsInScreen().ToString(); | |
| 377 if (!window_and_bounds.first) { | 426 if (!window_and_bounds.first) { |
| 378 return ui::devtools::protocol::Response::Error( | 427 return ui::devtools::protocol::Response::Error( |
| 379 "No node found with that id"); | 428 "No node found with that id"); |
| 380 } | 429 } |
| 381 SkColor border_color = | 430 SkColor border_color = |
| 382 RGBAToSkColor(highlight_config->getBorderColor(nullptr)); | 431 RGBAToSkColor(highlight_config->getBorderColor(nullptr)); |
| 383 SkColor content_color = | 432 SkColor content_color = |
| 384 RGBAToSkColor(highlight_config->getContentColor(nullptr)); | 433 RGBAToSkColor(highlight_config->getContentColor(nullptr)); |
| 385 UpdateHighlight(window_and_bounds, content_color, border_color); | 434 UpdateHighlight(window_and_bounds, content_color, border_color); |
| 386 | 435 |
| 387 if (!widget_for_highlighting_->IsVisible()) | 436 if (!widget_for_highlighting_->IsVisible()) |
| 388 widget_for_highlighting_->Show(); | 437 widget_for_highlighting_->Show(); |
| 389 | 438 |
| 390 return ui::devtools::protocol::Response::OK(); | 439 return ui::devtools::protocol::Response::OK(); |
| 391 } | 440 } |
| 392 | 441 |
| 393 } // namespace devtools | 442 } // namespace devtools |
| 394 } // namespace ash | 443 } // namespace ash |
| OLD | NEW |