1 // Copyright 2012 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_SHELF_SHELF_LAYOUT_MANAGER_H_
6 #define ASH_SHELF_SHELF_LAYOUT_MANAGER_H_
10 #include "ash/ash_export.h"
11 #include "ash/session/session_state_observer.h"
12 #include "ash/shelf/background_animator.h"
13 #include "ash/shelf/shelf.h"
14 #include "ash/shelf/shelf_types.h"
15 #include "ash/shell_observer.h"
16 #include "ash/snap_to_pixel_layout_manager.h"
17 #include "ash/system/status_area_widget.h"
18 #include "ash/wm/dock/docked_window_layout_manager_observer.h"
19 #include "ash/wm/lock_state_observer.h"
20 #include "ash/wm/workspace/workspace_types.h"
21 #include "base/basictypes.h"
22 #include "base/compiler_specific.h"
23 #include "base/logging.h"
24 #include "base/observer_list.h"
25 #include "base/timer/timer.h"
26 #include "ui/gfx/geometry/insets.h"
27 #include "ui/gfx/geometry/rect.h"
28 #include "ui/keyboard/keyboard_controller.h"
29 #include "ui/keyboard/keyboard_controller_observer.h"
30 #include "ui/wm/public/activation_change_observer.h"
38 class ImplicitAnimationObserver
;
42 class PanelLayoutManagerTest
;
44 class ShelfBezelEventFilter
;
45 class ShelfLayoutManagerObserver
;
46 class ShelfLayoutManagerTest
;
48 class StatusAreaWidget
;
49 class WorkspaceController
;
50 FORWARD_DECLARE_TEST(AshPopupAlignmentDelegateTest
, AutoHide
);
51 FORWARD_DECLARE_TEST(WebNotificationTrayTest
, PopupAndFullscreen
);
53 // ShelfLayoutManager is the layout manager responsible for the shelf and
54 // status widgets. The shelf is given the total available width and told the
55 // width of the status area. This allows the shelf to draw the background and
56 // layout to the status area.
57 // To respond to bounds changes in the status area StatusAreaLayoutManager works
58 // closely with ShelfLayoutManager.
59 class ASH_EXPORT ShelfLayoutManager
60 : public ash::ShellObserver
,
61 public aura::client::ActivationChangeObserver
,
62 public DockedWindowLayoutManagerObserver
,
63 public keyboard::KeyboardControllerObserver
,
64 public LockStateObserver
,
65 public SnapToPixelLayoutManager
,
66 public SessionStateObserver
{
69 // We reserve a small area on the edge of the workspace area to ensure that
70 // the resize handle at the edge of the window can be hit.
71 static const int kWorkspaceAreaVisibleInset
;
73 // When autohidden we extend the touch hit target onto the screen so that the
74 // user can drag the shelf out.
75 static const int kWorkspaceAreaAutoHideInset
;
77 // Size of the shelf when auto-hidden.
78 static const int kAutoHideSize
;
80 // Inset between the inner edge of the shelf (towards centre of screen), and
81 // the shelf items, notifications, status area etc.
82 static const int kShelfItemInset
;
84 explicit ShelfLayoutManager(ShelfWidget
* shelf
);
85 ~ShelfLayoutManager() override
;
87 // Sets the ShelfAutoHideBehavior. See enum description for details.
88 void SetAutoHideBehavior(ShelfAutoHideBehavior behavior
);
89 ShelfAutoHideBehavior
auto_hide_behavior() const {
90 return auto_hide_behavior_
;
93 // Sets the alignment. Returns true if the alignment got changed. If nothing
94 // has visually be changed, false will be returned. This can happen if either
95 // the alignment was already set, or the shelf is currently locked and cannot
96 // be changed at this time. In the latter case the change will be performed
97 // once the shelf gets unlocked.
98 bool SetAlignment(ShelfAlignment alignment
);
100 // Returns the desired alignment for the current state, either the user's
101 // set alignment (alignment_) or SHELF_ALIGNMENT_BOTTOM when the screen
103 ShelfAlignment
GetAlignment() const;
105 void set_workspace_controller(WorkspaceController
* controller
) {
106 workspace_controller_
= controller
;
109 bool updating_bounds() const { return updating_bounds_
; }
111 // Clears internal data for shutdown process.
112 void PrepareForShutdown();
114 // Returns whether the shelf and its contents (shelf, status) are visible
116 bool IsVisible() const;
118 // Returns the ideal bounds of the shelf assuming it is visible.
119 gfx::Rect
GetIdealBounds();
121 // Returns the docked area bounds.
122 const gfx::Rect
& dock_bounds() const { return dock_bounds_
; }
124 // Stops any animations and sets the bounds of the shelf and status
128 // Returns shelf visibility state based on current value of auto hide
130 ShelfVisibilityState
CalculateShelfVisibility();
132 // Updates the visibility state.
133 void UpdateVisibilityState();
135 // Invoked by the shelf when the auto-hide state may have changed.
136 void UpdateAutoHideState();
138 ShelfVisibilityState
visibility_state() const {
139 return state_
.visibility_state
;
141 ShelfAutoHideState
auto_hide_state() const { return state_
.auto_hide_state
; }
143 ShelfWidget
* shelf_widget() { return shelf_
; }
145 // Sets whether any windows overlap the shelf. If a window overlaps the shelf
146 // the shelf renders slightly differently.
147 void SetWindowOverlapsShelf(bool value
);
148 bool window_overlaps_shelf() const { return window_overlaps_shelf_
; }
150 void AddObserver(ShelfLayoutManagerObserver
* observer
);
151 void RemoveObserver(ShelfLayoutManagerObserver
* observer
);
153 // Gesture related functions:
154 void OnGestureEdgeSwipe(const ui::GestureEvent
& gesture
);
155 void StartGestureDrag(const ui::GestureEvent
& gesture
);
160 // Returns DRAG_SHELF if the gesture should continue to drag the entire shelf.
161 // Returns DRAG_TRAY if the gesture can start dragging the tray-bubble from
163 DragState
UpdateGestureDrag(const ui::GestureEvent
& gesture
);
164 void CompleteGestureDrag(const ui::GestureEvent
& gesture
);
165 void CancelGestureDrag();
167 // Set an animation duration override for the show / hide animation of the
168 // shelf. Specifying 0 leads to use the default.
169 void SetAnimationDurationOverride(int duration_override_in_ms
);
171 // Overridden from SnapLayoutManager:
172 void OnWindowResized() override
;
173 void SetChildBounds(aura::Window
* child
,
174 const gfx::Rect
& requested_bounds
) override
;
176 // Overridden from ash::ShellObserver:
177 void OnLockStateChanged(bool locked
) override
;
179 // Overriden from aura::client::ActivationChangeObserver:
180 void OnWindowActivated(aura::Window
* gained_active
,
181 aura::Window
* lost_active
) override
;
183 // Overridden from ash::LockStateObserver:
184 void OnLockStateEvent(LockStateObserver::EventType event
) override
;
186 // Overridden from ash::SessionStateObserver:
187 void SessionStateChanged(SessionStateDelegate::SessionState state
) override
;
189 // TODO(harrym|oshima): These templates will be moved to
191 // A helper function that provides a shortcut for choosing
192 // values specific to a shelf alignment.
194 T
SelectValueForShelfAlignment(T bottom
, T left
, T right
, T top
) const {
195 switch (GetAlignment()) {
196 case SHELF_ALIGNMENT_BOTTOM
:
198 case SHELF_ALIGNMENT_LEFT
:
200 case SHELF_ALIGNMENT_RIGHT
:
202 case SHELF_ALIGNMENT_TOP
:
210 T
PrimaryAxisValue(T horizontal
, T vertical
) const {
211 return IsHorizontalAlignment() ? horizontal
: vertical
;
214 // Is the shelf's alignment horizontal?
215 bool IsHorizontalAlignment() const;
217 // Returns a ShelfLayoutManager on the display which has a shelf for
218 // given |window|. See RootWindowController::ForShelf for more info.
219 static ShelfLayoutManager
* ForShelf(aura::Window
* window
);
222 class AutoHideEventFilter
;
223 class UpdateShelfObserver
;
224 friend class ash::ScreenAsh
;
225 friend class PanelLayoutManagerTest
;
226 friend class ShelfLayoutManagerTest
;
227 FRIEND_TEST_ALL_PREFIXES(ash::AshPopupAlignmentDelegateTest
, AutoHide
);
228 FRIEND_TEST_ALL_PREFIXES(ash::WebNotificationTrayTest
, PopupAndFullscreen
);
230 struct TargetBounds
{
235 float status_opacity
;
236 gfx::Rect shelf_bounds_in_root
;
237 gfx::Rect shelf_bounds_in_shelf
;
238 gfx::Rect status_bounds_in_shelf
;
239 gfx::Insets work_area_insets
;
243 State() : visibility_state(SHELF_VISIBLE
),
244 auto_hide_state(SHELF_AUTO_HIDE_HIDDEN
),
245 window_state(WORKSPACE_WINDOW_STATE_DEFAULT
),
246 is_screen_locked(false),
247 is_adding_user_screen(false) {}
249 // Returns true if the two states are considered equal. As
250 // |auto_hide_state| only matters if |visibility_state| is
251 // |SHELF_AUTO_HIDE|, Equals() ignores the |auto_hide_state| as
253 bool Equals(const State
& other
) const {
254 return other
.visibility_state
== visibility_state
&&
255 (visibility_state
!= SHELF_AUTO_HIDE
||
256 other
.auto_hide_state
== auto_hide_state
) &&
257 other
.window_state
== window_state
&&
258 other
.is_screen_locked
== is_screen_locked
&&
259 other
.is_adding_user_screen
== is_adding_user_screen
;
262 ShelfVisibilityState visibility_state
;
263 ShelfAutoHideState auto_hide_state
;
264 WorkspaceWindowState window_state
;
265 bool is_screen_locked
;
266 bool is_adding_user_screen
;
269 // Sets the visibility of the shelf to |state|.
270 void SetState(ShelfVisibilityState visibility_state
);
272 // Updates the bounds and opacity of the shelf and status widgets.
273 // If |observer| is specified, it will be called back when the animations, if
274 // any, are complete.
275 void UpdateBoundsAndOpacity(const TargetBounds
& target_bounds
,
277 ui::ImplicitAnimationObserver
* observer
);
279 // Stops any animations and progresses them to the end.
280 void StopAnimating();
282 // Returns the width (if aligned to the side) or height (if aligned to the
284 void GetShelfSize(int* width
, int* height
);
286 // Insets |bounds| by |inset| on the edge the shelf is aligned to.
287 void AdjustBoundsBasedOnAlignment(int inset
, gfx::Rect
* bounds
) const;
289 // Calculates the target bounds assuming visibility of |visible|.
290 void CalculateTargetBounds(const State
& state
, TargetBounds
* target_bounds
);
292 // Updates the target bounds if a gesture-drag is in progress. This is only
293 // used by |CalculateTargetBounds()|.
294 void UpdateTargetBoundsForGesture(TargetBounds
* target_bounds
) const;
296 // Updates the background of the shelf.
297 void UpdateShelfBackground(BackgroundAnimatorChangeType type
);
299 // Returns how the shelf background is painted.
300 ShelfBackgroundType
GetShelfBackgroundType() const;
302 // Updates the auto hide state immediately.
303 void UpdateAutoHideStateNow();
305 // Stops the auto hide timer and clears
306 // |mouse_over_shelf_when_auto_hide_timer_started_|.
307 void StopAutoHideTimer();
309 // Returns the bounds of an additional region which can trigger showing the
310 // shelf. This region exists to make it easier to trigger showing the shelf
311 // when the shelf is auto hidden and the shelf is on the boundary between
313 gfx::Rect
GetAutoHideShowShelfRegionInScreen() const;
315 // Returns the AutoHideState. This value is determined from the shelf and
317 ShelfAutoHideState
CalculateAutoHideState(
318 ShelfVisibilityState visibility_state
) const;
320 // Returns true if |window| is a descendant of the shelf.
321 bool IsShelfWindow(aura::Window
* window
);
323 int GetWorkAreaSize(const State
& state
, int size
) const;
325 // Overridden from keyboard::KeyboardControllerObserver:
326 void OnKeyboardBoundsChanging(const gfx::Rect
& new_bounds
) override
;
328 // Overridden from DockedWindowLayoutManagerObserver:
329 void OnDockBoundsChanging(
330 const gfx::Rect
& dock_bounds
,
331 DockedWindowLayoutManagerObserver::Reason reason
) override
;
333 // Called when the LoginUI changes from visible to invisible.
334 void UpdateShelfVisibilityAfterLoginUIChange();
336 // The RootWindow is cached so that we don't invoke Shell::GetInstance() from
337 // our destructor. We avoid that as at the time we're deleted Shell is being
339 aura::Window
* root_window_
;
341 // True when inside UpdateBoundsAndOpacity() method. Used to prevent calling
342 // UpdateBoundsAndOpacity() again from SetChildBounds().
343 bool updating_bounds_
;
345 // See description above setter.
346 ShelfAutoHideBehavior auto_hide_behavior_
;
348 // See description above getter.
349 ShelfAlignment alignment_
;
356 WorkspaceController
* workspace_controller_
;
358 // Do any windows overlap the shelf? This is maintained by WorkspaceManager.
359 bool window_overlaps_shelf_
;
361 base::OneShotTimer
<ShelfLayoutManager
> auto_hide_timer_
;
363 // Whether the mouse was over the shelf when the auto hide timer started.
364 // False when neither the auto hide timer nor the timer task are running.
365 bool mouse_over_shelf_when_auto_hide_timer_started_
;
367 // EventFilter used to detect when user moves the mouse over the shelf to
368 // trigger showing the shelf.
369 scoped_ptr
<AutoHideEventFilter
> auto_hide_event_filter_
;
371 // EventFilter used to detect when user issues a gesture on a bezel sensor.
372 scoped_ptr
<ShelfBezelEventFilter
> bezel_event_filter_
;
374 ObserverList
<ShelfLayoutManagerObserver
> observers_
;
376 // The shelf reacts to gesture-drags, and can be set to auto-hide for certain
377 // gestures. Some shelf behaviour (e.g. visibility state, background color
378 // etc.) are affected by various stages of the drag. The enum keeps track of
379 // the present status of the gesture drag.
380 enum GestureDragStatus
{
382 GESTURE_DRAG_IN_PROGRESS
,
383 GESTURE_DRAG_CANCEL_IN_PROGRESS
,
384 GESTURE_DRAG_COMPLETE_IN_PROGRESS
386 GestureDragStatus gesture_drag_status_
;
388 // Tracks the amount of the drag. The value is only valid when
389 // |gesture_drag_status_| is set to GESTURE_DRAG_IN_PROGRESS.
390 float gesture_drag_amount_
;
392 // Manage the auto-hide state during the gesture.
393 ShelfAutoHideState gesture_drag_auto_hide_state_
;
395 // Used to delay updating shelf background.
396 UpdateShelfObserver
* update_shelf_observer_
;
398 // The bounds of the keyboard.
399 gfx::Rect keyboard_bounds_
;
401 // The bounds of the dock.
402 gfx::Rect dock_bounds_
;
404 // The show hide animation duration override or 0 for default.
405 int duration_override_in_ms_
;
407 DISALLOW_COPY_AND_ASSIGN(ShelfLayoutManager
);
412 #endif // ASH_SHELF_SHELF_LAYOUT_MANAGER_H_