ui_element.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/ui_element.h"
  5. #include <algorithm>
  6. #include "base/check_op.h"
  7. #include "base/notreached.h"
  8. #include "components/ui_devtools/protocol.h"
  9. #include "components/ui_devtools/ui_element_delegate.h"
  10. namespace ui_devtools {
  11. namespace {
  12. static int node_ids = 0;
  13. } // namespace
  14. UIElement::ClassProperties::ClassProperties(
  15. std::string class_name,
  16. std::vector<UIElement::UIProperty> properties)
  17. : class_name_(class_name), properties_(properties) {}
  18. UIElement::ClassProperties::ClassProperties(
  19. const UIElement::ClassProperties& other) = default;
  20. UIElement::ClassProperties::~ClassProperties() = default;
  21. UIElement::Source::Source(std::string path, int line)
  22. : path_(path), line_(line) {}
  23. // static
  24. void UIElement::ResetNodeId() {
  25. node_ids = 0;
  26. }
  27. UIElement::~UIElement() {
  28. ClearChildren();
  29. }
  30. std::string UIElement::GetTypeName() const {
  31. switch (type_) {
  32. case UIElementType::ROOT:
  33. return "Root";
  34. case UIElementType::WINDOW:
  35. return "Window";
  36. case UIElementType::WIDGET:
  37. return "Widget";
  38. case UIElementType::VIEW:
  39. return "View";
  40. case UIElementType::FRAMESINK:
  41. return "FrameSink";
  42. case UIElementType::SURFACE:
  43. return "Surface";
  44. }
  45. NOTREACHED();
  46. return std::string();
  47. }
  48. void UIElement::AddChild(UIElement* child, UIElement* before) {
  49. if (before) {
  50. auto iter = std::find(children_.begin(), children_.end(), before);
  51. DCHECK(iter != children_.end());
  52. children_.insert(iter, child);
  53. } else {
  54. children_.push_back(child);
  55. }
  56. delegate_->OnUIElementAdded(this, child);
  57. }
  58. void UIElement::AddOrderedChild(UIElement* child,
  59. ElementCompare compare,
  60. bool notify_delegate) {
  61. auto iter =
  62. std::lower_bound(children_.begin(), children_.end(), child, compare);
  63. children_.insert(iter, child);
  64. if (notify_delegate)
  65. delegate_->OnUIElementAdded(this, child);
  66. }
  67. void UIElement::ClearChildren() {
  68. for (auto* child : children_)
  69. delete child;
  70. children_.clear();
  71. }
  72. void UIElement::RemoveChild(UIElement* child, bool notify_delegate) {
  73. if (notify_delegate)
  74. delegate_->OnUIElementRemoved(child);
  75. auto iter = std::find(children_.begin(), children_.end(), child);
  76. DCHECK(iter != children_.end());
  77. children_.erase(iter);
  78. }
  79. void UIElement::ReorderChild(UIElement* child, int index) {
  80. auto i = std::find(children_.begin(), children_.end(), child);
  81. DCHECK(i != children_.end());
  82. DCHECK_GE(index, 0);
  83. DCHECK_LT(static_cast<size_t>(index), children_.size());
  84. // If |child| is already at the desired position, there's nothing to do.
  85. const auto pos = std::next(children_.begin(), index);
  86. if (i == pos)
  87. return;
  88. // Rotate |child| to be at the desired position.
  89. if (pos < i)
  90. std::rotate(pos, i, std::next(i));
  91. else
  92. std::rotate(i, std::next(i), std::next(pos));
  93. delegate()->OnUIElementReordered(child->parent(), child);
  94. }
  95. template <class T>
  96. int UIElement::FindUIElementIdForBackendElement(T* element) const {
  97. NOTREACHED();
  98. return 0;
  99. }
  100. std::vector<UIElement::ClassProperties>
  101. UIElement::GetCustomPropertiesForMatchedStyle() const {
  102. return {};
  103. }
  104. UIElement::UIElement(const UIElementType type,
  105. UIElementDelegate* delegate,
  106. UIElement* parent)
  107. : node_id_(++node_ids), type_(type), parent_(parent), delegate_(delegate) {
  108. delegate_->OnUIElementAdded(nullptr, this);
  109. }
  110. bool UIElement::SetPropertiesFromString(const std::string& text) {
  111. return false;
  112. }
  113. void UIElement::AddSource(std::string path, int line) {
  114. sources_.emplace_back(path, line);
  115. }
  116. std::vector<UIElement::Source> UIElement::GetSources() {
  117. if (sources_.empty())
  118. InitSources();
  119. return sources_;
  120. }
  121. bool UIElement::FindMatchByElementID(const ui::ElementIdentifier& identifier) {
  122. return false;
  123. }
  124. bool UIElement::DispatchMouseEvent(protocol::DOM::MouseEvent* event) {
  125. return false;
  126. }
  127. bool UIElement::DispatchKeyEvent(protocol::DOM::KeyEvent* event) {
  128. return false;
  129. }
  130. } // namespace ui_devtools