Revert of Mandoline UI Process: Update namespaces and file names (patchset #9 id...
[chromium-blink-merge.git] / components / mus / public / cpp / view.h
blobe2cded914eccfa903559df73c66192646fe62e66
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_MUS_PUBLIC_CPP_VIEW_H_
6 #define COMPONENTS_MUS_PUBLIC_CPP_VIEW_H_
8 #include <stdint.h>
9 #include <vector>
11 #include "base/callback.h"
12 #include "base/observer_list.h"
13 #include "components/mus/public/cpp/types.h"
14 #include "components/mus/public/interfaces/surface_id.mojom.h"
15 #include "components/mus/public/interfaces/view_manager_constants.mojom.h"
16 #include "components/mus/public/interfaces/view_tree.mojom.h"
17 #include "mojo/application/public/interfaces/service_provider.mojom.h"
18 #include "third_party/mojo/src/mojo/public/cpp/bindings/array.h"
19 #include "third_party/mojo/src/mojo/public/cpp/system/macros.h"
20 #include "ui/mojo/geometry/geometry.mojom.h"
22 namespace mojo {
24 class ServiceProviderImpl;
25 class View;
26 class ViewObserver;
27 class ViewSurface;
28 class ViewTreeConnection;
30 // Defined in view_property.h (which we do not include)
31 template <typename T>
32 struct ViewProperty;
34 // Views are owned by the ViewTreeConnection. See ViewTreeDelegate for details
35 // on ownership.
37 // TODO(beng): Right now, you'll have to implement a ViewObserver to track
38 // destruction and NULL any pointers you have.
39 // Investigate some kind of smart pointer or weak pointer for these.
40 class View {
41 public:
42 using Children = std::vector<View*>;
43 using SharedProperties = std::map<std::string, std::vector<uint8_t>>;
44 using EmbedCallback = base::Callback<void(bool, ConnectionSpecificId)>;
46 // Destroys this view and all its children. Destruction is allowed for views
47 // that were created by this connection. For views from other connections
48 // (such as the root) Destroy() does nothing. If the destruction is allowed
49 // observers are notified and the View is immediately deleted.
50 void Destroy();
52 ViewTreeConnection* connection() { return connection_; }
54 // Configuration.
55 Id id() const { return id_; }
57 // Geometric disposition.
58 const Rect& bounds() const { return bounds_; }
59 void SetBounds(const Rect& bounds);
61 // Visibility (also see IsDrawn()). When created views are hidden.
62 bool visible() const { return visible_; }
63 void SetVisible(bool value);
65 const ViewportMetrics& viewport_metrics() { return *viewport_metrics_; }
67 scoped_ptr<ViewSurface> RequestSurface();
69 // Returns the set of string to bag of byte properties. These properties are
70 // shared with the view manager.
71 const SharedProperties& shared_properties() const { return properties_; }
72 // Sets a property. If |data| is null, this property is deleted.
73 void SetSharedProperty(const std::string& name,
74 const std::vector<uint8_t>* data);
76 // Sets the |value| of the given window |property|. Setting to the default
77 // value (e.g., NULL) removes the property. The caller is responsible for the
78 // lifetime of any object set as a property on the View.
80 // These properties are not visible to the view manager.
81 template <typename T>
82 void SetLocalProperty(const ViewProperty<T>* property, T value);
84 // Returns the value of the given window |property|. Returns the
85 // property-specific default value if the property was not previously set.
87 // These properties are only visible in the current process and are not
88 // shared with other mojo services.
89 template <typename T>
90 T GetLocalProperty(const ViewProperty<T>* property) const;
92 // Sets the |property| to its default value. Useful for avoiding a cast when
93 // setting to NULL.
95 // These properties are only visible in the current process and are not
96 // shared with other mojo services.
97 template <typename T>
98 void ClearLocalProperty(const ViewProperty<T>* property);
100 // Type of a function to delete a property that this view owns.
101 typedef void (*PropertyDeallocator)(int64_t value);
103 // A View is drawn if the View and all its ancestors are visible and the
104 // View is attached to the root.
105 bool IsDrawn() const;
107 // Observation.
108 void AddObserver(ViewObserver* observer);
109 void RemoveObserver(ViewObserver* observer);
111 // Tree.
112 View* parent() { return parent_; }
113 const View* parent() const { return parent_; }
114 const Children& children() const { return children_; }
115 View* GetRoot() {
116 return const_cast<View*>(const_cast<const View*>(this)->GetRoot());
118 const View* GetRoot() const;
120 void AddChild(View* child);
121 void RemoveChild(View* child);
123 void Reorder(View* relative, OrderDirection direction);
124 void MoveToFront();
125 void MoveToBack();
127 bool Contains(View* child) const;
129 View* GetChildById(Id id);
131 void SetTextInputState(TextInputStatePtr state);
132 void SetImeVisibility(bool visible, TextInputStatePtr state);
134 // Focus.
135 void SetFocus();
136 bool HasFocus() const;
138 // Embedding. See view_tree.mojom for details.
139 void Embed(ViewTreeClientPtr client);
141 // NOTE: callback is run synchronously if Embed() is not allowed on this
142 // View.
143 void Embed(ViewTreeClientPtr client,
144 uint32_t policy_bitmask,
145 const EmbedCallback& callback);
147 protected:
148 // This class is subclassed only by test classes that provide a public ctor.
149 View();
150 ~View();
152 private:
153 friend class ViewPrivate;
154 friend class ViewTreeClientImpl;
156 View(ViewTreeConnection* connection, Id id);
158 // Called by the public {Set,Get,Clear}Property functions.
159 int64_t SetLocalPropertyInternal(const void* key,
160 const char* name,
161 PropertyDeallocator deallocator,
162 int64_t value,
163 int64_t default_value);
164 int64_t GetLocalPropertyInternal(const void* key,
165 int64_t default_value) const;
167 void LocalDestroy();
168 void LocalAddChild(View* child);
169 void LocalRemoveChild(View* child);
170 // Returns true if the order actually changed.
171 bool LocalReorder(View* relative, OrderDirection direction);
172 void LocalSetBounds(const Rect& old_bounds, const Rect& new_bounds);
173 void LocalSetViewportMetrics(const ViewportMetrics& old_metrics,
174 const ViewportMetrics& new_metrics);
175 void LocalSetDrawn(bool drawn);
176 void LocalSetVisible(bool visible);
178 // Methods implementing visibility change notifications. See ViewObserver
179 // for more details.
180 void NotifyViewVisibilityChanged(View* target);
181 // Notifies this view's observers. Returns false if |this| was deleted during
182 // the call (by an observer), otherwise true.
183 bool NotifyViewVisibilityChangedAtReceiver(View* target);
184 // Notifies this view and its child hierarchy. Returns false if |this| was
185 // deleted during the call (by an observer), otherwise true.
186 bool NotifyViewVisibilityChangedDown(View* target);
187 // Notifies this view and its parent hierarchy.
188 void NotifyViewVisibilityChangedUp(View* target);
190 // Returns true if embed is allowed for this node. If embedding is allowed all
191 // the children are removed.
192 bool PrepareForEmbed();
194 ViewTreeConnection* connection_;
195 Id id_;
196 View* parent_;
197 Children children_;
199 base::ObserverList<ViewObserver> observers_;
201 Rect bounds_;
202 ViewportMetricsPtr viewport_metrics_;
204 bool visible_;
206 SharedProperties properties_;
208 // Drawn state is derived from the visible state and the parent's visible
209 // state. This field is only used if the view has no parent (eg it's a root).
210 bool drawn_;
212 // Value struct to keep the name and deallocator for this property.
213 // Key cannot be used for this purpose because it can be char* or
214 // WindowProperty<>.
215 struct Value {
216 const char* name;
217 int64_t value;
218 PropertyDeallocator deallocator;
221 std::map<const void*, Value> prop_map_;
223 MOJO_DISALLOW_COPY_AND_ASSIGN(View);
226 } // namespace mojo
228 #endif // COMPONENTS_MUS_PUBLIC_CPP_VIEW_H_