1 // Copyright 2014 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.
5 #ifndef COMPONENTS_VIEW_MANAGER_PUBLIC_CPP_VIEW_H_
6 #define COMPONENTS_VIEW_MANAGER_PUBLIC_CPP_VIEW_H_
11 #include "base/observer_list.h"
12 #include "components/view_manager/public/cpp/types.h"
13 #include "components/view_manager/public/interfaces/surface_id.mojom.h"
14 #include "components/view_manager/public/interfaces/view_manager_constants.mojom.h"
15 #include "components/view_manager/public/interfaces/view_tree.mojom.h"
16 #include "mojo/application/public/interfaces/service_provider.mojom.h"
17 #include "third_party/mojo/src/mojo/public/cpp/bindings/array.h"
18 #include "third_party/mojo/src/mojo/public/cpp/system/macros.h"
19 #include "ui/mojo/geometry/geometry.mojom.h"
23 class ServiceProviderImpl
;
27 class ViewTreeConnection
;
29 // Defined in view_property.h (which we do not include)
33 // Views are owned by the ViewTreeConnection. See ViewTreeDelegate for details
36 // TODO(beng): Right now, you'll have to implement a ViewObserver to track
37 // destruction and NULL any pointers you have.
38 // Investigate some kind of smart pointer or weak pointer for these.
41 using Children
= std::vector
<View
*>;
42 using SharedProperties
= std::map
<std::string
, std::vector
<uint8_t>>;
44 // Destroys this view and all its children. Destruction is allowed for views
45 // that were created by this connection. For views from other connections
46 // (such as the root) Destroy() does nothing. If the destruction is allowed
47 // observers are notified and the View is immediately deleted.
50 ViewTreeConnection
* connection() { return connection_
; }
53 Id
id() const { return id_
; }
55 // Geometric disposition.
56 const Rect
& bounds() const { return bounds_
; }
57 void SetBounds(const Rect
& bounds
);
59 // Visibility (also see IsDrawn()). When created views are hidden.
60 bool visible() const { return visible_
; }
61 void SetVisible(bool value
);
63 const ViewportMetrics
& viewport_metrics() { return *viewport_metrics_
; }
65 scoped_ptr
<ViewSurface
> RequestSurface();
67 // Returns the set of string to bag of byte properties. These properties are
68 // shared with the view manager.
69 const SharedProperties
& shared_properties() const { return properties_
; }
70 // Sets a property. If |data| is null, this property is deleted.
71 void SetSharedProperty(const std::string
& name
,
72 const std::vector
<uint8_t>* data
);
74 // Sets the |value| of the given window |property|. Setting to the default
75 // value (e.g., NULL) removes the property. The caller is responsible for the
76 // lifetime of any object set as a property on the View.
78 // These properties are not visible to the view manager.
80 void SetLocalProperty(const ViewProperty
<T
>* property
, T value
);
82 // Returns the value of the given window |property|. Returns the
83 // property-specific default value if the property was not previously set.
85 // These properties are only visible in the current process and are not
86 // shared with other mojo services.
88 T
GetLocalProperty(const ViewProperty
<T
>* property
) const;
90 // Sets the |property| to its default value. Useful for avoiding a cast when
93 // These properties are only visible in the current process and are not
94 // shared with other mojo services.
96 void ClearLocalProperty(const ViewProperty
<T
>* property
);
98 // Type of a function to delete a property that this view owns.
99 typedef void (*PropertyDeallocator
)(int64_t value
);
101 // A View is drawn if the View and all its ancestors are visible and the
102 // View is attached to the root.
103 bool IsDrawn() const;
105 // See mojom for details.
106 void SetAccessPolicy(uint32_t policy_bitmask
);
109 void AddObserver(ViewObserver
* observer
);
110 void RemoveObserver(ViewObserver
* observer
);
113 View
* parent() { return parent_
; }
114 const View
* parent() const { return parent_
; }
115 const Children
& children() const { return children_
; }
117 return const_cast<View
*>(const_cast<const View
*>(this)->GetRoot());
119 const View
* GetRoot() const;
121 void AddChild(View
* child
);
122 void RemoveChild(View
* child
);
124 void Reorder(View
* relative
, OrderDirection direction
);
128 bool Contains(View
* child
) const;
130 View
* GetChildById(Id id
);
132 void SetTextInputState(TextInputStatePtr state
);
133 void SetImeVisibility(bool visible
, TextInputStatePtr state
);
137 bool HasFocus() const;
139 // Embedding. See view_tree.mojom for details.
140 void Embed(ViewTreeClientPtr client
);
143 // This class is subclassed only by test classes that provide a public ctor.
148 friend class ViewPrivate
;
149 friend class ViewTreeClientImpl
;
151 View(ViewTreeConnection
* connection
, Id id
);
153 // Called by the public {Set,Get,Clear}Property functions.
154 int64_t SetLocalPropertyInternal(const void* key
,
156 PropertyDeallocator deallocator
,
158 int64_t default_value
);
159 int64_t GetLocalPropertyInternal(const void* key
,
160 int64_t default_value
) const;
163 void LocalAddChild(View
* child
);
164 void LocalRemoveChild(View
* child
);
165 // Returns true if the order actually changed.
166 bool LocalReorder(View
* relative
, OrderDirection direction
);
167 void LocalSetBounds(const Rect
& old_bounds
, const Rect
& new_bounds
);
168 void LocalSetViewportMetrics(const ViewportMetrics
& old_metrics
,
169 const ViewportMetrics
& new_metrics
);
170 void LocalSetDrawn(bool drawn
);
171 void LocalSetVisible(bool visible
);
173 // Methods implementing visibility change notifications. See ViewObserver
175 void NotifyViewVisibilityChanged(View
* target
);
176 // Notifies this view's observers. Returns false if |this| was deleted during
177 // the call (by an observer), otherwise true.
178 bool NotifyViewVisibilityChangedAtReceiver(View
* target
);
179 // Notifies this view and its child hierarchy. Returns false if |this| was
180 // deleted during the call (by an observer), otherwise true.
181 bool NotifyViewVisibilityChangedDown(View
* target
);
182 // Notifies this view and its parent hierarchy.
183 void NotifyViewVisibilityChangedUp(View
* target
);
185 // Returns true if embed is allowed for this node. If embedding is allowed all
186 // the children are removed.
187 bool PrepareForEmbed();
189 ViewTreeConnection
* connection_
;
194 base::ObserverList
<ViewObserver
> observers_
;
197 ViewportMetricsPtr viewport_metrics_
;
201 SharedProperties properties_
;
203 // Drawn state is derived from the visible state and the parent's visible
204 // state. This field is only used if the view has no parent (eg it's a root).
207 // Value struct to keep the name and deallocator for this property.
208 // Key cannot be used for this purpose because it can be char* or
213 PropertyDeallocator deallocator
;
216 std::map
<const void*, Value
> prop_map_
;
218 MOJO_DISALLOW_COPY_AND_ASSIGN(View
);
223 #endif // COMPONENTS_VIEW_MANAGER_PUBLIC_CPP_VIEW_H_