OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ash/devtools/window_element.h" |
| 6 |
| 7 #include "ash/devtools/ui_element_delegate.h" |
| 8 #include "ui/aura/window.h" |
| 9 |
| 10 namespace ash { |
| 11 namespace devtools { |
| 12 namespace { |
| 13 |
| 14 int FindSibling(aura::Window* window) { |
| 15 const aura::Window::Windows& siblings = window->parent()->children(); |
| 16 auto it = std::find(siblings.begin(), siblings.end(), window); |
| 17 DCHECK(it != siblings.end()); |
| 18 return std::distance(siblings.begin(), it); |
| 19 } |
| 20 |
| 21 } // namespace |
| 22 |
| 23 WindowElement::WindowElement(aura::Window* window, |
| 24 UIElementDelegate* ui_element_delegate, |
| 25 UIElement* parent) |
| 26 : UIElement(UIElementType::WINDOW, ui_element_delegate, parent), |
| 27 window_(window) { |
| 28 if (window_) |
| 29 window_->AddObserver(this); |
| 30 } |
| 31 |
| 32 WindowElement::~WindowElement() { |
| 33 if (window_) |
| 34 window_->RemoveObserver(this); |
| 35 } |
| 36 |
| 37 aura::Window* WindowElement::window() const { |
| 38 return window_; |
| 39 } |
| 40 |
| 41 // Handles removing window_. |
| 42 void WindowElement::OnWindowHierarchyChanging( |
| 43 const aura::WindowObserver::HierarchyChangeParams& params) { |
| 44 if (params.target == window_ && delegate()->OnUIElementRemoved(this)) { |
| 45 GetParent()->RemoveChild(this); |
| 46 Destroy(); |
| 47 } |
| 48 } |
| 49 |
| 50 // Handles adding window_. |
| 51 void WindowElement::OnWindowHierarchyChanged( |
| 52 const aura::WindowObserver::HierarchyChangeParams& params) { |
| 53 if (window_ == params.new_parent && params.receiver == params.new_parent) { |
| 54 delegate()->OnUIElementAdded( |
| 55 this, new WindowElement(params.target, delegate(), this), |
| 56 GetChildren().empty() ? nullptr : GetChildren().back()); |
| 57 } |
| 58 } |
| 59 |
| 60 void WindowElement::OnWindowStackingChanged(aura::Window* window) { |
| 61 DCHECK(window_ == window); |
| 62 if (delegate()->IsHighlightingWindow(window)) |
| 63 return; |
| 64 GetParent()->RemoveChild(this); |
| 65 UIElement* prev_sibling = |
| 66 GetParent()->ReorderChild(this, FindSibling(window)); |
| 67 delegate()->OnUIElementReordered(GetParent(), this, prev_sibling); |
| 68 } |
| 69 |
| 70 void WindowElement::OnWindowBoundsChanged(aura::Window* window, |
| 71 const gfx::Rect& old_bounds, |
| 72 const gfx::Rect& new_bounds) { |
| 73 DCHECK(window_ == window); |
| 74 delegate()->OnUIElementBoundsChanged(this); |
| 75 } |
| 76 |
| 77 void WindowElement::Destroy() { |
| 78 delete this; |
| 79 } |
| 80 |
| 81 void WindowElement::GetBounds(gfx::Rect* bounds) const { |
| 82 *bounds = window_->bounds(); |
| 83 } |
| 84 |
| 85 void WindowElement::SetBounds(const gfx::Rect& bounds) { |
| 86 window_->SetBounds(bounds); |
| 87 } |
| 88 |
| 89 void WindowElement::GetVisible(bool* visible) const { |
| 90 *visible = window_->IsVisible(); |
| 91 } |
| 92 |
| 93 void WindowElement::SetVisible(bool visible) { |
| 94 if (visible != window_->IsVisible()) { |
| 95 if (visible) |
| 96 window_->Show(); |
| 97 else |
| 98 window_->Hide(); |
| 99 } |
| 100 } |
| 101 |
| 102 std::pair<aura::Window*, gfx::Rect> WindowElement::GetNodeWindowAndBounds() |
| 103 const { |
| 104 return std::make_pair(window_, window_->GetBoundsInScreen()); |
| 105 } |
| 106 |
| 107 // static |
| 108 aura::Window* WindowElement::From(UIElement* element) { |
| 109 DCHECK_EQ(UIElementType::WINDOW, element->GetType()); |
| 110 return static_cast<WindowElement*>(element)->window_; |
| 111 } |
| 112 |
| 113 } // namespace devtools |
| 114 } // namespace ash |
OLD | NEW |