blob: 217e4063cc26d962f8b4f6455100ac33ac9d21c1 [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
Leonard Grey827f34d82018-02-13 23:25:2022namespace protocol {
23template <typename T>
24class Array;
25}
26
thanhph27d1ff52017-05-20 04:09:1327// UIElement type.
yiyix38f1e582018-09-06 21:23:4328enum UIElementType { WINDOW, WIDGET, VIEW, ROOT, FRAMESINK, SURFACE };
thanhph27d1ff52017-05-20 04:09:1329
Illia Martyniukf688acd62018-04-02 19:26:0730class UI_DEVTOOLS_EXPORT UIElement {
thanhph27d1ff52017-05-20 04:09:1331 public:
Peter Kasting76a60172019-03-18 19:14:0532 using UIElements = std::vector<UIElement*>;
33
thanhph27d1ff52017-05-20 04:09:1334 virtual ~UIElement();
Xiaohui Chenda1977a2018-10-16 20:13:0235 int node_id() const { return node_id_; }
thanhph27d1ff52017-05-20 04:09:1336 std::string GetTypeName() const;
Xiaohui Chenda1977a2018-10-16 20:13:0237 UIElement* parent() const { return parent_; }
38 void set_parent(UIElement* parent) { parent_ = parent; }
39 UIElementDelegate* delegate() const { return delegate_; }
40 UIElementType type() const { return type_; }
Peter Kasting76a60172019-03-18 19:14:0541 const UIElements& children() const { return children_; }
Xiaohui Chenda1977a2018-10-16 20:13:0242 bool is_updating() const { return is_updating_; }
43 void set_is_updating(bool is_updating) { is_updating_ = is_updating; }
Sean Gilhuly1e895c92018-12-03 19:52:4144 void set_owns_children(bool owns_children) { owns_children_ = owns_children; }
thanhph27d1ff52017-05-20 04:09:1345
Sean Gilhuly5e4e3fd2019-01-29 20:50:4746 using ElementCompare = bool (*)(const UIElement*, const UIElement*);
47
Peter Kasting76a60172019-03-18 19:14:0548 // Inserts |child| in front of |before|. If |before| is null, it is inserted
49 // at the end. Parent takes ownership of the added child.
thanhph27d1ff52017-05-20 04:09:1350 void AddChild(UIElement* child, UIElement* before = nullptr);
51
Peter Kasting76a60172019-03-18 19:14:0552 // Inserts |child| according to a custom ordering function. |notify_delegate|
53 // calls OnUIElementAdded(), which creates the subtree of UIElements at
54 // |child|, and the corresponding DOM nodes.
Sean Gilhuly5e4e3fd2019-01-29 20:50:4755 void AddOrderedChild(UIElement* child,
56 ElementCompare compare,
57 bool notify_delegate = true);
58
Sean Gilhuly8844de62018-11-21 15:45:0659 // Removes all elements from |children_|. Caller is responsible for destroying
60 // children.
61 void ClearChildren();
62
Peter Kasting76a60172019-03-18 19:14:0563 // Removes |child| out of |children_| without destroying |child|. The caller
64 // is responsible for destroying |child|. |notify_delegate| calls
65 // OnUIElementRemoved(), which destroys the DOM node for |child|.
Sean Gilhuly5e4e3fd2019-01-29 20:50:4766 void RemoveChild(UIElement* child, bool notify_delegate = true);
thanhph27d1ff52017-05-20 04:09:1367
Peter Kasting9f829d82019-03-30 06:27:2068 // Moves |child| to position |index| in |children_|.
69 void ReorderChild(UIElement* child, int index);
thanhph27d1ff52017-05-20 04:09:1370
Thanh Phamdabbb4dc2017-08-08 19:41:2271 template <class T>
72 int FindUIElementIdForBackendElement(T* element) const;
73
Peter Kasting76a60172019-03-18 19:14:0574 // Returns properties' names and values.
Leonard Grey827f34d82018-02-13 23:25:2075 virtual std::vector<std::pair<std::string, std::string>> GetCustomProperties()
Thanh Pham5e4f231c2017-08-14 20:45:4676 const = 0;
Peter Kasting76a60172019-03-18 19:14:0577
thanhph27d1ff52017-05-20 04:09:1378 virtual void GetBounds(gfx::Rect* bounds) const = 0;
79 virtual void SetBounds(const gfx::Rect& bounds) = 0;
80 virtual void GetVisible(bool* visible) const = 0;
81 virtual void SetVisible(bool visible) = 0;
82
Peter Kasting76a60172019-03-18 19:14:0583 // If element exists, returns its associated native window and its screen
84 // bounds. Otherwise, returns null and empty bounds.
Wei Lib847f9c2018-12-11 22:10:1985 virtual std::pair<gfx::NativeWindow, gfx::Rect> GetNodeWindowAndScreenBounds()
thanhph27d1ff52017-05-20 04:09:1386 const = 0;
Peter Kasting76a60172019-03-18 19:14:0587
88 // Returns a list of interleaved keys and values of attributes to be displayed
Leonard Grey827f34d82018-02-13 23:25:2089 // on the element in the dev tools hierarchy view.
90 virtual std::unique_ptr<protocol::Array<std::string>> GetAttributes()
91 const = 0;
thanhph27d1ff52017-05-20 04:09:1392
93 template <typename BackingT, typename T>
Thanh Phamdabbb4dc2017-08-08 19:41:2294 static BackingT* GetBackingElement(const UIElement* element) {
thanhph27d1ff52017-05-20 04:09:1395 return T::From(element);
Xiaohui Chenda1977a2018-10-16 20:13:0296 }
thanhph27d1ff52017-05-20 04:09:1397
98 protected:
99 UIElement(const UIElementType type,
100 UIElementDelegate* delegate,
101 UIElement* parent);
102
103 private:
104 const int node_id_;
105 const UIElementType type_;
Peter Kasting76a60172019-03-18 19:14:05106 UIElements children_;
thanhph27d1ff52017-05-20 04:09:13107 UIElement* parent_;
108 UIElementDelegate* delegate_;
Xiaohui Chenda1977a2018-10-16 20:13:02109 bool is_updating_ = false;
Sean Gilhuly1e895c92018-12-03 19:52:41110 bool owns_children_ = true;
thanhph27d1ff52017-05-20 04:09:13111
112 DISALLOW_COPY_AND_ASSIGN(UIElement);
113};
114
thanhph3f3968512017-06-21 00:37:23115} // namespace ui_devtools
thanhph27d1ff52017-05-20 04:09:13116
Illia Martyniukf688acd62018-04-02 19:26:07117#endif // COMPONENTS_UI_DEVTOOLS_UI_ELEMENT_H_