OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 import '../animation/animated_value.dart'; | |
6 import '../animation/curves.dart'; | |
7 import '../fn.dart'; | |
8 import '../theme/view_configuration.dart' as config; | |
9 import 'dart:async'; | |
10 import 'dart:math' as math; | |
11 import '../layout.dart'; | |
12 | |
13 const double _kSplashConfirmedDuration = 350.0; | |
14 const double _kSplashUnconfirmedDuration = config.kDefaultLongPressTimeout; | |
15 const double _kSplashAbortDuration = 100.0; | |
16 const double _kSplashInitialDelay = 0.0; // we could delay initially in case the
user scrolls | |
17 | |
18 double _getSplashTargetSize(Rect rect, double x, double y) { | |
19 return 2.0 * math.max(math.max(x - rect.x, rect.x + rect.width - x), | |
20 math.max(y - rect.y, rect.y + rect.height - y)); | |
21 } | |
22 | |
23 class SplashController { | |
24 final int pointer; | |
25 Stream<String> get onStyleChanged => _styleStream; | |
26 | |
27 final AnimatedValue _size = new AnimatedValue(0.0); | |
28 double _offsetX; | |
29 double _offsetY; | |
30 double _lastSize = 0.0; | |
31 bool _growing = true; | |
32 double _targetSize; | |
33 Stream<String> _styleStream; | |
34 | |
35 void start() { | |
36 _size.animateTo(_targetSize, _kSplashUnconfirmedDuration, curve: easeOut, in
itialDelay: _kSplashInitialDelay); | |
37 } | |
38 | |
39 void confirm() { | |
40 double fractionRemaining = (_targetSize - _size.value) / _targetSize; | |
41 double duration = fractionRemaining * _kSplashConfirmedDuration; | |
42 if (duration <= 0.0) | |
43 return; | |
44 _size.animateTo(_targetSize, duration, curve: easeOut); | |
45 } | |
46 | |
47 void abort() { | |
48 _growing = false; | |
49 double durationRemaining = _size.remainingTime; | |
50 if (durationRemaining <= _kSplashAbortDuration) | |
51 return; | |
52 _size.animateTo(_targetSize, _kSplashAbortDuration, curve: easeOut); | |
53 } | |
54 | |
55 void cancel() { | |
56 _size.stop(); | |
57 } | |
58 | |
59 SplashController(Rect rect, double x, double y, | |
60 { this.pointer, Function onDone }) | |
61 : _offsetX = x - rect.x, | |
62 _offsetY = y - rect.y, | |
63 _targetSize = _getSplashTargetSize(rect, x, y) { | |
64 | |
65 _styleStream = _size.onValueChanged.map((p) { | |
66 if (p == _targetSize) { | |
67 onDone(); | |
68 } | |
69 double size; | |
70 if (_growing) { | |
71 size = p; | |
72 _lastSize = p; | |
73 } else { | |
74 size = _lastSize; | |
75 } | |
76 return ''' | |
77 top: ${_offsetY - size/2}px; | |
78 left: ${_offsetX - size/2}px; | |
79 width: ${size}px; | |
80 height: ${size}px; | |
81 border-radius: ${size}px; | |
82 opacity: ${1.0 - (p / _targetSize)};'''; | |
83 }); | |
84 | |
85 start(); | |
86 } | |
87 } | |
88 | |
89 class InkSplash extends Component { | |
90 static final Style _clipperStyle = new Style(''' | |
91 position: absolute; | |
92 pointer-events: none; | |
93 overflow: hidden; | |
94 top: 0; | |
95 left: 0; | |
96 bottom: 0; | |
97 right: 0;'''); | |
98 | |
99 static final Style _splashStyle = new Style(''' | |
100 position: absolute; | |
101 background-color: rgba(0, 0, 0, 0.2);'''); | |
102 | |
103 Stream<String> onStyleChanged; | |
104 | |
105 double _offsetX; | |
106 double _offsetY; | |
107 String _inlineStyle; | |
108 | |
109 InkSplash(Stream<String> onStyleChanged) | |
110 : onStyleChanged = onStyleChanged, | |
111 super(stateful: true, key: onStyleChanged.hashCode); | |
112 | |
113 bool _listening = false; | |
114 | |
115 void _ensureListening() { | |
116 if (_listening) | |
117 return; | |
118 | |
119 _listening = true; | |
120 | |
121 onStyleChanged.listen((style) { | |
122 setState(() { | |
123 _inlineStyle = style; | |
124 }); | |
125 }); | |
126 } | |
127 | |
128 UINode build() { | |
129 _ensureListening(); | |
130 | |
131 return new Container( | |
132 style: _clipperStyle, | |
133 children: [ | |
134 new Container( | |
135 inlineStyle: _inlineStyle, | |
136 style: _splashStyle | |
137 ) | |
138 ] | |
139 ); | |
140 } | |
141 } | |
OLD | NEW |