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 'animated_component.dart'; | 5 import 'animated_component.dart'; |
6 import '../animation/animated_value.dart'; | 6 import '../animation/animated_value.dart'; |
7 import '../animation/curves.dart'; | 7 import '../animation/curves.dart'; |
8 import '../fn.dart'; | 8 import '../fn.dart'; |
9 import '../theme/colors.dart'; | 9 import '../theme/colors.dart'; |
10 import 'dart:math' as math; | 10 import 'dart:math' as math; |
11 import 'dart:sky' as sky; | 11 import 'dart:sky' as sky; |
12 import 'material.dart'; | 12 import 'material.dart'; |
13 | 13 |
14 const double _kWidth = 304.0; | 14 const double _kWidth = 304.0; |
15 const double _kMinFlingVelocity = 0.4; | 15 const double _kMinFlingVelocity = 0.4; |
16 const double _kBaseSettleDurationMS = 246.0; | 16 const double _kBaseSettleDurationMS = 246.0; |
17 const double _kMaxSettleDurationMS = 600.0; | 17 const double _kMaxSettleDurationMS = 600.0; |
18 const Curve _kAnimationCurve = parabolicRise; | 18 const Curve _kAnimationCurve = parabolicRise; |
19 | 19 |
| 20 typedef void DrawerStatusChangeHandler (bool showing); |
| 21 |
20 class DrawerController { | 22 class DrawerController { |
21 final AnimatedValue position = new AnimatedValue(-_kWidth); | 23 |
| 24 DrawerController(this.onStatusChange) { |
| 25 position = new AnimatedValue(-_kWidth, onChange: _checkValue); |
| 26 } |
| 27 final DrawerStatusChangeHandler onStatusChange; |
| 28 AnimatedValue position; |
| 29 |
| 30 bool _oldClosedState = true; |
| 31 void _checkValue() { |
| 32 var newClosedState = isClosed; |
| 33 if (onStatusChange != null && _oldClosedState != newClosedState) { |
| 34 onStatusChange(!newClosedState); |
| 35 _oldClosedState = newClosedState; |
| 36 } |
| 37 } |
22 | 38 |
23 bool get isClosed => position.value == -_kWidth; | 39 bool get isClosed => position.value == -_kWidth; |
24 | |
25 bool get _isMostlyClosed => position.value <= -_kWidth / 2; | 40 bool get _isMostlyClosed => position.value <= -_kWidth / 2; |
26 | |
27 void toggle(_) => _isMostlyClosed ? _open() : _close(); | 41 void toggle(_) => _isMostlyClosed ? _open() : _close(); |
28 | 42 |
29 void handleMaskTap(_) => _close(); | 43 void handleMaskTap(_) => _close(); |
30 | |
31 void handlePointerDown(_) => position.stop(); | 44 void handlePointerDown(_) => position.stop(); |
32 | 45 |
33 void handlePointerMove(sky.PointerEvent event) { | 46 void handlePointerMove(sky.PointerEvent event) { |
34 if (position.isAnimating) | 47 if (position.isAnimating) |
35 return; | 48 return; |
36 position.value = math.min(0.0, math.max(position.value + event.dx, -_kWidth)
); | 49 position.value = math.min(0.0, math.max(position.value + event.dx, -_kWidth)
); |
37 } | 50 } |
38 | 51 |
39 void handlePointerUp(_) { | 52 void handlePointerUp(_) { |
40 if (!position.isAnimating) | 53 if (!position.isAnimating) |
(...skipping 23 matching lines...) Expand all Loading... |
64 void handleFlingStart(event) { | 77 void handleFlingStart(event) { |
65 double direction = event.velocityX.sign; | 78 double direction = event.velocityX.sign; |
66 double velocityX = event.velocityX.abs() / 1000; | 79 double velocityX = event.velocityX.abs() / 1000; |
67 if (velocityX < _kMinFlingVelocity) | 80 if (velocityX < _kMinFlingVelocity) |
68 return; | 81 return; |
69 | 82 |
70 double targetPosition = direction < 0.0 ? -_kWidth : 0.0; | 83 double targetPosition = direction < 0.0 ? -_kWidth : 0.0; |
71 double distance = (targetPosition - position.value).abs(); | 84 double distance = (targetPosition - position.value).abs(); |
72 double duration = distance / velocityX; | 85 double duration = distance / velocityX; |
73 | 86 |
74 position.animateTo(targetPosition, duration, curve: linear); | 87 if (distance > 0) |
| 88 position.animateTo(targetPosition, duration, curve: linear); |
75 } | 89 } |
76 } | 90 } |
77 | 91 |
78 class Drawer extends AnimatedComponent { | 92 class Drawer extends AnimatedComponent { |
79 // TODO(abarth): We need a better way to become a container for absolutely | 93 // TODO(abarth): We need a better way to become a container for absolutely |
80 // positioned elements. | 94 // positioned elements. |
81 static final Style _style = new Style(''' | 95 static final Style _style = new Style(''' |
82 transform: translateX(0);'''); | 96 transform: translateX(0);'''); |
83 | 97 |
84 static final Style _maskStyle = new Style(''' | 98 static final Style _maskStyle = new Style(''' |
(...skipping 25 matching lines...) Expand all Loading... |
110 Drawer({ | 124 Drawer({ |
111 Object key, | 125 Object key, |
112 this.controller, | 126 this.controller, |
113 this.children, | 127 this.children, |
114 this.level: 0 | 128 this.level: 0 |
115 }) : super(key: key) { | 129 }) : super(key: key) { |
116 animateField(controller.position, #_position); | 130 animateField(controller.position, #_position); |
117 } | 131 } |
118 | 132 |
119 UINode build() { | 133 UINode build() { |
120 bool isClosed = _position <= -_kWidth; | |
121 String inlineStyle = 'display: ${isClosed ? 'none' : ''}'; | |
122 String maskInlineStyle = 'opacity: ${(_position / _kWidth + 1) * 0.5}'; | 134 String maskInlineStyle = 'opacity: ${(_position / _kWidth + 1) * 0.5}'; |
123 String contentInlineStyle = 'transform: translateX(${_position}px)'; | 135 String contentInlineStyle = 'transform: translateX(${_position}px)'; |
124 | 136 |
125 var mask = new EventListenerNode( | 137 var mask = new EventListenerNode( |
126 new Container( | 138 new Container( |
127 style: _maskStyle, | 139 style: _maskStyle, |
128 inlineStyle: maskInlineStyle | 140 inlineStyle: maskInlineStyle |
129 ), | 141 ), |
130 onGestureTap: controller.handleMaskTap, | 142 onGestureTap: controller.handleMaskTap, |
131 onGestureFlingStart: controller.handleFlingStart | 143 onGestureFlingStart: controller.handleFlingStart |
132 ); | 144 ); |
133 | 145 |
134 Material content = new Material( | 146 Material content = new Material( |
135 content: new Container( | 147 content: new Container( |
136 style: _contentStyle, | 148 style: _contentStyle, |
137 inlineStyle: contentInlineStyle, | 149 inlineStyle: contentInlineStyle, |
138 children: children | 150 children: children |
139 ), | 151 ), |
140 level: level); | 152 level: level); |
141 | 153 |
142 return new EventListenerNode( | 154 return new EventListenerNode( |
143 new Container( | 155 new Container( |
144 style: _style, | 156 style: _style, |
145 inlineStyle: inlineStyle, | |
146 children: [ mask, content ] | 157 children: [ mask, content ] |
147 ), | 158 ), |
148 onPointerDown: controller.handlePointerDown, | 159 onPointerDown: controller.handlePointerDown, |
149 onPointerMove: controller.handlePointerMove, | 160 onPointerMove: controller.handlePointerMove, |
150 onPointerUp: controller.handlePointerUp, | 161 onPointerUp: controller.handlePointerUp, |
151 onPointerCancel: controller.handlePointerCancel | 162 onPointerCancel: controller.handlePointerCancel |
152 ); | 163 ); |
153 } | 164 } |
154 } | 165 } |
OLD | NEW |