OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef MOJO_SERVICES_VIEW_MANAGER_PUBLIC_CPP_VIEW_H_ | |
6 #define MOJO_SERVICES_VIEW_MANAGER_PUBLIC_CPP_VIEW_H_ | |
7 | |
8 #include <vector> | |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/observer_list.h" | |
12 #include "geometry/public/interfaces/geometry.mojom.h" | |
13 #include "mojo/public/cpp/bindings/array.h" | |
14 #include "mojo/public/interfaces/application/service_provider.mojom.h" | |
15 #include "surfaces/public/interfaces/surface_id.mojom.h" | |
16 #include "view_manager/public/cpp/types.h" | |
17 #include "view_manager/public/interfaces/view_manager.mojom.h" | |
18 #include "view_manager/public/interfaces/view_manager_constants.mojom.h" | |
19 | |
20 namespace mojo { | |
21 | |
22 class ServiceProviderImpl; | |
23 class View; | |
24 class ViewManager; | |
25 class ViewObserver; | |
26 | |
27 // Defined in view_property.h (which we do not include) | |
28 template <typename T> | |
29 struct ViewProperty; | |
30 | |
31 // Views are owned by the ViewManager. | |
32 // TODO(beng): Right now, you'll have to implement a ViewObserver to track | |
33 // destruction and NULL any pointers you have. | |
34 // Investigate some kind of smart pointer or weak pointer for these. | |
35 class View { | |
36 public: | |
37 using Children = std::vector<View*>; | |
38 using SharedProperties = std::map<std::string, std::vector<uint8_t>>; | |
39 | |
40 // Destroys this view and all its children. | |
41 void Destroy(); | |
42 | |
43 ViewManager* view_manager() { return manager_; } | |
44 | |
45 // Configuration. | |
46 Id id() const { return id_; } | |
47 | |
48 // Geometric disposition. | |
49 const Rect& bounds() const { return bounds_; } | |
50 void SetBounds(const Rect& bounds); | |
51 | |
52 // Visibility (also see IsDrawn()). When created views are hidden. | |
53 bool visible() const { return visible_; } | |
54 void SetVisible(bool value); | |
55 | |
56 const ViewportMetrics& viewport_metrics() { return *viewport_metrics_; } | |
57 | |
58 // Returns the set of string to bag of byte properties. These properties are | |
59 // shared with the view manager. | |
60 const SharedProperties& shared_properties() const { return properties_; } | |
61 // Sets a property. If |data| is null, this property is deleted. | |
62 void SetSharedProperty(const std::string& name, | |
63 const std::vector<uint8_t>* data); | |
64 | |
65 // Sets the |value| of the given window |property|. Setting to the default | |
66 // value (e.g., NULL) removes the property. The caller is responsible for the | |
67 // lifetime of any object set as a property on the View. | |
68 // | |
69 // These properties are not visible to the view manager. | |
70 template <typename T> | |
71 void SetLocalProperty(const ViewProperty<T>* property, T value); | |
72 | |
73 // Returns the value of the given window |property|. Returns the | |
74 // property-specific default value if the property was not previously set. | |
75 // | |
76 // These properties are only visible in the current process and are not | |
77 // shared with other mojo services. | |
78 template <typename T> | |
79 T GetLocalProperty(const ViewProperty<T>* property) const; | |
80 | |
81 // Sets the |property| to its default value. Useful for avoiding a cast when | |
82 // setting to NULL. | |
83 // | |
84 // These properties are only visible in the current process and are not | |
85 // shared with other mojo services. | |
86 template <typename T> | |
87 void ClearLocalProperty(const ViewProperty<T>* property); | |
88 | |
89 // Type of a function to delete a property that this view owns. | |
90 typedef void (*PropertyDeallocator)(int64 value); | |
91 | |
92 // A View is drawn if the View and all its ancestors are visible and the | |
93 // View is attached to the root. | |
94 bool IsDrawn() const; | |
95 | |
96 // Observation. | |
97 void AddObserver(ViewObserver* observer); | |
98 void RemoveObserver(ViewObserver* observer); | |
99 | |
100 // Tree. | |
101 View* parent() { return parent_; } | |
102 const View* parent() const { return parent_; } | |
103 const Children& children() const { return children_; } | |
104 View* GetRoot() { | |
105 return const_cast<View*>(const_cast<const View*>(this)->GetRoot()); | |
106 } | |
107 const View* GetRoot() const; | |
108 | |
109 void AddChild(View* child); | |
110 void RemoveChild(View* child); | |
111 | |
112 void Reorder(View* relative, OrderDirection direction); | |
113 void MoveToFront(); | |
114 void MoveToBack(); | |
115 | |
116 bool Contains(View* child) const; | |
117 | |
118 View* GetChildById(Id id); | |
119 | |
120 void SetSurfaceId(SurfaceIdPtr id); | |
121 | |
122 // Focus. | |
123 void SetFocus(); | |
124 | |
125 // Embedding. | |
126 void Embed(const String& url); | |
127 void Embed(const String& url, | |
128 InterfaceRequest<ServiceProvider> services, | |
129 ServiceProviderPtr exposed_services); | |
130 | |
131 protected: | |
132 // This class is subclassed only by test classes that provide a public ctor. | |
133 View(); | |
134 ~View(); | |
135 | |
136 private: | |
137 friend class ViewPrivate; | |
138 friend class ViewManagerClientImpl; | |
139 | |
140 View(ViewManager* manager, Id id); | |
141 | |
142 // Called by the public {Set,Get,Clear}Property functions. | |
143 int64 SetLocalPropertyInternal(const void* key, | |
144 const char* name, | |
145 PropertyDeallocator deallocator, | |
146 int64 value, | |
147 int64 default_value); | |
148 int64 GetLocalPropertyInternal(const void* key, int64 default_value) const; | |
149 | |
150 void LocalDestroy(); | |
151 void LocalAddChild(View* child); | |
152 void LocalRemoveChild(View* child); | |
153 // Returns true if the order actually changed. | |
154 bool LocalReorder(View* relative, OrderDirection direction); | |
155 void LocalSetBounds(const Rect& old_bounds, const Rect& new_bounds); | |
156 void LocalSetViewportMetrics(const ViewportMetrics& old_metrics, | |
157 const ViewportMetrics& new_metrics); | |
158 void LocalSetDrawn(bool drawn); | |
159 | |
160 // Methods implementing visibility change notifications. See ViewObserver | |
161 // for more details. | |
162 void NotifyViewVisibilityChanged(View* target); | |
163 // Notifies this view's observers. Returns false if |this| was deleted during | |
164 // the call (by an observer), otherwise true. | |
165 bool NotifyViewVisibilityChangedAtReceiver(View* target); | |
166 // Notifies this view and its child hierarchy. Returns false if |this| was | |
167 // deleted during the call (by an observer), otherwise true. | |
168 bool NotifyViewVisibilityChangedDown(View* target); | |
169 // Notifies this view and its parent hierarchy. | |
170 void NotifyViewVisibilityChangedUp(View* target); | |
171 | |
172 ViewManager* manager_; | |
173 Id id_; | |
174 View* parent_; | |
175 Children children_; | |
176 | |
177 ObserverList<ViewObserver> observers_; | |
178 | |
179 Rect bounds_; | |
180 ViewportMetricsPtr viewport_metrics_; | |
181 | |
182 bool visible_; | |
183 | |
184 SharedProperties properties_; | |
185 | |
186 // Drawn state is derived from the visible state and the parent's visible | |
187 // state. This field is only used if the view has no parent (eg it's a root). | |
188 bool drawn_; | |
189 | |
190 // Value struct to keep the name and deallocator for this property. | |
191 // Key cannot be used for this purpose because it can be char* or | |
192 // WindowProperty<>. | |
193 struct Value { | |
194 const char* name; | |
195 int64 value; | |
196 PropertyDeallocator deallocator; | |
197 }; | |
198 | |
199 std::map<const void*, Value> prop_map_; | |
200 | |
201 DISALLOW_COPY_AND_ASSIGN(View); | |
202 }; | |
203 | |
204 } // namespace mojo | |
205 | |
206 #endif // MOJO_SERVICES_VIEW_MANAGER_PUBLIC_CPP_VIEW_H_ | |
OLD | NEW |