summaryrefslogtreecommitdiffstats
path: root/components/mus/public/cpp/window.h
blob: db3be8fc65973852a4f73627f168a3373ed15a91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_MUS_PUBLIC_CPP_VIEW_H_
#define COMPONENTS_MUS_PUBLIC_CPP_VIEW_H_

#include <stdint.h>
#include <vector>

#include "base/callback.h"
#include "base/observer_list.h"
#include "components/mus/public/cpp/types.h"
#include "components/mus/public/interfaces/mus_constants.mojom.h"
#include "components/mus/public/interfaces/surface_id.mojom.h"
#include "components/mus/public/interfaces/view_tree.mojom.h"
#include "mojo/application/public/interfaces/service_provider.mojom.h"
#include "third_party/mojo/src/mojo/public/cpp/bindings/array.h"
#include "third_party/mojo/src/mojo/public/cpp/system/macros.h"
#include "ui/mojo/geometry/geometry.mojom.h"

namespace mus {

class ServiceProviderImpl;
class WindowObserver;
class WindowSurface;
class WindowTreeConnection;

// Defined in window_property.h (which we do not include)
template <typename T>
struct WindowProperty;

// Windows are owned by the WindowTreeConnection. See WindowTreeDelegate for
// details
// on ownership.
//
// TODO(beng): Right now, you'll have to implement a WindowObserver to track
//             destruction and NULL any pointers you have.
//             Investigate some kind of smart pointer or weak pointer for these.
class Window {
 public:
  using Children = std::vector<Window*>;
  using SharedProperties = std::map<std::string, std::vector<uint8_t>>;
  using EmbedCallback = base::Callback<void(bool, ConnectionSpecificId)>;

  // Destroys this window and all its children. Destruction is allowed for
  // windows
  // that were created by this connection. For windows from other connections
  // (such as the root) Destroy() does nothing. If the destruction is allowed
  // observers are notified and the Window is immediately deleted.
  void Destroy();

  WindowTreeConnection* connection() { return connection_; }

  // Configuration.
  Id id() const { return id_; }

  // Geometric disposition.
  const mojo::Rect& bounds() const { return bounds_; }
  void SetBounds(const mojo::Rect& bounds);

  const mojo::Rect& client_area() const { return client_area_; }
  void SetClientArea(const mojo::Rect& client_area);

  // Visibility (also see IsDrawn()). When created windows are hidden.
  bool visible() const { return visible_; }
  void SetVisible(bool value);

  const mojo::ViewportMetrics& viewport_metrics() { return *viewport_metrics_; }

  scoped_ptr<WindowSurface> RequestSurface();

  // Returns the set of string to bag of byte properties. These properties are
  // shared with the window manager.
  const SharedProperties& shared_properties() const { return properties_; }
  // Sets a property. If |data| is null, this property is deleted.
  void SetSharedProperty(const std::string& name,
                         const std::vector<uint8_t>* data);

  // Sets the |value| of the given window |property|. Setting to the default
  // value (e.g., NULL) removes the property. The caller is responsible for the
  // lifetime of any object set as a property on the Window.
  //
  // These properties are not visible to the window manager.
  template <typename T>
  void SetLocalProperty(const WindowProperty<T>* property, T value);

  // Returns the value of the given window |property|.  Returns the
  // property-specific default value if the property was not previously set.
  //
  // These properties are only visible in the current process and are not
  // shared with other mojo services.
  template <typename T>
  T GetLocalProperty(const WindowProperty<T>* property) const;

  // Sets the |property| to its default value. Useful for avoiding a cast when
  // setting to NULL.
  //
  // These properties are only visible in the current process and are not
  // shared with other mojo services.
  template <typename T>
  void ClearLocalProperty(const WindowProperty<T>* property);

  // Type of a function to delete a property that this window owns.
  typedef void (*PropertyDeallocator)(int64_t value);

  // A Window is drawn if the Window and all its ancestors are visible and the
  // Window is attached to the root.
  bool IsDrawn() const;

  // Observation.
  void AddObserver(WindowObserver* observer);
  void RemoveObserver(WindowObserver* observer);

  // Tree.
  Window* parent() { return parent_; }
  const Window* parent() const { return parent_; }
  const Children& children() const { return children_; }
  Window* GetRoot() {
    return const_cast<Window*>(const_cast<const Window*>(this)->GetRoot());
  }
  const Window* GetRoot() const;

  void AddChild(Window* child);
  void RemoveChild(Window* child);

  void Reorder(Window* relative, mojo::OrderDirection direction);
  void MoveToFront();
  void MoveToBack();

  bool Contains(Window* child) const;

  Window* GetChildById(Id id);

  void SetTextInputState(mojo::TextInputStatePtr state);
  void SetImeVisibility(bool visible, mojo::TextInputStatePtr state);

  // Focus.
  void SetFocus();
  bool HasFocus() const;

  // Embedding. See view_tree.mojom for details.
  void Embed(mojo::ViewTreeClientPtr client);

  // NOTE: callback is run synchronously if Embed() is not allowed on this
  // Window.
  void Embed(mojo::ViewTreeClientPtr client,
             uint32_t policy_bitmask,
             const EmbedCallback& callback);

 protected:
  // This class is subclassed only by test classes that provide a public ctor.
  Window();
  ~Window();

 private:
  friend class WindowPrivate;
  friend class WindowTreeClientImpl;

  Window(WindowTreeConnection* connection, Id id);

  // Called by the public {Set,Get,Clear}Property functions.
  int64_t SetLocalPropertyInternal(const void* key,
                                   const char* name,
                                   PropertyDeallocator deallocator,
                                   int64_t value,
                                   int64_t default_value);
  int64_t GetLocalPropertyInternal(const void* key,
                                   int64_t default_value) const;

  void LocalDestroy();
  void LocalAddChild(Window* child);
  void LocalRemoveChild(Window* child);
  // Returns true if the order actually changed.
  bool LocalReorder(Window* relative, mojo::OrderDirection direction);
  void LocalSetBounds(const mojo::Rect& old_bounds,
                      const mojo::Rect& new_bounds);
  void LocalSetClientArea(const mojo::Rect& new_client_area);
  void LocalSetViewportMetrics(const mojo::ViewportMetrics& old_metrics,
                               const mojo::ViewportMetrics& new_metrics);
  void LocalSetDrawn(bool drawn);
  void LocalSetVisible(bool visible);

  // Methods implementing visibility change notifications. See WindowObserver
  // for more details.
  void NotifyWindowVisibilityChanged(Window* target);
  // Notifies this view's observers. Returns false if |this| was deleted during
  // the call (by an observer), otherwise true.
  bool NotifyWindowVisibilityChangedAtReceiver(Window* target);
  // Notifies this view and its child hierarchy. Returns false if |this| was
  // deleted during the call (by an observer), otherwise true.
  bool NotifyWindowVisibilityChangedDown(Window* target);
  // Notifies this view and its parent hierarchy.
  void NotifyWindowVisibilityChangedUp(Window* target);

  // Returns true if embed is allowed for this node. If embedding is allowed all
  // the children are removed.
  bool PrepareForEmbed();

  WindowTreeConnection* connection_;
  Id id_;
  Window* parent_;
  Children children_;

  base::ObserverList<WindowObserver> observers_;

  mojo::Rect bounds_;
  mojo::Rect client_area_;

  mojo::ViewportMetricsPtr viewport_metrics_;

  bool visible_;

  SharedProperties properties_;

  // Drawn state is derived from the visible state and the parent's visible
  // state. This field is only used if the view has no parent (eg it's a root).
  bool drawn_;

  // Value struct to keep the name and deallocator for this property.
  // Key cannot be used for this purpose because it can be char* or
  // WindowProperty<>.
  struct Value {
    const char* name;
    int64_t value;
    PropertyDeallocator deallocator;
  };

  std::map<const void*, Value> prop_map_;

  MOJO_DISALLOW_COPY_AND_ASSIGN(Window);
};

}  // namespace mus

#endif  // COMPONENTS_MUS_PUBLIC_CPP_VIEW_H_