blob: 4f392cf16a922545918dcb1542c8935908db7989 [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2017 The Chromium Authors
thanhph27d1ff52017-05-20 04:09:132// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Illia Martyniukf688acd62018-04-02 19:26:075#ifndef COMPONENTS_UI_DEVTOOLS_UI_ELEMENT_H_
6#define COMPONENTS_UI_DEVTOOLS_UI_ELEMENT_H_
thanhph27d1ff52017-05-20 04:09:137
Illia Martyniukf688acd62018-04-02 19:26:078#include <memory>
Xiaohui Chenda1977a2018-10-16 20:13:029#include <string>
10#include <utility>
thanhph27d1ff52017-05-20 04:09:1311#include <vector>
12
Illia Martyniukf688acd62018-04-02 19:26:0713#include "components/ui_devtools/devtools_export.h"
Andrey Kosyakov103b8f2a2021-11-16 08:06:2614#include "components/ui_devtools/dom.h"
Yuheng Huangd1d32e22021-11-10 21:01:2315#include "ui/base/interaction/element_identifier.h"
thanhph27d1ff52017-05-20 04:09:1316#include "ui/gfx/geometry/rect.h"
Leonard Grey601aae3f2018-01-19 17:45:5417#include "ui/gfx/native_widget_types.h"
thanhph27d1ff52017-05-20 04:09:1318
thanhph3f3968512017-06-21 00:37:2319namespace ui_devtools {
thanhph27d1ff52017-05-20 04:09:1320
21class UIElementDelegate;
22
23// UIElement type.
yiyix38f1e582018-09-06 21:23:4324enum UIElementType { WINDOW, WIDGET, VIEW, ROOT, FRAMESINK, SURFACE };
thanhph27d1ff52017-05-20 04:09:1325
Illia Martyniukf688acd62018-04-02 19:26:0726class UI_DEVTOOLS_EXPORT UIElement {
thanhph27d1ff52017-05-20 04:09:1327 public:
Kristyn Hamasaki8dee3e5b2019-07-11 22:46:3328 struct UI_DEVTOOLS_EXPORT UIProperty {
29 UIProperty(std::string name, std::string value)
30 : name_(name), value_(value) {}
31
32 std::string name_;
33 std::string value_;
34 };
35 struct UI_DEVTOOLS_EXPORT ClassProperties {
36 ClassProperties(std::string name, std::vector<UIProperty> properties);
37 ClassProperties(const ClassProperties& copy);
38 ~ClassProperties();
39
40 std::string class_name_;
41 std::vector<UIProperty> properties_;
42 };
43
Kristyn Hamasaki4de3d0a12019-08-12 19:31:4244 struct UI_DEVTOOLS_EXPORT Source {
45 Source(std::string path, int line);
46
47 std::string path_;
48 int line_;
49 };
50
Peter Kasting76a60172019-03-18 19:14:0551 using UIElements = std::vector<UIElement*>;
52
Allen Bauer55912512021-04-22 21:47:1453 UIElement(const UIElement&) = delete;
54 UIElement& operator=(const UIElement&) = delete;
55 virtual ~UIElement();
56
Andrew Lee5370cc52019-06-13 20:09:1657 // resets node ids to 0 so that they are reusable
58 static void ResetNodeId();
59
Xiaohui Chenda1977a2018-10-16 20:13:0260 int node_id() const { return node_id_; }
thanhph27d1ff52017-05-20 04:09:1361 std::string GetTypeName() const;
Xiaohui Chenda1977a2018-10-16 20:13:0262 UIElement* parent() const { return parent_; }
63 void set_parent(UIElement* parent) { parent_ = parent; }
64 UIElementDelegate* delegate() const { return delegate_; }
65 UIElementType type() const { return type_; }
Peter Kasting76a60172019-03-18 19:14:0566 const UIElements& children() const { return children_; }
Xiaohui Chenda1977a2018-10-16 20:13:0267 bool is_updating() const { return is_updating_; }
68 void set_is_updating(bool is_updating) { is_updating_ = is_updating; }
Kristyn Hamasaki8dee3e5b2019-07-11 22:46:3369 int GetBaseStylesheetId() const { return base_stylesheet_id_; }
70 void SetBaseStylesheetId(int id) { base_stylesheet_id_ = id; }
thanhph27d1ff52017-05-20 04:09:1371
Kristyn Hamasaki4de3d0a12019-08-12 19:31:4272 // Gets/sets whether the element has sent its stylesheet header to the
73 // frontend.
74 bool header_sent() const { return header_sent_; }
75 void set_header_sent() { header_sent_ = true; }
76
Sean Gilhuly5e4e3fd2019-01-29 20:50:4777 using ElementCompare = bool (*)(const UIElement*, const UIElement*);
78
Peter Kasting76a60172019-03-18 19:14:0579 // Inserts |child| in front of |before|. If |before| is null, it is inserted
80 // at the end. Parent takes ownership of the added child.
thanhph27d1ff52017-05-20 04:09:1381 void AddChild(UIElement* child, UIElement* before = nullptr);
82
Peter Kasting76a60172019-03-18 19:14:0583 // Inserts |child| according to a custom ordering function. |notify_delegate|
84 // calls OnUIElementAdded(), which creates the subtree of UIElements at
85 // |child|, and the corresponding DOM nodes.
Sean Gilhuly5e4e3fd2019-01-29 20:50:4786 void AddOrderedChild(UIElement* child,
87 ElementCompare compare,
88 bool notify_delegate = true);
89
Leonard Greyb4ac2cf2022-04-08 19:33:0890 // Removes and deletes all elements from |children_|.
Sean Gilhuly8844de62018-11-21 15:45:0691 void ClearChildren();
92
Peter Kasting76a60172019-03-18 19:14:0593 // Removes |child| out of |children_| without destroying |child|. The caller
94 // is responsible for destroying |child|. |notify_delegate| calls
95 // OnUIElementRemoved(), which destroys the DOM node for |child|.
Sean Gilhuly5e4e3fd2019-01-29 20:50:4796 void RemoveChild(UIElement* child, bool notify_delegate = true);
thanhph27d1ff52017-05-20 04:09:1397
Peter Kasting9f829d82019-03-30 06:27:2098 // Moves |child| to position |index| in |children_|.
99 void ReorderChild(UIElement* child, int index);
thanhph27d1ff52017-05-20 04:09:13100
Thanh Phamdabbb4dc2017-08-08 19:41:22101 template <class T>
102 int FindUIElementIdForBackendElement(T* element) const;
103
Kristyn Hamasaki8dee3e5b2019-07-11 22:46:33104 // Returns properties grouped by the class they are from.
105 virtual std::vector<ClassProperties> GetCustomPropertiesForMatchedStyle()
106 const;
107
thanhph27d1ff52017-05-20 04:09:13108 virtual void GetBounds(gfx::Rect* bounds) const = 0;
109 virtual void SetBounds(const gfx::Rect& bounds) = 0;
110 virtual void GetVisible(bool* visible) const = 0;
111 virtual void SetVisible(bool visible) = 0;
112
Wei Li6bce39542019-04-23 20:09:51113 // Set this element's property values according to |text|.
114 // |text| is the string passed in through StyleDeclarationEdit::text from
115 // the frontend.
116 virtual bool SetPropertiesFromString(const std::string& text);
117
Peter Kasting76a60172019-03-18 19:14:05118 // If element exists, returns its associated native window and its screen
119 // bounds. Otherwise, returns null and empty bounds.
Wei Lib847f9c2018-12-11 22:10:19120 virtual std::pair<gfx::NativeWindow, gfx::Rect> GetNodeWindowAndScreenBounds()
thanhph27d1ff52017-05-20 04:09:13121 const = 0;
Peter Kasting76a60172019-03-18 19:14:05122
123 // Returns a list of interleaved keys and values of attributes to be displayed
Leonard Grey827f34d82018-02-13 23:25:20124 // on the element in the dev tools hierarchy view.
Johannes Henkel53d2ce282019-06-18 23:14:27125 virtual std::vector<std::string> GetAttributes() const = 0;
thanhph27d1ff52017-05-20 04:09:13126
127 template <typename BackingT, typename T>
Thanh Phamdabbb4dc2017-08-08 19:41:22128 static BackingT* GetBackingElement(const UIElement* element) {
thanhph27d1ff52017-05-20 04:09:13129 return T::From(element);
Xiaohui Chenda1977a2018-10-16 20:13:02130 }
thanhph27d1ff52017-05-20 04:09:13131
Kristyn Hamasaki06170c052019-07-09 23:53:33132 // Called from PageAgent to repaint Views for Debug Bounds Rectangles
133 virtual void PaintRect() const {}
134
Kristyn Hamasaki4de3d0a12019-08-12 19:31:42135 // Called in the constructor to initialize the element's sources.
136 virtual void InitSources() {}
137
138 // Get the sources for the element.
139 std::vector<Source> GetSources();
140
Yuheng Huangd1d32e22021-11-10 21:01:23141 // Whether the Element Identifier matches the backing UI element.
142 // This is used to locate a UIElement by Element Identifier set
143 // on the browser side and different than node_id().
144 virtual bool FindMatchByElementID(const ui::ElementIdentifier& identifier);
Yuheng Huang87e3c672021-10-30 00:53:50145
Yuheng Huangd62276332021-03-26 02:13:27146 virtual bool DispatchMouseEvent(protocol::DOM::MouseEvent* event);
147
Yuheng Huang5f4035f2021-04-20 20:19:49148 virtual bool DispatchKeyEvent(protocol::DOM::KeyEvent* event);
149
thanhph27d1ff52017-05-20 04:09:13150 protected:
151 UIElement(const UIElementType type,
152 UIElementDelegate* delegate,
153 UIElement* parent);
Kristyn Hamasaki4de3d0a12019-08-12 19:31:42154 void AddSource(std::string path, int line);
thanhph27d1ff52017-05-20 04:09:13155
156 private:
157 const int node_id_;
158 const UIElementType type_;
Peter Kasting76a60172019-03-18 19:14:05159 UIElements children_;
thanhph27d1ff52017-05-20 04:09:13160 UIElement* parent_;
161 UIElementDelegate* delegate_;
Xiaohui Chenda1977a2018-10-16 20:13:02162 bool is_updating_ = false;
Kristyn Hamasaki8dee3e5b2019-07-11 22:46:33163 int base_stylesheet_id_;
Kristyn Hamasaki4de3d0a12019-08-12 19:31:42164 bool header_sent_ = false;
165 std::vector<Source> sources_;
thanhph27d1ff52017-05-20 04:09:13166};
167
thanhph3f3968512017-06-21 00:37:23168} // namespace ui_devtools
thanhph27d1ff52017-05-20 04:09:13169
Illia Martyniukf688acd62018-04-02 19:26:07170#endif // COMPONENTS_UI_DEVTOOLS_UI_ELEMENT_H_