Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
thanhph | 27d1ff5 | 2017-05-20 04:09:13 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Illia Martyniuk | f688acd6 | 2018-04-02 19:26:07 | [diff] [blame] | 5 | #ifndef COMPONENTS_UI_DEVTOOLS_UI_ELEMENT_H_ |
| 6 | #define COMPONENTS_UI_DEVTOOLS_UI_ELEMENT_H_ |
thanhph | 27d1ff5 | 2017-05-20 04:09:13 | [diff] [blame] | 7 | |
Illia Martyniuk | f688acd6 | 2018-04-02 19:26:07 | [diff] [blame] | 8 | #include <memory> |
Xiaohui Chen | da1977a | 2018-10-16 20:13:02 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <utility> |
thanhph | 27d1ff5 | 2017-05-20 04:09:13 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 13 | #include "base/memory/raw_ptr.h" |
Illia Martyniuk | f688acd6 | 2018-04-02 19:26:07 | [diff] [blame] | 14 | #include "components/ui_devtools/devtools_export.h" |
Andrey Kosyakov | 103b8f2a | 2021-11-16 08:06:26 | [diff] [blame] | 15 | #include "components/ui_devtools/dom.h" |
Yuheng Huang | d1d32e2 | 2021-11-10 21:01:23 | [diff] [blame] | 16 | #include "ui/base/interaction/element_identifier.h" |
thanhph | 27d1ff5 | 2017-05-20 04:09:13 | [diff] [blame] | 17 | #include "ui/gfx/geometry/rect.h" |
Leonard Grey | 601aae3f | 2018-01-19 17:45:54 | [diff] [blame] | 18 | #include "ui/gfx/native_widget_types.h" |
thanhph | 27d1ff5 | 2017-05-20 04:09:13 | [diff] [blame] | 19 | |
thanhph | 3f396851 | 2017-06-21 00:37:23 | [diff] [blame] | 20 | namespace ui_devtools { |
thanhph | 27d1ff5 | 2017-05-20 04:09:13 | [diff] [blame] | 21 | |
| 22 | class UIElementDelegate; |
| 23 | |
| 24 | // UIElement type. |
yiyix | 38f1e58 | 2018-09-06 21:23:43 | [diff] [blame] | 25 | enum UIElementType { WINDOW, WIDGET, VIEW, ROOT, FRAMESINK, SURFACE }; |
thanhph | 27d1ff5 | 2017-05-20 04:09:13 | [diff] [blame] | 26 | |
Illia Martyniuk | f688acd6 | 2018-04-02 19:26:07 | [diff] [blame] | 27 | class UI_DEVTOOLS_EXPORT UIElement { |
thanhph | 27d1ff5 | 2017-05-20 04:09:13 | [diff] [blame] | 28 | public: |
Kristyn Hamasaki | 8dee3e5b | 2019-07-11 22:46:33 | [diff] [blame] | 29 | struct UI_DEVTOOLS_EXPORT UIProperty { |
| 30 | UIProperty(std::string name, std::string value) |
| 31 | : name_(name), value_(value) {} |
| 32 | |
| 33 | std::string name_; |
| 34 | std::string value_; |
| 35 | }; |
| 36 | struct UI_DEVTOOLS_EXPORT ClassProperties { |
| 37 | ClassProperties(std::string name, std::vector<UIProperty> properties); |
| 38 | ClassProperties(const ClassProperties& copy); |
| 39 | ~ClassProperties(); |
| 40 | |
| 41 | std::string class_name_; |
| 42 | std::vector<UIProperty> properties_; |
| 43 | }; |
| 44 | |
Kristyn Hamasaki | 4de3d0a1 | 2019-08-12 19:31:42 | [diff] [blame] | 45 | struct UI_DEVTOOLS_EXPORT Source { |
| 46 | Source(std::string path, int line); |
| 47 | |
| 48 | std::string path_; |
| 49 | int line_; |
| 50 | }; |
| 51 | |
Ali Hijazi | e63cbaf6 | 2023-12-20 19:29:35 | [diff] [blame] | 52 | using UIElements = std::vector<raw_ptr<UIElement, VectorExperimental>>; |
Peter Kasting | 76a6017 | 2019-03-18 19:14:05 | [diff] [blame] | 53 | |
Allen Bauer | 5591251 | 2021-04-22 21:47:14 | [diff] [blame] | 54 | UIElement(const UIElement&) = delete; |
| 55 | UIElement& operator=(const UIElement&) = delete; |
| 56 | virtual ~UIElement(); |
| 57 | |
Andrew Lee | 5370cc5 | 2019-06-13 20:09:16 | [diff] [blame] | 58 | // resets node ids to 0 so that they are reusable |
| 59 | static void ResetNodeId(); |
| 60 | |
Xiaohui Chen | da1977a | 2018-10-16 20:13:02 | [diff] [blame] | 61 | int node_id() const { return node_id_; } |
thanhph | 27d1ff5 | 2017-05-20 04:09:13 | [diff] [blame] | 62 | std::string GetTypeName() const; |
Xiaohui Chen | da1977a | 2018-10-16 20:13:02 | [diff] [blame] | 63 | UIElement* parent() const { return parent_; } |
| 64 | void set_parent(UIElement* parent) { parent_ = parent; } |
| 65 | UIElementDelegate* delegate() const { return delegate_; } |
| 66 | UIElementType type() const { return type_; } |
Peter Kasting | 76a6017 | 2019-03-18 19:14:05 | [diff] [blame] | 67 | const UIElements& children() const { return children_; } |
Xiaohui Chen | da1977a | 2018-10-16 20:13:02 | [diff] [blame] | 68 | bool is_updating() const { return is_updating_; } |
| 69 | void set_is_updating(bool is_updating) { is_updating_ = is_updating; } |
Kristyn Hamasaki | 8dee3e5b | 2019-07-11 22:46:33 | [diff] [blame] | 70 | int GetBaseStylesheetId() const { return base_stylesheet_id_; } |
| 71 | void SetBaseStylesheetId(int id) { base_stylesheet_id_ = id; } |
thanhph | 27d1ff5 | 2017-05-20 04:09:13 | [diff] [blame] | 72 | |
Kristyn Hamasaki | 4de3d0a1 | 2019-08-12 19:31:42 | [diff] [blame] | 73 | // Gets/sets whether the element has sent its stylesheet header to the |
| 74 | // frontend. |
| 75 | bool header_sent() const { return header_sent_; } |
| 76 | void set_header_sent() { header_sent_ = true; } |
| 77 | |
Sean Gilhuly | 5e4e3fd | 2019-01-29 20:50:47 | [diff] [blame] | 78 | using ElementCompare = bool (*)(const UIElement*, const UIElement*); |
| 79 | |
Peter Kasting | 76a6017 | 2019-03-18 19:14:05 | [diff] [blame] | 80 | // Inserts |child| in front of |before|. If |before| is null, it is inserted |
| 81 | // at the end. Parent takes ownership of the added child. |
thanhph | 27d1ff5 | 2017-05-20 04:09:13 | [diff] [blame] | 82 | void AddChild(UIElement* child, UIElement* before = nullptr); |
| 83 | |
Peter Kasting | 76a6017 | 2019-03-18 19:14:05 | [diff] [blame] | 84 | // Inserts |child| according to a custom ordering function. |notify_delegate| |
| 85 | // calls OnUIElementAdded(), which creates the subtree of UIElements at |
| 86 | // |child|, and the corresponding DOM nodes. |
Sean Gilhuly | 5e4e3fd | 2019-01-29 20:50:47 | [diff] [blame] | 87 | void AddOrderedChild(UIElement* child, |
| 88 | ElementCompare compare, |
| 89 | bool notify_delegate = true); |
| 90 | |
Leonard Grey | b4ac2cf | 2022-04-08 19:33:08 | [diff] [blame] | 91 | // Removes and deletes all elements from |children_|. |
Sean Gilhuly | 8844de6 | 2018-11-21 15:45:06 | [diff] [blame] | 92 | void ClearChildren(); |
| 93 | |
Peter Kasting | 76a6017 | 2019-03-18 19:14:05 | [diff] [blame] | 94 | // Removes |child| out of |children_| without destroying |child|. The caller |
| 95 | // is responsible for destroying |child|. |notify_delegate| calls |
| 96 | // OnUIElementRemoved(), which destroys the DOM node for |child|. |
Sean Gilhuly | 5e4e3fd | 2019-01-29 20:50:47 | [diff] [blame] | 97 | void RemoveChild(UIElement* child, bool notify_delegate = true); |
thanhph | 27d1ff5 | 2017-05-20 04:09:13 | [diff] [blame] | 98 | |
Peter Kasting | 9f829d8 | 2019-03-30 06:27:20 | [diff] [blame] | 99 | // Moves |child| to position |index| in |children_|. |
| 100 | void ReorderChild(UIElement* child, int index); |
thanhph | 27d1ff5 | 2017-05-20 04:09:13 | [diff] [blame] | 101 | |
Thanh Pham | dabbb4dc | 2017-08-08 19:41:22 | [diff] [blame] | 102 | template <class T> |
| 103 | int FindUIElementIdForBackendElement(T* element) const; |
| 104 | |
Kristyn Hamasaki | 8dee3e5b | 2019-07-11 22:46:33 | [diff] [blame] | 105 | // Returns properties grouped by the class they are from. |
| 106 | virtual std::vector<ClassProperties> GetCustomPropertiesForMatchedStyle() |
| 107 | const; |
| 108 | |
thanhph | 27d1ff5 | 2017-05-20 04:09:13 | [diff] [blame] | 109 | virtual void GetBounds(gfx::Rect* bounds) const = 0; |
| 110 | virtual void SetBounds(const gfx::Rect& bounds) = 0; |
| 111 | virtual void GetVisible(bool* visible) const = 0; |
| 112 | virtual void SetVisible(bool visible) = 0; |
| 113 | |
Wei Li | 6bce3954 | 2019-04-23 20:09:51 | [diff] [blame] | 114 | // Set this element's property values according to |text|. |
| 115 | // |text| is the string passed in through StyleDeclarationEdit::text from |
| 116 | // the frontend. |
| 117 | virtual bool SetPropertiesFromString(const std::string& text); |
| 118 | |
Peter Kasting | 76a6017 | 2019-03-18 19:14:05 | [diff] [blame] | 119 | // If element exists, returns its associated native window and its screen |
| 120 | // bounds. Otherwise, returns null and empty bounds. |
Wei Li | b847f9c | 2018-12-11 22:10:19 | [diff] [blame] | 121 | virtual std::pair<gfx::NativeWindow, gfx::Rect> GetNodeWindowAndScreenBounds() |
thanhph | 27d1ff5 | 2017-05-20 04:09:13 | [diff] [blame] | 122 | const = 0; |
Peter Kasting | 76a6017 | 2019-03-18 19:14:05 | [diff] [blame] | 123 | |
| 124 | // Returns a list of interleaved keys and values of attributes to be displayed |
Leonard Grey | 827f34d8 | 2018-02-13 23:25:20 | [diff] [blame] | 125 | // on the element in the dev tools hierarchy view. |
Johannes Henkel | 53d2ce28 | 2019-06-18 23:14:27 | [diff] [blame] | 126 | virtual std::vector<std::string> GetAttributes() const = 0; |
thanhph | 27d1ff5 | 2017-05-20 04:09:13 | [diff] [blame] | 127 | |
| 128 | template <typename BackingT, typename T> |
Thanh Pham | dabbb4dc | 2017-08-08 19:41:22 | [diff] [blame] | 129 | static BackingT* GetBackingElement(const UIElement* element) { |
thanhph | 27d1ff5 | 2017-05-20 04:09:13 | [diff] [blame] | 130 | return T::From(element); |
Xiaohui Chen | da1977a | 2018-10-16 20:13:02 | [diff] [blame] | 131 | } |
thanhph | 27d1ff5 | 2017-05-20 04:09:13 | [diff] [blame] | 132 | |
Kristyn Hamasaki | 06170c05 | 2019-07-09 23:53:33 | [diff] [blame] | 133 | // Called from PageAgent to repaint Views for Debug Bounds Rectangles |
| 134 | virtual void PaintRect() const {} |
| 135 | |
Kristyn Hamasaki | 4de3d0a1 | 2019-08-12 19:31:42 | [diff] [blame] | 136 | // Called in the constructor to initialize the element's sources. |
| 137 | virtual void InitSources() {} |
| 138 | |
| 139 | // Get the sources for the element. |
| 140 | std::vector<Source> GetSources(); |
| 141 | |
Yuheng Huang | d1d32e2 | 2021-11-10 21:01:23 | [diff] [blame] | 142 | // Whether the Element Identifier matches the backing UI element. |
| 143 | // This is used to locate a UIElement by Element Identifier set |
| 144 | // on the browser side and different than node_id(). |
| 145 | virtual bool FindMatchByElementID(const ui::ElementIdentifier& identifier); |
Yuheng Huang | 87e3c67 | 2021-10-30 00:53:50 | [diff] [blame] | 146 | |
Yuheng Huang | d6227633 | 2021-03-26 02:13:27 | [diff] [blame] | 147 | virtual bool DispatchMouseEvent(protocol::DOM::MouseEvent* event); |
| 148 | |
Yuheng Huang | 5f4035f | 2021-04-20 20:19:49 | [diff] [blame] | 149 | virtual bool DispatchKeyEvent(protocol::DOM::KeyEvent* event); |
| 150 | |
thanhph | 27d1ff5 | 2017-05-20 04:09:13 | [diff] [blame] | 151 | protected: |
| 152 | UIElement(const UIElementType type, |
| 153 | UIElementDelegate* delegate, |
| 154 | UIElement* parent); |
Kristyn Hamasaki | 4de3d0a1 | 2019-08-12 19:31:42 | [diff] [blame] | 155 | void AddSource(std::string path, int line); |
thanhph | 27d1ff5 | 2017-05-20 04:09:13 | [diff] [blame] | 156 | |
| 157 | private: |
| 158 | const int node_id_; |
| 159 | const UIElementType type_; |
Peter Kasting | 76a6017 | 2019-03-18 19:14:05 | [diff] [blame] | 160 | UIElements children_; |
Bartek Nowierski | fc9563f | 2024-04-06 19:11:47 | [diff] [blame] | 161 | raw_ptr<UIElement, DanglingUntriaged> parent_; |
| 162 | raw_ptr<UIElementDelegate, DanglingUntriaged> delegate_; |
Xiaohui Chen | da1977a | 2018-10-16 20:13:02 | [diff] [blame] | 163 | bool is_updating_ = false; |
Kristyn Hamasaki | 8dee3e5b | 2019-07-11 22:46:33 | [diff] [blame] | 164 | int base_stylesheet_id_; |
Kristyn Hamasaki | 4de3d0a1 | 2019-08-12 19:31:42 | [diff] [blame] | 165 | bool header_sent_ = false; |
| 166 | std::vector<Source> sources_; |
thanhph | 27d1ff5 | 2017-05-20 04:09:13 | [diff] [blame] | 167 | }; |
| 168 | |
thanhph | 3f396851 | 2017-06-21 00:37:23 | [diff] [blame] | 169 | } // namespace ui_devtools |
thanhph | 27d1ff5 | 2017-05-20 04:09:13 | [diff] [blame] | 170 | |
Illia Martyniuk | f688acd6 | 2018-04-02 19:26:07 | [diff] [blame] | 171 | #endif // COMPONENTS_UI_DEVTOOLS_UI_ELEMENT_H_ |