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

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

Powered by Google App Engine
This is Rietveld 408576698