1 // Copyright 2019 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 "cc/animation/scroll_offset_animation_curve_factory.h"
6
7 #include "base/memory/ptr_util.h"
8 #include "cc/animation/timing_function.h"
9 #include "cc/base/features.h"
10
11 namespace cc {
12 namespace {
GetDurationBehaviorFromScrollType(ScrollOffsetAnimationCurveFactory::ScrollType scroll_type)13 ScrollOffsetAnimationCurve::DurationBehavior GetDurationBehaviorFromScrollType(
14 ScrollOffsetAnimationCurveFactory::ScrollType scroll_type) {
15 switch (scroll_type) {
16 case ScrollOffsetAnimationCurveFactory::ScrollType::kProgrammatic:
17 return ScrollOffsetAnimationCurve::DurationBehavior::DELTA_BASED;
18 case ScrollOffsetAnimationCurveFactory::ScrollType::kKeyboard:
19 return ScrollOffsetAnimationCurve::DurationBehavior::CONSTANT;
20 case ScrollOffsetAnimationCurveFactory::ScrollType::kMouseWheel:
21 return ScrollOffsetAnimationCurve::DurationBehavior::INVERSE_DELTA;
22 case ScrollOffsetAnimationCurveFactory::ScrollType::kAutoScroll:
23 NOTREACHED();
24 return ScrollOffsetAnimationCurve::DurationBehavior::DELTA_BASED;
25 }
26 }
27 } // namespace
28
29 // static
30 std::unique_ptr<ScrollOffsetAnimationCurve>
CreateAnimation(const gfx::ScrollOffset & target_value,ScrollType scroll_type)31 ScrollOffsetAnimationCurveFactory::CreateAnimation(
32 const gfx::ScrollOffset& target_value,
33 ScrollType scroll_type) {
34 if (scroll_type == ScrollType::kAutoScroll)
35 return CreateLinearAnimation(target_value);
36
37 if (base::FeatureList::IsEnabled(features::kImpulseScrollAnimations))
38 return CreateImpulseAnimation(target_value);
39
40 return CreateEaseInOutAnimation(
41 target_value, GetDurationBehaviorFromScrollType(scroll_type));
42 }
43
44 // static
45 std::unique_ptr<ScrollOffsetAnimationCurve>
CreateEaseInOutAnimationForTesting(const gfx::ScrollOffset & target_value,ScrollOffsetAnimationCurve::DurationBehavior duration_behavior)46 ScrollOffsetAnimationCurveFactory::CreateEaseInOutAnimationForTesting(
47 const gfx::ScrollOffset& target_value,
48 ScrollOffsetAnimationCurve::DurationBehavior duration_behavior) {
49 return CreateEaseInOutAnimation(target_value, duration_behavior);
50 }
51
52 // static
53 std::unique_ptr<ScrollOffsetAnimationCurve>
CreateLinearAnimationForTesting(const gfx::ScrollOffset & target_value)54 ScrollOffsetAnimationCurveFactory::CreateLinearAnimationForTesting(
55 const gfx::ScrollOffset& target_value) {
56 return CreateLinearAnimation(target_value);
57 }
58
59 // static
60 std::unique_ptr<ScrollOffsetAnimationCurve>
CreateImpulseAnimationForTesting(const gfx::ScrollOffset & target_value)61 ScrollOffsetAnimationCurveFactory::CreateImpulseAnimationForTesting(
62 const gfx::ScrollOffset& target_value) {
63 return CreateImpulseAnimation(target_value);
64 }
65
66 // static
67 std::unique_ptr<ScrollOffsetAnimationCurve>
CreateEaseInOutAnimation(const gfx::ScrollOffset & target_value,ScrollOffsetAnimationCurve::DurationBehavior duration_behavior)68 ScrollOffsetAnimationCurveFactory::CreateEaseInOutAnimation(
69 const gfx::ScrollOffset& target_value,
70 ScrollOffsetAnimationCurve::DurationBehavior duration_behavior) {
71 return base::WrapUnique(new ScrollOffsetAnimationCurve(
72 target_value, ScrollOffsetAnimationCurve::AnimationType::kEaseOutNatural,
73 duration_behavior));
74 }
75
76 // static
77 std::unique_ptr<ScrollOffsetAnimationCurve>
CreateLinearAnimation(const gfx::ScrollOffset & target_value)78 ScrollOffsetAnimationCurveFactory::CreateLinearAnimation(
79 const gfx::ScrollOffset& target_value) {
80 return base::WrapUnique(new ScrollOffsetAnimationCurve(
81 target_value, ScrollOffsetAnimationCurve::AnimationType::kLinear));
82 }
83
84 // static
85 std::unique_ptr<ScrollOffsetAnimationCurve>
CreateImpulseAnimation(const gfx::ScrollOffset & target_value)86 ScrollOffsetAnimationCurveFactory::CreateImpulseAnimation(
87 const gfx::ScrollOffset& target_value) {
88 return base::WrapUnique(new ScrollOffsetAnimationCurve(
89 target_value, ScrollOffsetAnimationCurve::AnimationType::kImpulse));
90 }
91 } // namespace cc
92