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_WM_WINDOW_STATE_H_ 6 #define ASH_WM_WINDOW_STATE_H_ 7 8 #include "ash/ash_export.h" 9 #include "ash/wm/wm_types.h" 10 #include "base/basictypes.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "base/observer_list.h" 13 #include "ui/aura/window_observer.h" 14 #include "ui/base/ui_base_types.h" 15 16 namespace aura { 17 class Window; 18 } 19 20 namespace gfx { 21 class Rect; 22 } 23 24 namespace ash { 25 class WindowResizer; 26 27 namespace wm { 28 class WindowStateDelegate; 29 class WindowStateObserver; 30 31 // WindowState manages and defines ash specific window state and 32 // behavior. Ash specific per-window state (such as ones that controls 33 // window manager behavior) and ash specific window behavior (such as 34 // maximize, minimize, snap sizing etc) should be added here instead 35 // of defining separate functions (like |MaximizeWindow(aura::Window* 36 // window)|) or using aura Window property. 37 // The WindowState gets created when first accessed by 38 // |wm::GetWindowState|, and deleted when the window is deleted. 39 // Prefer using this class instead of passing aura::Window* around in 40 // ash code as this is often what you need to interact with, and 41 // accessing the window using |window()| is cheap. 42 class ASH_EXPORT WindowState : public aura::WindowObserver { 43 public: 44 static bool IsMaximizedOrFullscreenState(ui::WindowShowState state); 45 46 explicit WindowState(aura::Window* window); 47 virtual ~WindowState(); 48 window()49 aura::Window* window() { return window_; } window()50 const aura::Window* window() const { return window_; } 51 52 bool HasDelegate() const; 53 void SetDelegate(scoped_ptr<WindowStateDelegate> delegate); 54 55 // Returns the window's current show state. 56 ui::WindowShowState GetShowState() const; 57 58 // Returns the window's current ash show type. 59 // Refer to WindowShowType definition in wm_types.h as for why Ash 60 // has its own show type. window_show_type()61 WindowShowType window_show_type() const { return window_show_type_; } 62 63 // Predicates to check window state. 64 bool IsMinimized() const; 65 bool IsMaximized() const; 66 bool IsFullscreen() const; 67 bool IsMaximizedOrFullscreen() const; 68 // True if the window's show state is SHOW_STATE_NORMAL or 69 // SHOW_STATE_DEFAULT. 70 bool IsNormalShowState() const; 71 bool IsActive() const; 72 bool IsDocked() const; 73 bool IsSnapped() const; 74 75 // Checks if the window can change its state accordingly. 76 bool CanMaximize() const; 77 bool CanMinimize() const; 78 bool CanResize() const; 79 bool CanSnap() const; 80 bool CanActivate() const; 81 82 // Returns true if the window has restore bounds. 83 bool HasRestoreBounds() const; 84 85 void Maximize(); 86 void Minimize(); 87 void Unminimize(); 88 void Activate(); 89 void Deactivate(); 90 void Restore(); 91 void ToggleMaximized(); 92 void ToggleFullscreen(); 93 void SnapLeft(const gfx::Rect& bounds); 94 void SnapRight(const gfx::Rect& bounds); 95 96 // Sets the window's bounds in screen coordinates. 97 void SetBoundsInScreen(const gfx::Rect& bounds_in_screen); 98 99 // Saves the current bounds to be used as a restore bounds. 100 void SaveCurrentBoundsForRestore(); 101 102 // Same as |GetRestoreBoundsInScreen| except that it returns the 103 // bounds in the parent's coordinates. 104 gfx::Rect GetRestoreBoundsInParent() const; 105 106 // Returns the restore bounds property on the window in the virtual screen 107 // coordinates. The bounds can be NULL if the bounds property does not 108 // exist for the window. The window owns the bounds object. 109 gfx::Rect GetRestoreBoundsInScreen() const; 110 111 // Same as |SetRestoreBoundsInScreen| except that the bounds is in the 112 // parent's coordinates. 113 void SetRestoreBoundsInParent(const gfx::Rect& bounds_in_parent); 114 115 // Sets the restore bounds property on the window in the virtual screen 116 // coordinates. Deletes existing bounds value if exists. 117 void SetRestoreBoundsInScreen(const gfx::Rect& bounds_in_screen); 118 119 // Deletes and clears the restore bounds property on the window. 120 void ClearRestoreBounds(); 121 122 // Sets whether the window should always be restored to the restore bounds 123 // (sometimes the workspace layout manager restores the window to its original 124 // bounds instead of the restore bounds. Setting this key overrides that 125 // behaviour). The flag is reset to the default value after the window is 126 // restored. always_restores_to_restore_bounds()127 bool always_restores_to_restore_bounds() const { 128 return always_restores_to_restore_bounds_; 129 } set_always_restores_to_restore_bounds(bool value)130 void set_always_restores_to_restore_bounds(bool value) { 131 always_restores_to_restore_bounds_ = value; 132 } 133 134 // Gets/sets whether the shelf should be hidden when this window is 135 // fullscreen. hide_shelf_when_fullscreen()136 bool hide_shelf_when_fullscreen() const { 137 return hide_shelf_when_fullscreen_; 138 } 139 set_hide_shelf_when_fullscreen(bool value)140 void set_hide_shelf_when_fullscreen(bool value) { 141 hide_shelf_when_fullscreen_ = value; 142 } 143 144 // Sets/gets the flag to suppress the cross-fade animation for 145 // the transition to the fullscreen state. animate_to_fullscreen()146 bool animate_to_fullscreen() const { 147 return animate_to_fullscreen_; 148 } set_animate_to_fullscreen(bool value)149 void set_animate_to_fullscreen(bool value) { 150 animate_to_fullscreen_ = value; 151 } 152 153 // If the minimum visibilty is true, ash will try to keep a 154 // minimum amount of the window is always visible on the work area 155 // when shown. 156 // TODO(oshima): Consolidate this and window_position_managed 157 // into single parameter to control the window placement. minimum_visibility()158 bool minimum_visibility() const { 159 return minimum_visibility_; 160 } set_minimum_visibility(bool minimum_visibility)161 void set_minimum_visibility(bool minimum_visibility) { 162 minimum_visibility_ = minimum_visibility; 163 } 164 165 // Gets/Sets the bounds of the window before it was moved by the auto window 166 // management. As long as it was not auto-managed, it will return NULL. pre_auto_manage_window_bounds()167 const gfx::Rect* pre_auto_manage_window_bounds() const { 168 return pre_auto_manage_window_bounds_.get(); 169 } 170 void SetPreAutoManageWindowBounds(const gfx::Rect& bounds); 171 172 // Layout related properties 173 174 void AddObserver(WindowStateObserver* observer); 175 void RemoveObserver(WindowStateObserver* observer); 176 177 // Whether the window is being dragged. is_dragged()178 bool is_dragged() const { return !!window_resizer_; } 179 180 // Whether or not the window's position can be managed by the 181 // auto management logic. window_position_managed()182 bool window_position_managed() const { return window_position_managed_; } set_window_position_managed(bool window_position_managed)183 void set_window_position_managed(bool window_position_managed) { 184 window_position_managed_ = window_position_managed; 185 } 186 187 // Whether or not the window's position or size was changed by a user. bounds_changed_by_user()188 bool bounds_changed_by_user() const { return bounds_changed_by_user_; } set_bounds_changed_by_user(bool bounds_changed_by_user)189 void set_bounds_changed_by_user(bool bounds_changed_by_user) { 190 bounds_changed_by_user_ = bounds_changed_by_user; 191 } 192 193 // True if this window is an attached panel. panel_attached()194 bool panel_attached() const { 195 return panel_attached_; 196 } set_panel_attached(bool panel_attached)197 void set_panel_attached(bool panel_attached) { 198 panel_attached_ = panel_attached; 199 } 200 201 // Indicates that an in progress drag should be continued after the 202 // window is reparented to another container. continue_drag_after_reparent()203 bool continue_drag_after_reparent() const { 204 return continue_drag_after_reparent_; 205 } set_continue_drag_after_reparent(bool value)206 void set_continue_drag_after_reparent(bool value) { 207 continue_drag_after_reparent_ = value; 208 } 209 210 // True if the window is ignored by the shelf layout manager for 211 // purposes of darkening the shelf. ignored_by_shelf()212 bool ignored_by_shelf() const { return ignored_by_shelf_; } set_ignored_by_shelf(bool ignored_by_shelf)213 void set_ignored_by_shelf(bool ignored_by_shelf) { 214 ignored_by_shelf_ = ignored_by_shelf; 215 } 216 217 // True if the window should be offered a chance to consume special system 218 // keys such as brightness, volume, etc. that are usually handled by the 219 // shell. can_consume_system_keys()220 bool can_consume_system_keys() const { return can_consume_system_keys_; } set_can_consume_system_keys(bool can_consume_system_keys)221 void set_can_consume_system_keys(bool can_consume_system_keys) { 222 can_consume_system_keys_ = can_consume_system_keys; 223 } 224 225 // True if this window has requested that the top-row keys (back, forward, 226 // brightness, volume) should be treated as function keys. top_row_keys_are_function_keys()227 bool top_row_keys_are_function_keys() const { 228 return top_row_keys_are_function_keys_; 229 } set_top_row_keys_are_function_keys(bool value)230 void set_top_row_keys_are_function_keys(bool value) { 231 top_row_keys_are_function_keys_ = value; 232 } 233 234 // Returns or sets a pointer to WindowResizer when resizing is active. 235 // The pointer to a WindowResizer that is returned is set when a resizer gets 236 // created and cleared when it gets destroyed. WindowState does not own the 237 // |window_resizer_| instance and the resizer's lifetime is controlled 238 // externally. It can be used to avoid creating multiple instances of a 239 // WindowResizer for the same window. window_resizer()240 WindowResizer* window_resizer() const { 241 return window_resizer_; 242 } set_window_resizer_(WindowResizer * window_resizer)243 void set_window_resizer_(WindowResizer* window_resizer) { 244 window_resizer_ = window_resizer; 245 } 246 247 // aura::WindowObserver overrides: 248 virtual void OnWindowPropertyChanged(aura::Window* window, 249 const void* key, 250 intptr_t old) OVERRIDE; 251 252 private: 253 // Snaps the window to left or right of the desktop with given bounds. 254 void SnapWindow(WindowShowType left_or_right, 255 const gfx::Rect& bounds); 256 257 // Sets the window show type and updates the show state if necessary. 258 void SetWindowShowType(WindowShowType new_window_show_type); 259 260 // The owner of this window settings. 261 aura::Window* window_; 262 scoped_ptr<WindowStateDelegate> delegate_; 263 264 bool window_position_managed_; 265 bool bounds_changed_by_user_; 266 bool panel_attached_; 267 bool continue_drag_after_reparent_; 268 bool ignored_by_shelf_; 269 bool can_consume_system_keys_; 270 bool top_row_keys_are_function_keys_; 271 WindowResizer* window_resizer_; 272 273 bool always_restores_to_restore_bounds_; 274 bool hide_shelf_when_fullscreen_; 275 bool animate_to_fullscreen_; 276 bool minimum_visibility_; 277 278 // A property to remember the window position which was set before the 279 // auto window position manager changed the window bounds, so that it can get 280 // restored when only this one window gets shown. 281 scoped_ptr<gfx::Rect> pre_auto_manage_window_bounds_; 282 283 ObserverList<WindowStateObserver> observer_list_; 284 285 // True when in SetWindowShowType(). This is used to avoid reentrance. 286 bool in_set_window_show_type_; 287 288 WindowShowType window_show_type_; 289 290 DISALLOW_COPY_AND_ASSIGN(WindowState); 291 }; 292 293 // Returns the WindowState for active window. Returns |NULL| 294 // if there is no active window. 295 ASH_EXPORT WindowState* GetActiveWindowState(); 296 297 // Returns the WindowState for |window|. Creates WindowState 298 // if it didn't exist. The settings object is owned by |window|. 299 ASH_EXPORT WindowState* GetWindowState(aura::Window* window); 300 301 // const version of GetWindowState. 302 ASH_EXPORT const WindowState* 303 GetWindowState(const aura::Window* window); 304 305 } // namespace wm 306 } // namespace ash 307 308 #endif // ASH_WM_WINDOW_STATE_H_ 309