blob: 8db1c3f03b657f527336438deef64443053d2dae [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
Ali Hijazie63cbaf62023-12-20 19:29:3513#include "base/memory/raw_ptr.h"
Illia Martyniukf688acd62018-04-02 19:26:0714#include "components/ui_devtools/devtools_export.h"
Andrey Kosyakov103b8f2a2021-11-16 08:06:2615#include "components/ui_devtools/dom.h"
Yuheng Huangd1d32e22021-11-10 21:01:2316#include "ui/base/interaction/element_identifier.h"
thanhph27d1ff52017-05-20 04:09:1317#include "ui/gfx/geometry/rect.h"
Leonard Grey601aae3f2018-01-19 17:45:5418#include "ui/gfx/native_widget_types.h"
thanhph27d1ff52017-05-20 04:09:1319
thanhph3f3968512017-06-21 00:37:2320namespace ui_devtools {
thanhph27d1ff52017-05-20 04:09:1321
22class UIElementDelegate;
23
24// UIElement type.
yiyix38f1e582018-09-06 21:23:4325enum UIElementType { WINDOW, WIDGET, VIEW, ROOT, FRAMESINK, SURFACE };
thanhph27d1ff52017-05-20 04:09:1326
Illia Martyniukf688acd62018-04-02 19:26:0727class UI_DEVTOOLS_EXPORT UIElement {
thanhph27d1ff52017-05-20 04:09:1328 public:
Kristyn Hamasaki8dee3e5b2019-07-11 22:46:3329 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 Hamasaki4de3d0a12019-08-12 19:31:4245 struct UI_DEVTOOLS_EXPORT Source {
46 Source(std::string path, int line);
47
48 std::string path_;
49 int line_;
50 };
51
Ali Hijazie63cbaf62023-12-20 19:29:3552 using UIElements = std::vector<raw_ptr<UIElement, VectorExperimental>>;
Peter Kasting76a60172019-03-18 19:14:0553
Allen Bauer55912512021-04-22 21:47:1454 UIElement(const UIElement&) = delete;
55 UIElement& operator=(const UIElement&) = delete;
56 virtual ~UIElement();
57
Andrew Lee5370cc52019-06-13 20:09:1658 // resets node ids to 0 so that they are reusable
59 static void ResetNodeId();
60
Xiaohui Chenda1977a2018-10-16 20:13:0261 int node_id() const { return node_id_; }
thanhph27d1ff52017-05-20 04:09:1362 std::string GetTypeName() const;
Xiaohui Chenda1977a2018-10-16 20:13:0263 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 Kasting76a60172019-03-18 19:14:0567 const UIElements& children() const { return children_; }
Xiaohui Chenda1977a2018-10-16 20:13:0268 bool is_updating() const { return is_updating_; }
69 void set_is_updating(bool is_updating) { is_updating_ = is_updating; }
Kristyn Hamasaki8dee3e5b2019-07-11 22:46:3370 int GetBaseStylesheetId() const { return base_stylesheet_id_; }
71 void SetBaseStylesheetId(int id) { base_stylesheet_id_ = id; }
thanhph27d1ff52017-05-20 04:09:1372
Kristyn Hamasaki4de3d0a12019-08-12 19:31:4273 // 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 Gilhuly5e4e3fd2019-01-29 20:50:4778 using ElementCompare = bool (*)(const UIElement*, const UIElement*);
79
Peter Kasting76a60172019-03-18 19:14:0580 // Inserts |child| in front of |before|. If |before| is null, it is inserted
81 // at the end. Parent takes ownership of the added child.
thanhph27d1ff52017-05-20 04:09:1382 void AddChild(UIElement* child, UIElement* before = nullptr);
83
Peter Kasting76a60172019-03-18 19:14:0584 // 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 Gilhuly5e4e3fd2019-01-29 20:50:4787 void AddOrderedChild(UIElement* child,
88 ElementCompare compare,
89 bool notify_delegate = true);
90
Leonard Greyb4ac2cf2022-04-08 19:33:0891 // Removes and deletes all elements from |children_|.
Sean Gilhuly8844de62018-11-21 15:45:0692 void ClearChildren();
93
Peter Kasting76a60172019-03-18 19:14:0594 // 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 Gilhuly5e4e3fd2019-01-29 20:50:4797 void RemoveChild(UIElement* child, bool notify_delegate = true);
thanhph27d1ff52017-05-20 04:09:1398
Peter Kasting9f829d82019-03-30 06:27:2099 // Moves |child| to position |index| in |children_|.
100 void ReorderChild(UIElement* child, int index);
thanhph27d1ff52017-05-20 04:09:13101
Thanh Phamdabbb4dc2017-08-08 19:41:22102 template <class T>
103 int FindUIElementIdForBackendElement(T* element) const;
104
Kristyn Hamasaki8dee3e5b2019-07-11 22:46:33105 // Returns properties grouped by the class they are from.
106 virtual std::vector<ClassProperties> GetCustomPropertiesForMatchedStyle()
107 const;
108
thanhph27d1ff52017-05-20 04:09:13109 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 Li6bce39542019-04-23 20:09:51114 // 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 Kasting76a60172019-03-18 19:14:05119 // If element exists, returns its associated native window and its screen
120 // bounds. Otherwise, returns null and empty bounds.
Wei Lib847f9c2018-12-11 22:10:19121 virtual std::pair<gfx::NativeWindow, gfx::Rect> GetNodeWindowAndScreenBounds()
thanhph27d1ff52017-05-20 04:09:13122 const = 0;
Peter Kasting76a60172019-03-18 19:14:05123
124 // Returns a list of interleaved keys and values of attributes to be displayed
Leonard Grey827f34d82018-02-13 23:25:20125 // on the element in the dev tools hierarchy view.
Johannes Henkel53d2ce282019-06-18 23:14:27126 virtual std::vector<std::string> GetAttributes() const = 0;
thanhph27d1ff52017-05-20 04:09:13127
128 template <typename BackingT, typename T>
Thanh Phamdabbb4dc2017-08-08 19:41:22129 static BackingT* GetBackingElement(const UIElement* element) {
thanhph27d1ff52017-05-20 04:09:13130 return T::From(element);
Xiaohui Chenda1977a2018-10-16 20:13:02131 }
thanhph27d1ff52017-05-20 04:09:13132
Kristyn Hamasaki06170c052019-07-09 23:53:33133 // Called from PageAgent to repaint Views for Debug Bounds Rectangles
134 virtual void PaintRect() const {}
135
Kristyn Hamasaki4de3d0a12019-08-12 19:31:42136 // 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 Huangd1d32e22021-11-10 21:01:23142 // 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 Huang87e3c672021-10-30 00:53:50146
Yuheng Huangd62276332021-03-26 02:13:27147 virtual bool DispatchMouseEvent(protocol::DOM::MouseEvent* event);
148
Yuheng Huang5f4035f2021-04-20 20:19:49149 virtual bool DispatchKeyEvent(protocol::DOM::KeyEvent* event);
150
thanhph27d1ff52017-05-20 04:09:13151 protected:
152 UIElement(const UIElementType type,
153 UIElementDelegate* delegate,
154 UIElement* parent);
Kristyn Hamasaki4de3d0a12019-08-12 19:31:42155 void AddSource(std::string path, int line);
thanhph27d1ff52017-05-20 04:09:13156
157 private:
158 const int node_id_;
159 const UIElementType type_;
Peter Kasting76a60172019-03-18 19:14:05160 UIElements children_;
Bartek Nowierskifc9563f2024-04-06 19:11:47161 raw_ptr<UIElement, DanglingUntriaged> parent_;
162 raw_ptr<UIElementDelegate, DanglingUntriaged> delegate_;
Xiaohui Chenda1977a2018-10-16 20:13:02163 bool is_updating_ = false;
Kristyn Hamasaki8dee3e5b2019-07-11 22:46:33164 int base_stylesheet_id_;
Kristyn Hamasaki4de3d0a12019-08-12 19:31:42165 bool header_sent_ = false;
166 std::vector<Source> sources_;
thanhph27d1ff52017-05-20 04:09:13167};
168
thanhph3f3968512017-06-21 00:37:23169} // namespace ui_devtools
thanhph27d1ff52017-05-20 04:09:13170
Illia Martyniukf688acd62018-04-02 19:26:07171#endif // COMPONENTS_UI_DEVTOOLS_UI_ELEMENT_H_