ui_element.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #ifndef COMPONENTS_UI_DEVTOOLS_UI_ELEMENT_H_
  5. #define COMPONENTS_UI_DEVTOOLS_UI_ELEMENT_H_
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  10. #include "components/ui_devtools/devtools_export.h"
  11. #include "components/ui_devtools/dom.h"
  12. #include "ui/base/interaction/element_identifier.h"
  13. #include "ui/gfx/geometry/rect.h"
  14. #include "ui/gfx/native_widget_types.h"
  15. namespace ui_devtools {
  16. class UIElementDelegate;
  17. // UIElement type.
  18. enum UIElementType { WINDOW, WIDGET, VIEW, ROOT, FRAMESINK, SURFACE };
  19. class UI_DEVTOOLS_EXPORT UIElement {
  20. public:
  21. struct UI_DEVTOOLS_EXPORT UIProperty {
  22. UIProperty(std::string name, std::string value)
  23. : name_(name), value_(value) {}
  24. std::string name_;
  25. std::string value_;
  26. };
  27. struct UI_DEVTOOLS_EXPORT ClassProperties {
  28. ClassProperties(std::string name, std::vector<UIProperty> properties);
  29. ClassProperties(const ClassProperties& copy);
  30. ~ClassProperties();
  31. std::string class_name_;
  32. std::vector<UIProperty> properties_;
  33. };
  34. struct UI_DEVTOOLS_EXPORT Source {
  35. Source(std::string path, int line);
  36. std::string path_;
  37. int line_;
  38. };
  39. using UIElements = std::vector<UIElement*>;
  40. UIElement(const UIElement&) = delete;
  41. UIElement& operator=(const UIElement&) = delete;
  42. virtual ~UIElement();
  43. // resets node ids to 0 so that they are reusable
  44. static void ResetNodeId();
  45. int node_id() const { return node_id_; }
  46. std::string GetTypeName() const;
  47. UIElement* parent() const { return parent_; }
  48. void set_parent(UIElement* parent) { parent_ = parent; }
  49. UIElementDelegate* delegate() const { return delegate_; }
  50. UIElementType type() const { return type_; }
  51. const UIElements& children() const { return children_; }
  52. bool is_updating() const { return is_updating_; }
  53. void set_is_updating(bool is_updating) { is_updating_ = is_updating; }
  54. int GetBaseStylesheetId() const { return base_stylesheet_id_; }
  55. void SetBaseStylesheetId(int id) { base_stylesheet_id_ = id; }
  56. // Gets/sets whether the element has sent its stylesheet header to the
  57. // frontend.
  58. bool header_sent() const { return header_sent_; }
  59. void set_header_sent() { header_sent_ = true; }
  60. using ElementCompare = bool (*)(const UIElement*, const UIElement*);
  61. // Inserts |child| in front of |before|. If |before| is null, it is inserted
  62. // at the end. Parent takes ownership of the added child.
  63. void AddChild(UIElement* child, UIElement* before = nullptr);
  64. // Inserts |child| according to a custom ordering function. |notify_delegate|
  65. // calls OnUIElementAdded(), which creates the subtree of UIElements at
  66. // |child|, and the corresponding DOM nodes.
  67. void AddOrderedChild(UIElement* child,
  68. ElementCompare compare,
  69. bool notify_delegate = true);
  70. // Removes and deletes all elements from |children_|.
  71. void ClearChildren();
  72. // Removes |child| out of |children_| without destroying |child|. The caller
  73. // is responsible for destroying |child|. |notify_delegate| calls
  74. // OnUIElementRemoved(), which destroys the DOM node for |child|.
  75. void RemoveChild(UIElement* child, bool notify_delegate = true);
  76. // Moves |child| to position |index| in |children_|.
  77. void ReorderChild(UIElement* child, int index);
  78. template <class T>
  79. int FindUIElementIdForBackendElement(T* element) const;
  80. // Returns properties grouped by the class they are from.
  81. virtual std::vector<ClassProperties> GetCustomPropertiesForMatchedStyle()
  82. const;
  83. virtual void GetBounds(gfx::Rect* bounds) const = 0;
  84. virtual void SetBounds(const gfx::Rect& bounds) = 0;
  85. virtual void GetVisible(bool* visible) const = 0;
  86. virtual void SetVisible(bool visible) = 0;
  87. // Set this element's property values according to |text|.
  88. // |text| is the string passed in through StyleDeclarationEdit::text from
  89. // the frontend.
  90. virtual bool SetPropertiesFromString(const std::string& text);
  91. // If element exists, returns its associated native window and its screen
  92. // bounds. Otherwise, returns null and empty bounds.
  93. virtual std::pair<gfx::NativeWindow, gfx::Rect> GetNodeWindowAndScreenBounds()
  94. const = 0;
  95. // Returns a list of interleaved keys and values of attributes to be displayed
  96. // on the element in the dev tools hierarchy view.
  97. virtual std::vector<std::string> GetAttributes() const = 0;
  98. template <typename BackingT, typename T>
  99. static BackingT* GetBackingElement(const UIElement* element) {
  100. return T::From(element);
  101. }
  102. // Called from PageAgent to repaint Views for Debug Bounds Rectangles
  103. virtual void PaintRect() const {}
  104. // Called in the constructor to initialize the element's sources.
  105. virtual void InitSources() {}
  106. // Get the sources for the element.
  107. std::vector<Source> GetSources();
  108. // Whether the Element Identifier matches the backing UI element.
  109. // This is used to locate a UIElement by Element Identifier set
  110. // on the browser side and different than node_id().
  111. virtual bool FindMatchByElementID(const ui::ElementIdentifier& identifier);
  112. virtual bool DispatchMouseEvent(protocol::DOM::MouseEvent* event);
  113. virtual bool DispatchKeyEvent(protocol::DOM::KeyEvent* event);
  114. protected:
  115. UIElement(const UIElementType type,
  116. UIElementDelegate* delegate,
  117. UIElement* parent);
  118. void AddSource(std::string path, int line);
  119. private:
  120. const int node_id_;
  121. const UIElementType type_;
  122. UIElements children_;
  123. UIElement* parent_;
  124. UIElementDelegate* delegate_;
  125. bool is_updating_ = false;
  126. int base_stylesheet_id_;
  127. bool header_sent_ = false;
  128. std::vector<Source> sources_;
  129. };
  130. } // namespace ui_devtools
  131. #endif // COMPONENTS_UI_DEVTOOLS_UI_ELEMENT_H_