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.mojom.h"
15 #include "components/view_manager/public/interfaces/view_manager_constants.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
;
28 // Defined in view_property.h (which we do not include)
32 // Views are owned by the ViewManager. See ViewManagerDelegate for details on
35 // TODO(beng): Right now, you'll have to implement a ViewObserver to track
36 // destruction and NULL any pointers you have.
37 // Investigate some kind of smart pointer or weak pointer for these.
40 using Children
= std::vector
<View
*>;
41 using SharedProperties
= std::map
<std::string
, std::vector
<uint8_t>>;
43 // Destroys this view and all its children. Destruction is allowed for views
44 // that were created by this connection. For views from other connections
45 // (such as the root) Destroy() does nothing. If the destruction is allowed
46 // observers are notified and the View is immediately deleted.
49 ViewManager
* view_manager() { return manager_
; }
52 Id
id() const { return id_
; }
54 // Geometric disposition.
55 const Rect
& bounds() const { return bounds_
; }
56 void SetBounds(const Rect
& bounds
);
58 // Visibility (also see IsDrawn()). When created views are hidden.
59 bool visible() const { return visible_
; }
60 void SetVisible(bool value
);
62 const ViewportMetrics
& viewport_metrics() { return *viewport_metrics_
; }
64 // Returns the set of string to bag of byte properties. These properties are
65 // shared with the view manager.
66 const SharedProperties
& shared_properties() const { return properties_
; }
67 // Sets a property. If |data| is null, this property is deleted.
68 void SetSharedProperty(const std::string
& name
,
69 const std::vector
<uint8_t>* data
);
71 // Sets the |value| of the given window |property|. Setting to the default
72 // value (e.g., NULL) removes the property. The caller is responsible for the
73 // lifetime of any object set as a property on the View.
75 // These properties are not visible to the view manager.
77 void SetLocalProperty(const ViewProperty
<T
>* property
, T value
);
79 // Returns the value of the given window |property|. Returns the
80 // property-specific default value if the property was not previously set.
82 // These properties are only visible in the current process and are not
83 // shared with other mojo services.
85 T
GetLocalProperty(const ViewProperty
<T
>* property
) const;
87 // Sets the |property| to its default value. Useful for avoiding a cast when
90 // These properties are only visible in the current process and are not
91 // shared with other mojo services.
93 void ClearLocalProperty(const ViewProperty
<T
>* property
);
95 // Type of a function to delete a property that this view owns.
96 typedef void (*PropertyDeallocator
)(int64_t value
);
98 // A View is drawn if the View and all its ancestors are visible and the
99 // View is attached to the root.
100 bool IsDrawn() const;
103 void AddObserver(ViewObserver
* observer
);
104 void RemoveObserver(ViewObserver
* observer
);
107 View
* parent() { return parent_
; }
108 const View
* parent() const { return parent_
; }
109 const Children
& children() const { return children_
; }
111 return const_cast<View
*>(const_cast<const View
*>(this)->GetRoot());
113 const View
* GetRoot() const;
115 void AddChild(View
* child
);
116 void RemoveChild(View
* child
);
118 void Reorder(View
* relative
, OrderDirection direction
);
122 bool Contains(View
* child
) const;
124 View
* GetChildById(Id id
);
126 void SetSurfaceId(SurfaceIdPtr id
);
128 void SetTextInputState(TextInputStatePtr state
);
129 void SetImeVisibility(bool visible
, TextInputStatePtr state
);
133 bool HasFocus() const;
135 // Embedding. See view_manager.mojom for details.
136 void Embed(ViewManagerClientPtr client
);
137 void EmbedAllowingReembed(mojo::URLRequestPtr request
);
140 // This class is subclassed only by test classes that provide a public ctor.
145 friend class ViewPrivate
;
146 friend class ViewManagerClientImpl
;
148 View(ViewManager
* manager
, Id id
);
150 // Called by the public {Set,Get,Clear}Property functions.
151 int64_t SetLocalPropertyInternal(const void* key
,
153 PropertyDeallocator deallocator
,
155 int64_t default_value
);
156 int64_t GetLocalPropertyInternal(const void* key
,
157 int64_t default_value
) const;
160 void LocalAddChild(View
* child
);
161 void LocalRemoveChild(View
* child
);
162 // Returns true if the order actually changed.
163 bool LocalReorder(View
* relative
, OrderDirection direction
);
164 void LocalSetBounds(const Rect
& old_bounds
, const Rect
& new_bounds
);
165 void LocalSetViewportMetrics(const ViewportMetrics
& old_metrics
,
166 const ViewportMetrics
& new_metrics
);
167 void LocalSetDrawn(bool drawn
);
168 void LocalSetVisible(bool visible
);
170 // Methods implementing visibility change notifications. See ViewObserver
172 void NotifyViewVisibilityChanged(View
* target
);
173 // Notifies this view's observers. Returns false if |this| was deleted during
174 // the call (by an observer), otherwise true.
175 bool NotifyViewVisibilityChangedAtReceiver(View
* target
);
176 // Notifies this view and its child hierarchy. Returns false if |this| was
177 // deleted during the call (by an observer), otherwise true.
178 bool NotifyViewVisibilityChangedDown(View
* target
);
179 // Notifies this view and its parent hierarchy.
180 void NotifyViewVisibilityChangedUp(View
* target
);
182 // Returns true if embed is allowed for this node. If embedding is allowed all
183 // the children are removed.
184 bool PrepareForEmbed();
186 ViewManager
* manager_
;
191 base::ObserverList
<ViewObserver
> observers_
;
194 ViewportMetricsPtr viewport_metrics_
;
198 SharedProperties properties_
;
200 // Drawn state is derived from the visible state and the parent's visible
201 // state. This field is only used if the view has no parent (eg it's a root).
204 // Value struct to keep the name and deallocator for this property.
205 // Key cannot be used for this purpose because it can be char* or
210 PropertyDeallocator deallocator
;
213 std::map
<const void*, Value
> prop_map_
;
215 MOJO_DISALLOW_COPY_AND_ASSIGN(View
);
220 #endif // COMPONENTS_VIEW_MANAGER_PUBLIC_CPP_VIEW_H_