MD downloads: implement incognito markers
[chromium-blink-merge.git] / ash / shelf / shelf_layout_manager.h
blob40e1355e5662a4aa1d860649d12bd3a97cc2676f
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_
8 #include <vector>
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"
32 namespace aura {
33 class RootWindow;
36 namespace ui {
37 class GestureEvent;
38 class ImplicitAnimationObserver;
41 namespace ash {
42 class PanelLayoutManagerTest;
43 class ScreenAsh;
44 class ShelfBezelEventFilter;
45 class ShelfLayoutManagerObserver;
46 class ShelfLayoutManagerTest;
47 class ShelfWidget;
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 {
67 public:
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
102 // is locked.
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
115 // on the screen.
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 // Returns the bounds within the root window not occupied by the shelf nor the
125 // virtual keyboard.
126 const gfx::Rect& user_work_area_bounds() const {
127 return user_work_area_bounds_;
130 // Stops any animations and sets the bounds of the shelf and status
131 // widgets.
132 void LayoutShelf();
134 // Returns shelf visibility state based on current value of auto hide
135 // behavior setting.
136 ShelfVisibilityState CalculateShelfVisibility();
138 // Updates the visibility state.
139 void UpdateVisibilityState();
141 // Invoked by the shelf when the auto-hide state may have changed.
142 void UpdateAutoHideState();
144 ShelfVisibilityState visibility_state() const {
145 return state_.visibility_state;
147 ShelfAutoHideState auto_hide_state() const { return state_.auto_hide_state; }
149 ShelfWidget* shelf_widget() { return shelf_; }
151 // Sets whether any windows overlap the shelf. If a window overlaps the shelf
152 // the shelf renders slightly differently.
153 void SetWindowOverlapsShelf(bool value);
154 bool window_overlaps_shelf() const { return window_overlaps_shelf_; }
156 void AddObserver(ShelfLayoutManagerObserver* observer);
157 void RemoveObserver(ShelfLayoutManagerObserver* observer);
159 // Gesture related functions:
160 void OnGestureEdgeSwipe(const ui::GestureEvent& gesture);
161 void StartGestureDrag(const ui::GestureEvent& gesture);
162 void UpdateGestureDrag(const ui::GestureEvent& gesture);
163 void CompleteGestureDrag(const ui::GestureEvent& gesture);
164 void CancelGestureDrag();
166 // Set an animation duration override for the show / hide animation of the
167 // shelf. Specifying 0 leads to use the default.
168 void SetAnimationDurationOverride(int duration_override_in_ms);
170 // Overridden from SnapLayoutManager:
171 void OnWindowResized() override;
172 void SetChildBounds(aura::Window* child,
173 const gfx::Rect& requested_bounds) override;
175 // Overridden from ash::ShellObserver:
176 void OnLockStateChanged(bool locked) override;
178 // Overriden from aura::client::ActivationChangeObserver:
179 void OnWindowActivated(
180 aura::client::ActivationChangeObserver::ActivationReason reason,
181 aura::Window* gained_active,
182 aura::Window* lost_active) override;
184 // Overridden from ash::LockStateObserver:
185 void OnLockStateEvent(LockStateObserver::EventType event) override;
187 // Overridden from ash::SessionStateObserver:
188 void SessionStateChanged(SessionStateDelegate::SessionState state) override;
190 // TODO(harrym|oshima): These templates will be moved to
191 // new Shelf class.
192 // A helper function that provides a shortcut for choosing
193 // values specific to a shelf alignment.
194 template<typename T>
195 T SelectValueForShelfAlignment(T bottom, T left, T right, T top) const {
196 switch (GetAlignment()) {
197 case SHELF_ALIGNMENT_BOTTOM:
198 return bottom;
199 case SHELF_ALIGNMENT_LEFT:
200 return left;
201 case SHELF_ALIGNMENT_RIGHT:
202 return right;
203 case SHELF_ALIGNMENT_TOP:
204 return top;
206 NOTREACHED();
207 return right;
210 template<typename T>
211 T PrimaryAxisValue(T horizontal, T vertical) const {
212 return IsHorizontalAlignment() ? horizontal : vertical;
215 // Is the shelf's alignment horizontal?
216 bool IsHorizontalAlignment() const;
218 // Returns a ShelfLayoutManager on the display which has a shelf for
219 // given |window|. See RootWindowController::ForShelf for more info.
220 static ShelfLayoutManager* ForShelf(aura::Window* window);
222 private:
223 class AutoHideEventFilter;
224 class UpdateShelfObserver;
225 friend class AshPopupAlignmentDelegateTest;
226 friend class ash::ScreenAsh;
227 friend class PanelLayoutManagerTest;
228 friend class ShelfLayoutManagerTest;
229 FRIEND_TEST_ALL_PREFIXES(ash::AshPopupAlignmentDelegateTest, AutoHide);
230 FRIEND_TEST_ALL_PREFIXES(ash::WebNotificationTrayTest, PopupAndFullscreen);
232 struct TargetBounds {
233 TargetBounds();
234 ~TargetBounds();
236 float opacity;
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;
244 struct State {
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),
249 is_adding_user_screen(false) {}
251 // Returns true if the two states are considered equal. As
252 // |auto_hide_state| only matters if |visibility_state| is
253 // |SHELF_AUTO_HIDE|, Equals() ignores the |auto_hide_state| as
254 // appropriate.
255 bool Equals(const State& other) const {
256 return other.visibility_state == visibility_state &&
257 (visibility_state != SHELF_AUTO_HIDE ||
258 other.auto_hide_state == auto_hide_state) &&
259 other.window_state == window_state &&
260 other.is_screen_locked == is_screen_locked &&
261 other.is_adding_user_screen == is_adding_user_screen;
264 ShelfVisibilityState visibility_state;
265 ShelfAutoHideState auto_hide_state;
266 WorkspaceWindowState window_state;
267 bool is_screen_locked;
268 bool is_adding_user_screen;
271 // Sets the visibility of the shelf to |state|.
272 void SetState(ShelfVisibilityState visibility_state);
274 // Updates the bounds and opacity of the shelf and status widgets.
275 // If |observer| is specified, it will be called back when the animations, if
276 // any, are complete.
277 void UpdateBoundsAndOpacity(const TargetBounds& target_bounds,
278 bool animate,
279 ui::ImplicitAnimationObserver* observer);
281 // Stops any animations and progresses them to the end.
282 void StopAnimating();
284 // Returns the width (if aligned to the side) or height (if aligned to the
285 // bottom).
286 void GetShelfSize(int* width, int* height);
288 // Insets |bounds| by |inset| on the edge the shelf is aligned to.
289 void AdjustBoundsBasedOnAlignment(int inset, gfx::Rect* bounds) const;
291 // Calculates the target bounds assuming visibility of |visible|.
292 void CalculateTargetBounds(const State& state, TargetBounds* target_bounds);
294 // Updates the target bounds if a gesture-drag is in progress. This is only
295 // used by |CalculateTargetBounds()|.
296 void UpdateTargetBoundsForGesture(TargetBounds* target_bounds) const;
298 // Updates the background of the shelf.
299 void UpdateShelfBackground(BackgroundAnimatorChangeType type);
301 // Returns how the shelf background is painted.
302 ShelfBackgroundType GetShelfBackgroundType() const;
304 // Updates the auto hide state immediately.
305 void UpdateAutoHideStateNow();
307 // Stops the auto hide timer and clears
308 // |mouse_over_shelf_when_auto_hide_timer_started_|.
309 void StopAutoHideTimer();
311 // Returns the bounds of an additional region which can trigger showing the
312 // shelf. This region exists to make it easier to trigger showing the shelf
313 // when the shelf is auto hidden and the shelf is on the boundary between
314 // two displays.
315 gfx::Rect GetAutoHideShowShelfRegionInScreen() const;
317 // Returns the AutoHideState. This value is determined from the shelf and
318 // tray.
319 ShelfAutoHideState CalculateAutoHideState(
320 ShelfVisibilityState visibility_state) const;
322 // Returns true if |window| is a descendant of the shelf.
323 bool IsShelfWindow(aura::Window* window);
325 int GetWorkAreaSize(const State& state, int size) const;
327 // Overridden from keyboard::KeyboardControllerObserver:
328 void OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) override;
330 // Overridden from DockedWindowLayoutManagerObserver:
331 void OnDockBoundsChanging(
332 const gfx::Rect& dock_bounds,
333 DockedWindowLayoutManagerObserver::Reason reason) override;
335 // Called when the LoginUI changes from visible to invisible.
336 void UpdateShelfVisibilityAfterLoginUIChange();
338 // Returns true when |alignment_| is locked. This can be caused by the screen
339 // being locked, or when adding a user. Returns false when transitioning to a
340 // user session, and while the session is active.
341 bool IsAlignmentLocked() const;
343 // The RootWindow is cached so that we don't invoke Shell::GetInstance() from
344 // our destructor. We avoid that as at the time we're deleted Shell is being
345 // deleted too.
346 aura::Window* root_window_;
348 // True when inside UpdateBoundsAndOpacity() method. Used to prevent calling
349 // UpdateBoundsAndOpacity() again from SetChildBounds().
350 bool updating_bounds_;
352 // See description above setter.
353 ShelfAutoHideBehavior auto_hide_behavior_;
355 // See description above getter.
356 ShelfAlignment alignment_;
358 // Current state.
359 State state_;
361 ShelfWidget* shelf_;
363 WorkspaceController* workspace_controller_;
365 // Do any windows overlap the shelf? This is maintained by WorkspaceManager.
366 bool window_overlaps_shelf_;
368 base::OneShotTimer<ShelfLayoutManager> auto_hide_timer_;
370 // Whether the mouse was over the shelf when the auto hide timer started.
371 // False when neither the auto hide timer nor the timer task are running.
372 bool mouse_over_shelf_when_auto_hide_timer_started_;
374 // EventFilter used to detect when user moves the mouse over the shelf to
375 // trigger showing the shelf.
376 scoped_ptr<AutoHideEventFilter> auto_hide_event_filter_;
378 // EventFilter used to detect when user issues a gesture on a bezel sensor.
379 scoped_ptr<ShelfBezelEventFilter> bezel_event_filter_;
381 base::ObserverList<ShelfLayoutManagerObserver> observers_;
383 // The shelf reacts to gesture-drags, and can be set to auto-hide for certain
384 // gestures. Some shelf behaviour (e.g. visibility state, background color
385 // etc.) are affected by various stages of the drag. The enum keeps track of
386 // the present status of the gesture drag.
387 enum GestureDragStatus {
388 GESTURE_DRAG_NONE,
389 GESTURE_DRAG_IN_PROGRESS,
390 GESTURE_DRAG_CANCEL_IN_PROGRESS,
391 GESTURE_DRAG_COMPLETE_IN_PROGRESS
393 GestureDragStatus gesture_drag_status_;
395 // Tracks the amount of the drag. The value is only valid when
396 // |gesture_drag_status_| is set to GESTURE_DRAG_IN_PROGRESS.
397 float gesture_drag_amount_;
399 // Manage the auto-hide state during the gesture.
400 ShelfAutoHideState gesture_drag_auto_hide_state_;
402 // Used to delay updating shelf background.
403 UpdateShelfObserver* update_shelf_observer_;
405 // The bounds of the keyboard.
406 gfx::Rect keyboard_bounds_;
408 // The bounds of the dock.
409 gfx::Rect dock_bounds_;
411 // The bounds within the root window not occupied by the shelf nor the virtual
412 // keyboard.
413 gfx::Rect user_work_area_bounds_;
415 // The show hide animation duration override or 0 for default.
416 int duration_override_in_ms_;
418 DISALLOW_COPY_AND_ASSIGN(ShelfLayoutManager);
421 } // namespace ash
423 #endif // ASH_SHELF_SHELF_LAYOUT_MANAGER_H_