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" |
11 | 11 |
12 namespace app_list { | 12 namespace app_list { |
13 | 13 |
14 PaginationModel::PaginationModel() | 14 PaginationModel::PaginationModel() |
15 : total_pages_(-1), | 15 : total_pages_(-1), |
16 selected_page_(-1), | 16 selected_page_(-1), |
17 transition_(-1, 0), | 17 transition_(-1, 0), |
18 pending_selected_page_(-1), | 18 pending_selected_page_(-1), |
19 transition_duration_ms_(0) { | 19 transition_duration_ms_(0), |
| 20 overscroll_transition_duration_ms_(0) { |
20 } | 21 } |
21 | 22 |
22 PaginationModel::~PaginationModel() { | 23 PaginationModel::~PaginationModel() { |
23 } | 24 } |
24 | 25 |
25 void PaginationModel::SetTotalPages(int total_pages) { | 26 void PaginationModel::SetTotalPages(int total_pages) { |
26 if (total_pages == total_pages_) | 27 if (total_pages == total_pages_) |
27 return; | 28 return; |
28 | 29 |
29 total_pages_ = total_pages; | 30 total_pages_ = total_pages; |
30 if (selected_page_ < 0) | 31 if (selected_page_ < 0) |
31 SelectPage(0, false /* animate */); | 32 SelectPage(0, false /* animate */); |
32 if (selected_page_ >= total_pages_) | 33 if (selected_page_ >= total_pages_) |
33 SelectPage(total_pages_ - 1, false /* animate */); | 34 SelectPage(total_pages_ - 1, false /* animate */); |
34 FOR_EACH_OBSERVER(PaginationModelObserver, observers_, TotalPagesChanged()); | 35 FOR_EACH_OBSERVER(PaginationModelObserver, observers_, TotalPagesChanged()); |
35 } | 36 } |
36 | 37 |
37 void PaginationModel::SelectPage(int page, bool animate) { | 38 void PaginationModel::SelectPage(int page, bool animate) { |
38 if (animate) { | 39 if (animate) { |
39 // -1 and |total_pages_| are valid target page for animation. | 40 // -1 and |total_pages_| are valid target page for animation. |
40 DCHECK(page >= -1 && page <= total_pages_); | 41 DCHECK(page >= -1 && page <= total_pages_); |
41 | 42 |
42 if (!transition_animation_.get()) { | 43 if (!transition_animation_.get()) { |
43 if (page == selected_page_) | 44 if (page == selected_page_) |
44 return; | 45 return; |
45 | 46 |
46 // Creates an animation if there is not one. | 47 // Creates an animation if there is not one. |
47 StartTranstionAnimation(page); | 48 StartTransitionAnimation(Transition(page, 0)); |
48 return; | 49 return; |
49 } else { | 50 } else { |
50 const bool showing = transition_animation_->IsShowing(); | 51 const bool showing = transition_animation_->IsShowing(); |
51 const int from_page = showing ? selected_page_ : transition_.target_page; | 52 const int from_page = showing ? selected_page_ : transition_.target_page; |
52 const int to_page = showing ? transition_.target_page : selected_page_; | 53 const int to_page = showing ? transition_.target_page : selected_page_; |
53 | 54 |
54 if (from_page == page) { | 55 if (from_page == page) { |
55 if (showing) | 56 if (showing) |
56 transition_animation_->Hide(); | 57 transition_animation_->Hide(); |
57 else | 58 else |
58 transition_animation_->Show(); | 59 transition_animation_->Show(); |
59 pending_selected_page_ = -1; | 60 pending_selected_page_ = -1; |
60 } else if (to_page != page) { | 61 } else if (to_page != page) { |
61 pending_selected_page_ = page; | 62 pending_selected_page_ = page; |
(...skipping 26 matching lines...) Expand all Loading... |
88 transition.target_page <= total_pages_); | 89 transition.target_page <= total_pages_); |
89 DCHECK(transition.progress >= 0 && transition.progress <= 1); | 90 DCHECK(transition.progress >= 0 && transition.progress <= 1); |
90 | 91 |
91 if (transition_.Equals(transition)) | 92 if (transition_.Equals(transition)) |
92 return; | 93 return; |
93 | 94 |
94 transition_ = transition; | 95 transition_ = transition; |
95 NotifyTransitionChanged(); | 96 NotifyTransitionChanged(); |
96 } | 97 } |
97 | 98 |
98 void PaginationModel::SetTransitionDuration(int duration_ms) { | 99 void PaginationModel::SetTransitionDurations(int duration_ms, |
| 100 int overscroll_duration_ms) { |
99 transition_duration_ms_ = duration_ms; | 101 transition_duration_ms_ = duration_ms; |
| 102 overscroll_transition_duration_ms_ = overscroll_duration_ms; |
100 } | 103 } |
101 | 104 |
102 void PaginationModel::StartScroll() { | 105 void PaginationModel::StartScroll() { |
103 // Cancels current transition animation (if any). | 106 // Cancels current transition animation (if any). |
104 transition_animation_.reset(); | 107 transition_animation_.reset(); |
105 } | 108 } |
106 | 109 |
107 void PaginationModel::UpdateScroll(double delta) { | 110 void PaginationModel::UpdateScroll(double delta) { |
108 // Translates scroll delta to desired page change direction. | 111 // Translates scroll delta to desired page change direction. |
109 int page_change_dir = delta > 0 ? -1 : 1; | 112 int page_change_dir = delta > 0 ? -1 : 1; |
(...skipping 17 matching lines...) Expand all Loading... |
127 } else { | 130 } else { |
128 transition_.progress = progress; | 131 transition_.progress = progress; |
129 NotifyTransitionChanged(); | 132 NotifyTransitionChanged(); |
130 } | 133 } |
131 } | 134 } |
132 | 135 |
133 void PaginationModel::EndScroll(bool cancel) { | 136 void PaginationModel::EndScroll(bool cancel) { |
134 if (!has_transition()) | 137 if (!has_transition()) |
135 return; | 138 return; |
136 | 139 |
137 CreateTransitionAnimation(); | 140 StartTransitionAnimation(transition_); |
138 transition_animation_->Reset(transition_.progress); | |
139 | 141 |
140 // Always call Show to ensure animation will run. | |
141 transition_animation_->Show(); | |
142 if (cancel) | 142 if (cancel) |
143 transition_animation_->Hide(); | 143 transition_animation_->Hide(); |
144 } | 144 } |
145 | 145 |
146 bool PaginationModel::IsRevertingCurrentTransition() const { | 146 bool PaginationModel::IsRevertingCurrentTransition() const { |
147 // Use !IsShowing() so that we return true at the end of hide animation. | 147 // Use !IsShowing() so that we return true at the end of hide animation. |
148 return transition_animation_.get() && !transition_animation_->IsShowing(); | 148 return transition_animation_.get() && !transition_animation_->IsShowing(); |
149 } | 149 } |
150 | 150 |
151 void PaginationModel::AddObserver(PaginationModelObserver* observer) { | 151 void PaginationModel::AddObserver(PaginationModelObserver* observer) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 | 183 |
184 // Use invalid page when |selected_page_| is at ends. | 184 // Use invalid page when |selected_page_| is at ends. |
185 if (target_page < start_page && selected_page_ == start_page) | 185 if (target_page < start_page && selected_page_ == start_page) |
186 start_page = -1; | 186 start_page = -1; |
187 else if (target_page > end_page && selected_page_ == end_page) | 187 else if (target_page > end_page && selected_page_ == end_page) |
188 end_page = total_pages_; | 188 end_page = total_pages_; |
189 | 189 |
190 return std::max(start_page, std::min(end_page, target_page)); | 190 return std::max(start_page, std::min(end_page, target_page)); |
191 } | 191 } |
192 | 192 |
193 void PaginationModel::StartTranstionAnimation(int target_page) { | 193 void PaginationModel::StartTransitionAnimation(const Transition& transition) { |
194 DCHECK(selected_page_ != target_page); | 194 DCHECK(selected_page_ != transition.target_page); |
195 | 195 |
196 SetTransition(Transition(target_page, 0)); | 196 SetTransition(transition); |
197 CreateTransitionAnimation(); | 197 |
| 198 transition_animation_.reset(new ui::SlideAnimation(this)); |
| 199 transition_animation_->SetTweenType(ui::Tween::LINEAR); |
| 200 transition_animation_->Reset(transition_.progress); |
| 201 |
| 202 const int duration = is_valid_page(transition_.target_page) ? |
| 203 transition_duration_ms_ : overscroll_transition_duration_ms_; |
| 204 if (duration) |
| 205 transition_animation_->SetSlideDuration(duration); |
| 206 |
198 transition_animation_->Show(); | 207 transition_animation_->Show(); |
199 } | 208 } |
200 | 209 |
201 void PaginationModel::CreateTransitionAnimation() { | |
202 transition_animation_.reset(new ui::SlideAnimation(this)); | |
203 transition_animation_->SetTweenType(ui::Tween::LINEAR); | |
204 if (transition_duration_ms_) | |
205 transition_animation_->SetSlideDuration(transition_duration_ms_); | |
206 } | |
207 | |
208 void PaginationModel::ResetTransitionAnimation() { | 210 void PaginationModel::ResetTransitionAnimation() { |
209 transition_animation_.reset(); | 211 transition_animation_.reset(); |
210 transition_.target_page = -1; | 212 transition_.target_page = -1; |
211 transition_.progress = 0; | 213 transition_.progress = 0; |
212 pending_selected_page_ = -1; | 214 pending_selected_page_ = -1; |
213 } | 215 } |
214 | 216 |
215 void PaginationModel::AnimationProgressed(const ui::Animation* animation) { | 217 void PaginationModel::AnimationProgressed(const ui::Animation* animation) { |
216 transition_.progress = transition_animation_->GetCurrentValue(); | 218 transition_.progress = transition_animation_->GetCurrentValue(); |
217 NotifyTransitionChanged(); | 219 NotifyTransitionChanged(); |
218 } | 220 } |
219 | 221 |
220 void PaginationModel::AnimationEnded(const ui::Animation* animation) { | 222 void PaginationModel::AnimationEnded(const ui::Animation* animation) { |
221 // Save |pending_selected_page_| because SelectPage resets it. | 223 // Save |pending_selected_page_| because SelectPage resets it. |
222 int next_target = pending_selected_page_; | 224 int next_target = pending_selected_page_; |
223 | 225 |
224 if (transition_animation_->GetCurrentValue() == 1) { | 226 if (transition_animation_->GetCurrentValue() == 1) { |
225 // Showing animation ends. | 227 // Showing animation ends. |
226 int target_page = transition_.target_page; | 228 if (!is_valid_page(transition_.target_page)) { |
227 | 229 // If target page is not in valid range, reverse the animation. |
228 // If target page is not in valid range, reverse the animation. | |
229 if (target_page < 0 || target_page >= total_pages_) { | |
230 transition_animation_->Hide(); | 230 transition_animation_->Hide(); |
231 return; | 231 return; |
232 } | 232 } |
233 | 233 |
234 // Otherwise, change page and finish the transition. | 234 // Otherwise, change page and finish the transition. |
235 DCHECK(selected_page_ != transition_.target_page); | 235 DCHECK(selected_page_ != transition_.target_page); |
236 SelectPage(transition_.target_page, false /* animate */); | 236 SelectPage(transition_.target_page, false /* animate */); |
237 } else if (transition_animation_->GetCurrentValue() == 0) { | 237 } else if (transition_animation_->GetCurrentValue() == 0) { |
238 // Hiding animation ends. No page change should happen. | 238 // Hiding animation ends. No page change should happen. |
239 ResetTransitionAnimation(); | 239 ResetTransitionAnimation(); |
240 } | 240 } |
241 | 241 |
242 if (next_target >= 0) | 242 if (next_target >= 0) |
243 SelectPage(next_target, true); | 243 SelectPage(next_target, true); |
244 } | 244 } |
245 | 245 |
246 } // namespace app_list | 246 } // namespace app_list |
OLD | NEW |