Index: ash/devtools/view_element.cc |
diff --git a/ash/devtools/view_element.cc b/ash/devtools/view_element.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..23b424350c12c907c145c2a825c8684aaf564da8 |
--- /dev/null |
+++ b/ash/devtools/view_element.cc |
@@ -0,0 +1,125 @@ |
+// 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/view_element.h" |
+ |
+#include "ash/devtools/ui_element_delegate.h" |
+#include "ash/wm_window.h" |
+#include "ui/views/widget/widget.h" |
+ |
+namespace ash { |
+namespace devtools { |
+ |
+ViewElement::ViewElement(views::View* view, |
+ UIElementDelegate* ui_element_delegate, |
+ UIElement* parent) |
+ : UIElement(UIElementType::VIEW, ui_element_delegate, parent), view_(view) { |
+ if (!view_->HasObserver(this)) |
+ view_->AddObserver(this); |
+} |
+ |
+ViewElement::~ViewElement() { |
+ if (view_ && view_->HasObserver(this)) |
+ view_->RemoveObserver(this); |
+} |
+ |
+views::View* ViewElement::view() { |
+ return view_; |
+} |
+ |
+void ViewElement::OnChildViewRemoved(views::View* parent, views::View* view) { |
+ if (parent == view_) { |
+ for (auto* child_element : GetChildren()) { |
+ if (UIElement::GetBackingElement<views::View, ViewElement>( |
+ child_element) == view && |
+ GetUIElementDelegate()->OnUIElementRemoved( |
+ child_element->GetNodeId())) { |
+ child_element->RemoveChildFromParent(); |
+ child_element->Destroy(); |
+ } |
+ } |
+ } |
+} |
+ |
+void ViewElement::OnChildViewAdded(views::View* parent, views::View* view) { |
+ if (parent == view_) { |
+ GetUIElementDelegate()->OnUIElementAdded( |
+ GetNodeId(), new ViewElement(view, GetUIElementDelegate(), this), |
+ GetChildren().end() - 1); |
+ } |
+} |
+ |
+void ViewElement::OnChildViewReordered(views::View* parent, views::View* view) { |
+ if (view == view_) { |
+ std::vector<UIElement*>::iterator prev_sibling_position; |
+ if (GetUIElementDelegate()->OnUIElementRemoved(GetNodeId())) |
+ prev_sibling_position = RemoveChildFromParent(); |
+ else |
+ prev_sibling_position = GetParent()->GetChildren().end(); |
+ |
+ GetUIElementDelegate()->OnUIElementAdded( |
+ GetParent()->GetNodeId(), |
+ new ViewElement(view, GetUIElementDelegate(), GetParent()), |
+ prev_sibling_position); |
+ Destroy(); |
+ } |
+} |
+ |
+void ViewElement::OnViewBoundsChanged(views::View* view) { |
+ if (view_ == view) |
+ GetUIElementDelegate()->OnUIElementBoundsChanged(GetNodeId()); |
+} |
+ |
+void ViewElement::Destroy() { |
+ delete this; |
+} |
+ |
+bool ViewElement::GetBounds(gfx::Rect* bounds) { |
+ if (view_) { |
+ *bounds = view_->bounds(); |
+ return true; |
+ } |
+ return false; |
+} |
+ |
+bool ViewElement::SetBounds(const gfx::Rect& bounds) { |
+ if (view_) { |
+ view_->SetBoundsRect(bounds); |
+ return true; |
+ } |
+ return false; |
+} |
+ |
+bool ViewElement::GetVisible(bool* visible) { |
+ if (view_) { |
+ *visible = view_->visible(); |
+ return true; |
+ } |
+ return false; |
+} |
+ |
+bool ViewElement::SetVisible(bool visible) { |
+ if (view_) { |
+ if (visible != view_->visible()) |
+ view_->SetVisible(visible); |
+ return true; |
+ } |
+ return false; |
+} |
+ |
+std::pair<aura::Window*, gfx::Rect> ViewElement::GetNodeWindowAndBounds() { |
+ if (view_) |
+ return std::make_pair(view_->GetWidget()->GetNativeWindow(), |
+ view_->bounds()); |
+ return std::make_pair(nullptr, gfx::Rect()); |
+} |
+ |
+// static |
+views::View* ViewElement::From(UIElement* element) { |
+ DCHECK_EQ(UIElementType::VIEW, element->GetType()); |
+ return static_cast<ViewElement*>(element)->view_; |
+} |
+ |
+} // namespace devtools |
+} // namespace ash |