scroll_behavior.dart 5.7 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 22 23
  double applyCurve(double scrollOffset, double scrollDelta);
}

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

29
  /// The linear extent of the content inside the scrollable widget
30
  double get contentExtent => _contentExtent;
31
  double _contentExtent;
32

33
  /// The linear extent of the exterior of the scrollable widget
34
  double get containerExtent => _containerExtent;
35
  double _containerExtent;
36

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

54
  /// The minimum value the scroll offset can obtain
55
  double get minScrollOffset;
56 57

  /// The maximum value the scroll offset can obatin
58 59 60
  double get maxScrollOffset;
}

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

  double applyCurve(double scrollOffset, double scrollDelta) {
70
    return (scrollOffset + scrollDelta).clamp(minScrollOffset, maxScrollOffset);
71 72 73
  }
}

74
/// A scroll behavior that does not prevent the user from exeeding scroll bounds
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
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;
  }
}

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

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

  Simulation release(double position, double velocity) {
107
    return _createDefaultScrollSimulation(position, velocity, minScrollOffset, maxScrollOffset);
108 109 110 111 112 113 114 115 116 117
  }

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

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

  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;
  }
}