Add screen space opacity to opacity tree
[chromium-blink-merge.git] / ash / wm / window_state.h
blobc42461cecf70a17d430bf828d569b1572a3a4b64
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.
5 #ifndef ASH_WM_WINDOW_STATE_H_
6 #define ASH_WM_WINDOW_STATE_H_
8 #include "ash/ash_export.h"
9 #include "ash/wm/drag_details.h"
10 #include "ash/wm/wm_types.h"
11 #include "base/basictypes.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/observer_list.h"
15 #include "ui/aura/window_observer.h"
16 #include "ui/base/ui_base_types.h"
18 namespace aura {
19 class Window;
22 namespace gfx {
23 class Rect;
26 namespace ash {
27 class WorkspaceLayoutManager;
28 class LockWindowState;
29 class MaximizeModeWindowState;
31 namespace wm {
32 class WindowStateDelegate;
33 class WindowStateObserver;
34 class WMEvent;
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 : public aura::WindowObserver {
48 public:
50 // A subclass of State class represents one of the window's states
51 // that corresponds to WindowStateType in Ash environment, e.g.
52 // maximized, minimized or side snapped, as subclass.
53 // Each subclass defines its own behavior and transition for each WMEvent.
54 class State {
55 public:
56 State() {}
57 virtual ~State() {}
59 // Update WindowState based on |event|.
60 virtual void OnWMEvent(WindowState* window_state, const WMEvent* event) = 0;
62 virtual WindowStateType GetType() const = 0;
64 // Gets called when the state object became active and the managed window
65 // needs to be adjusted to the State's requirement.
66 // The passed |previous_state| may be used to properly implement state
67 // transitions such as bound animations from the previous state.
68 // Note: This only gets called when the state object gets changed.
69 virtual void AttachState(WindowState* window_state,
70 State* previous_state) = 0;
72 // Gets called before the state objects gets deactivated / detached from the
73 // window, so that it can save the various states it is interested in.
74 // Note: This only gets called when the state object gets changed.
75 virtual void DetachState(WindowState* window_state) = 0;
77 private:
78 DISALLOW_COPY_AND_ASSIGN(State);
81 // Call GetWindowState() to instantiate this class.
82 ~WindowState() override;
84 aura::Window* window() { return window_; }
85 const aura::Window* window() const { return window_; }
87 bool HasDelegate() const;
88 void SetDelegate(scoped_ptr<WindowStateDelegate> delegate);
90 // Returns the window's current ash state type.
91 // Refer to WindowStateType definition in wm_types.h as for why Ash
92 // has its own state type.
93 WindowStateType GetStateType() const;
95 // Predicates to check window state.
96 bool IsMinimized() const;
97 bool IsMaximized() const;
98 bool IsFullscreen() const;
99 bool IsMaximizedOrFullscreen() const;
100 bool IsSnapped() const;
102 // True if the window's state type is WINDOW_STATE_TYPE_NORMAL or
103 // WINDOW_STATE_TYPE_DEFAULT.
104 bool IsNormalStateType() const;
106 bool IsNormalOrSnapped() const;
108 bool IsActive() const;
109 bool IsDocked() const;
111 // Returns true if the window's location can be controlled by the user.
112 bool IsUserPositionable() const;
114 // Checks if the window can change its state accordingly.
115 bool CanMaximize() const;
116 bool CanMinimize() const;
117 bool CanResize() const;
118 bool CanSnap() const;
119 bool CanActivate() const;
121 // Returns true if the window has restore bounds.
122 bool HasRestoreBounds() const;
124 // These methods use aura::WindowProperty to change the window's state
125 // instead of using WMEvent directly. This is to use the same mechanism as
126 // what views::Widget is using.
127 void Maximize();
128 void Minimize();
129 void Unminimize();
131 void Activate();
132 void Deactivate();
134 // Set the window state to normal.
135 // TODO(oshima): Change to use RESTORE event.
136 void Restore();
138 // Invoked when a WMevent occurs, which drives the internal
139 // state machine.
140 void OnWMEvent(const WMEvent* event);
142 // TODO(oshima): Try hiding these methods and making them accessible only to
143 // state impl. State changes should happen through events (as much
144 // as possible).
146 // Saves the current bounds to be used as a restore bounds.
147 void SaveCurrentBoundsForRestore();
149 // Same as |GetRestoreBoundsInScreen| except that it returns the
150 // bounds in the parent's coordinates.
151 gfx::Rect GetRestoreBoundsInParent() const;
153 // Returns the restore bounds property on the window in the virtual screen
154 // coordinates. The bounds can be NULL if the bounds property does not
155 // exist for the window. The window owns the bounds object.
156 gfx::Rect GetRestoreBoundsInScreen() const;
158 // Same as |SetRestoreBoundsInScreen| except that the bounds is in the
159 // parent's coordinates.
160 void SetRestoreBoundsInParent(const gfx::Rect& bounds_in_parent);
162 // Sets the restore bounds property on the window in the virtual screen
163 // coordinates. Deletes existing bounds value if exists.
164 void SetRestoreBoundsInScreen(const gfx::Rect& bounds_in_screen);
166 // Deletes and clears the restore bounds property on the window.
167 void ClearRestoreBounds();
169 // Replace the State object of a window with a state handler which can
170 // implement a new window manager type. The passed object will be owned
171 // by this object and the returned object will be owned by the caller.
172 scoped_ptr<State> SetStateObject(scoped_ptr<State> new_state);
174 // True if the window should be unminimized to the restore bounds, as
175 // opposed to the window's current bounds. |unminimized_to_restore_bounds_| is
176 // reset to the default value after the window is unminimized.
177 bool unminimize_to_restore_bounds() const {
178 return unminimize_to_restore_bounds_;
180 void set_unminimize_to_restore_bounds(bool value) {
181 unminimize_to_restore_bounds_ = value;
184 // Gets/sets whether the shelf should be hidden when this window is
185 // fullscreen.
186 bool hide_shelf_when_fullscreen() const {
187 return hide_shelf_when_fullscreen_;
190 void set_hide_shelf_when_fullscreen(bool value) {
191 hide_shelf_when_fullscreen_ = value;
194 // If the minimum visibilty is true, ash will try to keep a
195 // minimum amount of the window is always visible on the work area
196 // when shown.
197 // TODO(oshima): Consolidate this and window_position_managed
198 // into single parameter to control the window placement.
199 bool minimum_visibility() const {
200 return minimum_visibility_;
202 void set_minimum_visibility(bool minimum_visibility) {
203 minimum_visibility_ = minimum_visibility;
206 // Specifies if the window can be dragged by the user via the caption or not.
207 bool can_be_dragged() const {
208 return can_be_dragged_;
210 void set_can_be_dragged(bool can_be_dragged) {
211 can_be_dragged_ = can_be_dragged;
214 // Gets/Sets the bounds of the window before it was moved by the auto window
215 // management. As long as it was not auto-managed, it will return NULL.
216 const gfx::Rect* pre_auto_manage_window_bounds() const {
217 return pre_auto_manage_window_bounds_.get();
219 void SetPreAutoManageWindowBounds(const gfx::Rect& bounds);
221 // Layout related properties
223 void AddObserver(WindowStateObserver* observer);
224 void RemoveObserver(WindowStateObserver* observer);
226 // Whether the window is being dragged.
227 bool is_dragged() const {
228 return drag_details_;
231 // Whether or not the window's position can be managed by the
232 // auto management logic.
233 bool window_position_managed() const { return window_position_managed_; }
234 void set_window_position_managed(bool window_position_managed) {
235 window_position_managed_ = window_position_managed;
238 // Whether or not the window's position or size was changed by a user.
239 bool bounds_changed_by_user() const { return bounds_changed_by_user_; }
240 void set_bounds_changed_by_user(bool bounds_changed_by_user);
242 // True if this window is an attached panel.
243 bool panel_attached() const {
244 return panel_attached_;
246 void set_panel_attached(bool panel_attached) {
247 panel_attached_ = panel_attached;
250 // True if the window is ignored by the shelf layout manager for
251 // purposes of darkening the shelf.
252 bool ignored_by_shelf() const { return ignored_by_shelf_; }
253 void set_ignored_by_shelf(bool ignored_by_shelf) {
254 ignored_by_shelf_ = ignored_by_shelf;
257 // True if the window should be offered a chance to consume special system
258 // keys such as brightness, volume, etc. that are usually handled by the
259 // shell.
260 bool can_consume_system_keys() const { return can_consume_system_keys_; }
261 void set_can_consume_system_keys(bool can_consume_system_keys) {
262 can_consume_system_keys_ = can_consume_system_keys;
265 // True if this window has requested that the top-row keys (back, forward,
266 // brightness, volume) should be treated as function keys.
267 bool top_row_keys_are_function_keys() const {
268 return top_row_keys_are_function_keys_;
270 void set_top_row_keys_are_function_keys(bool value) {
271 top_row_keys_are_function_keys_ = value;
274 // True if the window is in "immersive full screen mode" which is slightly
275 // different from the normal fullscreen mode by allowing the user to reveal
276 // the top portion of the window through a touch / mouse gesture. It might
277 // also allow the shelf to be shown in some situations.
278 bool in_immersive_fullscreen() const {
279 return in_immersive_fullscreen_;
281 void set_in_immersive_fullscreen(bool enable) {
282 in_immersive_fullscreen_ = enable;
285 // Creates and takes ownership of a pointer to DragDetails when resizing is
286 // active. This should be done before a resizer gets created.
287 void CreateDragDetails(aura::Window* window,
288 const gfx::Point& point_in_parent,
289 int window_component,
290 aura::client::WindowMoveSource source);
292 // Deletes and clears a pointer to DragDetails. This should be done when the
293 // resizer gets destroyed.
294 void DeleteDragDetails();
296 // Sets the currently stored restore bounds and clears the restore bounds.
297 void SetAndClearRestoreBounds();
299 // Returns a pointer to DragDetails during drag operations.
300 const DragDetails* drag_details() const { return drag_details_.get(); }
301 DragDetails* drag_details() { return drag_details_.get(); }
303 // aura::WindowObserver overrides:
304 void OnWindowPropertyChanged(aura::Window* window,
305 const void* key,
306 intptr_t old) override;
308 private:
309 friend class DefaultState;
310 friend class ash::LockWindowState;
311 friend class ash::MaximizeModeWindowState;
312 friend ASH_EXPORT WindowState* GetWindowState(aura::Window*);
313 FRIEND_TEST_ALL_PREFIXES(WindowAnimationsTest, CrossFadeToBounds);
314 FRIEND_TEST_ALL_PREFIXES(WindowAnimationsTest,
315 CrossFadeToBoundsFromTransform);
317 explicit WindowState(aura::Window* window);
319 WindowStateDelegate* delegate() { return delegate_.get(); }
321 // Returns the window's current show state.
322 ui::WindowShowState GetShowState() const;
324 // Sets the window's bounds in screen coordinates.
325 void SetBoundsInScreen(const gfx::Rect& bounds_in_screen);
327 // Adjusts the |bounds| so that they are flush with the edge of the
328 // workspace if the window represented by |window_state| is side snapped.
329 void AdjustSnappedBounds(gfx::Rect* bounds);
331 // Updates the window show state according to the current window state type.
332 // Note that this does not update the window bounds.
333 void UpdateWindowShowStateFromStateType();
335 void NotifyPreStateTypeChange(WindowStateType old_window_state_type);
336 void NotifyPostStateTypeChange(WindowStateType old_window_state_type);
338 // Sets |bounds| as is and ensure the layer is aligned with pixel boundary.
339 void SetBoundsDirect(const gfx::Rect& bounds);
341 // Sets the window's |bounds| with constraint where the size of the
342 // new bounds will not exceeds the size of the work area.
343 void SetBoundsConstrained(const gfx::Rect& bounds);
345 // Sets the wndow's |bounds| and transitions to the new bounds with
346 // a scale animation.
347 void SetBoundsDirectAnimated(const gfx::Rect& bounds);
349 // Sets the window's |bounds| and transition to the new bounds with
350 // a cross fade animation.
351 void SetBoundsDirectCrossFade(const gfx::Rect& bounds);
353 // The owner of this window settings.
354 aura::Window* window_;
355 scoped_ptr<WindowStateDelegate> delegate_;
357 bool window_position_managed_;
358 bool bounds_changed_by_user_;
359 bool panel_attached_;
360 bool ignored_by_shelf_;
361 bool can_consume_system_keys_;
362 bool top_row_keys_are_function_keys_;
363 scoped_ptr<DragDetails> drag_details_;
365 bool unminimize_to_restore_bounds_;
366 bool in_immersive_fullscreen_;
367 bool hide_shelf_when_fullscreen_;
368 bool minimum_visibility_;
369 bool can_be_dragged_;
371 // A property to remember the window position which was set before the
372 // auto window position manager changed the window bounds, so that it can get
373 // restored when only this one window gets shown.
374 scoped_ptr<gfx::Rect> pre_auto_manage_window_bounds_;
376 base::ObserverList<WindowStateObserver> observer_list_;
378 // True to ignore a property change event to avoid reentrance in
379 // UpdateWindowStateType()
380 bool ignore_property_change_;
382 scoped_ptr<State> current_state_;
384 DISALLOW_COPY_AND_ASSIGN(WindowState);
387 // Returns the WindowState for active window. Returns |NULL|
388 // if there is no active window.
389 ASH_EXPORT WindowState* GetActiveWindowState();
391 // Returns the WindowState for |window|. Creates WindowState
392 // if it didn't exist. The settings object is owned by |window|.
393 ASH_EXPORT WindowState* GetWindowState(aura::Window* window);
395 // const version of GetWindowState.
396 ASH_EXPORT const WindowState*
397 GetWindowState(const aura::Window* window);
399 } // namespace wm
400 } // namespace ash
402 #endif // ASH_WM_WINDOW_STATE_H_