| 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 '../animation/animated_value.dart'; |
| 6 import '../animation/curves.dart'; |
| 5 import '../fn2.dart'; | 7 import '../fn2.dart'; |
| 8 import '../rendering/box.dart'; |
| 6 import '../rendering/flex.dart'; | 9 import '../rendering/flex.dart'; |
| 10 import '../rendering/object.dart'; |
| 11 import '../theme/view_configuration.dart' as config; |
| 12 import 'dart:async'; |
| 7 import 'dart:collection'; | 13 import 'dart:collection'; |
| 14 import 'dart:math' as math; |
| 8 import 'dart:sky' as sky; | 15 import 'dart:sky' as sky; |
| 9 // import 'ink_splash.dart'; | |
| 10 import 'scrollable.dart'; | |
| 11 | 16 |
| 12 class InkWell extends Component implements ScrollClient { | 17 const int _kSplashInitialOpacity = 0x80; |
| 18 const double _kSplashInitialSize = 0.0; |
| 19 const double _kSplashConfirmedDuration = 350.0; |
| 20 const double _kSplashUnconfirmedDuration = config.kDefaultLongPressTimeout; |
| 21 const double _kSplashInitialDelay = 0.0; // we could delay initially in case the
user scrolls |
| 22 |
| 23 double _getSplashTargetSize(Size bounds, Point position) { |
| 24 return 2.0 * math.max(math.max(position.x, bounds.width - position.x), |
| 25 math.max(position.y, bounds.height - position.y)); |
| 26 } |
| 27 |
| 28 class InkSplash { |
| 29 final int pointer; |
| 30 final Point position; |
| 31 final RenderInkWell well; |
| 32 |
| 33 double _targetRadius; |
| 34 AnimatedValue _radius; |
| 35 |
| 36 InkSplash(this.pointer, this.position, this.well) { |
| 37 _targetRadius = _getSplashTargetSize(well.size, position); |
| 38 _radius = new AnimatedValue(_kSplashInitialSize, onChange: _handleRadiusChan
ge); |
| 39 _radius.animateTo(_targetRadius, _kSplashUnconfirmedDuration, |
| 40 curve: easeOut, initialDelay: _kSplashInitialDelay); |
| 41 } |
| 42 |
| 43 void confirm() { |
| 44 double fractionRemaining = (_targetRadius - _radius.value) / _targetRadius; |
| 45 double duration = fractionRemaining * _kSplashConfirmedDuration; |
| 46 if (duration <= 0.0) |
| 47 return; |
| 48 _radius.animateTo(_targetRadius, duration, curve: easeOut); |
| 49 } |
| 50 |
| 51 void _handleRadiusChange() { |
| 52 if (_radius.value == _targetRadius) |
| 53 well._splashes.remove(this); |
| 54 well.markNeedsPaint(); |
| 55 } |
| 56 |
| 57 void paint(RenderObjectDisplayList canvas) { |
| 58 int opacity = (_kSplashInitialOpacity * (1.0 - (_radius.value / _targetRadiu
s))).floor(); |
| 59 sky.Paint paint = new sky.Paint()..color = new sky.Color(opacity << 24); |
| 60 canvas.drawCircle(position.x, position.y, _radius.value, paint); |
| 61 } |
| 62 } |
| 63 |
| 64 class RenderInkWell extends RenderProxyBox { |
| 65 final List<InkSplash> _splashes = new List<InkSplash>(); |
| 66 |
| 67 RenderInkWell({ RenderBox child }) : super(child); |
| 68 |
| 69 void handleEvent(sky.Event event) { |
| 70 switch (event.type) { |
| 71 case 'gesturetapdown': |
| 72 // TODO(abarth): We should position the splash at the location of the ta
p. |
| 73 _startSplash(event.primaryPointer, new Point(size.width / 2.0, size.heig
ht / 2.0)); |
| 74 break; |
| 75 case 'gesturetap': |
| 76 _confirmSplash(event.primaryPointer); |
| 77 break; |
| 78 } |
| 79 } |
| 80 |
| 81 void _startSplash(int pointer, Point position) { |
| 82 _splashes.add(new InkSplash(pointer, position, this)); |
| 83 markNeedsPaint(); |
| 84 } |
| 85 |
| 86 void _confirmSplash(int pointer) { |
| 87 _splashes.where((splash) => splash.pointer == pointer) |
| 88 .forEach((splash) { splash.confirm(); }); |
| 89 markNeedsPaint(); |
| 90 } |
| 91 |
| 92 void paint(RenderObjectDisplayList canvas) { |
| 93 if (!_splashes.isEmpty) { |
| 94 canvas.save(); |
| 95 canvas.clipRect(new Rect.fromSize(size)); |
| 96 for (InkSplash splash in _splashes) |
| 97 splash.paint(canvas); |
| 98 canvas.restore(); |
| 99 } |
| 100 super.paint(canvas); |
| 101 } |
| 102 } |
| 103 |
| 104 class InkWellWrapper extends OneChildRenderObjectWrapper { |
| 105 RenderInkWell root; |
| 106 |
| 107 InkWellWrapper({ UINode child, Object key }) |
| 108 : super(child: child, key: key); |
| 109 |
| 110 RenderInkWell createNode() => new RenderInkWell(); |
| 111 } |
| 112 |
| 113 class InkWell extends Component { |
| 114 List<UINode> children; |
| 13 | 115 |
| 14 InkWell({ Object key, this.children }) : super(key: key); | 116 InkWell({ Object key, this.children }) : super(key: key); |
| 15 | 117 |
| 16 // static final Style _containmentStyleHack = new Style(''' | |
| 17 // align-items: center; | |
| 18 // transform: translateX(0);'''); | |
| 19 | |
| 20 // LinkedHashSet<SplashController> _splashes; | |
| 21 | |
| 22 List<UINode> children; | |
| 23 | |
| 24 // InkWell({ Object key, this.inlineStyle, this.children }) | |
| 25 // : super(key: key) { | |
| 26 // onDidUnmount(() { | |
| 27 // _cancelSplashes(null); | |
| 28 // }); | |
| 29 // } | |
| 30 | |
| 31 UINode build() { | 118 UINode build() { |
| 32 return new FlexContainer( | 119 return new InkWellWrapper( |
| 33 direction: FlexDirection.horizontal, | 120 child: new FlexContainer( |
| 34 justifyContent: FlexJustifyContent.center, | 121 direction: FlexDirection.horizontal, |
| 35 children: children); | 122 justifyContent: FlexJustifyContent.center, |
| 36 // List<UINode> childrenIncludingSplashes = []; | 123 children: children |
| 37 | 124 ) |
| 38 // if (_splashes != null) { | 125 ); |
| 39 // childrenIncludingSplashes.addAll( | |
| 40 // _splashes.map((s) => new InkSplash(s.onStyleChanged))); | |
| 41 // } | |
| 42 | |
| 43 // if (children != null) | |
| 44 // childrenIncludingSplashes.addAll(children); | |
| 45 | |
| 46 // return new EventListenerNode( | |
| 47 // new FlexContainer( | |
| 48 // direction: FlexDirection.horizontal, | |
| 49 // style: _containmentStyleHack, | |
| 50 // inlineStyle: inlineStyle, | |
| 51 // children: childrenIncludingSplashes), | |
| 52 // onGestureTapDown: _startSplash, | |
| 53 // onGestureTap: _confirmSplash | |
| 54 // ); | |
| 55 } | 126 } |
| 56 | |
| 57 // void _startSplash(sky.GestureEvent event) { | |
| 58 // setState(() { | |
| 59 // if (_splashes == null) | |
| 60 // _splashes = new LinkedHashSet<SplashController>(); | |
| 61 // var splash; | |
| 62 // var root = getRoot(); | |
| 63 // splash = new SplashController(root.rect, event.x, event.y, | |
| 64 // pointer: event.primaryPointer, | |
| 65 // onDone: () { _splashDone(splash); }); | |
| 66 // _splashes.add(splash); | |
| 67 // UINode node = parent; | |
| 68 // while (node != null) { | |
| 69 // if (node is Scrollable) | |
| 70 // node.registerScrollClient(this); | |
| 71 // node = node.parent; | |
| 72 // } | |
| 73 // }); | |
| 74 // } | |
| 75 | |
| 76 bool ancestorScrolled(Scrollable ancestor) { | |
| 77 // _abortSplashes(); | |
| 78 return false; | |
| 79 } | |
| 80 | |
| 81 // void handleRemoved() { | |
| 82 // UINode node = parent; | |
| 83 // while (node != null) { | |
| 84 // if (node is Scrollable) | |
| 85 // node.unregisterScrollClient(this); | |
| 86 // node = node.parent; | |
| 87 // } | |
| 88 // super.handleRemoved(); | |
| 89 // } | |
| 90 | |
| 91 // void _confirmSplash(sky.GestureEvent event) { | |
| 92 // if (_splashes == null) | |
| 93 // return; | |
| 94 // _splashes.where((splash) => splash.pointer == event.primaryPointer) | |
| 95 // .forEach((splash) { splash.confirm(); }); | |
| 96 // } | |
| 97 | |
| 98 // void _abortSplashes() { | |
| 99 // if (_splashes == null) | |
| 100 // return; | |
| 101 // setState(() { | |
| 102 // _splashes.forEach((s) { s.abort(); }); | |
| 103 // }); | |
| 104 // } | |
| 105 | |
| 106 // void _cancelSplashes(sky.Event event) { | |
| 107 // if (_splashes == null) | |
| 108 // return; | |
| 109 // setState(() { | |
| 110 // var splashes = _splashes; | |
| 111 // _splashes = null; | |
| 112 // splashes.forEach((s) { s.cancel(); }); | |
| 113 // }); | |
| 114 // } | |
| 115 | |
| 116 // void _splashDone(SplashController splash) { | |
| 117 // if (_splashes == null) | |
| 118 // return; | |
| 119 // setState(() { | |
| 120 // _splashes.remove(splash); | |
| 121 // if (_splashes.length == 0) | |
| 122 // _splashes = null; | |
| 123 // }); | |
| 124 // } | |
| 125 | |
| 126 } | 127 } |
| OLD | NEW |