Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: sky/sdk/lib/widgets/drawer.dart

Issue 1211603003: Baby steps towards an odeon-like animation system. First victim: Drawer. (Closed) Base URL: git@github.com:/domokit/mojo.git@master
Patch Set: hixie Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 'package:vector_math/vector_math.dart'; 8 import 'package:vector_math/vector_math.dart';
9 9
10 import '../animation/animated_value.dart'; 10 import '../animation/animated_value.dart';
11 import '../animation/animation_performance.dart';
11 import '../animation/curves.dart'; 12 import '../animation/curves.dart';
12 import '../theme/colors.dart'; 13 import '../theme/colors.dart';
13 import '../theme/shadows.dart'; 14 import '../theme/shadows.dart';
14 import 'animated_component.dart'; 15 import 'animated_component.dart';
15 import 'basic.dart'; 16 import 'basic.dart';
16 17
17 // TODO(eseidel): Draw width should vary based on device size: 18 // TODO(eseidel): Draw width should vary based on device size:
18 // http://www.google.com/design/spec/layout/structure.html#structure-side-nav 19 // http://www.google.com/design/spec/layout/structure.html#structure-side-nav
19 20
20 // Mobile: 21 // Mobile:
21 // Width = Screen width − 56 dp 22 // Width = Screen width − 56 dp
22 // Maximum width: 320dp 23 // Maximum width: 320dp
23 // Maximum width applies only when using a left nav. When using a right nav, 24 // Maximum width applies only when using a left nav. When using a right nav,
24 // the panel can cover the full width of the screen. 25 // the panel can cover the full width of the screen.
25 26
26 // Desktop/Tablet: 27 // Desktop/Tablet:
27 // Maximum width for a left nav is 400dp. 28 // Maximum width for a left nav is 400dp.
28 // The right nav can vary depending on content. 29 // The right nav can vary depending on content.
29 30
30 const double _kWidth = 304.0; 31 const double _kWidth = 304.0;
31 const double _kMinFlingVelocity = 0.4; 32 const double _kMinFlingVelocity = 0.4;
32 const double _kBaseSettleDurationMS = 246.0; 33 const double _kBaseSettleDurationMS = 246.0;
33 const double _kMaxSettleDurationMS = 600.0; 34 const double _kMaxSettleDurationMS = 600.0;
34 const Curve _kAnimationCurve = parabolicRise; 35 const Curve _kAnimationCurve = parabolicRise;
35 36
36 typedef void DrawerStatusChangeHandler (bool showing); 37 typedef void DrawerStatusChangeHandler (bool showing);
37 38
38 class DrawerController { 39 class DrawerController {
39
40 DrawerController(this.onStatusChange) { 40 DrawerController(this.onStatusChange) {
41 position = new AnimatedValue(-_kWidth, onChange: _checkValue); 41 performance = new AnimationPerformance()
42 ..durationMS = _kBaseSettleDurationMS
43 ..variable = _position;
44 performance.timeline.onValueChanged.listen(_checkValue);
42 } 45 }
43 final DrawerStatusChangeHandler onStatusChange; 46 final DrawerStatusChangeHandler onStatusChange;
44 AnimatedValue position; 47
48 AnimationPerformance performance;
49 final AnimationVariable _position = new AnimationVariable(
50 -_kWidth, 0.0, curve: _kAnimationCurve);
51
52 double get position => _position.value;
45 53
46 bool _oldClosedState = true; 54 bool _oldClosedState = true;
47 void _checkValue() { 55 void _checkValue(_) {
48 var newClosedState = isClosed; 56 var newClosedState = isClosed;
49 if (onStatusChange != null && _oldClosedState != newClosedState) { 57 if (onStatusChange != null && _oldClosedState != newClosedState) {
50 onStatusChange(!newClosedState); 58 onStatusChange(!newClosedState);
51 _oldClosedState = newClosedState; 59 _oldClosedState = newClosedState;
52 } 60 }
53 } 61 }
54 62
55 bool get isClosed => position.value == -_kWidth; 63 bool get isClosed => performance.isDismissed;
56 bool get _isMostlyClosed => position.value <= -_kWidth / 2; 64 bool get _isMostlyClosed => position <= -_kWidth/2;
57 void toggle() => _isMostlyClosed ? open() : close(); 65
66 void open() => performance.present();
67
68 void close() => performance.dismiss();
69
70 void _settle() => _isMostlyClosed ? close() : open();
58 71
59 void handleMaskTap(_) => close(); 72 void handleMaskTap(_) => close();
60 void handlePointerDown(_) => position.stop(); 73
74 // TODO(mpcomplete): Figure out how to generalize these handlers on a
75 // "PannableThingy" interface.
76 void handlePointerDown(_) => performance.stop();
61 77
62 void handlePointerMove(sky.PointerEvent event) { 78 void handlePointerMove(sky.PointerEvent event) {
63 if (position.isAnimating) 79 if (performance.isAnimating)
64 return; 80 return;
65 position.value = math.min(0.0, math.max(position.value + event.dx, -_kWidth) ); 81 performance.percentage += event.dx / _kWidth;
66 } 82 }
67 83
68 void handlePointerUp(_) { 84 void handlePointerUp(_) {
69 if (!position.isAnimating) 85 if (!performance.isAnimating)
70 _settle(); 86 _settle();
71 } 87 }
72 88
73 void handlePointerCancel(_) { 89 void handlePointerCancel(_) {
74 if (!position.isAnimating) 90 if (!performance.isAnimating)
75 _settle(); 91 _settle();
76 } 92 }
77 93
78 void open() => _animateToPosition(0.0); 94 void handleFlingStart(event) {
79 95 double velocityX = event.velocityX / 1000;
80 void close() => _animateToPosition(-_kWidth); 96 if (velocityX.abs() >= _kMinFlingVelocity)
81 97 performance.fling(velocity: velocityX / _kWidth);
82 void _settle() => _isMostlyClosed ? close() : open();
83
84 void _animateToPosition(double targetPosition) {
85 double distance = (targetPosition - position.value).abs();
86 if (distance != 0) {
87 double targetDuration = distance / _kWidth * _kBaseSettleDurationMS;
88 double duration = math.min(targetDuration, _kMaxSettleDurationMS);
89 position.animateTo(targetPosition, duration, curve: _kAnimationCurve);
90 }
91 } 98 }
92
93 void handleFlingStart(event) {
94 double direction = event.velocityX.sign;
95 double velocityX = event.velocityX.abs() / 1000;
96 if (velocityX < _kMinFlingVelocity)
97 return;
98
99 double targetPosition = direction < 0.0 ? -_kWidth : 0.0;
100 double distance = (targetPosition - position.value).abs();
101 double duration = distance / velocityX;
102
103 if (distance > 0)
104 position.animateTo(targetPosition, duration, curve: linear);
105 }
106
107 } 99 }
108 100
109 class Drawer extends AnimatedComponent { 101 class Drawer extends AnimatedComponent {
110
111 Drawer({ 102 Drawer({
112 String key, 103 String key,
113 this.controller, 104 this.controller,
114 this.children, 105 this.children,
115 this.level: 0 106 this.level: 0
116 }) : super(key: key) { 107 }) : super(key: key) {
117 animate(controller.position, (double value) { 108 // TODO(mpcomplete): is |setter| ever needed?
118 _position = value; 109 animate(controller.performance.timeline, (_) {});
119 });
120 } 110 }
121 111
122 List<Widget> children; 112 List<Widget> children;
123 int level; 113 int level;
124 DrawerController controller; 114 DrawerController controller;
125 115
126 void syncFields(Drawer source) { 116 void syncFields(Drawer source) {
127 children = source.children; 117 children = source.children;
128 level = source.level; 118 level = source.level;
129 controller = source.controller; 119 controller = source.controller;
130 super.syncFields(source); 120 super.syncFields(source);
131 } 121 }
132 122
133 double _position; 123 double get _position { return controller.position; }
134 124
125 // TODO(mpcomplete): the animation system should handle building, maybe? Or
126 // at least setting the transform. Figure out how this could work for things
127 // like fades, slides, rotates, pinch, etc.
135 Widget build() { 128 Widget build() {
136 Matrix4 transform = new Matrix4.identity(); 129 Matrix4 transform = new Matrix4.identity();
137 transform.translate(_position); 130 transform.translate(_position);
138 131
139 double scaler = _position / _kWidth + 1; 132 double scaler = _position / _kWidth + 1;
140 Color maskColor = new Color.fromARGB((0x7F * scaler).floor(), 0, 0, 0); 133 Color maskColor = new Color.fromARGB((0x7F * scaler).floor(), 0, 0, 0);
141 134
142 var mask = new Listener( 135 var mask = new Listener(
143 child: new Container(decoration: new BoxDecoration(backgroundColor: maskCo lor)), 136 child: new Container(decoration: new BoxDecoration(backgroundColor: maskCo lor)),
144 onGestureTap: controller.handleMaskTap, 137 onGestureTap: controller.handleMaskTap
145 onGestureFlingStart: controller.handleFlingStart
146 ); 138 );
147 139
148 Container content = new Container( 140 Container content = new Container(
149 decoration: new BoxDecoration( 141 decoration: new BoxDecoration(
150 backgroundColor: Grey[50], 142 backgroundColor: Grey[50],
151 boxShadow: shadows[level]), 143 boxShadow: shadows[level]),
152 width: _kWidth, 144 width: _kWidth,
153 transform: transform, 145 transform: transform,
154 child: new Block(children) 146 child: new Block(children)
155 ); 147 );
156 148
157 return new Listener( 149 return new Listener(
158 child: new Stack([ mask, content ]), 150 child: new Stack([ mask, content ]),
159 onPointerDown: controller.handlePointerDown, 151 onPointerDown: controller.handlePointerDown,
160 onPointerMove: controller.handlePointerMove, 152 onPointerMove: controller.handlePointerMove,
161 onPointerUp: controller.handlePointerUp, 153 onPointerUp: controller.handlePointerUp,
162 onPointerCancel: controller.handlePointerCancel 154 onPointerCancel: controller.handlePointerCancel,
155 onGestureFlingStart: controller.handleFlingStart
163 ); 156 );
164 } 157 }
165 158
166 } 159 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698