immersive_mode_controller_ash.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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.
4
5#ifndef CHROME_BROWSER_UI_VIEWS_FRAME_IMMERSIVE_MODE_CONTROLLER_ASH_H_
6#define CHROME_BROWSER_UI_VIEWS_FRAME_IMMERSIVE_MODE_CONTROLLER_ASH_H_
7
8#include "chrome/browser/ui/views/frame/immersive_mode_controller.h"
9
10#include "base/timer.h"
11#include "content/public/browser/notification_observer.h"
12#include "content/public/browser/notification_registrar.h"
13#include "ui/base/events/event_handler.h"
14#include "ui/compositor/layer_animation_observer.h"
15#include "ui/views/focus/focus_manager.h"
16#include "ui/views/widget/widget_observer.h"
17
18class BrowserView;
19
20namespace aura {
21class Window;
22}
23
24namespace gfx {
25class Transform;
26}
27
28namespace ui {
29class Layer;
30}
31
32namespace views {
33class View;
34}
35
36class ImmersiveModeControllerAsh : public ImmersiveModeController,
37                                   public content::NotificationObserver,
38                                   public ui::EventHandler,
39                                   public ui::ImplicitAnimationObserver,
40                                   public views::FocusChangeListener,
41                                   public views::WidgetObserver {
42 public:
43  ImmersiveModeControllerAsh();
44  virtual ~ImmersiveModeControllerAsh();
45
46  // These methods are used to increment and decrement |revealed_lock_count_|.
47  // If immersive mode is enabled, a transition from 1 to 0 in
48  // |revealed_lock_count_| closes the top-of-window views and a transition
49  // from 0 to 1 in |revealed_lock_count_| reveals the top-of-window views.
50  void LockRevealedState(AnimateReveal animate_reveal);
51  void UnlockRevealedState();
52
53  // Shows the reveal view without any animations if immersive mode is enabled.
54  void MaybeRevealWithoutAnimation();
55
56  // ImmersiveModeController overrides:
57  virtual void Init(BrowserView* browser_view) OVERRIDE;
58  virtual void SetEnabled(bool enabled) OVERRIDE;
59  virtual bool IsEnabled() const OVERRIDE;
60  virtual bool ShouldHideTabIndicators() const OVERRIDE;
61  virtual bool ShouldHideTopViews() const OVERRIDE;
62  virtual bool IsRevealed() const OVERRIDE;
63  virtual void MaybeStackViewAtTop() OVERRIDE;
64  virtual ImmersiveRevealedLock* GetRevealedLock(
65      AnimateReveal animate_reveal) OVERRIDE WARN_UNUSED_RESULT;
66  virtual void AnchorWidgetToTopContainer(views::Widget* widget,
67                                          int y_offset) OVERRIDE;
68  virtual void UnanchorWidgetFromTopContainer(views::Widget* widget) OVERRIDE;
69  virtual void OnTopContainerBoundsChanged() OVERRIDE;
70
71  // content::NotificationObserver override:
72  virtual void Observe(int type,
73                       const content::NotificationSource& source,
74                       const content::NotificationDetails& details) OVERRIDE;
75
76  // ui::EventHandler overrides:
77  virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE;
78  virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
79
80  // views::FocusChangeObserver overrides:
81  virtual void OnWillChangeFocus(views::View* focused_before,
82                                 views::View* focused_now) OVERRIDE;
83  virtual void OnDidChangeFocus(views::View* focused_before,
84                                views::View* focused_now) OVERRIDE;
85
86  // views::WidgetObserver overrides:
87  virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE;
88  virtual void OnWidgetActivationChanged(views::Widget* widget,
89                                         bool active) OVERRIDE;
90
91  // ui::ImplicitAnimationObserver override:
92  virtual void OnImplicitAnimationsCompleted() OVERRIDE;
93
94  // Testing interface.
95  void SetForceHideTabIndicatorsForTest(bool force);
96  void StartRevealForTest(bool hovered);
97  void SetMouseHoveredForTest(bool hovered);
98
99 private:
100  enum Animate {
101    ANIMATE_NO,
102    ANIMATE_SLOW,
103    ANIMATE_FAST,
104  };
105  enum Layout {
106    LAYOUT_YES,
107    LAYOUT_NO
108  };
109  enum RevealState {
110    CLOSED,          // Top container only showing tabstrip, y = 0.
111    SLIDING_OPEN,    // All views showing, y animating from -height to 0.
112    REVEALED,        // All views showing, y = 0.
113    SLIDING_CLOSED,  // All views showing, y animating from 0 to -height.
114  };
115  enum TabIndicatorVisibility {
116    TAB_INDICATORS_FORCE_HIDE,
117    TAB_INDICATORS_HIDE,
118    TAB_INDICATORS_SHOW
119  };
120  enum SwipeType {
121    SWIPE_OPEN,
122    SWIPE_CLOSE,
123    SWIPE_NONE
124  };
125
126  // Enables or disables observers for mouse move, focus, and window restore.
127  void EnableWindowObservers(bool enable);
128
129  // Update |mouse_revealed_lock_| based on the current mouse state and the
130  // currently active widget.
131  // |maybe_drag| is true if the user may be in the middle of a drag.
132  void UpdateMouseRevealedLock(bool maybe_drag);
133
134  // Acquire the mouse revealed lock if it is not already held.
135  void AcquireMouseRevealedLock();
136
137  // Update |focus_revealed_lock_| based on the currently active view and the
138  // currently active widget.
139  void UpdateFocusRevealedLock();
140
141  // Updates whether fullscreen uses any chrome at all. When using minimal
142  // chrome, a 'light bar' is permanently visible for the launcher and possibly
143  // for the tabstrip.
144  void UpdateUseMinimalChrome(Layout layout);
145
146  // Returns the animation duration given |animate|.
147  int GetAnimationDuration(Animate animate) const;
148
149  // Temporarily reveals the top-of-window views while in immersive mode,
150  // hiding them when the cursor exits the area of the top views. If |animate|
151  // is not ANIMATE_NO, slides in the view, otherwise shows it immediately.
152  void MaybeStartReveal(Animate animate);
153
154  // Enables or disables layer-based painting to allow smooth animations.
155  void EnablePaintToLayer(bool enable);
156
157  // Updates the browser root view's layout including window caption controls.
158  void LayoutBrowserRootView();
159
160  // Called when the animation to slide open the top-of-window views has
161  // completed.
162  void OnSlideOpenAnimationCompleted();
163
164  // Hides the top-of-window views if immersive mode is enabled and nothing is
165  // keeping them revealed. Optionally animates.
166  void MaybeEndReveal(Animate animate);
167
168  // Called when the animation to slide out the top-of-window views has
169  // completed.
170  void OnSlideClosedAnimationCompleted();
171
172  // Starts an animation for the top-of-window views and any anchored widgets
173  // of |duration_ms| to |target_transform|.
174  void DoAnimation(const gfx::Transform& target_transform, int duration_ms);
175
176  // Starts an animation for |layer| of |duration_ms| to |target_transform|.
177  // If non-NULL, sets |observer| to be notified when the animation completes.
178  void DoLayerAnimation(ui::Layer* layer,
179                        const gfx::Transform& target_transform,
180                        int duration_ms,
181                        ui::ImplicitAnimationObserver* observer);
182
183  // Returns the type of swipe given |event|.
184  SwipeType GetSwipeType(ui::GestureEvent* event) const;
185
186  // True when |location| is "near" to the top container. When the top container
187  // is not closed "near" means within the displayed bounds. When the top
188  // container is closed "near" means either within the displayed bounds or
189  // within a few pixels of it. This allow the container to steal enough pixels
190  // to detect a swipe in.
191  bool IsNearTopContainer(gfx::Point location) const;
192
193  // Browser view holding the views to be shown and hidden. Not owned.
194  BrowserView* browser_view_;
195
196  // True if the window observers are enabled.
197  bool observers_enabled_;
198
199  // True when in immersive mode.
200  bool enabled_;
201
202  // State machine for the revealed/closed animations.
203  RevealState reveal_state_;
204
205  int revealed_lock_count_;
206
207  // The visibility of the miniature "tab indicators" in the main browser view
208  // when immersive mode is enabled and the top-of-window views are closed.
209  TabIndicatorVisibility tab_indicator_visibility_;
210
211  // Timer to track cursor being held at the top.
212  base::OneShotTimer<ImmersiveModeController> top_timer_;
213
214  // Lock which keeps the top-of-window views revealed based on the current
215  // mouse state.
216  scoped_ptr<ImmersiveRevealedLock> mouse_revealed_lock_;
217
218  // Lock which keeps the top-of-window views revealed based on the focused view
219  // and the active widget.
220  scoped_ptr<ImmersiveRevealedLock> focus_revealed_lock_;
221
222  // Native window for the browser, needed to clean up observers.
223  aura::Window* native_window_;
224
225  // Observer to disable immersive mode when window leaves the maximized state.
226  class WindowObserver;
227  scoped_ptr<WindowObserver> window_observer_;
228
229  // Manages widgets which are anchored to the top-of-window views.
230  class AnchoredWidgetManager;
231  scoped_ptr<AnchoredWidgetManager> anchored_widget_manager_;
232
233  content::NotificationRegistrar registrar_;
234
235  base::WeakPtrFactory<ImmersiveModeControllerAsh> weak_ptr_factory_;
236
237  // Tracks if the controller has seen a ET_GESTURE_SCROLL_BEGIN, without the
238  // following events.
239  bool gesture_begun_;
240
241  DISALLOW_COPY_AND_ASSIGN(ImmersiveModeControllerAsh);
242};
243
244#endif  // CHROME_BROWSER_UI_VIEWS_FRAME_IMMERSIVE_MODE_CONTROLLER_ASH_H_
245