1 // Copyright (c) 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/system/status_area_widget.h"
17 #include "ash/wm/dock/docked_window_layout_manager_observer.h"
18 #include "ash/wm/lock_state_observer.h"
19 #include "ash/wm/workspace/workspace_types.h"
20 #include "base/basictypes.h"
21 #include "base/compiler_specific.h"
22 #include "base/logging.h"
23 #include "base/observer_list.h"
24 #include "base/timer/timer.h"
25 #include "ui/aura/layout_manager.h"
26 #include "ui/gfx/insets.h"
27 #include "ui/gfx/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(WebNotificationTrayTest, PopupAndFullscreen);
52 // ShelfLayoutManager is the layout manager responsible for the shelf and
53 // status widgets. The shelf is given the total available width and told the
54 // width of the status area. This allows the shelf to draw the background and
55 // layout to the status area.
56 // To respond to bounds changes in the status area StatusAreaLayoutManager works
57 // closely with ShelfLayoutManager.
58 class ASH_EXPORT ShelfLayoutManager :
59 public aura::LayoutManager,
60 public ash::ShellObserver,
61 public aura::client::ActivationChangeObserver,
62 public DockedWindowLayoutManagerObserver,
63 public keyboard::KeyboardControllerObserver,
64 public LockStateObserver,
65 public SessionStateObserver {
68 // We reserve a small area on the edge of the workspace area to ensure that
69 // the resize handle at the edge of the window can be hit.
70 static const int kWorkspaceAreaVisibleInset;
72 // When autohidden we extend the touch hit target onto the screen so that the
73 // user can drag the shelf out.
74 static const int kWorkspaceAreaAutoHideInset;
76 // Size of the shelf when auto-hidden.
77 static const int kAutoHideSize;
79 // Inset between the inner edge of the shelf (towards centre of screen), and
80 // the shelf items, notifications, status area etc.
81 static const int kShelfItemInset;
83 explicit ShelfLayoutManager(ShelfWidget* shelf);
84 virtual ~ShelfLayoutManager();
86 // Sets the ShelfAutoHideBehavior. See enum description for details.
87 void SetAutoHideBehavior(ShelfAutoHideBehavior behavior);
88 ShelfAutoHideBehavior auto_hide_behavior() const {
89 return auto_hide_behavior_;
92 // Sets the alignment. Returns true if the alignment is changed. Otherwise,
94 bool SetAlignment(ShelfAlignment alignment);
95 // Returns the desired alignment for the current state, either the user's
96 // set alignment (alignment_) or SHELF_ALIGNMENT_BOTTOM when the screen
98 ShelfAlignment GetAlignment() const;
100 void set_workspace_controller(WorkspaceController* controller) {
101 workspace_controller_ = controller;
104 bool updating_bounds() const { return updating_bounds_; }
106 // Clears internal data for shutdown process.
107 void PrepareForShutdown();
109 // Returns whether the shelf and its contents (shelf, status) are visible
111 bool IsVisible() const;
113 // Returns the ideal bounds of the shelf assuming it is visible.
114 gfx::Rect GetIdealBounds();
116 // Returns the docked area bounds.
117 const gfx::Rect& dock_bounds() const { return dock_bounds_; }
119 // Stops any animations and sets the bounds of the shelf and status
123 // Returns shelf visibility state based on current value of auto hide
125 ShelfVisibilityState CalculateShelfVisibility();
127 // Updates the visibility state.
128 void UpdateVisibilityState();
130 // Invoked by the shelf when the auto-hide state may have changed.
131 void UpdateAutoHideState();
133 ShelfVisibilityState visibility_state() const {
134 return state_.visibility_state;
136 ShelfAutoHideState auto_hide_state() const { return state_.auto_hide_state; }
138 ShelfWidget* shelf_widget() { return shelf_; }
140 // Sets whether any windows overlap the shelf. If a window overlaps the shelf
141 // the shelf renders slightly differently.
142 void SetWindowOverlapsShelf(bool value);
143 bool window_overlaps_shelf() const { return window_overlaps_shelf_; }
145 void AddObserver(ShelfLayoutManagerObserver* observer);
146 void RemoveObserver(ShelfLayoutManagerObserver* observer);
148 // Gesture related functions:
149 void OnGestureEdgeSwipe(const ui::GestureEvent& gesture);
150 void StartGestureDrag(const ui::GestureEvent& gesture);
155 // Returns DRAG_SHELF if the gesture should continue to drag the entire shelf.
156 // Returns DRAG_TRAY if the gesture can start dragging the tray-bubble from
158 DragState UpdateGestureDrag(const ui::GestureEvent& gesture);
159 void CompleteGestureDrag(const ui::GestureEvent& gesture);
160 void CancelGestureDrag();
162 // Set an animation duration override for the show / hide animation of the
163 // shelf. Specifying 0 leads to use the default.
164 void SetAnimationDurationOverride(int duration_override_in_ms);
166 // Overridden from aura::LayoutManager:
167 virtual void OnWindowResized() OVERRIDE;
168 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE;
169 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE;
170 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE;
171 virtual void OnChildWindowVisibilityChanged(aura::Window* child,
172 bool visible) OVERRIDE;
173 virtual void SetChildBounds(aura::Window* child,
174 const gfx::Rect& requested_bounds) OVERRIDE;
176 // Overridden from ash::ShellObserver:
177 virtual void OnLockStateChanged(bool locked) OVERRIDE;
178 virtual void OnMaximizeModeStarted() OVERRIDE;
179 virtual void OnMaximizeModeEnded() OVERRIDE;
181 // Overriden from aura::client::ActivationChangeObserver:
182 virtual void OnWindowActivated(aura::Window* gained_active,
183 aura::Window* lost_active) OVERRIDE;
185 // Overridden from ash::LockStateObserver:
186 virtual void OnLockStateEvent(LockStateObserver::EventType event) OVERRIDE;
188 // Overridden from ash::SessionStateObserver:
189 virtual void SessionStateChanged(
190 SessionStateDelegate::SessionState state) OVERRIDE;
192 // TODO(harrym|oshima): These templates will be moved to
194 // A helper function that provides a shortcut for choosing
195 // values specific to a shelf alignment.
197 T SelectValueForShelfAlignment(T bottom, T left, T right, T top) const {
198 switch (GetAlignment()) {
199 case SHELF_ALIGNMENT_BOTTOM:
201 case SHELF_ALIGNMENT_LEFT:
203 case SHELF_ALIGNMENT_RIGHT:
205 case SHELF_ALIGNMENT_TOP:
213 T PrimaryAxisValue(T horizontal, T vertical) const {
214 return IsHorizontalAlignment() ? horizontal : vertical;
217 // Is the shelf's alignment horizontal?
218 bool IsHorizontalAlignment() const;
220 // Returns a ShelfLayoutManager on the display which has a shelf for
221 // given |window|. See RootWindowController::ForShelf for more info.
222 static ShelfLayoutManager* ForShelf(aura::Window* window);
225 class AutoHideEventFilter;
226 class UpdateShelfObserver;
227 friend class ash::ScreenAsh;
228 friend class PanelLayoutManagerTest;
229 friend class ShelfLayoutManagerTest;
230 FRIEND_TEST_ALL_PREFIXES(ash::WebNotificationTrayTest, PopupAndFullscreen);
232 struct TargetBounds {
237 float status_opacity;
238 gfx::Rect shelf_bounds_in_root;
239 gfx::Rect shelf_bounds_in_shelf;
240 gfx::Rect status_bounds_in_shelf;
241 gfx::Insets work_area_insets;
245 State() : visibility_state(SHELF_VISIBLE),
246 auto_hide_state(SHELF_AUTO_HIDE_HIDDEN),
247 window_state(WORKSPACE_WINDOW_STATE_DEFAULT),
248 is_screen_locked(false) {}
250 // Returns true if the two states are considered equal. As
251 // |auto_hide_state| only matters if |visibility_state| is
252 // |SHELF_AUTO_HIDE|, Equals() ignores the |auto_hide_state| as
254 bool Equals(const State& other) const {
255 return other.visibility_state == visibility_state &&
256 (visibility_state != SHELF_AUTO_HIDE ||
257 other.auto_hide_state == auto_hide_state) &&
258 other.window_state == window_state &&
259 other.is_screen_locked == is_screen_locked;
262 ShelfVisibilityState visibility_state;
263 ShelfAutoHideState auto_hide_state;
264 WorkspaceWindowState window_state;
265 bool is_screen_locked;
268 // Sets the visibility of the shelf to |state|.
269 void SetState(ShelfVisibilityState visibility_state);
271 // Updates the bounds and opacity of the shelf and status widgets.
272 // If |observer| is specified, it will be called back when the animations, if
273 // any, are complete.
274 void UpdateBoundsAndOpacity(const TargetBounds& target_bounds,
276 ui::ImplicitAnimationObserver* observer);
278 // Stops any animations and progresses them to the end.
279 void StopAnimating();
281 // Returns the width (if aligned to the side) or height (if aligned to the
283 void GetShelfSize(int* width, int* height);
285 // Insets |bounds| by |inset| on the edge the shelf is aligned to.
286 void AdjustBoundsBasedOnAlignment(int inset, gfx::Rect* bounds) const;
288 // Calculates the target bounds assuming visibility of |visible|.
289 void CalculateTargetBounds(const State& state, TargetBounds* target_bounds);
291 // Updates the target bounds if a gesture-drag is in progress. This is only
292 // used by |CalculateTargetBounds()|.
293 void UpdateTargetBoundsForGesture(TargetBounds* target_bounds) const;
295 // Updates the background of the shelf.
296 void UpdateShelfBackground(BackgroundAnimatorChangeType type);
298 // Returns how the shelf background is painted.
299 ShelfBackgroundType GetShelfBackgroundType() const;
301 // Updates the auto hide state immediately.
302 void UpdateAutoHideStateNow();
304 // Stops the auto hide timer and clears
305 // |mouse_over_shelf_when_auto_hide_timer_started_|.
306 void StopAutoHideTimer();
308 // Returns the bounds of an additional region which can trigger showing the
309 // shelf. This region exists to make it easier to trigger showing the shelf
310 // when the shelf is auto hidden and the shelf is on the boundary between
312 gfx::Rect GetAutoHideShowShelfRegionInScreen() const;
314 // Returns the AutoHideState. This value is determined from the shelf and
316 ShelfAutoHideState CalculateAutoHideState(
317 ShelfVisibilityState visibility_state) const;
319 // Returns true if |window| is a descendant of the shelf.
320 bool IsShelfWindow(aura::Window* window);
322 int GetWorkAreaSize(const State& state, int size) const;
324 // Returns true if the shelf should be forced to be visible.
325 bool IsShelfForcedToBeVisible() const;
327 // Overridden from keyboard::KeyboardControllerObserver:
328 virtual void OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) OVERRIDE;
330 // Overridden from DockedWindowLayoutManagerObserver:
331 virtual void OnDockBoundsChanging(
332 const gfx::Rect& dock_bounds,
333 DockedWindowLayoutManagerObserver::Reason reason) OVERRIDE;
335 // The RootWindow is cached so that we don't invoke Shell::GetInstance() from
336 // our destructor. We avoid that as at the time we're deleted Shell is being
338 aura::Window* root_window_;
340 // True when inside UpdateBoundsAndOpacity() method. Used to prevent calling
341 // UpdateBoundsAndOpacity() again from SetChildBounds().
342 bool updating_bounds_;
344 // If true, the shelf gets forced (e.g. by the maximize mode) to be always
346 bool force_shelf_always_visibile_;
348 // See description above setter.
349 ShelfAutoHideBehavior auto_hide_behavior_;
351 // See description above getter.
352 ShelfAlignment alignment_;
359 WorkspaceController* workspace_controller_;
361 // Do any windows overlap the shelf? This is maintained by WorkspaceManager.
362 bool window_overlaps_shelf_;
364 base::OneShotTimer<ShelfLayoutManager> auto_hide_timer_;
366 // Whether the mouse was over the shelf when the auto hide timer started.
367 // False when neither the auto hide timer nor the timer task are running.
368 bool mouse_over_shelf_when_auto_hide_timer_started_;
370 // EventFilter used to detect when user moves the mouse over the shelf to
371 // trigger showing the shelf.
372 scoped_ptr<AutoHideEventFilter> auto_hide_event_filter_;
374 // EventFilter used to detect when user issues a gesture on a bezel sensor.
375 scoped_ptr<ShelfBezelEventFilter> bezel_event_filter_;
377 ObserverList<ShelfLayoutManagerObserver> observers_;
379 // The shelf reacts to gesture-drags, and can be set to auto-hide for certain
380 // gestures. Some shelf behaviour (e.g. visibility state, background color
381 // etc.) are affected by various stages of the drag. The enum keeps track of
382 // the present status of the gesture drag.
383 enum GestureDragStatus {
385 GESTURE_DRAG_IN_PROGRESS,
386 GESTURE_DRAG_CANCEL_IN_PROGRESS,
387 GESTURE_DRAG_COMPLETE_IN_PROGRESS
389 GestureDragStatus gesture_drag_status_;
391 // Tracks the amount of the drag. The value is only valid when
392 // |gesture_drag_status_| is set to GESTURE_DRAG_IN_PROGRESS.
393 float gesture_drag_amount_;
395 // Manage the auto-hide state during the gesture.
396 ShelfAutoHideState gesture_drag_auto_hide_state_;
398 // Used to delay updating shelf background.
399 UpdateShelfObserver* update_shelf_observer_;
401 // The bounds of the keyboard.
402 gfx::Rect keyboard_bounds_;
404 // The bounds of the dock.
405 gfx::Rect dock_bounds_;
407 // The show hide animation duration override or 0 for default.
408 int duration_override_in_ms_;
410 DISALLOW_COPY_AND_ASSIGN(ShelfLayoutManager);
415 #endif // ASH_SHELF_SHELF_LAYOUT_MANAGER_H_