1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/ui_devtools/views/widget_element.h"
#include "base/strings/to_string.h"
#include "components/ui_devtools/protocol.h"
#include "components/ui_devtools/ui_element_delegate.h"
#include "components/ui_devtools/views/devtools_event_util.h"
namespace ui_devtools {
WidgetElement::WidgetElement(views::Widget* widget,
UIElementDelegate* ui_element_delegate,
UIElement* parent)
: UIElementWithMetaData(UIElementType::WIDGET, ui_element_delegate, parent),
widget_(widget) {
widget_->AddRemovalsObserver(this);
widget_->AddObserver(this);
}
WidgetElement::~WidgetElement() {
if (widget_) {
widget_->RemoveRemovalsObserver(this);
widget_->RemoveObserver(this);
}
CHECK(!IsInObserverList());
}
void WidgetElement::OnWillRemoveView(views::Widget* widget, views::View* view) {
if (view != widget->GetRootView())
return;
DCHECK_EQ(1u, children().size());
UIElement* child = children()[0];
RemoveChild(child);
delete child;
}
void WidgetElement::OnWidgetBoundsChanged(views::Widget* widget,
const gfx::Rect& new_bounds) {
DCHECK_EQ(widget, widget_);
delegate()->OnUIElementBoundsChanged(this);
}
void WidgetElement::OnWidgetDestroyed(views::Widget* widget) {
DCHECK_EQ(widget, widget_);
if (parent())
parent()->RemoveChild(this);
else
delegate()->OnUIElementRemoved(this);
widget_ = nullptr;
}
void WidgetElement::GetBounds(gfx::Rect* bounds) const {
*bounds = widget_->GetRestoredBounds();
}
void WidgetElement::SetBounds(const gfx::Rect& bounds) {
widget_->SetBounds(bounds);
}
void WidgetElement::GetVisible(bool* visible) const {
*visible = widget_->IsVisible();
}
void WidgetElement::SetVisible(bool visible) {
if (visible == widget_->IsVisible())
return;
if (visible)
widget_->Show();
else
widget_->Hide();
}
std::vector<std::string> WidgetElement::GetAttributes() const {
return {"name", widget_->GetName(), "active",
base::ToString(widget_->IsActive())};
}
std::pair<gfx::NativeWindow, gfx::Rect>
WidgetElement::GetNodeWindowAndScreenBounds() const {
return std::make_pair(widget_->GetNativeWindow(),
widget_->GetWindowBoundsInScreen());
}
gfx::Rect WidgetElement::GetNodeBoundsInScreen() const {
return widget_->GetWindowBoundsInScreen();
}
// static
views::Widget* WidgetElement::From(const UIElement* element) {
DCHECK_EQ(UIElementType::WIDGET, element->type());
return static_cast<const WidgetElement*>(element)->widget_;
}
template <>
int UIElement::FindUIElementIdForBackendElement<views::Widget>(
views::Widget* element) const {
if (type_ == UIElementType::WIDGET &&
UIElement::GetBackingElement<views::Widget, WidgetElement>(this) ==
element) {
return node_id_;
}
for (ui_devtools::UIElement* child : children_) {
int ui_element_id = child->FindUIElementIdForBackendElement(element);
if (ui_element_id)
return ui_element_id;
}
return 0;
}
bool WidgetElement::DispatchKeyEvent(protocol::DOM::KeyEvent* event) {
ui::KeyEvent key_event = ConvertToUIKeyEvent(event);
widget_->OnKeyEvent(&key_event);
return true;
}
ui::metadata::ClassMetaData* WidgetElement::GetClassMetaData() const {
return widget_->GetClassMetaData();
}
void* WidgetElement::GetClassInstance() const {
return widget_;
}
ui::Layer* WidgetElement::GetLayer() const {
return widget_->GetLayer();
}
} // namespace ui_devtools
|