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

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: abarth and hixie Created 5 years, 5 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 int _kBaseSettleDurationMS = 246;
33 const double _kMaxSettleDurationMS = 600.0;
34 const Curve _kAnimationCurve = parabolicRise; 34 const Curve _kAnimationCurve = parabolicRise;
35 35
36 typedef void DrawerStatusChangeHandler (bool showing); 36 typedef void DrawerStatusChangeHandler (bool showing);
37 37
38 class DrawerController { 38 class DrawerController {
39
40 DrawerController(this.onStatusChange) { 39 DrawerController(this.onStatusChange) {
41 position = new AnimatedValue(-_kWidth, onChange: _checkValue); 40 performance = new AnimationPerformance()
41 ..duration = new Duration(milliseconds: _kBaseSettleDurationMS)
42 ..variable = position;
43 performance.timeline.onValueChanged.listen(_checkValue);
42 } 44 }
43 final DrawerStatusChangeHandler onStatusChange; 45 final DrawerStatusChangeHandler onStatusChange;
44 AnimatedValue position; 46
47 AnimationPerformance performance;
48 final AnimatedPosition position = new AnimatedPosition(
49 new Point(-_kWidth, 0.0), new Point(0.0, 0.0), curve: _kAnimationCurve);
abarth-chromium 2015/07/01 19:44:09 s/new Point(0.0, 0.0)/Point.origin/
Matt Perry 2015/07/01 20:05:40 Done.
45 50
46 bool _oldClosedState = true; 51 bool _oldClosedState = true;
47 void _checkValue() { 52 void _checkValue(_) {
48 var newClosedState = isClosed; 53 var newClosedState = isClosed;
49 if (onStatusChange != null && _oldClosedState != newClosedState) { 54 if (onStatusChange != null && _oldClosedState != newClosedState) {
50 onStatusChange(!newClosedState); 55 onStatusChange(!newClosedState);
51 _oldClosedState = newClosedState; 56 _oldClosedState = newClosedState;
52 } 57 }
53 } 58 }
54 59
55 bool get isClosed => position.value == -_kWidth; 60 bool get isClosed => performance.isDismissed;
56 bool get _isMostlyClosed => position.value <= -_kWidth / 2; 61 bool get _isMostlyClosed => position.value.x <= -_kWidth/2;
57 void toggle() => _isMostlyClosed ? open() : close(); 62
63 void open() => performance.play();
64
65 void close() => performance.reverse();
66
67 void _settle() => _isMostlyClosed ? close() : open();
58 68
59 void handleMaskTap(_) => close(); 69 void handleMaskTap(_) => close();
60 void handlePointerDown(_) => position.stop(); 70
71 // TODO(mpcomplete): Figure out how to generalize these handlers on a
72 // "PannableThingy" interface.
73 void handlePointerDown(_) => performance.stop();
61 74
62 void handlePointerMove(sky.PointerEvent event) { 75 void handlePointerMove(sky.PointerEvent event) {
63 if (position.isAnimating) 76 if (performance.isAnimating)
64 return; 77 return;
65 position.value = math.min(0.0, math.max(position.value + event.dx, -_kWidth) ); 78 performance.progress += event.dx / _kWidth;
66 } 79 }
67 80
68 void handlePointerUp(_) { 81 void handlePointerUp(_) {
69 if (!position.isAnimating) 82 if (!performance.isAnimating)
70 _settle(); 83 _settle();
71 } 84 }
72 85
73 void handlePointerCancel(_) { 86 void handlePointerCancel(_) {
74 if (!position.isAnimating) 87 if (!performance.isAnimating)
75 _settle(); 88 _settle();
76 } 89 }
77 90
78 void open() => _animateToPosition(0.0); 91 void handleFlingStart(event) {
79 92 double velocityX = event.velocityX / 1000;
80 void close() => _animateToPosition(-_kWidth); 93 if (velocityX.abs() >= _kMinFlingVelocity)
81 94 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 } 95 }
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 } 96 }
108 97
109 class Drawer extends AnimatedComponent { 98 class Drawer extends AnimatedComponent {
110
111 Drawer({ 99 Drawer({
112 String key, 100 String key,
113 this.controller, 101 this.controller,
114 this.children, 102 this.children,
115 this.level: 0 103 this.level: 0
116 }) : super(key: key) { 104 }) : super(key: key) {
117 watch(controller.position); 105 watch(controller.performance.timeline);
118 } 106 }
119 107
120 List<Widget> children; 108 List<Widget> children;
121 int level; 109 int level;
122 DrawerController controller; 110 DrawerController controller;
123 111
124 void syncFields(Drawer source) { 112 void syncFields(Drawer source) {
125 children = source.children; 113 children = source.children;
126 level = source.level; 114 level = source.level;
127 controller = source.controller; 115 controller = source.controller;
128 super.syncFields(source); 116 super.syncFields(source);
129 } 117 }
130 118
119 // TODO(mpcomplete): the animation system should handle building, maybe? Or
120 // at least setting the transform. Figure out how this could work for things
121 // like fades, slides, rotates, pinch, etc.
131 Widget build() { 122 Widget build() {
132 Matrix4 transform = new Matrix4.identity(); 123 // TODO(mpcomplete): animate as a fade-in.
133 transform.translate(controller.position.value); 124 double scaler = controller.performance.progress + 1.0;
134
135 double scaler = controller.position.value / _kWidth + 1;
136 Color maskColor = new Color.fromARGB((0x7F * scaler).floor(), 0, 0, 0); 125 Color maskColor = new Color.fromARGB((0x7F * scaler).floor(), 0, 0, 0);
137 126
138 var mask = new Listener( 127 var mask = new Listener(
139 child: new Container(decoration: new BoxDecoration(backgroundColor: maskCo lor)), 128 child: new Container(decoration: new BoxDecoration(backgroundColor: maskCo lor)),
140 onGestureTap: controller.handleMaskTap, 129 onGestureTap: controller.handleMaskTap
141 onGestureFlingStart: controller.handleFlingStart
142 ); 130 );
143 131
144 Container content = new Container( 132 Widget content = controller.position.build(
145 decoration: new BoxDecoration( 133 child: new Container(
146 backgroundColor: Grey[50], 134 decoration: new BoxDecoration(
147 boxShadow: shadows[level]), 135 backgroundColor: Grey[50],
148 width: _kWidth, 136 boxShadow: shadows[level]),
149 transform: transform, 137 width: _kWidth,
150 child: new Block(children) 138 child: new Block(children)
151 ); 139 ));
152 140
153 return new Listener( 141 return new Listener(
154 child: new Stack([ mask, content ]), 142 child: new Stack([ mask, content ]),
155 onPointerDown: controller.handlePointerDown, 143 onPointerDown: controller.handlePointerDown,
156 onPointerMove: controller.handlePointerMove, 144 onPointerMove: controller.handlePointerMove,
157 onPointerUp: controller.handlePointerUp, 145 onPointerUp: controller.handlePointerUp,
158 onPointerCancel: controller.handlePointerCancel 146 onPointerCancel: controller.handlePointerCancel,
147 onGestureFlingStart: controller.handleFlingStart
159 ); 148 );
160 } 149 }
161 150
162 } 151 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698