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
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/input/snap_fling_controller.h"
#include <utility>
#include "cc/input/snap_fling_curve.h"
namespace cc {
SnapFlingController::SnapFlingController(SnapFlingClient* client)
: client_(client), state_(State::kIdle) {}
SnapFlingController::~SnapFlingController() = default;
bool SnapFlingController::FilterEventForSnap(
SnapFlingController::GestureScrollType gesture_scroll_type) {
switch (gesture_scroll_type) {
case GestureScrollType::kBegin: {
ClearSnapFling();
return false;
}
// TODO(sunyunjia): Need to update the existing snap curve if the GSU is
// from a fling boosting event.
case GestureScrollType::kUpdate:
case GestureScrollType::kEnd: {
return state_ == State::kActive || state_ == State::kFinished;
}
}
}
void SnapFlingController::ClearSnapFling() {
if (state_ == State::kActive)
client_->ScrollEndForSnapFling(false /* did_finish */);
curve_.reset();
state_ = State::kIdle;
}
bool SnapFlingController::HandleGestureScrollUpdate(
const SnapFlingController::GestureScrollUpdateInfo& info) {
DCHECK(state_ == State::kIdle || state_ == State::kIgnored);
if (!info.is_in_inertial_phase)
return false;
gfx::Vector2dF ending_displacement =
SnapFlingCurve::EstimateDisplacement(info.delta);
gfx::PointF target_offset, start_offset;
if (!client_->GetSnapFlingInfoAndSetAnimatingSnapTarget(
info.delta, ending_displacement, &start_offset, &target_offset)) {
state_ = State::kIgnored;
return false;
}
if (start_offset == target_offset) {
client_->ScrollEndForSnapFling(true /* did_finish */);
state_ = State::kFinished;
return true;
}
curve_ = std::make_unique<SnapFlingCurve>(start_offset, target_offset,
info.event_time);
state_ = State::kActive;
Animate(info.event_time);
return true;
}
void SnapFlingController::Animate(base::TimeTicks time) {
if (state_ != State::kActive)
return;
if (curve_->IsFinished()) {
client_->ScrollEndForSnapFling(true /* did_finish */);
state_ = State::kFinished;
return;
}
gfx::Vector2dF snapped_delta = curve_->GetScrollDelta(time);
gfx::PointF current_offset = client_->ScrollByForSnapFling(snapped_delta);
curve_->UpdateCurrentOffset(current_offset);
client_->RequestAnimationForSnapFling();
}
void SnapFlingController::SetCurveForTest(
std::unique_ptr<SnapFlingCurve> curve) {
curve_ = std::move(curve);
state_ = State::kActive;
}
} // namespace cc
|