| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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 ASH_COMMON_WM_WINDOW_STATE_H_ | |
| 6 #define ASH_COMMON_WM_WINDOW_STATE_H_ | |
| 7 | |
| 8 #include <memory> | |
| 9 | |
| 10 #include "ash/ash_export.h" | |
| 11 #include "ash/common/wm/drag_details.h" | |
| 12 #include "ash/common/wm/wm_types.h" | |
| 13 #include "base/gtest_prod_util.h" | |
| 14 #include "base/macros.h" | |
| 15 #include "base/observer_list.h" | |
| 16 #include "ui/base/ui_base_types.h" | |
| 17 | |
| 18 namespace aura { | |
| 19 class Window; | |
| 20 } | |
| 21 | |
| 22 namespace gfx { | |
| 23 class Rect; | |
| 24 } | |
| 25 | |
| 26 namespace ash { | |
| 27 class LockWindowState; | |
| 28 class MaximizeModeWindowState; | |
| 29 class WmWindow; | |
| 30 | |
| 31 namespace wm { | |
| 32 class WindowStateDelegate; | |
| 33 class WindowStateObserver; | |
| 34 class WMEvent; | |
| 35 | |
| 36 // WindowState manages and defines ash specific window state and | |
| 37 // behavior. Ash specific per-window state (such as ones that controls | |
| 38 // window manager behavior) and ash specific window behavior (such as | |
| 39 // maximize, minimize, snap sizing etc) should be added here instead | |
| 40 // of defining separate functions (like |MaximizeWindow(aura::Window* | |
| 41 // window)|) or using aura Window property. | |
| 42 // The WindowState gets created when first accessed by | |
| 43 // |wm::GetWindowState|, and deleted when the window is deleted. | |
| 44 // Prefer using this class instead of passing aura::Window* around in | |
| 45 // ash code as this is often what you need to interact with, and | |
| 46 // accessing the window using |window()| is cheap. | |
| 47 class ASH_EXPORT WindowState { | |
| 48 public: | |
| 49 // A subclass of State class represents one of the window's states | |
| 50 // that corresponds to WindowStateType in Ash environment, e.g. | |
| 51 // maximized, minimized or side snapped, as subclass. | |
| 52 // Each subclass defines its own behavior and transition for each WMEvent. | |
| 53 class State { | |
| 54 public: | |
| 55 State() {} | |
| 56 virtual ~State() {} | |
| 57 | |
| 58 // Update WindowState based on |event|. | |
| 59 virtual void OnWMEvent(WindowState* window_state, const WMEvent* event) = 0; | |
| 60 | |
| 61 virtual WindowStateType GetType() const = 0; | |
| 62 | |
| 63 // Gets called when the state object became active and the managed window | |
| 64 // needs to be adjusted to the State's requirement. | |
| 65 // The passed |previous_state| may be used to properly implement state | |
| 66 // transitions such as bound animations from the previous state. | |
| 67 // Note: This only gets called when the state object gets changed. | |
| 68 virtual void AttachState(WindowState* window_state, | |
| 69 State* previous_state) = 0; | |
| 70 | |
| 71 // Gets called before the state objects gets deactivated / detached from the | |
| 72 // window, so that it can save the various states it is interested in. | |
| 73 // Note: This only gets called when the state object gets changed. | |
| 74 virtual void DetachState(WindowState* window_state) = 0; | |
| 75 | |
| 76 private: | |
| 77 DISALLOW_COPY_AND_ASSIGN(State); | |
| 78 }; | |
| 79 | |
| 80 // Call GetWindowState() to instantiate this class. | |
| 81 virtual ~WindowState(); | |
| 82 | |
| 83 WmWindow* window() { return window_; } | |
| 84 const WmWindow* window() const { return window_; } | |
| 85 | |
| 86 bool HasDelegate() const; | |
| 87 void SetDelegate(std::unique_ptr<WindowStateDelegate> delegate); | |
| 88 | |
| 89 // Returns the window's current ash state type. | |
| 90 // Refer to WindowStateType definition in wm_types.h as for why Ash | |
| 91 // has its own state type. | |
| 92 WindowStateType GetStateType() const; | |
| 93 | |
| 94 // Predicates to check window state. | |
| 95 bool IsMinimized() const; | |
| 96 bool IsMaximized() const; | |
| 97 bool IsFullscreen() const; | |
| 98 bool IsSnapped() const; | |
| 99 bool IsPinned() const; | |
| 100 bool IsTrustedPinned() const; | |
| 101 | |
| 102 // True if the window's state type is WINDOW_STATE_TYPE_MAXIMIZED, | |
| 103 // WINDOW_STATE_TYPE_FULLSCREEN or WINDOW_STATE_TYPE_PINNED. | |
| 104 bool IsMaximizedOrFullscreenOrPinned() const; | |
| 105 | |
| 106 // True if the window's state type is WINDOW_STATE_TYPE_NORMAL or | |
| 107 // WINDOW_STATE_TYPE_DEFAULT. | |
| 108 bool IsNormalStateType() const; | |
| 109 | |
| 110 bool IsNormalOrSnapped() const; | |
| 111 | |
| 112 bool IsActive() const; | |
| 113 bool IsDocked() const; | |
| 114 | |
| 115 // Returns true if the window's location can be controlled by the user. | |
| 116 bool IsUserPositionable() const; | |
| 117 | |
| 118 // Checks if the window can change its state accordingly. | |
| 119 bool CanMaximize() const; | |
| 120 bool CanMinimize() const; | |
| 121 bool CanResize() const; | |
| 122 bool CanSnap() const; | |
| 123 bool CanActivate() const; | |
| 124 | |
| 125 // Returns true if the window has restore bounds. | |
| 126 bool HasRestoreBounds() const; | |
| 127 | |
| 128 // These methods use aura::WindowProperty to change the window's state | |
| 129 // instead of using WMEvent directly. This is to use the same mechanism as | |
| 130 // what views::Widget is using. | |
| 131 void Maximize(); | |
| 132 void Minimize(); | |
| 133 void Unminimize(); | |
| 134 | |
| 135 void Activate(); | |
| 136 void Deactivate(); | |
| 137 | |
| 138 // Set the window state to normal. | |
| 139 // TODO(oshima): Change to use RESTORE event. | |
| 140 void Restore(); | |
| 141 | |
| 142 // Caches, then disables always on top state and then stacks |window_| below | |
| 143 // |window_on_top| if a |window_| is currently in always on top state. | |
| 144 void DisableAlwaysOnTop(WmWindow* window_on_top); | |
| 145 | |
| 146 // Restores always on top state that a window might have cached. | |
| 147 void RestoreAlwaysOnTop(); | |
| 148 | |
| 149 // Invoked when a WMevent occurs, which drives the internal | |
| 150 // state machine. | |
| 151 void OnWMEvent(const WMEvent* event); | |
| 152 | |
| 153 // TODO(oshima): Try hiding these methods and making them accessible only to | |
| 154 // state impl. State changes should happen through events (as much | |
| 155 // as possible). | |
| 156 | |
| 157 // Saves the current bounds to be used as a restore bounds. | |
| 158 void SaveCurrentBoundsForRestore(); | |
| 159 | |
| 160 // Same as |GetRestoreBoundsInScreen| except that it returns the | |
| 161 // bounds in the parent's coordinates. | |
| 162 gfx::Rect GetRestoreBoundsInParent() const; | |
| 163 | |
| 164 // Returns the restore bounds property on the window in the virtual screen | |
| 165 // coordinates. The bounds can be NULL if the bounds property does not | |
| 166 // exist for the window. The window owns the bounds object. | |
| 167 gfx::Rect GetRestoreBoundsInScreen() const; | |
| 168 | |
| 169 // Same as |SetRestoreBoundsInScreen| except that the bounds is in the | |
| 170 // parent's coordinates. | |
| 171 void SetRestoreBoundsInParent(const gfx::Rect& bounds_in_parent); | |
| 172 | |
| 173 // Sets the restore bounds property on the window in the virtual screen | |
| 174 // coordinates. Deletes existing bounds value if exists. | |
| 175 void SetRestoreBoundsInScreen(const gfx::Rect& bounds_in_screen); | |
| 176 | |
| 177 // Deletes and clears the restore bounds property on the window. | |
| 178 void ClearRestoreBounds(); | |
| 179 | |
| 180 // Replace the State object of a window with a state handler which can | |
| 181 // implement a new window manager type. The passed object will be owned | |
| 182 // by this object and the returned object will be owned by the caller. | |
| 183 std::unique_ptr<State> SetStateObject(std::unique_ptr<State> new_state); | |
| 184 | |
| 185 // True if the window should be unminimized to the restore bounds, as | |
| 186 // opposed to the window's current bounds. |unminimized_to_restore_bounds_| is | |
| 187 // reset to the default value after the window is unminimized. | |
| 188 bool unminimize_to_restore_bounds() const { | |
| 189 return unminimize_to_restore_bounds_; | |
| 190 } | |
| 191 void set_unminimize_to_restore_bounds(bool value) { | |
| 192 unminimize_to_restore_bounds_ = value; | |
| 193 } | |
| 194 | |
| 195 // Gets/sets whether the shelf should be hidden when this window is | |
| 196 // fullscreen. | |
| 197 bool hide_shelf_when_fullscreen() const { | |
| 198 return hide_shelf_when_fullscreen_; | |
| 199 } | |
| 200 | |
| 201 void set_hide_shelf_when_fullscreen(bool value) { | |
| 202 hide_shelf_when_fullscreen_ = value; | |
| 203 } | |
| 204 | |
| 205 // If the minimum visibility is true, ash will try to keep a | |
| 206 // minimum amount of the window is always visible on the work area | |
| 207 // when shown. | |
| 208 // TODO(oshima): Consolidate this and window_position_managed | |
| 209 // into single parameter to control the window placement. | |
| 210 bool minimum_visibility() const { return minimum_visibility_; } | |
| 211 void set_minimum_visibility(bool minimum_visibility) { | |
| 212 minimum_visibility_ = minimum_visibility; | |
| 213 } | |
| 214 | |
| 215 // Specifies if the window can be dragged by the user via the caption or not. | |
| 216 bool can_be_dragged() const { return can_be_dragged_; } | |
| 217 void set_can_be_dragged(bool can_be_dragged) { | |
| 218 can_be_dragged_ = can_be_dragged; | |
| 219 } | |
| 220 | |
| 221 // Gets/Sets the bounds of the window before it was moved by the auto window | |
| 222 // management. As long as it was not auto-managed, it will return NULL. | |
| 223 const gfx::Rect* pre_auto_manage_window_bounds() const { | |
| 224 return pre_auto_manage_window_bounds_.get(); | |
| 225 } | |
| 226 void SetPreAutoManageWindowBounds(const gfx::Rect& bounds); | |
| 227 | |
| 228 // Layout related properties | |
| 229 | |
| 230 void AddObserver(WindowStateObserver* observer); | |
| 231 void RemoveObserver(WindowStateObserver* observer); | |
| 232 | |
| 233 // Whether the window is being dragged. | |
| 234 bool is_dragged() const { return !!drag_details_; } | |
| 235 | |
| 236 // Whether or not the window's position can be managed by the | |
| 237 // auto management logic. | |
| 238 bool window_position_managed() const { return window_position_managed_; } | |
| 239 void set_window_position_managed(bool window_position_managed) { | |
| 240 window_position_managed_ = window_position_managed; | |
| 241 } | |
| 242 | |
| 243 // Whether or not the window's position or size was changed by a user. | |
| 244 bool bounds_changed_by_user() const { return bounds_changed_by_user_; } | |
| 245 void set_bounds_changed_by_user(bool bounds_changed_by_user); | |
| 246 | |
| 247 // True if the window is ignored by the shelf layout manager for | |
| 248 // purposes of darkening the shelf. | |
| 249 bool ignored_by_shelf() const { return ignored_by_shelf_; } | |
| 250 void set_ignored_by_shelf(bool ignored_by_shelf) { | |
| 251 ignored_by_shelf_ = ignored_by_shelf; | |
| 252 } | |
| 253 | |
| 254 // True if the window should be offered a chance to consume special system | |
| 255 // keys such as brightness, volume, etc. that are usually handled by the | |
| 256 // shell. | |
| 257 bool can_consume_system_keys() const { return can_consume_system_keys_; } | |
| 258 void set_can_consume_system_keys(bool can_consume_system_keys) { | |
| 259 can_consume_system_keys_ = can_consume_system_keys; | |
| 260 } | |
| 261 | |
| 262 // True if the window is in "immersive full screen mode" which is slightly | |
| 263 // different from the normal fullscreen mode by allowing the user to reveal | |
| 264 // the top portion of the window through a touch / mouse gesture. It might | |
| 265 // also allow the shelf to be shown in some situations. | |
| 266 bool in_immersive_fullscreen() const { return in_immersive_fullscreen_; } | |
| 267 void set_in_immersive_fullscreen(bool enable) { | |
| 268 in_immersive_fullscreen_ = enable; | |
| 269 } | |
| 270 | |
| 271 // True if the window should not adjust the window's bounds when | |
| 272 // virtual keyboard bounds changes. | |
| 273 // TODO(oshima): This is hack. Replace this with proper | |
| 274 // implementation based on EnsureCaretNotInRect. | |
| 275 bool ignore_keyboard_bounds_change() const { | |
| 276 return ignore_keyboard_bounds_change_; | |
| 277 } | |
| 278 void set_ignore_keyboard_bounds_change(bool ignore_keyboard_bounds_change) { | |
| 279 ignore_keyboard_bounds_change_ = ignore_keyboard_bounds_change; | |
| 280 } | |
| 281 | |
| 282 // True if the window's bounds can be updated using SET_BOUNDS event in | |
| 283 // maiximzed/fullscreen mode. | |
| 284 void set_allow_set_bounds_in_maximized(bool value) { | |
| 285 allow_set_bounds_in_maximized_ = value; | |
| 286 } | |
| 287 bool allow_set_bounds_in_maximized() const { | |
| 288 return allow_set_bounds_in_maximized_; | |
| 289 } | |
| 290 | |
| 291 // Creates and takes ownership of a pointer to DragDetails when resizing is | |
| 292 // active. This should be done before a resizer gets created. | |
| 293 void CreateDragDetails(const gfx::Point& point_in_parent, | |
| 294 int window_component, | |
| 295 aura::client::WindowMoveSource source); | |
| 296 | |
| 297 // Deletes and clears a pointer to DragDetails. This should be done when the | |
| 298 // resizer gets destroyed. | |
| 299 void DeleteDragDetails(); | |
| 300 | |
| 301 // Sets the currently stored restore bounds and clears the restore bounds. | |
| 302 void SetAndClearRestoreBounds(); | |
| 303 | |
| 304 // Returns a pointer to DragDetails during drag operations. | |
| 305 const DragDetails* drag_details() const { return drag_details_.get(); } | |
| 306 DragDetails* drag_details() { return drag_details_.get(); } | |
| 307 | |
| 308 // Called from the associated WmWindow once the show state changes. | |
| 309 void OnWindowShowStateChanged(); | |
| 310 | |
| 311 protected: | |
| 312 explicit WindowState(WmWindow* window); | |
| 313 | |
| 314 private: | |
| 315 friend class DefaultState; | |
| 316 friend class ash::LockWindowState; | |
| 317 friend class ash::MaximizeModeWindowState; | |
| 318 FRIEND_TEST_ALL_PREFIXES(WindowAnimationsTest, CrossFadeToBounds); | |
| 319 FRIEND_TEST_ALL_PREFIXES(WindowAnimationsTest, | |
| 320 CrossFadeToBoundsFromTransform); | |
| 321 | |
| 322 WindowStateDelegate* delegate() { return delegate_.get(); } | |
| 323 | |
| 324 // Returns the window's current always_on_top state. | |
| 325 bool GetAlwaysOnTop() const; | |
| 326 | |
| 327 // Returns the window's current show state. | |
| 328 ui::WindowShowState GetShowState() const; | |
| 329 | |
| 330 // Sets the window's bounds in screen coordinates. | |
| 331 void SetBoundsInScreen(const gfx::Rect& bounds_in_screen); | |
| 332 | |
| 333 // Adjusts the |bounds| so that they are flush with the edge of the | |
| 334 // workspace if the window represented by |window_state| is side snapped. | |
| 335 void AdjustSnappedBounds(gfx::Rect* bounds); | |
| 336 | |
| 337 // Updates the window show state according to the current window state type. | |
| 338 // Note that this does not update the window bounds. | |
| 339 void UpdateWindowShowStateFromStateType(); | |
| 340 | |
| 341 void NotifyPreStateTypeChange(WindowStateType old_window_state_type); | |
| 342 void NotifyPostStateTypeChange(WindowStateType old_window_state_type); | |
| 343 | |
| 344 // Sets |bounds| as is and ensure the layer is aligned with pixel boundary. | |
| 345 void SetBoundsDirect(const gfx::Rect& bounds); | |
| 346 | |
| 347 // Sets the window's |bounds| with constraint where the size of the | |
| 348 // new bounds will not exceeds the size of the work area. | |
| 349 void SetBoundsConstrained(const gfx::Rect& bounds); | |
| 350 | |
| 351 // Sets the wndow's |bounds| and transitions to the new bounds with | |
| 352 // a scale animation. | |
| 353 void SetBoundsDirectAnimated(const gfx::Rect& bounds); | |
| 354 | |
| 355 // Sets the window's |bounds| and transition to the new bounds with | |
| 356 // a cross fade animation. | |
| 357 void SetBoundsDirectCrossFade(const gfx::Rect& bounds); | |
| 358 | |
| 359 // The owner of this window settings. | |
| 360 WmWindow* window_; | |
| 361 std::unique_ptr<WindowStateDelegate> delegate_; | |
| 362 | |
| 363 bool window_position_managed_; | |
| 364 bool bounds_changed_by_user_; | |
| 365 bool ignored_by_shelf_; | |
| 366 bool can_consume_system_keys_; | |
| 367 std::unique_ptr<DragDetails> drag_details_; | |
| 368 | |
| 369 bool unminimize_to_restore_bounds_; | |
| 370 bool in_immersive_fullscreen_; | |
| 371 bool ignore_keyboard_bounds_change_ = false; | |
| 372 bool hide_shelf_when_fullscreen_; | |
| 373 bool minimum_visibility_; | |
| 374 bool can_be_dragged_; | |
| 375 bool cached_always_on_top_; | |
| 376 bool allow_set_bounds_in_maximized_ = false; | |
| 377 | |
| 378 // A property to remember the window position which was set before the | |
| 379 // auto window position manager changed the window bounds, so that it can get | |
| 380 // restored when only this one window gets shown. | |
| 381 std::unique_ptr<gfx::Rect> pre_auto_manage_window_bounds_; | |
| 382 | |
| 383 base::ObserverList<WindowStateObserver> observer_list_; | |
| 384 | |
| 385 // True to ignore a property change event to avoid reentrance in | |
| 386 // UpdateWindowStateType() | |
| 387 bool ignore_property_change_; | |
| 388 | |
| 389 std::unique_ptr<State> current_state_; | |
| 390 | |
| 391 DISALLOW_COPY_AND_ASSIGN(WindowState); | |
| 392 }; | |
| 393 | |
| 394 } // namespace wm | |
| 395 } // namespace ash | |
| 396 | |
| 397 #endif // ASH_COMMON_WM_WINDOW_STATE_H_ | |
| OLD | NEW |