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