1 // Copyright 2019 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_GRID_EVENT_HANDLER_H_
6 #define ASH_WM_OVERVIEW_OVERVIEW_GRID_EVENT_HANDLER_H_
7 
8 #include "base/macros.h"
9 #include "base/optional.h"
10 #include "ui/compositor/compositor_animation_observer.h"
11 #include "ui/events/event_handler.h"
12 #include "ui/gfx/geometry/point.h"
13 
14 namespace ui {
15 class Compositor;
16 class Event;
17 class FlingCurve;
18 class GestureEvent;
19 class MouseEvent;
20 }  // namespace ui
21 
22 namespace ash {
23 class OverviewGrid;
24 class OverviewSession;
25 
26 // This event handler receives events in the pre-target phase and takes care of
27 // the following:
28 //   - Disabling overview mode on touch release.
29 //   - Disabling overview mode on mouse release.
30 //   - Scrolling through tablet overview mode on scrolling.
31 //   - Scrolling through tablet overview mode on flinging.
32 class OverviewGridEventHandler : public ui::EventHandler,
33                                  public ui::CompositorAnimationObserver {
34  public:
35   explicit OverviewGridEventHandler(OverviewGrid* grid);
36   ~OverviewGridEventHandler() override;
37 
38   // ui::EventHandler:
39   void OnMouseEvent(ui::MouseEvent* event) override;
40   void OnGestureEvent(ui::GestureEvent* event) override;
41 
IsFlingInProgressForTesting()42   bool IsFlingInProgressForTesting() const { return !!fling_curve_; }
43 
44  private:
45   // ui::CompositorAnimationObserver:
46   void OnAnimationStep(base::TimeTicks timestamp) override;
47   void OnCompositingShuttingDown(ui::Compositor* compositor) override;
48 
49   void HandleClickOrTap(ui::Event* event);
50 
51   void HandleFlingScroll(ui::GestureEvent* event);
52 
53   void EndFling();
54 
55   // Cached value of the OverviewGrid that handles a series of gesture scroll
56   // events. Guaranteed to be alive during the lifetime of |this|.
57   OverviewGrid* grid_;
58 
59   // Guaranteed to be alive during the lifetime of |this|.
60   OverviewSession* const overview_session_;
61 
62   // The cumulative scroll offset. This is used so that tiny scrolls will not
63   // make minuscule shifts on the grid, but are not completely ignored.
64   float scroll_offset_x_cumulative_ = 0.f;
65 
66   // Gesture curve of the current active fling. nullptr while a fling is not
67   // active.
68   std::unique_ptr<ui::FlingCurve> fling_curve_;
69 
70   // Velocity of the fling that will gradually decrease during a fling.
71   gfx::Vector2dF fling_velocity_;
72 
73   // Cached value of an earlier offset that determines values to scroll through
74   // overview mode by being compared to an updated offset.
75   base::Optional<gfx::Vector2dF> fling_last_offset_;
76 
77   // The compositor we are observing when a fling is underway.
78   ui::Compositor* observed_compositor_ = nullptr;
79 
80   DISALLOW_COPY_AND_ASSIGN(OverviewGridEventHandler);
81 };
82 
83 }  // namespace ash
84 
85 #endif  // ASH_WM_OVERVIEW_OVERVIEW_GRID_EVENT_HANDLER_H_
86