Index: ash/devtools/window_element.cc |
diff --git a/ash/devtools/window_element.cc b/ash/devtools/window_element.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8465848fe69fb11eb2efaeac54cae4f513fb2711 |
--- /dev/null |
+++ b/ash/devtools/window_element.cc |
@@ -0,0 +1,146 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ash/devtools/window_element.h" |
+ |
+#include "ash/devtools/ui_element_delegate.h" |
+#include "ui/aura/window.h" |
+ |
+namespace ash { |
+namespace devtools { |
+ |
+WindowElement::WindowElement(WmWindow* window, |
+ UIElementDelegate* ui_element_delegate) |
+ : window_(window) { |
+ this->SetUIElementDelegate(ui_element_delegate); |
+ this->SetType(UIElementType::WINDOW); |
+ if (window_ && !window_->aura_window()->HasObserver(this)) |
+ window_->aura_window()->AddObserver(this); |
+} |
+ |
+WindowElement::WindowElement(WmWindow* window, int node_id) : window_(window) { |
+ this->SetType(UIElementType::WINDOW); |
+ this->SetNodeId(node_id); |
+ if (window_ && !window_->aura_window()->HasObserver(this)) |
+ window_->aura_window()->AddObserver(this); |
+} |
+ |
+WindowElement::~WindowElement() { |
+ if (window_ && window_->aura_window()->HasObserver(this)) |
+ window_->aura_window()->RemoveObserver(this); |
+} |
+ |
+WmWindow* WindowElement::window() { |
+ return window_; |
+} |
+ |
+int GetNodeIdFromWindow(devtools::UIElement* ui_element, WmWindow* window) { |
sadrul
2017/04/19 18:21:39
It doesn't look like we use this function?
thanhph
2017/04/24 15:56:50
I moved it to unittest.
|
+ for (auto* child : ui_element->GetChildren()) { |
+ if (child->GetType() == UIElementType::WINDOW && |
+ static_cast<devtools::WindowElement*>(child)->window() == window) { |
+ return child->GetNodeId(); |
+ } |
+ } |
+ for (auto* child : ui_element->GetChildren()) { |
+ if (child->GetType() == UIElementType::WINDOW) { |
+ int node_id = GetNodeIdFromWindow(child, window); |
+ if (node_id > 0) |
+ return node_id; |
+ } |
+ } |
+ return 0; |
+} |
+ |
+// Handles removing window_. |
+void WindowElement::OnWindowHierarchyChanging( |
+ const aura::WindowObserver::HierarchyChangeParams& params) { |
+ if (WmWindow::Get(params.target) == window_) |
+ GetUIElementDelegate()->OnUIElementRemoved(this->GetNodeId()); |
+} |
+ |
+// Handles adding window_. |
+void WindowElement::OnWindowHierarchyChanged( |
+ const aura::WindowObserver::HierarchyChangeParams& params) { |
+ if (window_ == WmWindow::Get(params.new_parent) && |
+ params.receiver == params.new_parent) { |
+ UIElement* child_window_element = |
+ new WindowElement(WmWindow::Get(params.target), GetUIElementDelegate()); |
+ GetUIElementDelegate()->OnUIElementAdded( |
+ this->GetNodeId(), child_window_element, this->GetChildren().end() - 1); |
+ } |
+} |
+ |
+void WindowElement::OnWindowStackingChanged(aura::Window* window) { |
+ if (this->window_ == WmWindow::Get(window)) { |
+ std::vector<UIElement*>::iterator prev_sibling_position = |
+ GetUIElementDelegate()->OnUIElementRemoved(this->GetNodeId()); |
+ GetUIElementDelegate()->OnUIElementAdded( |
+ this->GetParent()->GetNodeId(), |
+ new WindowElement(WmWindow::Get(window), GetUIElementDelegate()), |
+ prev_sibling_position); |
+ } |
+} |
+ |
+void WindowElement::OnWindowBoundsChanged(aura::Window* window, |
+ const gfx::Rect& old_bounds, |
+ const gfx::Rect& new_bounds) { |
+ if (this->window_ == WmWindow::Get(window)) |
+ GetUIElementDelegate()->OnUIElementBoundsChanged(this->GetNodeId()); |
+} |
+ |
+void WindowElement::Destroy() { |
+ this->~WindowElement(); |
+} |
+ |
+bool WindowElement::GetBounds(gfx::Rect* bounds) { |
+ if (window_) { |
+ *bounds = window_->GetBounds(); |
+ return true; |
+ } |
+ return false; |
+} |
+ |
+bool WindowElement::SetBounds(const gfx::Rect& bounds) { |
+ if (window_) { |
+ window_->SetBounds(bounds); |
+ return true; |
+ } |
+ return false; |
+} |
+ |
+bool WindowElement::GetVisible(bool* visible) { |
+ if (window_) { |
+ *visible = window_->IsVisible(); |
+ return true; |
+ } |
+ return false; |
+} |
+ |
+bool WindowElement::SetVisible(bool visible) { |
+ if (window_) { |
+ if (visible != window_->IsVisible()) { |
+ if (visible) |
+ window_->Show(); |
+ else |
+ window_->Hide(); |
+ } |
+ return true; |
+ } |
+ return false; |
+} |
+ |
+std::pair<WmWindow*, gfx::Rect> WindowElement::GetNodeWindowAndBounds() { |
+ if (window_) |
+ return std::make_pair(window_, window_->GetBoundsInScreen()); |
+ return std::make_pair(nullptr, gfx::Rect()); |
+} |
+ |
+// static |
+WmWindow* WindowElement::From(UIElement* element) { |
+ DCHECK_EQ(UIElementType::WINDOW, element->GetType()); |
+ return static_cast<WindowElement*>(element)->window_; |
+} |
+ |
+} // namespace devtools |
+} // namespace ash |