OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "athena/home/home_card_gesture_manager.h" | 5 #include "athena/home/home_card_gesture_manager.h" |
6 | 6 |
7 #include "athena/home/home_card_constants.h" | 7 #include "athena/home/home_card_constants.h" |
8 #include "ui/events/event.h" | 8 #include "ui/events/event.h" |
9 | 9 |
10 namespace athena { | 10 namespace athena { |
11 | 11 |
| 12 namespace { |
| 13 |
| 14 // The maximum height, in pixels, of a home card with final state |
| 15 // VISIBLE_MINIMIZED. |
| 16 const int kMinimizedFinalStateMaxHeight = 50 + kHomeCardMinimizedHeight; |
| 17 |
| 18 // The maximum height, in pixels, of an initially centered home card with final |
| 19 // state VISIBLE_MINIMIZED. |
| 20 const int kMinimizedFinalStateMaxHeightInitiallyCentered = |
| 21 90 + kHomeCardMinimizedHeight; |
| 22 |
| 23 // The minimum height, as a percentage of the screen height, of a home card with |
| 24 // final state VISIBLE_CENTERED. |
| 25 const float kCenteredFinalStateMinScreenRatio = 0.5f; |
| 26 |
| 27 // The minimum height, as a percentage of the screen height, of an initially |
| 28 // minimized home card with final state VISIBLE_CENTERED. |
| 29 const float kCenteredFinalStateMinScreenRatioInitiallyMinimized = 0.3f; |
| 30 |
| 31 } |
| 32 |
12 HomeCardGestureManager::HomeCardGestureManager(Delegate* delegate, | 33 HomeCardGestureManager::HomeCardGestureManager(Delegate* delegate, |
13 const gfx::Rect& screen_bounds) | 34 const gfx::Rect& screen_bounds) |
14 : delegate_(delegate), | 35 : delegate_(delegate), |
| 36 original_state_(HomeCard::HIDDEN), |
15 y_offset_(0), | 37 y_offset_(0), |
16 last_estimated_height_(0), | 38 last_estimated_height_(0), |
17 screen_bounds_(screen_bounds) {} | 39 screen_bounds_(screen_bounds) {} |
18 | 40 |
19 HomeCardGestureManager::~HomeCardGestureManager() {} | 41 HomeCardGestureManager::~HomeCardGestureManager() {} |
20 | 42 |
21 void HomeCardGestureManager::ProcessGestureEvent(ui::GestureEvent* event) { | 43 void HomeCardGestureManager::ProcessGestureEvent(ui::GestureEvent* event) { |
22 switch (event->type()) { | 44 switch (event->type()) { |
23 case ui::ET_GESTURE_SCROLL_BEGIN: | 45 case ui::ET_GESTURE_SCROLL_BEGIN: |
24 y_offset_ = event->location().y(); | 46 y_offset_ = event->location().y(); |
| 47 original_state_ = HomeCard::Get()->GetState(); |
| 48 DCHECK_NE(HomeCard::HIDDEN, original_state_); |
25 event->SetHandled(); | 49 event->SetHandled(); |
26 break; | 50 break; |
27 case ui::ET_GESTURE_SCROLL_END: | 51 case ui::ET_GESTURE_SCROLL_END: |
28 event->SetHandled(); | 52 event->SetHandled(); |
29 delegate_->OnGestureEnded(GetClosestState()); | 53 delegate_->OnGestureEnded(GetFinalState()); |
30 break; | 54 break; |
31 case ui::ET_GESTURE_SCROLL_UPDATE: | 55 case ui::ET_GESTURE_SCROLL_UPDATE: |
32 UpdateScrollState(*event); | 56 UpdateScrollState(*event); |
33 break; | 57 break; |
34 case ui::ET_SCROLL_FLING_START: { | 58 case ui::ET_SCROLL_FLING_START: { |
35 const ui::GestureEventDetails& details = event->details(); | 59 const ui::GestureEventDetails& details = event->details(); |
36 const float kFlingCompletionVelocity = 100.0f; | 60 const float kFlingCompletionVelocity = 100.0f; |
37 HomeCard::State final_state = GetClosestState(); | 61 HomeCard::State final_state = GetFinalState(); |
38 if (::fabs(details.velocity_y()) > kFlingCompletionVelocity) { | 62 |
| 63 // When the user does not drag far enough to switch the final state, but |
| 64 // a fling happens at the end of the gesture, the state should change |
| 65 // based on the direction of the fling. |
| 66 // Checking |final_state| == |original_state| may cause unexpected results |
| 67 // for gestures where the user flings in the opposite direction that they |
| 68 // moved the home card (e.g. drag home card up from minimized state and |
| 69 // then fling down) |
| 70 // TODO(mukai): Consider this case once reported. |
| 71 if (final_state == original_state_ && |
| 72 ::fabs(details.velocity_y()) > kFlingCompletionVelocity) { |
39 if (details.velocity_y() > 0) { | 73 if (details.velocity_y() > 0) { |
40 final_state = std::min(HomeCard::VISIBLE_MINIMIZED, | 74 final_state = std::min(HomeCard::VISIBLE_MINIMIZED, |
41 static_cast<HomeCard::State>(final_state + 1)); | 75 static_cast<HomeCard::State>(final_state + 1)); |
42 } else { | 76 } else { |
43 final_state = std::max(HomeCard::VISIBLE_CENTERED, | 77 final_state = std::max(HomeCard::VISIBLE_CENTERED, |
44 static_cast<HomeCard::State>(final_state - 1)); | 78 static_cast<HomeCard::State>(final_state - 1)); |
45 } | 79 } |
46 } | 80 } |
47 delegate_->OnGestureEnded(final_state); | 81 delegate_->OnGestureEnded(final_state); |
48 break; | 82 break; |
49 } | 83 } |
50 default: | 84 default: |
51 // do nothing. | 85 // do nothing. |
52 break; | 86 break; |
53 } | 87 } |
54 } | 88 } |
55 | 89 |
56 HomeCard::State HomeCardGestureManager::GetClosestState() const { | 90 HomeCard::State HomeCardGestureManager::GetFinalState() const { |
57 const int kMinimizedHomeBufferSize = 50; | 91 int max_height = (original_state_ == HomeCard::VISIBLE_CENTERED) |
58 if (last_estimated_height_ <= | 92 ? kMinimizedFinalStateMaxHeightInitiallyCentered |
59 kHomeCardMinimizedHeight + kMinimizedHomeBufferSize) { | 93 : kMinimizedFinalStateMaxHeight; |
| 94 if (last_estimated_height_ < max_height) |
60 return HomeCard::VISIBLE_MINIMIZED; | 95 return HomeCard::VISIBLE_MINIMIZED; |
61 } | |
62 | 96 |
63 int centered_height = screen_bounds_.height(); | 97 float ratio = (original_state_ == HomeCard::VISIBLE_MINIMIZED) |
64 if (last_estimated_height_ - kHomeCardHeight <= | 98 ? kCenteredFinalStateMinScreenRatioInitiallyMinimized |
65 (centered_height - kHomeCardHeight) / 3) { | 99 : kCenteredFinalStateMinScreenRatio; |
| 100 if (last_estimated_height_ < screen_bounds_.height() * ratio) |
66 return HomeCard::VISIBLE_BOTTOM; | 101 return HomeCard::VISIBLE_BOTTOM; |
67 } | 102 |
68 return HomeCard::VISIBLE_CENTERED; | 103 return HomeCard::VISIBLE_CENTERED; |
69 } | 104 } |
70 | 105 |
71 void HomeCardGestureManager::UpdateScrollState(const ui::GestureEvent& event) { | 106 void HomeCardGestureManager::UpdateScrollState(const ui::GestureEvent& event) { |
72 last_estimated_height_ = | 107 last_estimated_height_ = |
73 screen_bounds_.height() - event.root_location().y() + y_offset_; | 108 screen_bounds_.height() - event.root_location().y() + y_offset_; |
74 | 109 |
75 if (last_estimated_height_ <= kHomeCardMinimizedHeight) { | 110 if (last_estimated_height_ <= kHomeCardMinimizedHeight) { |
76 delegate_->OnGestureProgressed( | 111 delegate_->OnGestureProgressed( |
77 HomeCard::VISIBLE_BOTTOM, HomeCard::VISIBLE_MINIMIZED, 1.0f); | 112 HomeCard::VISIBLE_BOTTOM, HomeCard::VISIBLE_MINIMIZED, 1.0f); |
(...skipping 14 matching lines...) Expand all Loading... |
92 (bigger_height - smaller_height); | 127 (bigger_height - smaller_height); |
93 progress = std::min(1.0f, std::max(0.0f, progress)); | 128 progress = std::min(1.0f, std::max(0.0f, progress)); |
94 | 129 |
95 delegate_->OnGestureProgressed( | 130 delegate_->OnGestureProgressed( |
96 static_cast<HomeCard::State>(bigger_state + 1), | 131 static_cast<HomeCard::State>(bigger_state + 1), |
97 bigger_state, | 132 bigger_state, |
98 progress); | 133 progress); |
99 } | 134 } |
100 | 135 |
101 } // namespace athena | 136 } // namespace athena |
OLD | NEW |