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

Unified Diff: ash/devtools/view_element.cc

Issue 2776543002: Create a unified UIElement interface for Widget, View and Window. (Closed)
Patch Set: . Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
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..f1000dcd4af050f6aecd30a5484d6d6544575127
--- /dev/null
+++ b/ash/devtools/view_element.cc
@@ -0,0 +1,139 @@
+// 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 {
+
+namespace {
+
+int FindSibling(views::View* view) {
+ views::View* parent = view->parent();
+ int view_index = -1;
+ for (int i = 0, count = parent->child_count(); i < count; i++) {
+ if (view == parent->child_at(i)) {
+ view_index = i;
+ break;
+ }
+ }
+ DCHECK_GE(view_index, 0);
+ return view_index;
+}
+
+} // namespace
+
+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 &&
+ delegate()->OnUIElementRemoved(child_element->GetNodeId())) {
+ child_element->RemoveChildFromParent();
+ child_element->Destroy();
+ }
+ }
+ }
+}
+
+void ViewElement::OnChildViewAdded(views::View* parent, views::View* view) {
+ if (parent == view_) {
+ delegate()->OnUIElementAdded(this, new ViewElement(view, delegate(), this),
+ LastChildIterator() - 1);
+ }
+}
+
+void ViewElement::OnChildViewReordered(views::View* parent, views::View* view) {
+ if (view == view_) {
+ RemoveChildFromParent();
+
+ int i = FindSibling(view);
+ std::vector<UIElement*>::iterator new_sibling_position =
+ GetParent()->BeginChildIterator();
+ while (i-- > 0)
+ new_sibling_position++;
+
+ GetParent()->AddChild(new_sibling_position, this);
+ }
+}
+
+void ViewElement::OnViewBoundsChanged(views::View* view) {
+ if (view_ == view)
+ delegate()->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

Powered by Google App Engine
This is Rietveld 408576698