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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
// 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/window_element.h"
#include <algorithm>
#include "base/strings/string_number_conversions.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"
#include "components/ui_devtools/views/element_utility.h"
#include "components/viz/common/surfaces/surface_id.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/aura/window_tree_host_platform.h"
#include "ui/base/ime/input_method.h"
#include "ui/base/ime/text_input_client.h"
#include "ui/wm/core/window_util.h"
namespace ui_devtools {
namespace {
int GetIndexOfChildInParent(aura::Window* window) {
const aura::Window::Windows& siblings = window->parent()->children();
auto it = std::ranges::find(siblings, window);
CHECK(it != siblings.end());
return std::distance(siblings.begin(), it);
}
} // namespace
WindowElement::WindowElement(aura::Window* window,
UIElementDelegate* ui_element_delegate,
UIElement* parent)
: UIElementWithMetaData(UIElementType::WINDOW, ui_element_delegate, parent),
window_(window) {
if (window)
window_->AddObserver(this);
}
WindowElement::~WindowElement() {
if (window_)
window_->RemoveObserver(this);
}
// Handles removing window_.
void WindowElement::OnWindowHierarchyChanging(
const aura::WindowObserver::HierarchyChangeParams& params) {
if (params.target == window_.get()) {
parent()->RemoveChild(this);
delete this;
}
}
// Handles adding window_.
void WindowElement::OnWindowHierarchyChanged(
const aura::WindowObserver::HierarchyChangeParams& params) {
if (window_.get() == params.new_parent &&
params.receiver == params.new_parent) {
AddChild(new WindowElement(params.target, delegate(), this));
}
}
void WindowElement::OnWindowStackingChanged(aura::Window* window) {
DCHECK_EQ(window_, window);
parent()->ReorderChild(this, GetIndexOfChildInParent(window));
}
void WindowElement::OnWindowBoundsChanged(aura::Window* window,
const gfx::Rect& old_bounds,
const gfx::Rect& new_bounds,
ui::PropertyChangeReason reason) {
DCHECK_EQ(window_, window);
delegate()->OnUIElementBoundsChanged(this);
}
// TODO (kylixrd@): Still need to add support for the following property.
//
// cur_properties.emplace_back(
// "is-activatable", wm::CanActivateWindow(window_) ? "true" : "false");
void WindowElement::GetBounds(gfx::Rect* bounds) const {
*bounds = window_->bounds();
}
void WindowElement::SetBounds(const gfx::Rect& bounds) {
window_->SetBounds(bounds);
}
void WindowElement::GetVisible(bool* visible) const {
*visible = window_->IsVisible();
}
void WindowElement::SetVisible(bool visible) {
if (visible)
window_->Show();
else
window_->Hide();
}
std::vector<std::string> WindowElement::GetAttributes() const {
return {"name", window_->GetName(), "active",
base::ToString(::wm::IsActiveWindow(window_))};
}
std::pair<gfx::NativeWindow, gfx::Rect>
WindowElement::GetNodeWindowAndScreenBounds() const {
return std::make_pair(static_cast<aura::Window*>(window_),
window_->GetBoundsInScreen());
}
// static
aura::Window* WindowElement::From(const UIElement* element) {
DCHECK_EQ(UIElementType::WINDOW, element->type());
return static_cast<const WindowElement*>(element)->window_;
}
template <>
int UIElement::FindUIElementIdForBackendElement<aura::Window>(
aura::Window* element) const {
if (type_ == UIElementType::WINDOW &&
UIElement::GetBackingElement<aura::Window, WindowElement>(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;
}
void WindowElement::InitSources() {
if (window_->layer()) {
AddSource("ui/compositor/layer.h", 0);
}
AddSource("ui/aura/window.h", 0);
}
bool WindowElement::DispatchKeyEvent(protocol::DOM::KeyEvent* event) {
ui::KeyEvent key_event = ConvertToUIKeyEvent(event);
// Key events are processed differently based on classes. Character events are
// routed to the text input client while key stroke events are propragated
// through the normal event flow. The IME flow is bypassed.
if (key_event.is_char()) {
ui::InputMethod* input_method = window_->GetHost()->GetInputMethod();
DCHECK(input_method);
if (input_method->GetTextInputClient()) {
input_method->GetTextInputClient()->InsertChar(key_event);
} else {
return false;
}
} else {
window_->GetHost()->DispatchKeyEventPostIME(&key_event);
}
return true;
}
ui::metadata::ClassMetaData* WindowElement::GetClassMetaData() const {
return window_->GetClassMetaData();
}
void* WindowElement::GetClassInstance() const {
return window_;
}
ui::Layer* WindowElement::GetLayer() const {
return window_->layer();
}
} // namespace ui_devtools
|