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

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

Issue 1228233004: Port PopupMenu to the new animation system (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: 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
« no previous file with comments | « sky/sdk/lib/animation/timeline.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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: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;
Matt Perry 2015/07/10 00:32:17 This might be better if it were inferred from the
abarth-chromium 2015/07/10 02:42:11 Yeah, that's a good idea. Would it be ok to do th
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 Completer localCompleter = _closeCompleter;
51 _closeCompleter = null;
52 localCompleter.complete();
53 }
38 return; 54 return;
39 _state = MenuState.opening; 55 }
40 if (await position.animateTo(1.0, _kMenuOpenDuration) == 1.0) 56
57 if (position.value == 1.0)
41 _state = MenuState.open; 58 _state = MenuState.open;
42 } 59 }
43 60
44 Future _closeState; 61 void open() {
45 close() async { 62 if (_state != MenuState.closed)
46 var result = new Completer(); 63 return;
47 _closeState = result.future; 64 _state = MenuState.opening;
48 if ((_state == MenuState.opening) || (_state == MenuState.open)) { 65 performance..duration = _kMenuOpenDuration
49 _state = MenuState.closing; 66 ..play();
50 await position.animateTo(0.0, _kMenuCloseDuration, initialDelay: _kMenuClo seDelay); 67 }
51 _state = MenuState.hidden; 68
52 _closeState = null; 69 Completer _closeCompleter;
53 result.complete(); 70 Future close() {
54 return result.future; 71 if (_state == MenuState.closing || _state == MenuState.closed)
55 } 72 return new Future.value(null);
Matt Perry 2015/07/10 00:32:17 Do you mean to return _closeCompleter.future here?
abarth-chromium 2015/07/10 02:42:11 That's different from what the old code did, but t
56 assert(_closeState != null); 73
57 return _closeState; 74 _state = MenuState.closing;
75 assert(_closeCompleter == null);
76 _closeCompleter = new Completer();
77 performance
78 ..duration = _kMenuCloseDuration
79 ..reverse(delay: _kMenuCloseDelay);
Matt Perry 2015/07/10 00:32:17 a timeout API would work here as long as we cancel
abarth-chromium 2015/07/10 02:42:11 If you'd prefer, I'm happy to switch this to a tim
80 return _closeCompleter.future;
58 } 81 }
59 } 82 }
60 83
61 class PopupMenu extends AnimatedComponent { 84 class PopupMenu extends AnimatedComponent {
62 85
63 PopupMenu({ String key, this.controller, this.items, this.level }) 86 PopupMenu({ String key, this.controller, this.items, this.level })
64 : super(key: key) { 87 : super(key: key) {
65 _painter = new BoxPainter(new BoxDecoration( 88 _painter = new BoxPainter(new BoxDecoration(
66 backgroundColor: Grey[50], 89 backgroundColor: Grey[50],
67 borderRadius: 2.0, 90 borderRadius: 2.0,
68 boxShadow: shadows[level])); 91 boxShadow: shadows[level]));
69 watch(controller.position); 92 watchPerformance(controller.performance);
70 } 93 }
71 94
72 PopupMenuController controller; 95 PopupMenuController controller;
73 List<PopupMenuItem> items; 96 List<PopupMenuItem> items;
74 int level; 97 int level;
75 98
76 void syncFields(PopupMenu source) { 99 void syncFields(PopupMenu source) {
77 controller = source.controller; 100 controller = source.controller;
78 items = source.items; 101 items = source.items;
79 level = source.level; 102 level = source.level;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 child: new Block(children) 148 child: new Block(children)
126 ) 149 )
127 ) 150 )
128 ) 151 )
129 ) 152 )
130 ) 153 )
131 ); 154 );
132 } 155 }
133 156
134 } 157 }
OLDNEW
« no previous file with comments | « sky/sdk/lib/animation/timeline.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698