1 // 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.
5 #ifndef ASH_WM_IMMERSIVE_FULLSCREEN_CONTROLLER_H_
6 #define ASH_WM_IMMERSIVE_FULLSCREEN_CONTROLLER_H_
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 #include "ui/wm/core/transient_window_observer.h"
41 class ASH_EXPORT ImmersiveFullscreenController
42 : public gfx::AnimationDelegate
,
43 public ui::EventHandler
,
44 public ::wm::TransientWindowObserver
,
45 public views::FocusChangeListener
,
46 public views::WidgetObserver
,
47 public ImmersiveRevealedLock::Delegate
{
49 static const int kMouseRevealBoundsHeight
;
51 // The enum is used for an enumerated histogram. New items should be only
56 WINDOW_TYPE_HOSTED_APP
,
57 WINDOW_TYPE_PACKAGED_APP
,
63 // Called when a reveal of the top-of-window views starts.
64 virtual void OnImmersiveRevealStarted() = 0;
66 // Called when the top-of-window views have finished closing. This call
67 // implies a visible fraction of 0. SetVisibleFraction(0) may not be called
68 // prior to OnImmersiveRevealEnded().
69 virtual void OnImmersiveRevealEnded() = 0;
71 // Called as a result of disabling immersive fullscreen via SetEnabled().
72 virtual void OnImmersiveFullscreenExited() = 0;
74 // Called to update the fraction of the top-of-window views height which is
76 virtual void SetVisibleFraction(double visible_fraction
) = 0;
78 // Returns a list of rects whose union makes up the top-of-window views.
79 // The returned list is used for hittesting when the top-of-window views
80 // are revealed. GetVisibleBoundsInScreen() must return a valid value when
81 // not in immersive fullscreen for the sake of SetupForTest().
82 virtual std::vector
<gfx::Rect
> GetVisibleBoundsInScreen() const = 0;
85 virtual ~Delegate() {}
88 ImmersiveFullscreenController();
89 ~ImmersiveFullscreenController() override
;
91 // Initializes the controller. Must be called prior to enabling immersive
92 // fullscreen via SetEnabled(). |top_container| is used to keep the
93 // top-of-window views revealed when a child of |top_container| has focus.
94 // |top_container| does not affect which mouse and touch events keep the
95 // top-of-window views revealed.
96 void Init(Delegate
* delegate
,
97 views::Widget
* widget
,
98 views::View
* top_container
);
100 // Enables or disables immersive fullscreen.
101 // |window_type| is the type of window which is put in immersive fullscreen.
102 // It is only used for histogramming.
103 void SetEnabled(WindowType window_type
, bool enable
);
105 // Returns true if |native_window_| is in immersive fullscreen.
106 bool IsEnabled() const;
108 // Returns true if |native_window_| is in immersive fullscreen and the
109 // top-of-window views are fully or partially visible.
110 bool IsRevealed() const;
112 // Returns a lock which will keep the top-of-window views revealed for its
113 // lifetime. Several locks can be obtained. When all of the locks are
114 // destroyed, if immersive fullscreen is enabled and there is nothing else
115 // keeping the top-of-window views revealed, the top-of-window views will be
116 // closed. This method always returns a valid lock regardless of whether
117 // immersive fullscreen is enabled. The lock's lifetime can span immersive
118 // fullscreen being enabled / disabled. If acquiring the lock causes a reveal,
119 // the top-of-window views will animate according to |animate_reveal|. The
120 // caller takes ownership of the returned lock.
121 ImmersiveRevealedLock
* GetRevealedLock(
122 AnimateReveal animate_reveal
) WARN_UNUSED_RESULT
;
124 // Disables animations and moves the mouse so that it is not over the
125 // top-of-window views for the sake of testing.
128 // ui::EventHandler overrides:
129 void OnMouseEvent(ui::MouseEvent
* event
) override
;
130 void OnTouchEvent(ui::TouchEvent
* event
) override
;
131 void OnGestureEvent(ui::GestureEvent
* event
) override
;
133 // views::FocusChangeObserver overrides:
134 void OnWillChangeFocus(views::View
* focused_before
,
135 views::View
* focused_now
) override
;
136 void OnDidChangeFocus(views::View
* focused_before
,
137 views::View
* focused_now
) override
;
139 // views::WidgetObserver overrides:
140 void OnWidgetDestroying(views::Widget
* widget
) override
;
141 void OnWidgetActivationChanged(views::Widget
* widget
, bool active
) override
;
143 // gfx::AnimationDelegate overrides:
144 void AnimationEnded(const gfx::Animation
* animation
) override
;
145 void AnimationProgressed(const gfx::Animation
* animation
) override
;
147 // ::wm::TransientWindowObserver overrides:
148 void OnTransientChildAdded(aura::Window
* window
,
149 aura::Window
* transient
) override
;
150 void OnTransientChildRemoved(aura::Window
* window
,
151 aura::Window
* transient
) override
;
153 // ash::ImmersiveRevealedLock::Delegate overrides:
154 void LockRevealedState(AnimateReveal animate_reveal
) override
;
155 void UnlockRevealedState() override
;
158 friend class ImmersiveFullscreenControllerTest
;
177 // Enables or disables observers for mouse, touch, focus, and activation.
178 void EnableWindowObservers(bool enable
);
180 // Updates |top_edge_hover_timer_| based on a mouse |event|. If the mouse is
181 // hovered at the top of the screen the timer is started. If the mouse moves
182 // away from the top edge, or moves too much in the x direction, the timer is
184 void UpdateTopEdgeHoverTimer(ui::MouseEvent
* event
);
186 // Updates |located_event_revealed_lock_| based on the current mouse state and
187 // the current touch state.
188 // |event| is NULL if the source event is not known.
189 void UpdateLocatedEventRevealedLock(ui::LocatedEvent
* event
);
191 // Acquires |located_event_revealed_lock_| if it is not already held.
192 void AcquireLocatedEventRevealedLock();
194 // Updates |focus_revealed_lock_| based on the currently active view and the
195 // currently active widget.
196 void UpdateFocusRevealedLock();
198 // Update |located_event_revealed_lock_| and |focus_revealed_lock_| as a
199 // result of a gesture of |swipe_type|. Returns true if any locks were
200 // acquired or released.
201 bool UpdateRevealedLocksForSwipe(SwipeType swipe_type
);
203 // Returns the animation duration given |animate|.
204 int GetAnimationDuration(Animate animate
) const;
206 // Temporarily reveals the top-of-window views while in immersive mode,
207 // hiding them when the cursor exits the area of the top views. If |animate|
208 // is not ANIMATE_NO, slides in the view, otherwise shows it immediately.
209 void MaybeStartReveal(Animate animate
);
211 // Called when the animation to slide open the top-of-window views has
213 void OnSlideOpenAnimationCompleted();
215 // Hides the top-of-window views if immersive mode is enabled and nothing is
216 // keeping them revealed. Optionally animates.
217 void MaybeEndReveal(Animate animate
);
219 // Called when the animation to slide out the top-of-window views has
221 void OnSlideClosedAnimationCompleted();
223 // Returns the type of swipe given |event|.
224 SwipeType
GetSwipeType(ui::GestureEvent
* event
) const;
226 // Returns true if a mouse event at |location_in_screen| should be ignored.
227 // Ignored mouse events should not contribute to revealing or unrevealing the
228 // top-of-window views.
229 bool ShouldIgnoreMouseEventAtLocation(
230 const gfx::Point
& location_in_screen
) const;
232 // True when |location| is "near" to the top container. When the top container
233 // is not closed "near" means within the displayed bounds or above it. When
234 // the top container is closed "near" means either within the displayed
235 // bounds, above it, or within a few pixels below it. This allow the container
236 // to steal enough pixels to detect a swipe in and handles the case that there
237 // is a bezel sensor above the top container.
238 bool ShouldHandleGestureEvent(const gfx::Point
& location
) const;
240 // Recreate |bubble_manager_| and start observing any bubbles anchored to a
241 // child of |top_container_|.
242 void RecreateBubbleManager();
246 views::View
* top_container_
;
247 views::Widget
* widget_
;
248 aura::Window
* native_window_
;
250 // True if the observers have been enabled.
251 bool observers_enabled_
;
253 // True when in immersive fullscreen.
256 // State machine for the revealed/closed animations.
257 RevealState reveal_state_
;
259 int revealed_lock_count_
;
261 // Timer to track cursor being held at the top edge of the screen.
262 base::OneShotTimer
<ImmersiveFullscreenController
> top_edge_hover_timer_
;
264 // The cursor x position in screen coordinates when the cursor first hit the
265 // top edge of the screen.
266 int mouse_x_when_hit_top_in_screen_
;
268 // Tracks if the controller has seen a ET_GESTURE_SCROLL_BEGIN, without the
272 // Lock which keeps the top-of-window views revealed based on the current
273 // mouse state and the current touch state. Acquiring the lock is used to
274 // trigger a reveal when the user moves the mouse to the top of the screen
275 // and when the user does a SWIPE_OPEN edge gesture.
276 scoped_ptr
<ImmersiveRevealedLock
> located_event_revealed_lock_
;
278 // Lock which keeps the top-of-window views revealed based on the focused view
279 // and the active widget. Acquiring the lock never triggers a reveal because
280 // a view is not focusable till a reveal has made it visible.
281 scoped_ptr
<ImmersiveRevealedLock
> focus_revealed_lock_
;
283 // The animation which controls sliding the top-of-window views in and out.
284 scoped_ptr
<gfx::SlideAnimation
> animation_
;
286 // Whether the animations are disabled for testing.
287 bool animations_disabled_for_test_
;
289 // Manages bubbles which are anchored to a child of |top_container_|.
291 scoped_ptr
<BubbleManager
> bubble_manager_
;
293 base::WeakPtrFactory
<ImmersiveFullscreenController
> weak_ptr_factory_
;
295 DISALLOW_COPY_AND_ASSIGN(ImmersiveFullscreenController
);
300 #endif // ASH_WM_IMMERSIVE_FULLSCREEN_CONTROLLER_H_