Chromium Code Reviews| 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:async'; | 5 import 'dart:async'; |
| 6 import 'dart:math' as math; | 6 import 'dart:math' as math; |
| 7 import 'dart:sky' as sky; | 7 import 'dart:sky' as sky; |
| 8 | 8 |
| 9 import '../animation/animated_value.dart'; | 9 import '../animation/animation_performance.dart'; |
| 10 import '../painting/box_painter.dart'; | 10 import '../painting/box_painter.dart'; |
| 11 import '../theme/colors.dart'; | 11 import '../theme/colors.dart'; |
| 12 import '../theme/shadows.dart'; | 12 import '../theme/shadows.dart'; |
| 13 import 'animated_component.dart'; | 13 import 'animated_component.dart'; |
| 14 import 'basic.dart'; | 14 import 'basic.dart'; |
| 15 import 'popup_menu_item.dart'; | 15 import 'popup_menu_item.dart'; |
| 16 | 16 |
| 17 const double _kMenuOpenDuration = 300.0; | 17 const Duration _kMenuOpenDuration = const Duration(milliseconds: 300); |
| 18 const double _kMenuCloseDuration = 200.0; | 18 const Duration _kMenuCloseDuration = const Duration(milliseconds: 200); |
| 19 const double _kMenuCloseDelay = 100.0; | 19 const Duration _kMenuCloseDelay = const Duration(milliseconds: 100); |
| 20 const double _kMenuWidthStep = 56.0; | 20 const double _kMenuWidthStep = 56.0; |
| 21 const double _kMenuMargin = 16.0; // 24.0 on tablet | 21 const double _kMenuMargin = 16.0; // 24.0 on tablet |
| 22 const double _kMenuMinWidth = 2.0 * _kMenuWidthStep; | 22 const double _kMenuMinWidth = 2.0 * _kMenuWidthStep; |
| 23 const double _kMenuMaxWidth = 5.0 * _kMenuWidthStep; | 23 const double _kMenuMaxWidth = 5.0 * _kMenuWidthStep; |
| 24 const double _kMenuHorizontalPadding = 16.0; | 24 const double _kMenuHorizontalPadding = 16.0; |
| 25 const double _kMenuVerticalPadding = 8.0; | 25 const double _kMenuVerticalPadding = 8.0; |
| 26 | 26 |
| 27 enum MenuState { hidden, opening, open, closing } | 27 enum MenuState { closed, opening, open, closing } |
| 28 | 28 |
| 29 class PopupMenuController { | 29 class PopupMenuController { |
| 30 AnimatedValue position = new AnimatedValue(0.0); | 30 |
| 31 MenuState _state = MenuState.hidden; | 31 PopupMenuController() { |
| 32 position = new AnimatedType<double>(0.0, end: 1.0); | |
| 33 performance = new AnimationPerformance() | |
| 34 ..variable = position | |
| 35 ..addListener(_updateState); | |
| 36 } | |
| 37 | |
| 38 AnimatedType<double> position; | |
| 39 AnimationPerformance performance; | |
| 40 | |
| 41 MenuState _state = MenuState.closed; | |
| 32 MenuState get state => _state; | 42 MenuState get state => _state; |
| 33 | 43 |
| 34 bool get canReact => (_state == MenuState.opening) || (_state == MenuState.ope n); | 44 bool get canReact => (_state == MenuState.opening) || (_state == MenuState.ope n); |
| 35 | 45 |
| 36 open() async { | 46 void _updateState() { |
| 37 if (_state != MenuState.hidden) | 47 if (position.value == 0.0) { |
| 48 _state = MenuState.closed; | |
| 49 if (_closeCompleter != null) | |
| 50 _closeCompleter.complete(); | |
| 38 return; | 51 return; |
| 39 _state = MenuState.opening; | 52 } |
| 40 if (await position.animateTo(1.0, _kMenuOpenDuration) == 1.0) | 53 |
| 54 if (position.value == 1.0) | |
| 41 _state = MenuState.open; | 55 _state = MenuState.open; |
| 42 } | 56 } |
| 43 | 57 |
| 44 Future _closeState; | 58 Completer _closeCompleter; |
| 45 close() async { | 59 Timer _closeTimer; |
| 46 var result = new Completer(); | 60 |
| 47 _closeState = result.future; | 61 void open() { |
| 48 if ((_state == MenuState.opening) || (_state == MenuState.open)) { | 62 if (_state != MenuState.closed) |
| 49 _state = MenuState.closing; | 63 return; |
| 50 await position.animateTo(0.0, _kMenuCloseDuration, initialDelay: _kMenuClo seDelay); | 64 if (_closeTimer != null) { |
| 51 _state = MenuState.hidden; | 65 _closeTimer.cancel(); |
| 52 _closeState = null; | 66 _closeTimer = null; |
| 53 result.complete(); | |
| 54 return result.future; | |
| 55 } | 67 } |
| 56 assert(_closeState != null); | 68 _closeCompleter = null; |
| 57 return _closeState; | 69 _state = MenuState.opening; |
| 70 performance..duration = _kMenuOpenDuration | |
| 71 ..play(); | |
| 72 } | |
| 73 | |
| 74 Future close() { | |
| 75 if (_state == MenuState.closing || _state == MenuState.closed) | |
| 76 return _closeCompleter.future; | |
|
Matt Perry
2015/07/10 17:10:35
Does this still work after _closeCompleter.complet
abarth-chromium
2015/07/10 17:23:00
Yes. It gives you a promise that is already resol
| |
| 77 | |
| 78 _state = MenuState.closing; | |
| 79 assert(_closeCompleter == null); | |
| 80 _closeCompleter = new Completer(); | |
| 81 performance.duration = _kMenuCloseDuration; | |
| 82 | |
| 83 assert(_closeTimer == null); | |
| 84 _closeTimer = new Timer(_kMenuCloseDelay, performance.reverse); | |
| 85 | |
| 86 return _closeCompleter.future; | |
| 58 } | 87 } |
| 59 } | 88 } |
| 60 | 89 |
| 61 class PopupMenu extends AnimatedComponent { | 90 class PopupMenu extends AnimatedComponent { |
| 62 | 91 |
| 63 PopupMenu({ String key, this.controller, this.items, this.level }) | 92 PopupMenu({ String key, this.controller, this.items, this.level }) |
| 64 : super(key: key) { | 93 : super(key: key) { |
| 65 _painter = new BoxPainter(new BoxDecoration( | 94 _painter = new BoxPainter(new BoxDecoration( |
| 66 backgroundColor: Grey[50], | 95 backgroundColor: Grey[50], |
| 67 borderRadius: 2.0, | 96 borderRadius: 2.0, |
| 68 boxShadow: shadows[level])); | 97 boxShadow: shadows[level])); |
| 69 watch(controller.position); | 98 watchPerformance(controller.performance); |
| 70 } | 99 } |
| 71 | 100 |
| 72 PopupMenuController controller; | 101 PopupMenuController controller; |
| 73 List<PopupMenuItem> items; | 102 List<PopupMenuItem> items; |
| 74 int level; | 103 int level; |
| 75 | 104 |
| 76 void syncFields(PopupMenu source) { | 105 void syncFields(PopupMenu source) { |
| 77 controller = source.controller; | 106 controller = source.controller; |
| 78 items = source.items; | 107 items = source.items; |
| 79 level = source.level; | 108 level = source.level; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 child: new Block(children) | 154 child: new Block(children) |
| 126 ) | 155 ) |
| 127 ) | 156 ) |
| 128 ) | 157 ) |
| 129 ) | 158 ) |
| 130 ) | 159 ) |
| 131 ); | 160 ); |
| 132 } | 161 } |
| 133 | 162 |
| 134 } | 163 } |
| OLD | NEW |