| 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 'dart:sky' as sky; | 5 import 'dart:sky' as sky; |
| 6 | 6 |
| 7 import 'package:sky/animation/animation_performance.dart'; | 7 import 'package:sky/animation/animation_performance.dart'; |
| 8 import 'package:sky/animation/curves.dart'; | 8 import 'package:sky/animation/curves.dart'; |
| 9 import 'package:sky/theme/shadows.dart'; | 9 import 'package:sky/theme/shadows.dart'; |
| 10 import 'package:sky/widgets/animated_component.dart'; | 10 import 'package:sky/theme/colors.dart' as colors; |
| 11 import 'package:sky/widgets/animation_builder.dart'; | 11 import 'package:sky/widgets/animation_builder.dart'; |
| 12 import 'package:sky/widgets/animated.dart'; |
| 12 import 'package:sky/widgets/basic.dart'; | 13 import 'package:sky/widgets/basic.dart'; |
| 13 import 'package:sky/widgets/navigator.dart'; | 14 import 'package:sky/widgets/navigator.dart'; |
| 14 import 'package:sky/widgets/scrollable_viewport.dart'; | 15 import 'package:sky/widgets/scrollable_viewport.dart'; |
| 15 import 'package:sky/widgets/theme.dart'; | 16 import 'package:sky/widgets/theme.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 Duration _kBaseSettleDuration = const Duration(milliseconds: 246); | 33 const Duration _kBaseSettleDuration = const Duration(milliseconds: 246); |
| 33 // TODO(mpcomplete): The curve must be linear if we want the drawer to track | 34 // TODO(mpcomplete): The curve must be linear if we want the drawer to track |
| 34 // the user's finger. Odeon remedies this by attaching spring forces to the | 35 // the user's finger. Odeon remedies this by attaching spring forces to the |
| 35 // initial timeline when animating (so it doesn't look linear). | 36 // initial timeline when animating (so it doesn't look linear). |
| 37 const Point _kOpenPosition = Point.origin; |
| 38 const Point _kClosedPosition = const Point(-_kWidth, 0.0); |
| 36 const Curve _kAnimationCurve = linear; | 39 const Curve _kAnimationCurve = linear; |
| 37 | 40 |
| 38 typedef void DrawerStatusChangeHandler (bool showing); | 41 typedef void DrawerStatusChangeHandler (bool showing); |
| 39 | 42 |
| 40 enum DrawerStatus { | 43 enum DrawerStatus { |
| 41 active, | 44 active, |
| 42 inactive, | 45 inactive, |
| 43 } | 46 } |
| 44 | 47 |
| 45 typedef void DrawerStatusChangedCallback(DrawerStatus status); | 48 typedef void DrawerStatusChangedCallback(DrawerStatus status); |
| 46 | 49 |
| 47 class Drawer extends AnimatedComponent { | 50 class Drawer extends StatefulComponent { |
| 48 Drawer({ | 51 Drawer({ |
| 49 String key, | 52 String key, |
| 50 this.children, | 53 this.children, |
| 51 this.showing: false, | 54 this.showing: false, |
| 52 this.level: 0, | 55 this.level: 0, |
| 53 this.onStatusChanged, | 56 this.onStatusChanged, |
| 54 this.navigator | 57 this.navigator |
| 55 }) : super(key: key); | 58 }) : super(key: key); |
| 56 | 59 |
| 57 List<Widget> children; | 60 List<Widget> children; |
| 58 bool showing; | 61 bool showing; |
| 59 int level; | 62 int level; |
| 60 DrawerStatusChangedCallback onStatusChanged; | 63 DrawerStatusChangedCallback onStatusChanged; |
| 61 Navigator navigator; | 64 Navigator navigator; |
| 62 | 65 |
| 66 AnimatedType<Point> _position; |
| 67 AnimatedColor _maskColor; |
| 63 AnimationPerformance _performance; | 68 AnimationPerformance _performance; |
| 64 AnimationBuilder _builder; | |
| 65 | 69 |
| 66 void initState() { | 70 void initState() { |
| 67 _builder = new AnimationBuilder() | 71 _position = new AnimatedType<Point>(_kClosedPosition, end: _kOpenPosition, c
urve: _kAnimationCurve); |
| 68 ..position = new AnimatedType<Point>( | 72 _maskColor = new AnimatedColor(colors.transparent, end: const Color(0x7F0000
00)); |
| 69 new Point(-_kWidth, 0.0), end: Point.origin, curve: _kAnimationCurve); | 73 _performance = new AnimationPerformance() |
| 70 _performance = _builder.createPerformance([_builder.position], | 74 ..duration = _kBaseSettleDuration |
| 71 duration: _kBaseSettleDuration) | 75 ..variable = new AnimatedList([_position, _maskColor]) |
| 72 ..addListener(_checkForStateChanged); | 76 ..addListener(_checkForStateChanged); |
| 73 watch(_performance); | |
| 74 if (showing) | 77 if (showing) |
| 75 _show(); | 78 _show(); |
| 76 } | 79 } |
| 77 | 80 |
| 81 void syncFields(Drawer source) { |
| 82 children = source.children; |
| 83 level = source.level; |
| 84 navigator = source.navigator; |
| 85 if (showing != source.showing) { |
| 86 showing = source.showing; |
| 87 showing ? _show() : _hide(); |
| 88 } |
| 89 onStatusChanged = source.onStatusChanged; |
| 90 } |
| 91 |
| 78 void _show() { | 92 void _show() { |
| 79 if (navigator != null) | 93 if (navigator != null) |
| 80 navigator.pushState(this, (_) => _performance.reverse()); | 94 navigator.pushState(this, (_) => _performance.reverse()); |
| 81 _performance.play(); | 95 _performance.play(); |
| 82 } | 96 } |
| 83 | 97 |
| 84 void syncFields(Drawer source) { | 98 void _hide() { |
| 85 children = source.children; | 99 _performance.reverse(); |
| 86 level = source.level; | |
| 87 navigator = source.navigator; | |
| 88 if (showing != source.showing) { | |
| 89 showing = source.showing; | |
| 90 if (showing) { | |
| 91 _show(); | |
| 92 } else { | |
| 93 _performance.reverse(); | |
| 94 } | |
| 95 } | |
| 96 onStatusChanged = source.onStatusChanged; | |
| 97 super.syncFields(source); | |
| 98 } | 100 } |
| 99 | 101 |
| 100 // TODO(mpcomplete): the animation system should handle building, maybe? Or | |
| 101 // at least setting the transform. Figure out how this could work for things | |
| 102 // like fades, slides, rotates, pinch, etc. | |
| 103 Widget build() { | 102 Widget build() { |
| 104 // TODO(mpcomplete): animate as a fade-in. | |
| 105 double scaler = _performance.progress; | |
| 106 Color maskColor = new Color.fromARGB((0x7F * scaler).floor(), 0, 0, 0); | |
| 107 | |
| 108 var mask = new Listener( | 103 var mask = new Listener( |
| 109 child: new Container(decoration: new BoxDecoration(backgroundColor: maskCo
lor)), | 104 child: new ColorAnimation(color: _maskColor, performance: _performance), |
| 110 onGestureTap: handleMaskTap | 105 onGestureTap: handleMaskTap |
| 111 ); | 106 ); |
| 112 | 107 |
| 113 Widget content = _builder.build( | 108 Widget content = new PositionAnimation( |
| 114 new Container( | 109 position: _position, |
| 110 performance: _performance, |
| 111 child: new Container( |
| 115 decoration: new BoxDecoration( | 112 decoration: new BoxDecoration( |
| 116 backgroundColor: Theme.of(this).canvasColor, | 113 backgroundColor: Theme.of(this).canvasColor, |
| 117 boxShadow: shadows[level]), | 114 boxShadow: shadows[level]), |
| 118 width: _kWidth, | 115 width: _kWidth, |
| 119 child: new ScrollableBlock(children) | 116 child: new ScrollableBlock(children) |
| 120 )); | 117 )); |
| 121 | 118 |
| 122 return new Listener( | 119 return new Listener( |
| 123 child: new Stack([ mask, content ]), | 120 child: new Stack([ mask, content ]), |
| 124 onPointerDown: handlePointerDown, | 121 onPointerDown: handlePointerDown, |
| 125 onPointerMove: handlePointerMove, | 122 onPointerMove: handlePointerMove, |
| 126 onPointerUp: handlePointerUp, | 123 onPointerUp: handlePointerUp, |
| 127 onPointerCancel: handlePointerCancel, | 124 onPointerCancel: handlePointerCancel, |
| 128 onGestureFlingStart: handleFlingStart | 125 onGestureFlingStart: handleFlingStart |
| 129 ); | 126 ); |
| 130 } | 127 } |
| 131 | 128 |
| 132 double get xPosition => _builder.position.value.x; | 129 double get xPosition => _position.value.x; |
| 133 | 130 |
| 134 DrawerStatus _lastStatus; | 131 DrawerStatus _lastStatus; |
| 135 void _checkForStateChanged() { | 132 void _checkForStateChanged() { |
| 136 DrawerStatus status = _status; | 133 DrawerStatus status = _status; |
| 137 if (_lastStatus != null && status != _lastStatus) { | 134 if (_lastStatus != null && status != _lastStatus) { |
| 138 if (status == DrawerStatus.inactive && | 135 if (status == DrawerStatus.inactive && |
| 139 navigator != null && | 136 navigator != null && |
| 140 navigator.currentRoute.key == this) | 137 navigator.currentRoute.key == this) |
| 141 navigator.pop(); | 138 navigator.pop(); |
| 142 if (onStatusChanged != null) | 139 if (onStatusChanged != null) |
| 143 onStatusChanged(status); | 140 onStatusChanged(status); |
| 144 } | 141 } |
| 145 _lastStatus = status; | 142 _lastStatus = status; |
| 146 } | 143 } |
| 147 | 144 |
| 148 DrawerStatus get _status => _performance.isDismissed ? DrawerStatus.inactive :
DrawerStatus.active; | 145 DrawerStatus get _status => _performance.isDismissed ? DrawerStatus.inactive :
DrawerStatus.active; |
| 149 bool get _isMostlyClosed => xPosition <= -_kWidth/2; | 146 bool get _isMostlyClosed => xPosition <= -_kWidth/2; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 171 if (!_performance.isAnimating) | 168 if (!_performance.isAnimating) |
| 172 _settle(); | 169 _settle(); |
| 173 } | 170 } |
| 174 | 171 |
| 175 void handleFlingStart(event) { | 172 void handleFlingStart(event) { |
| 176 double velocityX = event.velocityX / 1000; | 173 double velocityX = event.velocityX / 1000; |
| 177 if (velocityX.abs() >= _kMinFlingVelocity) | 174 if (velocityX.abs() >= _kMinFlingVelocity) |
| 178 _performance.fling(velocity: velocityX / _kWidth); | 175 _performance.fling(velocity: velocityX / _kWidth); |
| 179 } | 176 } |
| 180 } | 177 } |
| OLD | NEW |