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..1b6c1ff913e29a7b22cd3e393f622a75f0c61d66 |
--- /dev/null |
+++ b/ash/devtools/view_element.cc |
@@ -0,0 +1,117 @@ |
+// 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()); |
+ } |
+ } |
+} |
+ |
+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 = |
+ GetUIElementDelegate()->OnUIElementRemoved(GetNodeId()); |
+ |
+ GetUIElementDelegate()->OnUIElementAdded( |
+ GetParent()->GetNodeId(), |
+ new ViewElement(view, GetUIElementDelegate(), GetParent()), |
sadrul
2017/04/24 18:35:21
This means there's a second ViewElement being crea
thanhph
2017/04/26 13:50:38
This was the cause. I move Destroy() out of OnUIEl
|
+ prev_sibling_position); |
+ } |
sadrul
2017/04/24 18:35:22
When stacking/ordering changes, instead of 'remove
thanhph
2017/04/26 13:50:38
I just call OnUIElementRemoved() and follow OnUIEl
|
+} |
+ |
+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 |