blob: 541b4ffd41faa8b803c46b27ee2997a66944c07f [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"
Illia Martyniukf688acd62018-04-02 19:26:0714#include "components/ui_devtools/devtools_export.h"
thanhph27d1ff52017-05-20 04:09:1315#include "ui/gfx/geometry/rect.h"
Leonard Grey601aae3f2018-01-19 17:45:5416#include "ui/gfx/native_widget_types.h"
thanhph27d1ff52017-05-20 04:09:1317
thanhph3f3968512017-06-21 00:37:2318namespace ui_devtools {
thanhph27d1ff52017-05-20 04:09:1319
20class UIElementDelegate;
21
22// UIElement type.
yiyix38f1e582018-09-06 21:23:4323enum UIElementType { WINDOW, WIDGET, VIEW, ROOT, FRAMESINK, SURFACE };
thanhph27d1ff52017-05-20 04:09:1324
Illia Martyniukf688acd62018-04-02 19:26:0725class UI_DEVTOOLS_EXPORT UIElement {
thanhph27d1ff52017-05-20 04:09:1326 public:
Kristyn Hamasaki8dee3e5b2019-07-11 22:46:3327 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 Hamasaki4de3d0a12019-08-12 19:31:4243 struct UI_DEVTOOLS_EXPORT Source {
44 Source(std::string path, int line);
45
46 std::string path_;
47 int line_;
48 };
49
Peter Kasting76a60172019-03-18 19:14:0550 using UIElements = std::vector<UIElement*>;
51
Andrew Lee5370cc52019-06-13 20:09:1652 // resets node ids to 0 so that they are reusable
53 static void ResetNodeId();
54
thanhph27d1ff52017-05-20 04:09:1355 virtual ~UIElement();
Xiaohui Chenda1977a2018-10-16 20:13:0256 int node_id() const { return node_id_; }
thanhph27d1ff52017-05-20 04:09:1357 std::string GetTypeName() const;
Xiaohui Chenda1977a2018-10-16 20:13:0258 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 Kasting76a60172019-03-18 19:14:0562 const UIElements& children() const { return children_; }
Xiaohui Chenda1977a2018-10-16 20:13:0263 bool is_updating() const { return is_updating_; }
64 void set_is_updating(bool is_updating) { is_updating_ = is_updating; }
Sean Gilhuly1e895c92018-12-03 19:52:4165 void set_owns_children(bool owns_children) { owns_children_ = owns_children; }
Kristyn Hamasaki8dee3e5b2019-07-11 22:46:3366 int GetBaseStylesheetId() const { return base_stylesheet_id_; }
67 void SetBaseStylesheetId(int id) { base_stylesheet_id_ = id; }
thanhph27d1ff52017-05-20 04:09:1368
Kristyn Hamasaki4de3d0a12019-08-12 19:31:4269 // 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 Gilhuly5e4e3fd2019-01-29 20:50:4774 using ElementCompare = bool (*)(const UIElement*, const UIElement*);
75
Peter Kasting76a60172019-03-18 19:14:0576 // Inserts |child| in front of |before|. If |before| is null, it is inserted
77 // at the end. Parent takes ownership of the added child.
thanhph27d1ff52017-05-20 04:09:1378 void AddChild(UIElement* child, UIElement* before = nullptr);
79
Peter Kasting76a60172019-03-18 19:14:0580 // 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 Gilhuly5e4e3fd2019-01-29 20:50:4783 void AddOrderedChild(UIElement* child,
84 ElementCompare compare,
85 bool notify_delegate = true);
86
Sean Gilhuly8844de62018-11-21 15:45:0687 // Removes all elements from |children_|. Caller is responsible for destroying
88 // children.
89 void ClearChildren();
90
Peter Kasting76a60172019-03-18 19:14:0591 // 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 Gilhuly5e4e3fd2019-01-29 20:50:4794 void RemoveChild(UIElement* child, bool notify_delegate = true);
thanhph27d1ff52017-05-20 04:09:1395
Peter Kasting9f829d82019-03-30 06:27:2096 // Moves |child| to position |index| in |children_|.
97 void ReorderChild(UIElement* child, int index);
thanhph27d1ff52017-05-20 04:09:1398
Thanh Phamdabbb4dc2017-08-08 19:41:2299 template <class T>
100 int FindUIElementIdForBackendElement(T* element) const;
101
Kristyn Hamasaki8dee3e5b2019-07-11 22:46:33102 // Returns properties grouped by the class they are from.
103 virtual std::vector<ClassProperties> GetCustomPropertiesForMatchedStyle()
104 const;
105
thanhph27d1ff52017-05-20 04:09:13106 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 Li6bce39542019-04-23 20:09:51111 // 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 Kasting76a60172019-03-18 19:14:05116 // If element exists, returns its associated native window and its screen
117 // bounds. Otherwise, returns null and empty bounds.
Wei Lib847f9c2018-12-11 22:10:19118 virtual std::pair<gfx::NativeWindow, gfx::Rect> GetNodeWindowAndScreenBounds()
thanhph27d1ff52017-05-20 04:09:13119 const = 0;
Peter Kasting76a60172019-03-18 19:14:05120
121 // Returns a list of interleaved keys and values of attributes to be displayed
Leonard Grey827f34d82018-02-13 23:25:20122 // on the element in the dev tools hierarchy view.
Johannes Henkel53d2ce282019-06-18 23:14:27123 virtual std::vector<std::string> GetAttributes() const = 0;
thanhph27d1ff52017-05-20 04:09:13124
125 template <typename BackingT, typename T>
Thanh Phamdabbb4dc2017-08-08 19:41:22126 static BackingT* GetBackingElement(const UIElement* element) {
thanhph27d1ff52017-05-20 04:09:13127 return T::From(element);
Xiaohui Chenda1977a2018-10-16 20:13:02128 }
thanhph27d1ff52017-05-20 04:09:13129
Kristyn Hamasaki06170c052019-07-09 23:53:33130 // Called from PageAgent to repaint Views for Debug Bounds Rectangles
131 virtual void PaintRect() const {}
132
Kristyn Hamasaki4de3d0a12019-08-12 19:31:42133 // 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
thanhph27d1ff52017-05-20 04:09:13139 protected:
140 UIElement(const UIElementType type,
141 UIElementDelegate* delegate,
142 UIElement* parent);
Kristyn Hamasaki4de3d0a12019-08-12 19:31:42143 void AddSource(std::string path, int line);
thanhph27d1ff52017-05-20 04:09:13144
145 private:
146 const int node_id_;
147 const UIElementType type_;
Peter Kasting76a60172019-03-18 19:14:05148 UIElements children_;
thanhph27d1ff52017-05-20 04:09:13149 UIElement* parent_;
150 UIElementDelegate* delegate_;
Xiaohui Chenda1977a2018-10-16 20:13:02151 bool is_updating_ = false;
Sean Gilhuly1e895c92018-12-03 19:52:41152 bool owns_children_ = true;
Kristyn Hamasaki8dee3e5b2019-07-11 22:46:33153 int base_stylesheet_id_;
Kristyn Hamasaki4de3d0a12019-08-12 19:31:42154 bool header_sent_ = false;
155 std::vector<Source> sources_;
thanhph27d1ff52017-05-20 04:09:13156
157 DISALLOW_COPY_AND_ASSIGN(UIElement);
158};
159
thanhph3f3968512017-06-21 00:37:23160} // namespace ui_devtools
thanhph27d1ff52017-05-20 04:09:13161
Illia Martyniukf688acd62018-04-02 19:26:07162#endif // COMPONENTS_UI_DEVTOOLS_UI_ELEMENT_H_