| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/app_list/pagination_model.h" | 5 #include "ui/app_list/pagination_model.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "ui/app_list/pagination_model_observer.h" | 9 #include "ui/app_list/pagination_model_observer.h" |
| 10 #include "ui/base/animation/slide_animation.h" | 10 #include "ui/base/animation/slide_animation.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 // -1 and |total_pages_| are valid target page for animation. | 39 // -1 and |total_pages_| are valid target page for animation. |
| 40 DCHECK(page >= -1 && page <= total_pages_); | 40 DCHECK(page >= -1 && page <= total_pages_); |
| 41 | 41 |
| 42 if (!transition_animation_.get()) { | 42 if (!transition_animation_.get()) { |
| 43 if (page == selected_page_) | 43 if (page == selected_page_) |
| 44 return; | 44 return; |
| 45 | 45 |
| 46 // Creates an animation if there is not one. | 46 // Creates an animation if there is not one. |
| 47 StartTranstionAnimation(page); | 47 StartTranstionAnimation(page); |
| 48 return; | 48 return; |
| 49 } else if (transition_.target_page != page) { | 49 } else { |
| 50 // If there is a running transition, remembers it if it's new. | 50 const bool showing = transition_animation_->IsShowing(); |
| 51 pending_selected_page_ = page; | 51 const int from_page = showing ? selected_page_ : transition_.target_page; |
| 52 const int to_page = showing ? transition_.target_page : selected_page_; |
| 53 |
| 54 if (from_page == page) { |
| 55 if (showing) |
| 56 transition_animation_->Hide(); |
| 57 else |
| 58 transition_animation_->Show(); |
| 59 pending_selected_page_ = -1; |
| 60 } else if (to_page != page) { |
| 61 pending_selected_page_ = page; |
| 62 } else { |
| 63 pending_selected_page_ = -1; |
| 64 } |
| 52 } | 65 } |
| 53 } else { | 66 } else { |
| 54 DCHECK(page >= 0 && page < total_pages_); | 67 DCHECK(page >= 0 && page < total_pages_); |
| 55 | 68 |
| 56 if (page == selected_page_) | 69 if (page == selected_page_) |
| 57 return; | 70 return; |
| 58 | 71 |
| 59 ResetTransitionAnimation(); | 72 ResetTransitionAnimation(); |
| 60 | 73 |
| 61 int old_selected = selected_page_; | 74 int old_selected = selected_page_; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 79 return; | 92 return; |
| 80 | 93 |
| 81 transition_ = transition; | 94 transition_ = transition; |
| 82 NotifyTransitionChanged(); | 95 NotifyTransitionChanged(); |
| 83 } | 96 } |
| 84 | 97 |
| 85 void PaginationModel::SetTransitionDuration(int duration_ms) { | 98 void PaginationModel::SetTransitionDuration(int duration_ms) { |
| 86 transition_duration_ms_ = duration_ms; | 99 transition_duration_ms_ = duration_ms; |
| 87 } | 100 } |
| 88 | 101 |
| 89 void PaginationModel::ResetTransitionAnimation() { | |
| 90 transition_animation_.reset(); | |
| 91 transition_.target_page = -1; | |
| 92 transition_.progress = 0; | |
| 93 pending_selected_page_ = -1; | |
| 94 } | |
| 95 | |
| 96 void PaginationModel::StartScroll() { | 102 void PaginationModel::StartScroll() { |
| 97 // Cancels current transition animation (if any). | 103 // Cancels current transition animation (if any). |
| 98 transition_animation_.reset(); | 104 transition_animation_.reset(); |
| 99 } | 105 } |
| 100 | 106 |
| 101 void PaginationModel::UpdateScroll(double delta) { | 107 void PaginationModel::UpdateScroll(double delta) { |
| 102 // Translates scroll delta to desired page change direction. | 108 // Translates scroll delta to desired page change direction. |
| 103 int page_change_dir = delta > 0 ? -1 : 1; | 109 int page_change_dir = delta > 0 ? -1 : 1; |
| 104 | 110 |
| 105 // Initializes a transition if there is none. | 111 // Initializes a transition if there is none. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 117 if (is_valid_page(transition_.target_page)) { | 123 if (is_valid_page(transition_.target_page)) { |
| 118 SelectPage(transition_.target_page, false); | 124 SelectPage(transition_.target_page, false); |
| 119 clear_transition(); | 125 clear_transition(); |
| 120 } | 126 } |
| 121 } else { | 127 } else { |
| 122 transition_.progress = progress; | 128 transition_.progress = progress; |
| 123 NotifyTransitionChanged(); | 129 NotifyTransitionChanged(); |
| 124 } | 130 } |
| 125 } | 131 } |
| 126 | 132 |
| 127 void PaginationModel::EndScroll() { | 133 void PaginationModel::EndScroll(bool cancel) { |
| 128 if (!has_transition()) | 134 if (!has_transition()) |
| 129 return; | 135 return; |
| 130 | 136 |
| 131 CreateTransitionAnimation(); | 137 CreateTransitionAnimation(); |
| 132 transition_animation_->Reset(transition_.progress); | 138 transition_animation_->Reset(transition_.progress); |
| 133 | 139 |
| 134 // Always call Show to ensure animation will run. | 140 // Always call Show to ensure animation will run. |
| 135 transition_animation_->Show(); | 141 transition_animation_->Show(); |
| 136 if (transition_.progress < 0.5) | 142 if (cancel) |
| 137 transition_animation_->Hide(); | 143 transition_animation_->Hide(); |
| 138 } | 144 } |
| 139 | 145 |
| 140 void PaginationModel::AddObserver(PaginationModelObserver* observer) { | 146 void PaginationModel::AddObserver(PaginationModelObserver* observer) { |
| 141 observers_.AddObserver(observer); | 147 observers_.AddObserver(observer); |
| 142 } | 148 } |
| 143 | 149 |
| 144 void PaginationModel::RemoveObserver(PaginationModelObserver* observer) { | 150 void PaginationModel::RemoveObserver(PaginationModelObserver* observer) { |
| 145 observers_.RemoveObserver(observer); | 151 observers_.RemoveObserver(observer); |
| 146 } | 152 } |
| 147 | 153 |
| 148 void PaginationModel::NotifySelectedPageChanged(int old_selected, | 154 void PaginationModel::NotifySelectedPageChanged(int old_selected, |
| 149 int new_selected) { | 155 int new_selected) { |
| 150 FOR_EACH_OBSERVER(PaginationModelObserver, | 156 FOR_EACH_OBSERVER(PaginationModelObserver, |
| 151 observers_, | 157 observers_, |
| 152 SelectedPageChanged(old_selected, new_selected)); | 158 SelectedPageChanged(old_selected, new_selected)); |
| 153 } | 159 } |
| 154 | 160 |
| 155 void PaginationModel::NotifyTransitionChanged() { | 161 void PaginationModel::NotifyTransitionChanged() { |
| 156 FOR_EACH_OBSERVER(PaginationModelObserver, observers_, TransitionChanged()); | 162 FOR_EACH_OBSERVER(PaginationModelObserver, observers_, TransitionChanged()); |
| 157 } | 163 } |
| 158 | 164 |
| 159 int PaginationModel::CalculateTargetPage(int delta) const { | 165 int PaginationModel::CalculateTargetPage(int delta) const { |
| 160 DCHECK_GT(total_pages_, 0); | 166 DCHECK_GT(total_pages_, 0); |
| 161 | 167 |
| 162 int current_page = selected_page_; | 168 int current_page = selected_page_; |
| 163 if (transition_animation_.get()) { | 169 if (transition_animation_.get() && transition_animation_->IsShowing()) { |
| 164 current_page = pending_selected_page_ >= 0 ? | 170 current_page = pending_selected_page_ >= 0 ? |
| 165 pending_selected_page_ : transition_.target_page; | 171 pending_selected_page_ : transition_.target_page; |
| 166 } | 172 } |
| 167 | 173 |
| 168 const int target_page = current_page + delta; | 174 const int target_page = current_page + delta; |
| 169 | 175 |
| 170 int start_page = 0; | 176 int start_page = 0; |
| 171 int end_page = total_pages_ - 1; | 177 int end_page = total_pages_ - 1; |
| 172 | 178 |
| 173 // Use invalid page when |selected_page_| is at ends. | 179 // Use invalid page when |selected_page_| is at ends. |
| 174 if (target_page < start_page && selected_page_ == start_page) | 180 if (target_page < start_page && selected_page_ == start_page) |
| 175 start_page = -1; | 181 start_page = -1; |
| 176 else if (target_page > end_page && selected_page_ == end_page) | 182 else if (target_page > end_page && selected_page_ == end_page) |
| 177 end_page = total_pages_; | 183 end_page = total_pages_; |
| 178 | 184 |
| 179 return std::max(start_page, std::min(end_page, target_page)); | 185 return std::max(start_page, std::min(end_page, target_page)); |
| 180 } | 186 } |
| 181 | 187 |
| 182 void PaginationModel::StartTranstionAnimation(int target_page) { | 188 void PaginationModel::StartTranstionAnimation(int target_page) { |
| 183 DCHECK(selected_page_ != target_page); | 189 DCHECK(selected_page_ != target_page); |
| 184 | 190 |
| 185 SetTransition(Transition(target_page, 0)); | 191 SetTransition(Transition(target_page, 0)); |
| 186 CreateTransitionAnimation(); | 192 CreateTransitionAnimation(); |
| 187 transition_animation_->Show(); | 193 transition_animation_->Show(); |
| 188 } | 194 } |
| 189 | 195 |
| 190 void PaginationModel::CreateTransitionAnimation() { | 196 void PaginationModel::CreateTransitionAnimation() { |
| 191 transition_animation_.reset(new ui::SlideAnimation(this)); | 197 transition_animation_.reset(new ui::SlideAnimation(this)); |
| 198 transition_animation_->SetTweenType(ui::Tween::LINEAR); |
| 192 if (transition_duration_ms_) | 199 if (transition_duration_ms_) |
| 193 transition_animation_->SetSlideDuration(transition_duration_ms_); | 200 transition_animation_->SetSlideDuration(transition_duration_ms_); |
| 194 } | 201 } |
| 195 | 202 |
| 203 void PaginationModel::ResetTransitionAnimation() { |
| 204 transition_animation_.reset(); |
| 205 transition_.target_page = -1; |
| 206 transition_.progress = 0; |
| 207 pending_selected_page_ = -1; |
| 208 } |
| 209 |
| 196 void PaginationModel::AnimationProgressed(const ui::Animation* animation) { | 210 void PaginationModel::AnimationProgressed(const ui::Animation* animation) { |
| 197 transition_.progress = transition_animation_->GetCurrentValue(); | 211 transition_.progress = transition_animation_->GetCurrentValue(); |
| 198 NotifyTransitionChanged(); | 212 NotifyTransitionChanged(); |
| 199 } | 213 } |
| 200 | 214 |
| 201 void PaginationModel::AnimationEnded(const ui::Animation* animation) { | 215 void PaginationModel::AnimationEnded(const ui::Animation* animation) { |
| 202 // Save |pending_selected_page_| because SelectPage resets it. | 216 // Save |pending_selected_page_| because SelectPage resets it. |
| 203 int next_target = pending_selected_page_; | 217 int next_target = pending_selected_page_; |
| 204 | 218 |
| 205 if (transition_animation_->GetCurrentValue() == 1) { | 219 if (transition_animation_->GetCurrentValue() == 1) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 218 } else if (transition_animation_->GetCurrentValue() == 0) { | 232 } else if (transition_animation_->GetCurrentValue() == 0) { |
| 219 // Hiding animation ends. No page change should happen. | 233 // Hiding animation ends. No page change should happen. |
| 220 ResetTransitionAnimation(); | 234 ResetTransitionAnimation(); |
| 221 } | 235 } |
| 222 | 236 |
| 223 if (next_target >= 0) | 237 if (next_target >= 0) |
| 224 SelectPage(next_target, true); | 238 SelectPage(next_target, true); |
| 225 } | 239 } |
| 226 | 240 |
| 227 } // namespace app_list | 241 } // namespace app_list |
| OLD | NEW |