OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "athena/home/home_card_gesture_manager.h" |
| 6 |
| 7 #include "athena/home/home_card_constants.h" |
| 8 #include "ui/events/event.h" |
| 9 |
| 10 namespace athena { |
| 11 |
| 12 HomeCardGestureManager::HomeCardGestureManager(Delegate* delegate, |
| 13 const gfx::Rect& screen_bounds) |
| 14 : delegate_(delegate), |
| 15 last_state_(HomeCard::Get()->GetState()), |
| 16 y_offset_(0), |
| 17 last_estimated_height_(0), |
| 18 screen_bounds_(screen_bounds) {} |
| 19 |
| 20 HomeCardGestureManager::~HomeCardGestureManager() {} |
| 21 |
| 22 void HomeCardGestureManager::ProcessGestureEvent(ui::GestureEvent* event) { |
| 23 switch (event->type()) { |
| 24 case ui::ET_GESTURE_SCROLL_BEGIN: |
| 25 y_offset_ = event->location().y(); |
| 26 event->SetHandled(); |
| 27 break; |
| 28 case ui::ET_GESTURE_SCROLL_END: |
| 29 event->SetHandled(); |
| 30 delegate_->OnGestureEnded(GetClosestState()); |
| 31 break; |
| 32 case ui::ET_GESTURE_SCROLL_UPDATE: |
| 33 UpdateScrollState(*event); |
| 34 break; |
| 35 case ui::ET_SCROLL_FLING_START: { |
| 36 const ui::GestureEventDetails& details = event->details(); |
| 37 const float kFlingCompletionVelocity = 100.0f; |
| 38 if (::fabs(details.velocity_y()) > kFlingCompletionVelocity) { |
| 39 int step = (details.velocity_y() > 0) ? 1 : -1; |
| 40 int new_state = static_cast<int>(last_state_) + step; |
| 41 if (new_state >= HomeCard::VISIBLE_CENTERED && |
| 42 new_state <= HomeCard::VISIBLE_MINIMIZED) { |
| 43 last_state_ = static_cast<HomeCard::State>(new_state); |
| 44 } |
| 45 delegate_->OnGestureEnded(last_state_); |
| 46 } |
| 47 break; |
| 48 } |
| 49 default: |
| 50 // do nothing. |
| 51 break; |
| 52 } |
| 53 } |
| 54 |
| 55 HomeCard::State HomeCardGestureManager::GetClosestState() const { |
| 56 // The top position of the bounds for one smaller state than the current |
| 57 // one. |
| 58 |
| 59 if (last_estimated_height_ <= kHomeCardMinimizedHeight) |
| 60 return HomeCard::VISIBLE_MINIMIZED; |
| 61 |
| 62 HomeCard::State smaller_state = HomeCard::VISIBLE_MINIMIZED; |
| 63 int smaller_height = kHomeCardMinimizedHeight; |
| 64 int bigger_height = kHomeCardHeight; |
| 65 if (last_estimated_height_ > kHomeCardHeight) { |
| 66 smaller_state = HomeCard::VISIBLE_BOTTOM; |
| 67 smaller_height = kHomeCardHeight; |
| 68 bigger_height = screen_bounds_.height(); |
| 69 } |
| 70 |
| 71 if (last_estimated_height_ - smaller_height <= |
| 72 (bigger_height - smaller_height) / 5) { |
| 73 return smaller_state; |
| 74 } |
| 75 |
| 76 return static_cast<HomeCard::State>(smaller_state - 1); |
| 77 } |
| 78 |
| 79 void HomeCardGestureManager::UpdateScrollState(const ui::GestureEvent& event) { |
| 80 last_estimated_height_ = |
| 81 screen_bounds_.height() - event.root_location().y() + y_offset_; |
| 82 |
| 83 if (last_estimated_height_ <= kHomeCardMinimizedHeight) { |
| 84 delegate_->OnGestureProgressed( |
| 85 last_state_, HomeCard::VISIBLE_MINIMIZED, 1.0f); |
| 86 last_state_ = HomeCard::VISIBLE_MINIMIZED; |
| 87 return; |
| 88 } |
| 89 |
| 90 HomeCard::State state = HomeCard::VISIBLE_BOTTOM; |
| 91 float smaller_height = kHomeCardMinimizedHeight; |
| 92 float bigger_height = kHomeCardHeight; |
| 93 if (last_estimated_height_ > kHomeCardHeight) { |
| 94 state = HomeCard::VISIBLE_CENTERED; |
| 95 smaller_height = kHomeCardHeight; |
| 96 bigger_height = screen_bounds_.height(); |
| 97 } |
| 98 |
| 99 // The finger is between two states. |
| 100 float progress = (last_estimated_height_ - smaller_height) / |
| 101 (bigger_height - smaller_height); |
| 102 |
| 103 if (last_state_ == state) { |
| 104 if (event.details().scroll_y() > 0) { |
| 105 state = static_cast<HomeCard::State>(state + 1); |
| 106 progress = 1.0f - progress; |
| 107 } else { |
| 108 last_state_ = static_cast<HomeCard::State>(last_state_ + 1); |
| 109 } |
| 110 } |
| 111 delegate_->OnGestureProgressed(last_state_, state, progress); |
| 112 last_state_ = state; |
| 113 } |
| 114 |
| 115 } // namespace athena |
OLD | NEW |