1 // Copyright 2014 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 #include "ui/app_list/pagination_controller.h"
7 #include "ui/app_list/pagination_model.h"
8 #include "ui/events/event.h"
9 #include "ui/gfx/geometry/rect.h"
10 #include "ui/gfx/geometry/vector2d.h"
16 // Constants for dealing with scroll events.
17 const int kMinScrollToSwitchPage
= 20;
18 const int kMinHorizVelocityToSwitchPage
= 800;
20 const double kFinishTransitionThreshold
= 0.33;
24 PaginationController::PaginationController(PaginationModel
* model
,
25 ScrollAxis scroll_axis
)
26 : pagination_model_(model
), scroll_axis_(scroll_axis
) {
29 bool PaginationController::OnScroll(const gfx::Vector2d
& offset
,
30 ScrollEventType type
) {
32 if (scroll_axis_
== SCROLL_AXIS_HORIZONTAL
) {
33 // If the view scrolls horizontally, both horizontal and vertical scroll
34 // events are valid (since most mouse wheels only have vertical scrolling).
36 abs(offset
.x()) > abs(offset
.y()) ? offset
.x() : offset
.y();
38 // If the view scrolls vertically, only vertical scroll events are valid.
39 offset_magnitude
= offset
.y();
42 // Do not scroll on very small touchpad events. Mouse wheel events should
43 // scroll, no matter how small the value change.
44 if (type
== SCROLL_MOUSE_WHEEL
||
45 abs(offset_magnitude
) > kMinScrollToSwitchPage
) {
46 if (!pagination_model_
->has_transition()) {
47 pagination_model_
->SelectPageRelative(offset_magnitude
> 0 ? -1 : 1,
56 bool PaginationController::OnGestureEvent(const ui::GestureEvent
& event
,
57 const gfx::Rect
& bounds
) {
58 const ui::GestureEventDetails
& details
= event
.details();
59 switch (event
.type()) {
60 case ui::ET_GESTURE_SCROLL_BEGIN
:
61 pagination_model_
->StartScroll();
63 case ui::ET_GESTURE_SCROLL_UPDATE
: {
64 float scroll
= scroll_axis_
== SCROLL_AXIS_HORIZONTAL
67 int width
= scroll_axis_
== SCROLL_AXIS_HORIZONTAL
? bounds
.width()
69 // scroll > 0 means moving contents right or down. That is, transitioning
70 // to the previous page.
71 pagination_model_
->UpdateScroll(scroll
/ width
);
74 case ui::ET_GESTURE_SCROLL_END
:
75 pagination_model_
->EndScroll(pagination_model_
->transition().progress
<
76 kFinishTransitionThreshold
);
78 case ui::ET_SCROLL_FLING_START
: {
79 float velocity
= scroll_axis_
== SCROLL_AXIS_HORIZONTAL
80 ? details
.velocity_x()
81 : details
.velocity_y();
82 pagination_model_
->EndScroll(true);
83 if (fabs(velocity
) > kMinHorizVelocityToSwitchPage
)
84 pagination_model_
->SelectPageRelative(velocity
< 0 ? 1 : -1, true);
92 } // namespace app_list