| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:math' as math; | 5 import 'dart:math' as math; |
| 6 import 'dart:sky' as sky; | 6 import 'dart:sky' as sky; |
| 7 | 7 |
| 8 import '../animation/animated_value.dart'; | 8 import '../animation/animation_performance.dart'; |
| 9 import '../animation/curves.dart'; | 9 import '../animation/curves.dart'; |
| 10 import '../rendering/box.dart'; | 10 import '../rendering/box.dart'; |
| 11 import '../rendering/object.dart'; | 11 import '../rendering/object.dart'; |
| 12 import 'basic.dart'; | 12 import 'basic.dart'; |
| 13 import 'widget.dart'; | 13 import 'widget.dart'; |
| 14 | 14 |
| 15 const int _kSplashInitialOpacity = 0x80; | 15 const int _kSplashInitialOpacity = 0x80; |
| 16 const double _kSplashInitialDelay = 0.0; // we could delay initially in case the
user scrolls | 16 const double _kSplashCancelledVelocity = 0.3; |
| 17 const double _kSplashConfirmedVelocity = 0.3; |
| 17 const double _kSplashInitialSize = 0.0; | 18 const double _kSplashInitialSize = 0.0; |
| 18 const double _kSplashConfirmedVelocity = 0.3; | |
| 19 const double _kSplashUnconfirmedVelocity = 0.1; | 19 const double _kSplashUnconfirmedVelocity = 0.1; |
| 20 | 20 |
| 21 double _getSplashTargetSize(Size bounds, Point position) { | 21 double _getSplashTargetSize(Size bounds, Point position) { |
| 22 return math.max(math.max(position.x, bounds.width - position.x), | 22 return math.max(math.max(position.x, bounds.width - position.x), |
| 23 math.max(position.y, bounds.height - position.y)); | 23 math.max(position.y, bounds.height - position.y)); |
| 24 } | 24 } |
| 25 | 25 |
| 26 class InkSplash { | 26 class InkSplash { |
| 27 InkSplash(this.pointer, this.position, this.well) { | 27 InkSplash(this.pointer, this.position, this.well) { |
| 28 _targetRadius = _getSplashTargetSize(well.size, position); | 28 _targetRadius = _getSplashTargetSize(well.size, position); |
| 29 double duration = _targetRadius / _kSplashUnconfirmedVelocity; | 29 _radius = new AnimatedType<double>( |
| 30 _radius = new AnimatedValue(_kSplashInitialSize, onChange: _handleRadiusChan
ge); | 30 _kSplashInitialSize, end: _targetRadius, curve: easeOut); |
| 31 _radius.animateTo(_targetRadius, duration, curve: easeOut, | 31 |
| 32 initialDelay: _kSplashInitialDelay); | 32 _performance = new AnimationPerformance() |
| 33 ..variable = _radius |
| 34 ..duration = new Duration(milliseconds: (_targetRadius / _kSplashUnconfirm
edVelocity).floor()) |
| 35 ..addListener(_handleRadiusChange) |
| 36 ..play(); |
| 33 } | 37 } |
| 34 | 38 |
| 35 final int pointer; | 39 final int pointer; |
| 36 final Point position; | 40 final Point position; |
| 37 final RenderInkWell well; | 41 final RenderInkWell well; |
| 38 | 42 |
| 39 double _targetRadius; | 43 double _targetRadius; |
| 40 AnimatedValue _radius; | 44 double _pinnedRadius; |
| 45 AnimatedType<double> _radius; |
| 46 AnimationPerformance _performance; |
| 47 |
| 48 void _updateVelocity(double velocity) { |
| 49 int duration = (_targetRadius / velocity).floor(); |
| 50 _performance |
| 51 ..duration = new Duration(milliseconds: duration) |
| 52 ..play(); |
| 53 } |
| 41 | 54 |
| 42 void confirm() { | 55 void confirm() { |
| 43 double duration = (_targetRadius - _radius.value) / _kSplashConfirmedVelocit
y; | 56 _updateVelocity(_kSplashConfirmedVelocity); |
| 44 if (duration <= 0.0) | 57 } |
| 45 return; | 58 |
| 46 _radius.animateTo(_targetRadius, duration, curve: easeOut); | 59 void cancel() { |
| 60 _updateVelocity(_kSplashCancelledVelocity); |
| 61 _pinnedRadius = _radius.value; |
| 47 } | 62 } |
| 48 | 63 |
| 49 void _handleRadiusChange() { | 64 void _handleRadiusChange() { |
| 50 if (_radius.value == _targetRadius) | 65 if (_radius.value == _targetRadius) |
| 51 well._splashes.remove(this); | 66 well._splashes.remove(this); |
| 52 well.markNeedsPaint(); | 67 well.markNeedsPaint(); |
| 53 } | 68 } |
| 54 | 69 |
| 55 void paint(PaintingCanvas canvas) { | 70 void paint(PaintingCanvas canvas) { |
| 56 int opacity = (_kSplashInitialOpacity * (1.0 - (_radius.value / _targetRadiu
s))).floor(); | 71 int opacity = (_kSplashInitialOpacity * (1.0 - (_radius.value / _targetRadiu
s))).floor(); |
| 57 sky.Paint paint = new sky.Paint()..color = new sky.Color(opacity << 24); | 72 sky.Paint paint = new sky.Paint()..color = new sky.Color(opacity << 24); |
| 58 canvas.drawCircle(position, _radius.value, paint); | 73 double radius = _pinnedRadius == null ? _radius.value : _pinnedRadius; |
| 74 canvas.drawCircle(position, radius, paint); |
| 59 } | 75 } |
| 60 } | 76 } |
| 61 | 77 |
| 62 class RenderInkWell extends RenderProxyBox { | 78 class RenderInkWell extends RenderProxyBox { |
| 63 bool get createNewDisplayList => true; | 79 bool get createNewDisplayList => true; |
| 64 | 80 |
| 65 RenderInkWell({ RenderBox child }) : super(child); | 81 RenderInkWell({ RenderBox child }) : super(child); |
| 66 | 82 |
| 67 final List<InkSplash> _splashes = new List<InkSplash>(); | 83 final List<InkSplash> _splashes = new List<InkSplash>(); |
| 68 | 84 |
| 69 void handleEvent(sky.Event event, BoxHitTestEntry entry) { | 85 void handleEvent(sky.Event event, BoxHitTestEntry entry) { |
| 70 if (event is sky.GestureEvent) { | 86 if (event is sky.GestureEvent) { |
| 71 switch (event.type) { | 87 switch (event.type) { |
| 72 case 'gesturetapdown': | 88 case 'gesturetapdown': |
| 73 _startSplash(event.primaryPointer, entry.localPosition); | 89 _startSplash(event.primaryPointer, entry.localPosition); |
| 74 break; | 90 break; |
| 75 case 'gesturetap': | 91 case 'gesturetap': |
| 76 _confirmSplash(event.primaryPointer); | 92 _confirmSplash(event.primaryPointer); |
| 77 break; | 93 break; |
| 78 } | 94 } |
| 79 } | 95 } |
| 80 } | 96 } |
| 81 | 97 |
| 82 void _startSplash(int pointer, Point position) { | 98 void _startSplash(int pointer, Point position) { |
| 83 _splashes.add(new InkSplash(pointer, position, this)); | 99 _splashes.add(new InkSplash(pointer, position, this)); |
| 84 markNeedsPaint(); | 100 markNeedsPaint(); |
| 85 } | 101 } |
| 86 | 102 |
| 103 void _forEachSplash(int pointer, Function callback) { |
| 104 _splashes.where((splash) => splash.pointer == pointer) |
| 105 .forEach(callback); |
| 106 } |
| 107 |
| 87 void _confirmSplash(int pointer) { | 108 void _confirmSplash(int pointer) { |
| 88 _splashes.where((splash) => splash.pointer == pointer) | 109 _forEachSplash(pointer, (splash) { splash.confirm(); }); |
| 89 .forEach((splash) { splash.confirm(); }); | |
| 90 markNeedsPaint(); | 110 markNeedsPaint(); |
| 91 } | 111 } |
| 92 | 112 |
| 113 void _cancelSplash(int pointer) { |
| 114 _forEachSplash(pointer, (splash) { splash.cancel(); }); |
| 115 markNeedsPaint(); |
| 116 } |
| 117 |
| 93 void paint(PaintingCanvas canvas, Offset offset) { | 118 void paint(PaintingCanvas canvas, Offset offset) { |
| 94 if (!_splashes.isEmpty) { | 119 if (!_splashes.isEmpty) { |
| 95 canvas.save(); | 120 canvas.save(); |
| 96 canvas.translate(offset.dx, offset.dy); | 121 canvas.translate(offset.dx, offset.dy); |
| 97 canvas.clipRect(Point.origin & size); | 122 canvas.clipRect(Point.origin & size); |
| 98 for (InkSplash splash in _splashes) | 123 for (InkSplash splash in _splashes) |
| 99 splash.paint(canvas); | 124 splash.paint(canvas); |
| 100 canvas.restore(); | 125 canvas.restore(); |
| 101 } | 126 } |
| 102 super.paint(canvas, offset); | 127 super.paint(canvas, offset); |
| 103 } | 128 } |
| 104 } | 129 } |
| 105 | 130 |
| 106 class InkWell extends OneChildRenderObjectWrapper { | 131 class InkWell extends OneChildRenderObjectWrapper { |
| 107 InkWell({ String key, Widget child }) | 132 InkWell({ String key, Widget child }) |
| 108 : super(key: key, child: child); | 133 : super(key: key, child: child); |
| 109 | 134 |
| 110 RenderInkWell get root => super.root; | 135 RenderInkWell get root => super.root; |
| 111 RenderInkWell createNode() => new RenderInkWell(); | 136 RenderInkWell createNode() => new RenderInkWell(); |
| 112 } | 137 } |
| OLD | NEW |