| 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 COMPONENTS_MUS_PUBLIC_CPP_WINDOW_H_ | |
| 6 #define COMPONENTS_MUS_PUBLIC_CPP_WINDOW_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/callback.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/observer_list.h" | |
| 14 #include "components/mus/common/types.h" | |
| 15 #include "components/mus/public/interfaces/mus_constants.mojom.h" | |
| 16 #include "components/mus/public/interfaces/window_tree.mojom.h" | |
| 17 #include "mojo/public/cpp/bindings/array.h" | |
| 18 #include "services/shell/public/interfaces/interface_provider.mojom.h" | |
| 19 #include "ui/gfx/geometry/insets.h" | |
| 20 #include "ui/gfx/geometry/rect.h" | |
| 21 | |
| 22 namespace gfx { | |
| 23 class Size; | |
| 24 } | |
| 25 | |
| 26 namespace mus { | |
| 27 | |
| 28 class InputEventHandler; | |
| 29 class ServiceProviderImpl; | |
| 30 class WindowObserver; | |
| 31 class WindowSurface; | |
| 32 class WindowSurfaceBinding; | |
| 33 class WindowTreeClient; | |
| 34 class WindowTreeClientPrivate; | |
| 35 | |
| 36 namespace { | |
| 37 class OrderChangedNotifier; | |
| 38 } | |
| 39 | |
| 40 // Defined in window_property.h (which we do not include) | |
| 41 template <typename T> | |
| 42 struct WindowProperty; | |
| 43 | |
| 44 // Windows are owned by the WindowTreeClient. See WindowTreeClientDelegate for | |
| 45 // details on ownership. | |
| 46 // | |
| 47 // TODO(beng): Right now, you'll have to implement a WindowObserver to track | |
| 48 // destruction and NULL any pointers you have. | |
| 49 // Investigate some kind of smart pointer or weak pointer for these. | |
| 50 class Window { | |
| 51 public: | |
| 52 using Children = std::vector<Window*>; | |
| 53 using EmbedCallback = base::Callback<void(bool)>; | |
| 54 using PropertyDeallocator = void (*)(int64_t value); | |
| 55 using SharedProperties = std::map<std::string, std::vector<uint8_t>>; | |
| 56 | |
| 57 // Destroys this window and all its children. Destruction is allowed for | |
| 58 // windows that were created by this connection, or the roots. For windows | |
| 59 // from other connections (except the roots), Destroy() does nothing. If the | |
| 60 // destruction is allowed observers are notified and the Window is | |
| 61 // immediately deleted. | |
| 62 void Destroy(); | |
| 63 | |
| 64 WindowTreeClient* window_tree() { return client_; } | |
| 65 | |
| 66 // The local_id is provided for client code. The local_id is not set or | |
| 67 // manipulated by mus. The default value is -1. | |
| 68 void set_local_id(int id) { local_id_ = id; } | |
| 69 int local_id() const { return local_id_; } | |
| 70 | |
| 71 int64_t display_id() const { return display_id_; } | |
| 72 | |
| 73 // Geometric disposition relative to parent window. | |
| 74 const gfx::Rect& bounds() const { return bounds_; } | |
| 75 void SetBounds(const gfx::Rect& bounds); | |
| 76 | |
| 77 // Geometric disposition relative to root window. | |
| 78 gfx::Rect GetBoundsInRoot() const; | |
| 79 | |
| 80 const gfx::Insets& client_area() const { return client_area_; } | |
| 81 const std::vector<gfx::Rect>& additional_client_areas() { | |
| 82 return additional_client_areas_; | |
| 83 } | |
| 84 void SetClientArea(const gfx::Insets& new_client_area) { | |
| 85 SetClientArea(new_client_area, std::vector<gfx::Rect>()); | |
| 86 } | |
| 87 void SetClientArea(const gfx::Insets& new_client_area, | |
| 88 const std::vector<gfx::Rect>& additional_client_areas); | |
| 89 | |
| 90 // Mouse events outside the hit test mask do not hit the window. Returns null | |
| 91 // if there is no mask. | |
| 92 const gfx::Rect* hit_test_mask() const { return hit_test_mask_.get(); } | |
| 93 void SetHitTestMask(const gfx::Rect& mask_rect); | |
| 94 void ClearHitTestMask(); | |
| 95 | |
| 96 // Visibility (also see IsDrawn()). When created windows are hidden. | |
| 97 bool visible() const { return visible_; } | |
| 98 void SetVisible(bool value); | |
| 99 | |
| 100 float opacity() const { return opacity_; } | |
| 101 void SetOpacity(float opacity); | |
| 102 | |
| 103 // Cursors | |
| 104 mojom::Cursor predefined_cursor() const { return cursor_id_; } | |
| 105 void SetPredefinedCursor(mus::mojom::Cursor cursor_id); | |
| 106 | |
| 107 // A Window is drawn if the Window and all its ancestors are visible and the | |
| 108 // Window is attached to the root. | |
| 109 bool IsDrawn() const; | |
| 110 | |
| 111 std::unique_ptr<WindowSurface> RequestSurface(mojom::SurfaceType type); | |
| 112 | |
| 113 void AttachSurface(mojom::SurfaceType type, | |
| 114 std::unique_ptr<WindowSurfaceBinding> surface_binding); | |
| 115 | |
| 116 // The template-ized versions of the following methods rely on the presence | |
| 117 // of a mojo::TypeConverter<const std::vector<uint8_t>, T>. | |
| 118 // Sets a shared property on the window which is sent to the window server and | |
| 119 // shared with other clients that can view this window. | |
| 120 template <typename T> | |
| 121 void SetSharedProperty(const std::string& name, const T& data); | |
| 122 // Gets a shared property set on the window. The property must exist. Call | |
| 123 // HasSharedProperty() before calling. | |
| 124 template <typename T> | |
| 125 T GetSharedProperty(const std::string& name) const; | |
| 126 // Removes the shared property. | |
| 127 void ClearSharedProperty(const std::string& name); | |
| 128 bool HasSharedProperty(const std::string& name) const; | |
| 129 | |
| 130 // TODO(beng): Test only, should move to a helper. | |
| 131 const SharedProperties& shared_properties() { return properties_; } | |
| 132 | |
| 133 // Sets the |value| of the given window |property|. Setting to the default | |
| 134 // value (e.g., NULL) removes the property. The caller is responsible for the | |
| 135 // lifetime of any object set as a property on the Window. | |
| 136 // | |
| 137 // These properties are not visible to the window server. | |
| 138 template <typename T> | |
| 139 void SetLocalProperty(const WindowProperty<T>* property, T value); | |
| 140 | |
| 141 // Returns the value of the given window |property|. Returns the | |
| 142 // property-specific default value if the property was not previously set. | |
| 143 // | |
| 144 // These properties are only visible in the current process and are not | |
| 145 // shared with other mojo services. | |
| 146 template <typename T> | |
| 147 T GetLocalProperty(const WindowProperty<T>* property) const; | |
| 148 | |
| 149 // Sets the |property| to its default value. Useful for avoiding a cast when | |
| 150 // setting to NULL. | |
| 151 // | |
| 152 // These properties are only visible in the current process and are not | |
| 153 // shared with other mojo services. | |
| 154 template <typename T> | |
| 155 void ClearLocalProperty(const WindowProperty<T>* property); | |
| 156 | |
| 157 void set_input_event_handler(InputEventHandler* input_event_handler) { | |
| 158 input_event_handler_ = input_event_handler; | |
| 159 } | |
| 160 | |
| 161 // Observation. | |
| 162 void AddObserver(WindowObserver* observer); | |
| 163 void RemoveObserver(WindowObserver* observer); | |
| 164 | |
| 165 // Tree. | |
| 166 Window* parent() { return parent_; } | |
| 167 const Window* parent() const { return parent_; } | |
| 168 | |
| 169 Window* GetRoot() { | |
| 170 return const_cast<Window*>(const_cast<const Window*>(this)->GetRoot()); | |
| 171 } | |
| 172 const Window* GetRoot() const; | |
| 173 | |
| 174 void AddChild(Window* child); | |
| 175 void RemoveChild(Window* child); | |
| 176 const Children& children() const { return children_; } | |
| 177 | |
| 178 void Reorder(Window* relative, mojom::OrderDirection direction); | |
| 179 void MoveToFront(); | |
| 180 void MoveToBack(); | |
| 181 | |
| 182 // Returns true if |child| is this or a descendant of this. | |
| 183 bool Contains(const Window* child) const; | |
| 184 | |
| 185 void AddTransientWindow(Window* transient_window); | |
| 186 void RemoveTransientWindow(Window* transient_window); | |
| 187 | |
| 188 // TODO(fsamuel): Figure out if we want to refactor transient window | |
| 189 // management into a separate class. | |
| 190 // Transient tree. | |
| 191 Window* transient_parent() { return transient_parent_; } | |
| 192 const Window* transient_parent() const { return transient_parent_; } | |
| 193 const Children& transient_children() const { return transient_children_; } | |
| 194 | |
| 195 void SetModal(); | |
| 196 bool is_modal() const { return is_modal_; } | |
| 197 | |
| 198 Window* GetChildByLocalId(int id); | |
| 199 | |
| 200 void SetTextInputState(mojo::TextInputStatePtr state); | |
| 201 void SetImeVisibility(bool visible, mojo::TextInputStatePtr state); | |
| 202 | |
| 203 bool HasCapture() const; | |
| 204 void SetCapture(); | |
| 205 void ReleaseCapture(); | |
| 206 | |
| 207 // Focus. See WindowTreeClient::ClearFocus() to reset focus. | |
| 208 void SetFocus(); | |
| 209 bool HasFocus() const; | |
| 210 void SetCanFocus(bool can_focus); | |
| 211 | |
| 212 // Embedding. See window_tree.mojom for details. | |
| 213 void Embed(mus::mojom::WindowTreeClientPtr client, uint32_t flags = 0); | |
| 214 | |
| 215 // NOTE: callback is run synchronously if Embed() is not allowed on this | |
| 216 // Window. | |
| 217 void Embed(mus::mojom::WindowTreeClientPtr client, | |
| 218 const EmbedCallback& callback, | |
| 219 uint32_t flags = 0); | |
| 220 | |
| 221 // TODO(sky): this API is only applicable to the WindowManager. Move it | |
| 222 // to a better place. | |
| 223 void RequestClose(); | |
| 224 | |
| 225 // Returns an internal name, set by a client app when it creates a window. | |
| 226 std::string GetName() const; | |
| 227 | |
| 228 protected: | |
| 229 // This class is subclassed only by test classes that provide a public ctor. | |
| 230 Window(); | |
| 231 ~Window(); | |
| 232 | |
| 233 private: | |
| 234 friend class WindowPrivate; | |
| 235 friend class WindowTreeClient; | |
| 236 friend class WindowTreeClientPrivate; | |
| 237 | |
| 238 Window(WindowTreeClient* client, Id id); | |
| 239 | |
| 240 // Used to identify this Window on the server. Clients can not change this | |
| 241 // value. | |
| 242 Id server_id() const { return server_id_; } | |
| 243 | |
| 244 // Applies a shared property change locally and forwards to the server. If | |
| 245 // |data| is null, this property is deleted. | |
| 246 void SetSharedPropertyInternal(const std::string& name, | |
| 247 const std::vector<uint8_t>* data); | |
| 248 // Called by the public {Set,Get,Clear}Property functions. | |
| 249 int64_t SetLocalPropertyInternal(const void* key, | |
| 250 const char* name, | |
| 251 PropertyDeallocator deallocator, | |
| 252 int64_t value, | |
| 253 int64_t default_value); | |
| 254 int64_t GetLocalPropertyInternal(const void* key, | |
| 255 int64_t default_value) const; | |
| 256 | |
| 257 void LocalDestroy(); | |
| 258 void LocalAddChild(Window* child); | |
| 259 void LocalRemoveChild(Window* child); | |
| 260 void LocalAddTransientWindow(Window* transient_window); | |
| 261 void LocalRemoveTransientWindow(Window* transient_window); | |
| 262 void LocalSetModal(); | |
| 263 // Returns true if the order actually changed. | |
| 264 bool LocalReorder(Window* relative, mojom::OrderDirection direction); | |
| 265 void LocalSetBounds(const gfx::Rect& old_bounds, const gfx::Rect& new_bounds); | |
| 266 void LocalSetClientArea( | |
| 267 const gfx::Insets& new_client_area, | |
| 268 const std::vector<gfx::Rect>& additional_client_areas); | |
| 269 void LocalSetParentDrawn(bool drawn); | |
| 270 void LocalSetDisplay(int64_t display_id); | |
| 271 void LocalSetVisible(bool visible); | |
| 272 void LocalSetOpacity(float opacity); | |
| 273 void LocalSetPredefinedCursor(mojom::Cursor cursor_id); | |
| 274 void LocalSetSharedProperty(const std::string& name, | |
| 275 const std::vector<uint8_t>* data); | |
| 276 | |
| 277 // Notifies this winodw that its stacking position has changed. | |
| 278 void NotifyWindowStackingChanged(); | |
| 279 // Methods implementing visibility change notifications. See WindowObserver | |
| 280 // for more details. | |
| 281 void NotifyWindowVisibilityChanged(Window* target); | |
| 282 // Notifies this window's observers. Returns false if |this| was deleted | |
| 283 // during the call (by an observer), otherwise true. | |
| 284 bool NotifyWindowVisibilityChangedAtReceiver(Window* target); | |
| 285 // Notifies this window and its child hierarchy. Returns false if |this| was | |
| 286 // deleted during the call (by an observer), otherwise true. | |
| 287 bool NotifyWindowVisibilityChangedDown(Window* target); | |
| 288 // Notifies this window and its parent hierarchy. | |
| 289 void NotifyWindowVisibilityChangedUp(Window* target); | |
| 290 | |
| 291 // Returns true if embed is allowed for this node. If embedding is allowed all | |
| 292 // the children are removed. | |
| 293 bool PrepareForEmbed(); | |
| 294 | |
| 295 void RemoveTransientWindowImpl(Window* child); | |
| 296 static void ReorderWithoutNotification(Window* window, | |
| 297 Window* relative, | |
| 298 mojom::OrderDirection direction); | |
| 299 static bool ReorderImpl(Window* window, | |
| 300 Window* relative, | |
| 301 mojom::OrderDirection direction, | |
| 302 OrderChangedNotifier* notifier); | |
| 303 | |
| 304 // Returns a pointer to the stacking target that can be used by | |
| 305 // RestackTransientDescendants. | |
| 306 static Window** GetStackingTarget(Window* window); | |
| 307 | |
| 308 WindowTreeClient* client_; | |
| 309 Id server_id_; | |
| 310 int local_id_ = -1; | |
| 311 Window* parent_; | |
| 312 Children children_; | |
| 313 | |
| 314 Window* stacking_target_; | |
| 315 Window* transient_parent_; | |
| 316 Children transient_children_; | |
| 317 | |
| 318 bool is_modal_; | |
| 319 | |
| 320 base::ObserverList<WindowObserver> observers_; | |
| 321 InputEventHandler* input_event_handler_; | |
| 322 | |
| 323 gfx::Rect bounds_; | |
| 324 gfx::Insets client_area_; | |
| 325 std::vector<gfx::Rect> additional_client_areas_; | |
| 326 std::unique_ptr<gfx::Rect> hit_test_mask_; | |
| 327 | |
| 328 bool visible_; | |
| 329 float opacity_; | |
| 330 int64_t display_id_; | |
| 331 | |
| 332 mojom::Cursor cursor_id_; | |
| 333 | |
| 334 SharedProperties properties_; | |
| 335 | |
| 336 // Drawn state of our parent. This is only meaningful for root Windows, in | |
| 337 // which the parent Window isn't exposed to the client. | |
| 338 bool parent_drawn_; | |
| 339 | |
| 340 // Value struct to keep the name and deallocator for this property. | |
| 341 // Key cannot be used for this purpose because it can be char* or | |
| 342 // WindowProperty<>. | |
| 343 struct Value { | |
| 344 const char* name; | |
| 345 int64_t value; | |
| 346 PropertyDeallocator deallocator; | |
| 347 }; | |
| 348 | |
| 349 std::map<const void*, Value> prop_map_; | |
| 350 | |
| 351 DISALLOW_COPY_AND_ASSIGN(Window); | |
| 352 }; | |
| 353 | |
| 354 } // namespace mus | |
| 355 | |
| 356 #endif // COMPONENTS_MUS_PUBLIC_CPP_WINDOW_H_ | |
| OLD | NEW |