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