blob: 9a081f7648cedb7410cc20103a15a831013594ee [file] [log] [blame]
thanhph27d1ff52017-05-20 04:09:131// 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 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
thanhph27d1ff52017-05-20 04:09:1313#include "base/macros.h"
Yuheng Huangd62276332021-03-26 02:13:2714#include "components/ui_devtools/DOM.h"
Illia Martyniukf688acd62018-04-02 19:26:0715#include "components/ui_devtools/devtools_export.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; }
Sean Gilhuly1e895c92018-12-03 19:52:4169 void set_owns_children(bool owns_children) { owns_children_ = owns_children; }
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
Sean Gilhuly8844de62018-11-21 15:45:0691 // Removes all elements from |children_|. Caller is responsible for destroying
92 // children.
93 void ClearChildren();
94
Peter Kasting76a60172019-03-18 19:14:0595 // Removes |child| out of |children_| without destroying |child|. The caller
96 // is responsible for destroying |child|. |notify_delegate| calls
97 // OnUIElementRemoved(), which destroys the DOM node for |child|.
Sean Gilhuly5e4e3fd2019-01-29 20:50:4798 void RemoveChild(UIElement* child, bool notify_delegate = true);
thanhph27d1ff52017-05-20 04:09:1399
Peter Kasting9f829d82019-03-30 06:27:20100 // Moves |child| to position |index| in |children_|.
101 void ReorderChild(UIElement* child, int index);
thanhph27d1ff52017-05-20 04:09:13102
Thanh Phamdabbb4dc2017-08-08 19:41:22103 template <class T>
104 int FindUIElementIdForBackendElement(T* element) const;
105
Kristyn Hamasaki8dee3e5b2019-07-11 22:46:33106 // Returns properties grouped by the class they are from.
107 virtual std::vector<ClassProperties> GetCustomPropertiesForMatchedStyle()
108 const;
109
thanhph27d1ff52017-05-20 04:09:13110 virtual void GetBounds(gfx::Rect* bounds) const = 0;
111 virtual void SetBounds(const gfx::Rect& bounds) = 0;
112 virtual void GetVisible(bool* visible) const = 0;
113 virtual void SetVisible(bool visible) = 0;
114
Wei Li6bce39542019-04-23 20:09:51115 // Set this element's property values according to |text|.
116 // |text| is the string passed in through StyleDeclarationEdit::text from
117 // the frontend.
118 virtual bool SetPropertiesFromString(const std::string& text);
119
Peter Kasting76a60172019-03-18 19:14:05120 // If element exists, returns its associated native window and its screen
121 // bounds. Otherwise, returns null and empty bounds.
Wei Lib847f9c2018-12-11 22:10:19122 virtual std::pair<gfx::NativeWindow, gfx::Rect> GetNodeWindowAndScreenBounds()
thanhph27d1ff52017-05-20 04:09:13123 const = 0;
Peter Kasting76a60172019-03-18 19:14:05124
125 // Returns a list of interleaved keys and values of attributes to be displayed
Leonard Grey827f34d82018-02-13 23:25:20126 // on the element in the dev tools hierarchy view.
Johannes Henkel53d2ce282019-06-18 23:14:27127 virtual std::vector<std::string> GetAttributes() const = 0;
thanhph27d1ff52017-05-20 04:09:13128
129 template <typename BackingT, typename T>
Thanh Phamdabbb4dc2017-08-08 19:41:22130 static BackingT* GetBackingElement(const UIElement* element) {
thanhph27d1ff52017-05-20 04:09:13131 return T::From(element);
Xiaohui Chenda1977a2018-10-16 20:13:02132 }
thanhph27d1ff52017-05-20 04:09:13133
Kristyn Hamasaki06170c052019-07-09 23:53:33134 // Called from PageAgent to repaint Views for Debug Bounds Rectangles
135 virtual void PaintRect() const {}
136
Kristyn Hamasaki4de3d0a12019-08-12 19:31:42137 // Called in the constructor to initialize the element's sources.
138 virtual void InitSources() {}
139
140 // Get the sources for the element.
141 std::vector<Source> GetSources();
142
Yuheng Huangd62276332021-03-26 02:13:27143 virtual bool DispatchMouseEvent(protocol::DOM::MouseEvent* event);
144
Yuheng Huang5f4035f2021-04-20 20:19:49145 virtual bool DispatchKeyEvent(protocol::DOM::KeyEvent* event);
146
thanhph27d1ff52017-05-20 04:09:13147 protected:
148 UIElement(const UIElementType type,
149 UIElementDelegate* delegate,
150 UIElement* parent);
Kristyn Hamasaki4de3d0a12019-08-12 19:31:42151 void AddSource(std::string path, int line);
thanhph27d1ff52017-05-20 04:09:13152
153 private:
154 const int node_id_;
155 const UIElementType type_;
Peter Kasting76a60172019-03-18 19:14:05156 UIElements children_;
thanhph27d1ff52017-05-20 04:09:13157 UIElement* parent_;
158 UIElementDelegate* delegate_;
Xiaohui Chenda1977a2018-10-16 20:13:02159 bool is_updating_ = false;
Sean Gilhuly1e895c92018-12-03 19:52:41160 bool owns_children_ = true;
Kristyn Hamasaki8dee3e5b2019-07-11 22:46:33161 int base_stylesheet_id_;
Kristyn Hamasaki4de3d0a12019-08-12 19:31:42162 bool header_sent_ = false;
163 std::vector<Source> sources_;
thanhph27d1ff52017-05-20 04:09:13164};
165
thanhph3f3968512017-06-21 00:37:23166} // namespace ui_devtools
thanhph27d1ff52017-05-20 04:09:13167
Illia Martyniukf688acd62018-04-02 19:26:07168#endif // COMPONENTS_UI_DEVTOOLS_UI_ELEMENT_H_