1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/app_list/pagination_model.h"
#include <algorithm>
#include "ui/app_list/pagination_model_observer.h"
#include "ui/gfx/animation/slide_animation.h"
namespace app_list {
PaginationModel::PaginationModel()
: total_pages_(-1),
selected_page_(-1),
transition_(-1, 0),
pending_selected_page_(-1),
transition_duration_ms_(0),
overscroll_transition_duration_ms_(0),
last_overscroll_target_page_(0) {
}
PaginationModel::~PaginationModel() {
}
void PaginationModel::SetTotalPages(int total_pages) {
if (total_pages == total_pages_)
return;
total_pages_ = total_pages;
if (selected_page_ < 0)
SelectPage(0, false /* animate */);
if (selected_page_ >= total_pages_)
SelectPage(std::max(total_pages_ - 1, 0), false /* animate */);
FOR_EACH_OBSERVER(PaginationModelObserver, observers_, TotalPagesChanged());
}
void PaginationModel::SelectPage(int page, bool animate) {
if (animate) {
// -1 and |total_pages_| are valid target page for animation.
DCHECK(page >= -1 && page <= total_pages_);
if (!transition_animation_) {
if (page == selected_page_)
return;
// Suppress over scroll animation if the same one happens too fast.
if (!is_valid_page(page)) {
const base::TimeTicks now = base::TimeTicks::Now();
if (page == last_overscroll_target_page_) {
const int kMinOverScrollTimeGapInMs = 500;
const base::TimeDelta time_elapsed =
now - last_overscroll_animation_start_time_;
if (time_elapsed.InMilliseconds() < kMinOverScrollTimeGapInMs)
return;
}
last_overscroll_target_page_ = page;
last_overscroll_animation_start_time_ = now;
}
// Creates an animation if there is not one.
StartTransitionAnimation(Transition(page, 0));
return;
} else {
const bool showing = transition_animation_->IsShowing();
const int from_page = showing ? selected_page_ : transition_.target_page;
const int to_page = showing ? transition_.target_page : selected_page_;
if (from_page == page) {
if (showing)
transition_animation_->Hide();
else
transition_animation_->Show();
pending_selected_page_ = -1;
} else if (to_page != page) {
pending_selected_page_ = page;
} else {
pending_selected_page_ = -1;
}
}
} else {
DCHECK(total_pages_ == 0 || (page >= 0 && page < total_pages_));
if (page == selected_page_)
return;
ResetTransitionAnimation();
int old_selected = selected_page_;
selected_page_ = page;
NotifySelectedPageChanged(old_selected, selected_page_);
}
}
void PaginationModel::SelectPageRelative(int delta, bool animate) {
SelectPage(CalculateTargetPage(delta), animate);
}
void PaginationModel::FinishAnimation() {
SelectPage(SelectedTargetPage(), false);
}
void PaginationModel::SetTransition(const Transition& transition) {
// -1 and |total_pages_| is a valid target page, which means user is at
// the end and there is no target page for this scroll.
DCHECK(transition.target_page >= -1 &&
transition.target_page <= total_pages_);
DCHECK(transition.progress >= 0 && transition.progress <= 1);
if (transition_.Equals(transition))
return;
transition_ = transition;
NotifyTransitionChanged();
}
void PaginationModel::SetTransitionDurations(int duration_ms,
int overscroll_duration_ms) {
transition_duration_ms_ = duration_ms;
overscroll_transition_duration_ms_ = overscroll_duration_ms;
}
void PaginationModel::StartScroll() {
// Cancels current transition animation (if any).
transition_animation_.reset();
}
void PaginationModel::UpdateScroll(double delta) {
// Translates scroll delta to desired page change direction.
int page_change_dir = delta > 0 ? -1 : 1;
// Initializes a transition if there is none.
if (!has_transition())
transition_.target_page = CalculateTargetPage(page_change_dir);
// Updates transition progress.
int transition_dir = transition_.target_page > selected_page_ ? 1 : -1;
double progress = transition_.progress +
fabs(delta) * page_change_dir * transition_dir;
if (progress < 0) {
if (transition_.progress) {
transition_.progress = 0;
NotifyTransitionChanged();
}
clear_transition();
} else if (progress > 1) {
if (is_valid_page(transition_.target_page)) {
SelectPage(transition_.target_page, false);
clear_transition();
}
} else {
transition_.progress = progress;
NotifyTransitionChanged();
}
}
void PaginationModel::EndScroll(bool cancel) {
if (!has_transition())
return;
StartTransitionAnimation(transition_);
if (cancel)
transition_animation_->Hide();
}
bool PaginationModel::IsRevertingCurrentTransition() const {
// Use !IsShowing() so that we return true at the end of hide animation.
return transition_animation_ && !transition_animation_->IsShowing();
}
void PaginationModel::AddObserver(PaginationModelObserver* observer) {
observers_.AddObserver(observer);
}
void PaginationModel::RemoveObserver(PaginationModelObserver* observer) {
observers_.RemoveObserver(observer);
}
int PaginationModel::SelectedTargetPage() const {
// If no animation, or animation is in reverse, just the selected page.
if (!transition_animation_ || !transition_animation_->IsShowing())
return selected_page_;
// If, at the end of the current animation, we will animate to another page,
// return that eventual page.
if (pending_selected_page_ >= 0)
return pending_selected_page_;
// Just the target of the current animation.
return transition_.target_page;
}
void PaginationModel::NotifySelectedPageChanged(int old_selected,
int new_selected) {
FOR_EACH_OBSERVER(PaginationModelObserver,
observers_,
SelectedPageChanged(old_selected, new_selected));
}
void PaginationModel::NotifyTransitionStarted() {
FOR_EACH_OBSERVER(PaginationModelObserver, observers_, TransitionStarted());
}
void PaginationModel::NotifyTransitionChanged() {
FOR_EACH_OBSERVER(PaginationModelObserver, observers_, TransitionChanged());
}
int PaginationModel::CalculateTargetPage(int delta) const {
DCHECK_GT(total_pages_, 0);
const int target_page = SelectedTargetPage() + delta;
int start_page = 0;
int end_page = total_pages_ - 1;
// Use invalid page when |selected_page_| is at ends.
if (target_page < start_page && selected_page_ == start_page)
start_page = -1;
else if (target_page > end_page && selected_page_ == end_page)
end_page = total_pages_;
return std::max(start_page, std::min(end_page, target_page));
}
void PaginationModel::StartTransitionAnimation(const Transition& transition) {
DCHECK(selected_page_ != transition.target_page);
NotifyTransitionStarted();
SetTransition(transition);
transition_animation_.reset(new gfx::SlideAnimation(this));
transition_animation_->SetTweenType(gfx::Tween::FAST_OUT_SLOW_IN);
transition_animation_->Reset(transition_.progress);
const int duration = is_valid_page(transition_.target_page) ?
transition_duration_ms_ : overscroll_transition_duration_ms_;
if (duration)
transition_animation_->SetSlideDuration(duration);
transition_animation_->Show();
}
void PaginationModel::ResetTransitionAnimation() {
transition_animation_.reset();
transition_.target_page = -1;
transition_.progress = 0;
pending_selected_page_ = -1;
}
void PaginationModel::AnimationProgressed(const gfx::Animation* animation) {
transition_.progress = transition_animation_->GetCurrentValue();
NotifyTransitionChanged();
}
void PaginationModel::AnimationEnded(const gfx::Animation* animation) {
// Save |pending_selected_page_| because SelectPage resets it.
int next_target = pending_selected_page_;
if (transition_animation_->GetCurrentValue() == 1) {
// Showing animation ends.
if (!is_valid_page(transition_.target_page)) {
// If target page is not in valid range, reverse the animation.
transition_animation_->Hide();
return;
}
// Otherwise, change page and finish the transition.
DCHECK(selected_page_ != transition_.target_page);
SelectPage(transition_.target_page, false /* animate */);
} else if (transition_animation_->GetCurrentValue() == 0) {
// Hiding animation ends. No page change should happen.
ResetTransitionAnimation();
}
if (next_target >= 0)
SelectPage(next_target, true);
}
} // namespace app_list
|