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 "athena/home/home_card_gesture_manager.h"
7 #include "athena/home/home_card_constants.h"
8 #include "ui/events/event.h"
12 HomeCardGestureManager::HomeCardGestureManager(Delegate
* delegate
,
13 const gfx::Rect
& screen_bounds
)
14 : delegate_(delegate
),
15 last_state_(HomeCard::Get()->GetState()),
17 last_estimated_height_(0),
18 screen_bounds_(screen_bounds
) {}
20 HomeCardGestureManager::~HomeCardGestureManager() {}
22 void HomeCardGestureManager::ProcessGestureEvent(ui::GestureEvent
* event
) {
23 switch (event
->type()) {
24 case ui::ET_GESTURE_SCROLL_BEGIN
:
25 y_offset_
= event
->location().y();
28 case ui::ET_GESTURE_SCROLL_END
:
30 delegate_
->OnGestureEnded(GetClosestState());
32 case ui::ET_GESTURE_SCROLL_UPDATE
:
33 UpdateScrollState(*event
);
35 case ui::ET_SCROLL_FLING_START
: {
36 const ui::GestureEventDetails
& details
= event
->details();
37 const float kFlingCompletionVelocity
= 100.0f
;
38 HomeCard::State final_state
= GetClosestState();
39 if (::fabs(details
.velocity_y()) > kFlingCompletionVelocity
) {
40 if (details
.velocity_y() > 0) {
41 final_state
= std::min(HomeCard::VISIBLE_MINIMIZED
,
42 static_cast<HomeCard::State
>(final_state
+ 1));
44 final_state
= std::max(HomeCard::VISIBLE_CENTERED
,
45 static_cast<HomeCard::State
>(final_state
- 1));
48 delegate_
->OnGestureEnded(final_state
);
57 HomeCard::State
HomeCardGestureManager::GetClosestState() const {
58 const int kMinimizedHomeBufferSize
= 50;
59 if (last_estimated_height_
<=
60 kHomeCardMinimizedHeight
+ kMinimizedHomeBufferSize
) {
61 return HomeCard::VISIBLE_MINIMIZED
;
64 int centered_height
= screen_bounds_
.height();
65 if (last_estimated_height_
- kHomeCardHeight
<=
66 (centered_height
- kHomeCardHeight
) / 3) {
67 return HomeCard::VISIBLE_BOTTOM
;
69 return HomeCard::VISIBLE_CENTERED
;
72 void HomeCardGestureManager::UpdateScrollState(const ui::GestureEvent
& event
) {
73 last_estimated_height_
=
74 screen_bounds_
.height() - event
.root_location().y() + y_offset_
;
76 if (last_estimated_height_
<= kHomeCardMinimizedHeight
) {
77 delegate_
->OnGestureProgressed(
78 last_state_
, HomeCard::VISIBLE_MINIMIZED
, 1.0f
);
79 last_state_
= HomeCard::VISIBLE_MINIMIZED
;
83 HomeCard::State state
= HomeCard::VISIBLE_BOTTOM
;
84 float smaller_height
= kHomeCardMinimizedHeight
;
85 float bigger_height
= kHomeCardHeight
;
86 if (last_estimated_height_
> kHomeCardHeight
) {
87 state
= HomeCard::VISIBLE_CENTERED
;
88 smaller_height
= kHomeCardHeight
;
89 bigger_height
= screen_bounds_
.height();
92 // The finger is between two states.
93 float progress
= (last_estimated_height_
- smaller_height
) /
94 (bigger_height
- smaller_height
);
95 progress
= std::min(1.0f
, std::max(0.0f
, progress
));
97 if (last_state_
== state
) {
98 if (event
.details().scroll_y() > 0) {
99 state
= static_cast<HomeCard::State
>(state
+ 1);
100 progress
= 1.0f
- progress
;
102 last_state_
= static_cast<HomeCard::State
>(last_state_
+ 1);
105 delegate_
->OnGestureProgressed(last_state_
, state
, progress
);
109 } // namespace athena