window_element.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #include "components/ui_devtools/views/window_element.h"
  5. #include "base/strings/string_number_conversions.h"
  6. #include "components/ui_devtools/protocol.h"
  7. #include "components/ui_devtools/ui_element_delegate.h"
  8. #include "components/ui_devtools/views/devtools_event_util.h"
  9. #include "components/ui_devtools/views/element_utility.h"
  10. #include "components/viz/common/surfaces/surface_id.h"
  11. #include "ui/aura/window.h"
  12. #include "ui/aura/window_tree_host.h"
  13. #include "ui/aura/window_tree_host_platform.h"
  14. #include "ui/base/ime/input_method.h"
  15. #include "ui/base/ime/text_input_client.h"
  16. #include "ui/wm/core/window_util.h"
  17. namespace ui_devtools {
  18. namespace {
  19. int GetIndexOfChildInParent(aura::Window* window) {
  20. const aura::Window::Windows& siblings = window->parent()->children();
  21. auto it = std::find(siblings.begin(), siblings.end(), window);
  22. DCHECK(it != siblings.end());
  23. return std::distance(siblings.begin(), it);
  24. }
  25. } // namespace
  26. WindowElement::WindowElement(aura::Window* window,
  27. UIElementDelegate* ui_element_delegate,
  28. UIElement* parent)
  29. : UIElementWithMetaData(UIElementType::WINDOW, ui_element_delegate, parent),
  30. window_(window) {
  31. if (window)
  32. window_->AddObserver(this);
  33. }
  34. WindowElement::~WindowElement() {
  35. if (window_)
  36. window_->RemoveObserver(this);
  37. }
  38. // Handles removing window_.
  39. void WindowElement::OnWindowHierarchyChanging(
  40. const aura::WindowObserver::HierarchyChangeParams& params) {
  41. if (params.target == window_) {
  42. parent()->RemoveChild(this);
  43. delete this;
  44. }
  45. }
  46. // Handles adding window_.
  47. void WindowElement::OnWindowHierarchyChanged(
  48. const aura::WindowObserver::HierarchyChangeParams& params) {
  49. if (window_ == params.new_parent && params.receiver == params.new_parent) {
  50. AddChild(new WindowElement(params.target, delegate(), this));
  51. }
  52. }
  53. void WindowElement::OnWindowStackingChanged(aura::Window* window) {
  54. DCHECK_EQ(window_, window);
  55. parent()->ReorderChild(this, GetIndexOfChildInParent(window));
  56. }
  57. void WindowElement::OnWindowBoundsChanged(aura::Window* window,
  58. const gfx::Rect& old_bounds,
  59. const gfx::Rect& new_bounds,
  60. ui::PropertyChangeReason reason) {
  61. DCHECK_EQ(window_, window);
  62. delegate()->OnUIElementBoundsChanged(this);
  63. }
  64. // TODO (kylixrd@): Still need to add support for the following property.
  65. //
  66. // cur_properties.emplace_back(
  67. // "is-activatable", wm::CanActivateWindow(window_) ? "true" : "false");
  68. void WindowElement::GetBounds(gfx::Rect* bounds) const {
  69. *bounds = window_->bounds();
  70. }
  71. void WindowElement::SetBounds(const gfx::Rect& bounds) {
  72. window_->SetBounds(bounds);
  73. }
  74. void WindowElement::GetVisible(bool* visible) const {
  75. *visible = window_->IsVisible();
  76. }
  77. void WindowElement::SetVisible(bool visible) {
  78. if (visible)
  79. window_->Show();
  80. else
  81. window_->Hide();
  82. }
  83. std::vector<std::string> WindowElement::GetAttributes() const {
  84. return {"name", window_->GetName(), "active",
  85. ::wm::IsActiveWindow(window_) ? "true" : "false"};
  86. }
  87. std::pair<gfx::NativeWindow, gfx::Rect>
  88. WindowElement::GetNodeWindowAndScreenBounds() const {
  89. return std::make_pair(static_cast<aura::Window*>(window_),
  90. window_->GetBoundsInScreen());
  91. }
  92. // static
  93. aura::Window* WindowElement::From(const UIElement* element) {
  94. DCHECK_EQ(UIElementType::WINDOW, element->type());
  95. return static_cast<const WindowElement*>(element)->window_;
  96. }
  97. template <>
  98. int UIElement::FindUIElementIdForBackendElement<aura::Window>(
  99. aura::Window* element) const {
  100. if (type_ == UIElementType::WINDOW &&
  101. UIElement::GetBackingElement<aura::Window, WindowElement>(this) ==
  102. element) {
  103. return node_id_;
  104. }
  105. for (auto* child : children_) {
  106. int ui_element_id = child->FindUIElementIdForBackendElement(element);
  107. if (ui_element_id)
  108. return ui_element_id;
  109. }
  110. return 0;
  111. }
  112. void WindowElement::InitSources() {
  113. if (window_->layer()) {
  114. AddSource("ui/compositor/layer.h", 0);
  115. }
  116. AddSource("ui/aura/window.h", 0);
  117. }
  118. bool WindowElement::DispatchKeyEvent(protocol::DOM::KeyEvent* event) {
  119. ui::KeyEvent key_event = ConvertToUIKeyEvent(event);
  120. // Key events are processed differently based on classes. Character events are
  121. // routed to the text input client while key stroke events are propragated
  122. // through the normal event flow. The IME flow is bypassed.
  123. if (key_event.is_char()) {
  124. ui::InputMethod* input_method = window_->GetHost()->GetInputMethod();
  125. DCHECK(input_method);
  126. if (input_method->GetTextInputClient()) {
  127. input_method->GetTextInputClient()->InsertChar(key_event);
  128. } else {
  129. return false;
  130. }
  131. } else {
  132. window_->GetHost()->DispatchKeyEventPostIME(&key_event);
  133. }
  134. return true;
  135. }
  136. ui::metadata::ClassMetaData* WindowElement::GetClassMetaData() const {
  137. return window_->GetClassMetaData();
  138. }
  139. void* WindowElement::GetClassInstance() const {
  140. return window_;
  141. }
  142. ui::Layer* WindowElement::GetLayer() const {
  143. return window_->layer();
  144. }
  145. } // namespace ui_devtools