scroll_behavior.dart 5.79 KB
Newer Older
1 2 3 4 5 6 7 8 9
// 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;

import 'package:newton/newton.dart';

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

12
/// An interface for controlling the behavior of scrollable widgets
13
abstract class ScrollBehavior {
14 15 16 17
  /// A simulation to run to determine the scroll offset
  ///
  /// Called when the user stops scrolling at a given position with a given
  /// instantaneous velocity.
18 19
  Simulation release(double position, double velocity) => null;

20
  /// The new scroll offset to use when the user attempts to scroll from the given offset by the given delta
21
  double applyCurve(double scrollOffset, double scrollDelta);
22 23 24

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

27
/// A scroll behavior for a scrollable widget with linear extent
28 29 30
abstract class ExtentScrollBehavior extends ScrollBehavior {
  ExtentScrollBehavior({ double contentExtent: 0.0, double containerExtent: 0.0 })
    : _contentExtent = contentExtent, _containerExtent = containerExtent;
31

32
  /// The linear extent of the content inside the scrollable widget
33
  double get contentExtent => _contentExtent;
34
  double _contentExtent;
35

36
  /// The linear extent of the exterior of the scrollable widget
37
  double get containerExtent => _containerExtent;
38
  double _containerExtent;
39

40 41 42 43 44
  /// 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.
45
  double updateExtents({
46 47
    double contentExtent,
    double containerExtent,
48 49
    double scrollOffset: 0.0
  }) {
50 51 52 53
    if (contentExtent != null)
      _contentExtent = contentExtent;
    if (containerExtent != null)
      _containerExtent = containerExtent;
54
    return scrollOffset.clamp(minScrollOffset, maxScrollOffset);
55 56
  }

57
  /// The minimum value the scroll offset can obtain
58
  double get minScrollOffset;
59 60

  /// The maximum value the scroll offset can obatin
61 62 63
  double get maxScrollOffset;
}

64
/// A scroll behavior that prevents the user from exeeding scroll bounds
65 66 67 68 69 70
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);
71 72

  double applyCurve(double scrollOffset, double scrollDelta) {
73
    return (scrollOffset + scrollDelta).clamp(minScrollOffset, maxScrollOffset);
74 75 76
  }
}

77
/// A scroll behavior that does not prevent the user from exeeding scroll bounds
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
class UnboundedBehavior extends ExtentScrollBehavior {
  UnboundedBehavior({ double contentExtent: 0.0, double containerExtent: 0.0 })
    : super(contentExtent: contentExtent, containerExtent: containerExtent);

  Simulation release(double position, double velocity) {
    double velocityPerSecond = velocity * 1000.0;
    return new BoundedFrictionSimulation(
      _kScrollDrag, position, velocityPerSecond, double.NEGATIVE_INFINITY, double.INFINITY
    );
  }

  double get minScrollOffset => double.NEGATIVE_INFINITY;
  double get maxScrollOffset => double.INFINITY;

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

97
Simulation _createDefaultScrollSimulation(double position, double velocity, double minScrollOffset, double maxScrollOffset) {
98 99 100
  double velocityPerSecond = velocity * _kSecondsPerMillisecond;
  SpringDescription spring = new SpringDescription.withDampingRatio(
      mass: 1.0, springConstant: 170.0, ratio: 1.1);
101
  return new ScrollSimulation(position, velocityPerSecond, minScrollOffset, maxScrollOffset, spring, _kScrollDrag);
102 103
}

104
/// A scroll behavior that lets the user scroll beyond the scroll bounds with some resistance
105
class OverscrollBehavior extends BoundedBehavior {
106 107
  OverscrollBehavior({ double contentExtent: 0.0, double containerExtent: 0.0 })
    : super(contentExtent: contentExtent, containerExtent: containerExtent);
108 109

  Simulation release(double position, double velocity) {
110
    return _createDefaultScrollSimulation(position, velocity, minScrollOffset, maxScrollOffset);
111 112 113 114 115 116 117 118 119 120
  }

  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.
121 122
    if (newScrollOffset < minScrollOffset) {
      newScrollOffset -= (newScrollOffset - math.min(minScrollOffset, scrollOffset)) / 2.0;
123 124 125 126 127 128
    } else if (newScrollOffset > maxScrollOffset) {
      newScrollOffset -= (newScrollOffset - math.max(maxScrollOffset, scrollOffset)) / 2.0;
    }
    return newScrollOffset;
  }
}
129

130
/// A scroll behavior that lets the user scroll beyond the scroll bounds only when the bounds are disjoint
131
class OverscrollWhenScrollableBehavior extends OverscrollBehavior {
132
  bool get isScrollable => contentExtent > containerExtent;
133 134 135 136 137 138 139 140 141 142 143 144 145

  Simulation release(double position, double velocity) {
    if (isScrollable || position < minScrollOffset || position > maxScrollOffset)
      return super.release(position, velocity);
    return null;
  }

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