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.
4 
5 #ifndef ASH_WM_OVERVIEW_OVERVIEW_CONTROLLER_H_
6 #define ASH_WM_OVERVIEW_OVERVIEW_CONTROLLER_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "ash/ash_export.h"
12 #include "ash/wm/overview/delayed_animation_observer.h"
13 #include "ash/wm/overview/overview_delegate.h"
14 #include "ash/wm/overview/overview_observer.h"
15 #include "ash/wm/overview/overview_session.h"
16 #include "ash/wm/overview/overview_types.h"
17 #include "base/macros.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/observer_list.h"
20 #include "base/time/time.h"
21 #include "ui/aura/window_occlusion_tracker.h"
22 #include "ui/views/widget/widget.h"
23 
24 namespace ash {
25 
26 class OverviewWallpaperController;
27 
28 // Manages a overview session which displays an overview of all windows and
29 // allows selecting a window to activate it.
30 class ASH_EXPORT OverviewController : public OverviewDelegate,
31                                       public ::wm::ActivationChangeObserver {
32  public:
33   OverviewController();
34   ~OverviewController() override;
35 
36   // Starts/Ends overview with |type|. Returns true if enter or exit overview
37   // successful. Depending on |type| the enter/exit animation will look
38   // different.
39   bool StartOverview(
40       OverviewEnterExitType type = OverviewEnterExitType::kNormal);
41   bool EndOverview(OverviewEnterExitType type = OverviewEnterExitType::kNormal);
42 
43   // Returns true if overview mode is active.
44   bool InOverviewSession() const;
45 
46   // Moves the current selection forward or backward.
47   void IncrementSelection(bool forward);
48 
49   // Accepts current selection if any. Returns true if a selection was made,
50   // false otherwise.
51   bool AcceptSelection();
52 
53   // Returns true if we're in start-overview animation.
54   bool IsInStartAnimation();
55 
56   // Returns true if overview has been shutdown, but is still animating to the
57   // end state ui.
58   bool IsCompletingShutdownAnimations() const;
59 
60   // Pause or unpause the occlusion tracker. Resets the unpause delay if we were
61   // already in the process of unpausing.
62   void PauseOcclusionTracker();
63   void UnpauseOcclusionTracker(base::TimeDelta delay);
64 
65   void AddObserver(OverviewObserver* observer);
66   void RemoveObserver(OverviewObserver* observer);
67 
68   // Post a task to update the shadow and rounded corners of overview windows.
69   void DelayedUpdateRoundedCornersAndShadow();
70 
71   // OverviewDelegate:
72   void AddExitAnimationObserver(
73       std::unique_ptr<DelayedAnimationObserver> animation) override;
74   void RemoveAndDestroyExitAnimationObserver(
75       DelayedAnimationObserver* animation) override;
76   void AddEnterAnimationObserver(
77       std::unique_ptr<DelayedAnimationObserver> animation_observer) override;
78   void RemoveAndDestroyEnterAnimationObserver(
79       DelayedAnimationObserver* animation_observer) override;
80 
81   // ::wm::ActivationChangeObserver:
82   void OnWindowActivating(ActivationReason reason,
83                           aura::Window* gained_active,
84                           aura::Window* lost_active) override;
OnWindowActivated(ActivationReason reason,aura::Window * gained_active,aura::Window * lost_active)85   void OnWindowActivated(ActivationReason reason,
86                          aura::Window* gained_active,
87                          aura::Window* lost_active) override {}
88 
overview_session()89   OverviewSession* overview_session() { return overview_session_.get(); }
90 
overview_wallpaper_controller()91   OverviewWallpaperController* overview_wallpaper_controller() {
92     return overview_wallpaper_controller_.get();
93   }
94 
set_occlusion_pause_duration_for_end_for_test(base::TimeDelta duration)95   void set_occlusion_pause_duration_for_end_for_test(base::TimeDelta duration) {
96     occlusion_pause_duration_for_end_ = duration;
97   }
set_delayed_animation_task_delay_for_test(base::TimeDelta delta)98   void set_delayed_animation_task_delay_for_test(base::TimeDelta delta) {
99     delayed_animation_task_delay_ = delta;
100   }
101 
102   // Gets the windows list that are shown in the overview windows grids if the
103   // overview mode is active for testing.
104   std::vector<aura::Window*> GetWindowsListInOverviewGridsForTest();
105   std::vector<aura::Window*> GetItemWindowListInOverviewGridsForTest();
106 
107  private:
108   friend class OverviewSessionTest;
109 
110   // Toggle overview mode. Depending on |type| the enter/exit animation will
111   // look different.
112   void ToggleOverview(
113       OverviewEnterExitType type = OverviewEnterExitType::kNormal);
114 
115   // Returns true if it's possible to enter or exit overview mode in the current
116   // configuration. This can be false at certain times, such as when the lock
117   // screen is visible we can't overview mode.
118   bool CanEnterOverview();
119   bool CanEndOverview(OverviewEnterExitType type);
120 
121   void OnStartingAnimationComplete(bool canceled);
122   void OnEndingAnimationComplete(bool canceled);
123   void ResetPauser();
124 
125   void UpdateRoundedCornersAndShadow();
126 
127   // Collection of DelayedAnimationObserver objects that own widgets that may be
128   // still animating after overview mode ends. If shell needs to shut down while
129   // those animations are in progress, the animations are shut down and the
130   // widgets destroyed.
131   std::vector<std::unique_ptr<DelayedAnimationObserver>> delayed_animations_;
132   // Collection of DelayedAnimationObserver objects. When this becomes empty,
133   // notify shell that the starting animations have been completed.
134   std::vector<std::unique_ptr<DelayedAnimationObserver>> start_animations_;
135 
136   // Indicates that overview shall gain focus when the starting animations have
137   // completed.
138   bool should_focus_overview_ = false;
139 
140   std::unique_ptr<aura::WindowOcclusionTracker::ScopedPause>
141       occlusion_tracker_pauser_;
142 
143   std::unique_ptr<OverviewSession> overview_session_;
144   base::Time last_overview_session_time_;
145 
146   base::TimeDelta occlusion_pause_duration_for_end_;
147 
148   // Handles blurring and dimming of the wallpaper when entering or exiting
149   // overview mode. Animates the blurring and dimming if necessary.
150   std::unique_ptr<OverviewWallpaperController> overview_wallpaper_controller_;
151 
152   base::CancelableOnceClosure reset_pauser_task_;
153 
154   // App dragging enters overview right away. This task is used to delay the
155   // |OnStartingAnimationComplete| call so that some animations do not make the
156   // initial setup less performant.
157   base::TimeDelta delayed_animation_task_delay_;
158 
159   base::ObserverList<OverviewObserver> observers_;
160 
161   std::unique_ptr<views::Widget::PaintAsActiveLock> paint_as_active_lock_;
162 
163   base::WeakPtrFactory<OverviewController> weak_ptr_factory_{this};
164 
165   DISALLOW_COPY_AND_ASSIGN(OverviewController);
166 };
167 
168 }  // namespace ash
169 
170 #endif  // ASH_WM_OVERVIEW_OVERVIEW_CONTROLLER_H_
171