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