blob: 7e0ca78bd9b49a88c184557236d63eba4580596f [file] [log] [blame]
[email protected]1367bd632013-11-06 22:44:381// 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.
4
5#ifndef ASH_WM_IMMERSIVE_FULLSCREEN_CONTROLLER_H_
6#define ASH_WM_IMMERSIVE_FULLSCREEN_CONTROLLER_H_
7
8#include <vector>
9
10#include "ash/ash_export.h"
11#include "ash/wm/immersive_revealed_lock.h"
12#include "base/timer/timer.h"
13#include "ui/aura/window_observer.h"
14#include "ui/events/event_handler.h"
15#include "ui/gfx/animation/animation_delegate.h"
16#include "ui/views/focus/focus_manager.h"
17#include "ui/views/widget/widget_observer.h"
18
19namespace aura {
20class Window;
21}
22
23namespace gfx {
24class Point;
25class Rect;
26class SlideAnimation;
27}
28
29namespace ui {
30class LocatedEvent;
31}
32
33namespace views {
34class View;
35class Widget;
36}
37
38namespace ash {
39
40class ASH_EXPORT ImmersiveFullscreenController
41 : public gfx::AnimationDelegate,
42 public ui::EventHandler,
43 public views::FocusChangeListener,
44 public views::WidgetObserver,
45 public aura::WindowObserver,
46 public ImmersiveRevealedLock::Delegate {
47 public:
48 class Delegate {
49 public:
50 // Called when a reveal of the top-of-window views starts.
51 virtual void OnImmersiveRevealStarted() = 0;
52
53 // Called when the top-of-window views have finished closing. This call
54 // implies a visible fraction of 0. SetVisibleFraction(0) may not be called
55 // prior to OnImmersiveRevealEnded().
56 virtual void OnImmersiveRevealEnded() = 0;
57
58 // Called as a result of disabling immersive fullscreen via SetEnabled().
59 virtual void OnImmersiveFullscreenExited() = 0;
60
61 // Called to update the fraction of the top-of-window views height which is
62 // visible.
63 virtual void SetVisibleFraction(double visible_fraction) = 0;
64
65 // Returns a list of rects whose union makes up the top-of-window views.
66 // The returned list is used for hittesting when the top-of-window views
67 // are revealed. GetVisibleBoundsInScreen() must return a valid value when
68 // not in immersive fullscreen for the sake of SetupForTest().
[email protected]63458432013-11-20 17:28:5369 virtual std::vector<gfx::Rect> GetVisibleBoundsInScreen() const = 0;
[email protected]1367bd632013-11-06 22:44:3870
71 protected:
72 virtual ~Delegate() {}
73 };
74
75 ImmersiveFullscreenController();
76 virtual ~ImmersiveFullscreenController();
77
78 // Initializes the controller. Must be called prior to enabling immersive
79 // fullscreen via SetEnabled(). |top_container| is used to keep the
80 // top-of-window views revealed when a child of |top_container| has focus.
81 // |top_container| does not affect which mouse and touch events keep the
82 // top-of-window views revealed.
83 void Init(Delegate* delegate,
84 views::Widget* widget,
85 views::View* top_container);
86
87 // Enables or disables immersive fullscreen.
88 void SetEnabled(bool enable);
89
90 // Returns true if |native_window_| is in immersive fullscreen.
91 bool IsEnabled() const;
92
93 // Returns true if |native_window_| is in immersive fullscreen and the
94 // top-of-window views are fully or partially visible.
95 bool IsRevealed() const;
96
97 // Returns a lock which will keep the top-of-window views revealed for its
98 // lifetime. Several locks can be obtained. When all of the locks are
99 // destroyed, if immersive fullscreen is enabled and there is nothing else
100 // keeping the top-of-window views revealed, the top-of-window views will be
101 // closed. This method always returns a valid lock regardless of whether
102 // immersive fullscreen is enabled. The lock's lifetime can span immersive
103 // fullscreen being enabled / disabled. If acquiring the lock causes a reveal,
104 // the top-of-window views will animate according to |animate_reveal|. The
105 // caller takes ownership of the returned lock.
106 ImmersiveRevealedLock* GetRevealedLock(
107 AnimateReveal animate_reveal) WARN_UNUSED_RESULT;
108
109 // Disables animations and moves the mouse so that it is not over the
110 // top-of-window views for the sake of testing.
111 void SetupForTest();
112
113 // ui::EventHandler overrides:
114 virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE;
115 virtual void OnTouchEvent(ui::TouchEvent* event) OVERRIDE;
116 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
117
118 // views::FocusChangeObserver overrides:
119 virtual void OnWillChangeFocus(views::View* focused_before,
120 views::View* focused_now) OVERRIDE;
121 virtual void OnDidChangeFocus(views::View* focused_before,
122 views::View* focused_now) OVERRIDE;
123
124 // views::WidgetObserver overrides:
125 virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE;
126 virtual void OnWidgetActivationChanged(views::Widget* widget,
127 bool active) OVERRIDE;
128
129 // gfx::AnimationDelegate overrides:
130 virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE;
131 virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
132
133 // aura::WindowObserver overrides:
134 virtual void OnAddTransientChild(aura::Window* window,
135 aura::Window* transient) OVERRIDE;
136 virtual void OnRemoveTransientChild(aura::Window* window,
137 aura::Window* transient) OVERRIDE;
138
139 // ash::ImmersiveRevealedLock::Delegate overrides:
140 virtual void LockRevealedState(AnimateReveal animate_reveal) OVERRIDE;
141 virtual void UnlockRevealedState() OVERRIDE;
142
143 private:
144 friend class ImmersiveFullscreenControllerTest;
145
[email protected]1367bd632013-11-06 22:44:38146 enum Animate {
147 ANIMATE_NO,
148 ANIMATE_SLOW,
149 ANIMATE_FAST,
150 };
151 enum RevealState {
152 CLOSED,
153 SLIDING_OPEN,
154 REVEALED,
155 SLIDING_CLOSED,
156 };
157 enum SwipeType {
158 SWIPE_OPEN,
159 SWIPE_CLOSE,
160 SWIPE_NONE
161 };
162
163 // Enables or disables observers for mouse, touch, focus, and activation.
164 void EnableWindowObservers(bool enable);
165
166 // Updates |top_edge_hover_timer_| based on a mouse |event|. If the mouse is
167 // hovered at the top of the screen the timer is started. If the mouse moves
168 // away from the top edge, or moves too much in the x direction, the timer is
169 // stopped.
170 void UpdateTopEdgeHoverTimer(ui::MouseEvent* event);
171
172 // Updates |located_event_revealed_lock_| based on the current mouse state and
173 // the current touch state.
174 // |event| is NULL if the source event is not known.
[email protected]5991fc52013-12-03 03:17:55175 void UpdateLocatedEventRevealedLock(ui::LocatedEvent* event);
[email protected]1367bd632013-11-06 22:44:38176
177 // Acquires |located_event_revealed_lock_| if it is not already held.
178 void AcquireLocatedEventRevealedLock();
179
180 // Updates |focus_revealed_lock_| based on the currently active view and the
181 // currently active widget.
182 void UpdateFocusRevealedLock();
183
184 // Update |located_event_revealed_lock_| and |focus_revealed_lock_| as a
185 // result of a gesture of |swipe_type|. Returns true if any locks were
186 // acquired or released.
187 bool UpdateRevealedLocksForSwipe(SwipeType swipe_type);
188
189 // Returns the animation duration given |animate|.
190 int GetAnimationDuration(Animate animate) const;
191
192 // Temporarily reveals the top-of-window views while in immersive mode,
193 // hiding them when the cursor exits the area of the top views. If |animate|
194 // is not ANIMATE_NO, slides in the view, otherwise shows it immediately.
195 void MaybeStartReveal(Animate animate);
196
197 // Called when the animation to slide open the top-of-window views has
198 // completed.
199 void OnSlideOpenAnimationCompleted();
200
201 // Hides the top-of-window views if immersive mode is enabled and nothing is
202 // keeping them revealed. Optionally animates.
203 void MaybeEndReveal(Animate animate);
204
205 // Called when the animation to slide out the top-of-window views has
206 // completed.
207 void OnSlideClosedAnimationCompleted();
208
209 // Returns the type of swipe given |event|.
210 SwipeType GetSwipeType(ui::GestureEvent* event) const;
211
212 // Returns true if a mouse event at |location_in_screen| should be ignored.
213 // Ignored mouse events should not contribute to revealing or unrevealing the
214 // top-of-window views.
215 bool ShouldIgnoreMouseEventAtLocation(
216 const gfx::Point& location_in_screen) const;
217
218 // True when |location| is "near" to the top container. When the top container
219 // is not closed "near" means within the displayed bounds or above it. When
220 // the top container is closed "near" means either within the displayed
221 // bounds, above it, or within a few pixels below it. This allow the container
222 // to steal enough pixels to detect a swipe in and handles the case that there
223 // is a bezel sensor above the top container.
224 bool ShouldHandleGestureEvent(const gfx::Point& location) const;
225
226 // Recreate |bubble_manager_| and start observing any bubbles anchored to a
227 // child of |top_container_|.
228 void RecreateBubbleManager();
229
230 // Not owned.
231 Delegate* delegate_;
232 views::View* top_container_;
233 views::Widget* widget_;
234 aura::Window* native_window_;
235
236 // True if the observers have been enabled.
237 bool observers_enabled_;
238
239 // True when in immersive fullscreen.
240 bool enabled_;
241
242 // State machine for the revealed/closed animations.
243 RevealState reveal_state_;
244
245 int revealed_lock_count_;
246
247 // Timer to track cursor being held at the top edge of the screen.
248 base::OneShotTimer<ImmersiveFullscreenController> top_edge_hover_timer_;
249
250 // The cursor x position in screen coordinates when the cursor first hit the
251 // top edge of the screen.
252 int mouse_x_when_hit_top_in_screen_;
253
254 // Tracks if the controller has seen a ET_GESTURE_SCROLL_BEGIN, without the
255 // following events.
256 bool gesture_begun_;
257
258 // Lock which keeps the top-of-window views revealed based on the current
259 // mouse state and the current touch state. Acquiring the lock is used to
260 // trigger a reveal when the user moves the mouse to the top of the screen
261 // and when the user does a SWIPE_OPEN edge gesture.
262 scoped_ptr<ImmersiveRevealedLock> located_event_revealed_lock_;
263
264 // Lock which keeps the top-of-window views revealed based on the focused view
265 // and the active widget. Acquiring the lock never triggers a reveal because
266 // a view is not focusable till a reveal has made it visible.
267 scoped_ptr<ImmersiveRevealedLock> focus_revealed_lock_;
268
269 // The animation which controls sliding the top-of-window views in and out.
270 scoped_ptr<gfx::SlideAnimation> animation_;
271
272 // Whether the animations are disabled for testing.
273 bool animations_disabled_for_test_;
274
275 // Manages bubbles which are anchored to a child of |top_container_|.
276 class BubbleManager;
277 scoped_ptr<BubbleManager> bubble_manager_;
278
279 base::WeakPtrFactory<ImmersiveFullscreenController> weak_ptr_factory_;
280
281 DISALLOW_COPY_AND_ASSIGN(ImmersiveFullscreenController);
282};
283
284} // namespace ash
285
286#endif // ASH_WM_IMMERSIVE_FULLSCREEN_CONTROLLER_H_