scroll_behavior.dart 7.33 KB
Newer Older
1 2 3 4 5
// Copyright 2015 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.

import 'dart:math' as math;
6
import 'dart:ui' as ui;
7 8 9 10

import 'package:newton/newton.dart';

const double _kSecondsPerMillisecond = 1000.0;
11
const double _kScrollDrag = 0.025;
12

13
/// An interface for controlling the behavior of scrollable widgets
14
abstract class ScrollBehavior {
15 16
  /// Called when a drag gesture ends. Returns a simulation that
  /// propels the scrollOffset.
17
  Simulation createFlingScrollSimulation(double position, double velocity) => null;
18

19 20 21 22 23 24
  /// Called when a drag gesture ends and toSnapOffset is specified.
  /// Returns an animation that ends at the snap offset.
  Simulation createSnapScrollSimulation(double startOffset, double endOffset, double velocity) => null;

  /// Return the scroll offset to use when the user attempts to scroll
  /// from the given offset by the given delta
25
  double applyCurve(double scrollOffset, double scrollDelta);
26 27 28

  /// Whether this scroll behavior currently permits scrolling
  bool get isScrollable => true;
29 30
}

31
/// A scroll behavior for a scrollable widget with linear extent
32 33 34
abstract class ExtentScrollBehavior extends ScrollBehavior {
  ExtentScrollBehavior({ double contentExtent: 0.0, double containerExtent: 0.0 })
    : _contentExtent = contentExtent, _containerExtent = containerExtent;
35

36
  /// The linear extent of the content inside the scrollable widget
37
  double get contentExtent => _contentExtent;
38
  double _contentExtent;
39

40
  /// The linear extent of the exterior of the scrollable widget
41
  double get containerExtent => _containerExtent;
42
  double _containerExtent;
43

44 45 46 47 48
  /// Update either content or container extent (or both)
  ///
  /// The scrollOffset parameter is the scroll offset of the widget before the
  /// change in extent. Returns the new scroll offset of the widget after the
  /// change in extent.
49
  double updateExtents({
50 51
    double contentExtent,
    double containerExtent,
52 53
    double scrollOffset: 0.0
  }) {
54 55 56 57
    if (contentExtent != null)
      _contentExtent = contentExtent;
    if (containerExtent != null)
      _containerExtent = containerExtent;
58
    return scrollOffset.clamp(minScrollOffset, maxScrollOffset);
59 60
  }

61
  /// The minimum value the scroll offset can obtain
62
  double get minScrollOffset;
63 64

  /// The maximum value the scroll offset can obatin
65 66 67
  double get maxScrollOffset;
}

68
/// A scroll behavior that prevents the user from exeeding scroll bounds
69 70 71 72 73 74
class BoundedBehavior extends ExtentScrollBehavior {
  BoundedBehavior({ double contentExtent: 0.0, double containerExtent: 0.0 })
    : super(contentExtent: contentExtent, containerExtent: containerExtent);

  double minScrollOffset = 0.0;
  double get maxScrollOffset => math.max(minScrollOffset, minScrollOffset + _contentExtent - _containerExtent);
75 76

  double applyCurve(double scrollOffset, double scrollDelta) {
77
    return (scrollOffset + scrollDelta).clamp(minScrollOffset, maxScrollOffset);
78 79 80
  }
}

81
/// A scroll behavior that does not prevent the user from exeeding scroll bounds
82 83 84 85
class UnboundedBehavior extends ExtentScrollBehavior {
  UnboundedBehavior({ double contentExtent: 0.0, double containerExtent: 0.0 })
    : super(contentExtent: contentExtent, containerExtent: containerExtent);

86
  Simulation createFlingScrollSimulation(double position, double velocity) {
87 88 89 90 91 92
    double velocityPerSecond = velocity * 1000.0;
    return new BoundedFrictionSimulation(
      _kScrollDrag, position, velocityPerSecond, double.NEGATIVE_INFINITY, double.INFINITY
    );
  }

93 94 95 96
  Simulation createSnapScrollSimulation(double startOffset, double endOffset, double velocity) {
    return _createSnapScrollSimulation(startOffset, endOffset, velocity);
  }

97 98 99 100 101 102 103 104
  double get minScrollOffset => double.NEGATIVE_INFINITY;
  double get maxScrollOffset => double.INFINITY;

  double applyCurve(double scrollOffset, double scrollDelta) {
    return scrollOffset + scrollDelta;
  }
}

105
Simulation _createFlingScrollSimulation(double position, double velocity, double minScrollOffset, double maxScrollOffset) {
106 107 108 109 110 111
  double startVelocity = velocity * _kSecondsPerMillisecond;

  // Assume that we're rendering at atleast 15 FPS. Stop when we're
  // scrolling less than one logical pixel per frame. We're essentially
  // normalizing by the devicePixelRatio so that the threshold has the
  // same effect independent of the device's pixel density.
112
  double endVelocity = 15.0 * ui.window.devicePixelRatio;
113 114 115

  // Similar to endVelocity. Stop scrolling when we're this close to
  // destiniation scroll offset.
116
  double endDistance = 0.5 * ui.window.devicePixelRatio;
117 118

  SpringDescription spring = new SpringDescription.withDampingRatio(mass: 1.0, springConstant: 170.0, ratio: 1.1);
119 120
  ScrollSimulation simulation =
      new ScrollSimulation(position, startVelocity, minScrollOffset, maxScrollOffset, spring, _kScrollDrag)
121
    ..tolerance = new Tolerance(velocity: endVelocity.abs(), distance: endDistance);
122
  return simulation;
123 124
}

125 126
Simulation _createSnapScrollSimulation(double startOffset, double endOffset, double velocity) {
  double startVelocity = velocity * _kSecondsPerMillisecond;
127
  double endVelocity = 15.0 * ui.window.devicePixelRatio * velocity.sign;
128 129 130
  return new FrictionSimulation.through(startOffset, endOffset, startVelocity, endVelocity);
}

131
/// A scroll behavior that lets the user scroll beyond the scroll bounds with some resistance
132
class OverscrollBehavior extends BoundedBehavior {
133 134
  OverscrollBehavior({ double contentExtent: 0.0, double containerExtent: 0.0 })
    : super(contentExtent: contentExtent, containerExtent: containerExtent);
135

136
  Simulation createFlingScrollSimulation(double position, double velocity) {
137 138 139 140 141
    return _createFlingScrollSimulation(position, velocity, minScrollOffset, maxScrollOffset);
  }

  Simulation createSnapScrollSimulation(double startOffset, double endOffset, double velocity) {
    return _createSnapScrollSimulation(startOffset, endOffset, velocity);
142 143 144 145 146 147 148 149 150 151
  }

  double applyCurve(double scrollOffset, double scrollDelta) {
    double newScrollOffset = scrollOffset + scrollDelta;
    // If we're overscrolling, we want move the scroll offset 2x
    // slower than we would otherwise. Therefore, we "rewind" the
    // newScrollOffset by half the amount that we moved it above.
    // Notice that we clamp the "old" value to 0.0 so that we only
    // reduce the portion of scrollDelta that's applied beyond 0.0. We
    // do similar things for overscroll in the other direction.
152 153
    if (newScrollOffset < minScrollOffset) {
      newScrollOffset -= (newScrollOffset - math.min(minScrollOffset, scrollOffset)) / 2.0;
154 155 156 157 158 159
    } else if (newScrollOffset > maxScrollOffset) {
      newScrollOffset -= (newScrollOffset - math.max(maxScrollOffset, scrollOffset)) / 2.0;
    }
    return newScrollOffset;
  }
}
160

161
/// A scroll behavior that lets the user scroll beyond the scroll bounds only when the bounds are disjoint
162
class OverscrollWhenScrollableBehavior extends OverscrollBehavior {
163
  bool get isScrollable => contentExtent > containerExtent;
164

165
  Simulation createFlingScrollSimulation(double position, double velocity) {
166
    if (isScrollable || position < minScrollOffset || position > maxScrollOffset)
167
      return super.createFlingScrollSimulation(position, velocity);
168 169 170 171 172 173 174 175 176
    return null;
  }

  double applyCurve(double scrollOffset, double scrollDelta) {
    if (isScrollable)
      return super.applyCurve(scrollOffset, scrollDelta);
    return minScrollOffset;
  }
}