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 'animated_component.dart'; | 5 import 'animated_component.dart'; |
6 import '../animation/animated_value.dart'; | 6 import '../animation/animated_value.dart'; |
7 import '../fn.dart'; | 7 import '../fn.dart'; |
8 import '../theme/colors.dart'; | 8 import '../theme/colors.dart'; |
9 import '../theme/view-configuration.dart'; | 9 import '../theme/view-configuration.dart'; |
| 10 import 'dart:async'; |
10 import 'dart:math' as math; | 11 import 'dart:math' as math; |
11 import 'dart:sky' as sky; | 12 import 'dart:sky' as sky; |
12 import 'material.dart'; | 13 import 'material.dart'; |
13 import 'popup_menu_item.dart'; | 14 import 'popup_menu_item.dart'; |
14 | 15 |
15 const double _kItemFadeDuration = 300.0; | 16 const double _kMenuOpenDuration = 300.0; |
16 const double _kItemFadeDelay = 100.0; | 17 const double _kMenuCloneDuration = 200.0; |
17 const double _kMenuExpandDuration = 300.0; | |
18 | 18 |
19 class PopupMenuController { | 19 class PopupMenuController { |
20 bool isOpen = false; | 20 bool isOpen = false; |
21 AnimatedValue position = new AnimatedValue(0.0); | 21 AnimatedValue position = new AnimatedValue(0.0); |
22 | 22 |
23 void open() { | 23 void open() { |
24 isOpen = true; | 24 isOpen = true; |
25 position.animateTo(1.0, _kMenuExpandDuration); | 25 position.animateTo(1.0, _kMenuOpenDuration); |
26 } | 26 } |
27 | 27 |
28 void close() { | 28 Future close() { |
29 position.animateTo(0.0, _kMenuExpandDuration); | 29 return position.animateTo(0.0, _kMenuCloneDuration).then((_) { |
30 // TODO(abarth): We shouldn't mark the menu as closed until the animation | 30 isOpen = false; |
31 // completes. | 31 }); |
32 isOpen = false; | |
33 } | 32 } |
34 } | 33 } |
35 | 34 |
36 class PopupMenu extends AnimatedComponent { | 35 class PopupMenu extends AnimatedComponent { |
37 static final Style _style = new Style(''' | 36 static final Style _style = new Style(''' |
38 border-radius: 2px; | 37 border-radius: 2px; |
39 padding: 8px 0; | 38 padding: 8px 0; |
40 box-sizing: border-box; | 39 box-sizing: border-box; |
41 background-color: ${Grey[50]};'''); | 40 background-color: ${Grey[50]};'''); |
42 | 41 |
43 List<List<Node>> items; | 42 List<List<Node>> items; |
44 int level; | 43 int level; |
45 PopupMenuController controller; | 44 PopupMenuController controller; |
46 | 45 |
47 double _position; | 46 double _position; |
48 List<AnimatedValue> _opacities; | |
49 int _width; | 47 int _width; |
50 int _height; | 48 int _height; |
51 | 49 |
52 PopupMenu({ Object key, this.controller, this.items, this.level }) | 50 PopupMenu({ Object key, this.controller, this.items, this.level }) |
53 : super(key: key) { | 51 : super(key: key) { |
54 animateField(controller.position, #_position); | 52 animateField(controller.position, #_position); |
55 | |
56 onDidMount(_measureSize); | 53 onDidMount(_measureSize); |
57 } | 54 } |
58 | 55 |
59 void _ensureItemAnimations() { | 56 double _opacityFor(int i) { |
60 if (_opacities != null && controller.isOpen) | 57 if (_position == null || _position == 1.0) |
61 return; | 58 return null; |
62 _opacities = new List.from(items.map((_) => new AnimatedValue(0.0))); | 59 double unit = 1.0 / items.length; |
63 int i = 0; | 60 double duration = 1.5 * unit; |
64 _opacities.forEach((opacity) { | 61 double start = i * unit; |
65 opacity.animateTo(1.0, _kItemFadeDuration, | 62 return math.max(0.0, math.min(1.0, (_position - start) / duration)); |
66 initialDelay: _kItemFadeDelay * ++i); | |
67 }); | |
68 } | 63 } |
69 | 64 |
70 String _inlineStyle() { | 65 String _inlineStyle() { |
71 if (_position == null || _position == 1.0 || _height == null || _width == nu
ll) | 66 if (_position == null || _position == 1.0 || |
| 67 _height == null || _width == null) |
72 return null; | 68 return null; |
73 return ''' | 69 return ''' |
74 opacity: ${math.min(1.0, _position * 1.5)}; | 70 opacity: ${math.min(1.0, _position * 3.0)}; |
75 width: ${math.min(_width, _width * _position * 3.0)}px; | 71 width: ${math.min(_width, _width * (0.5 + _position * 2.0))}px; |
76 height: ${_height * _position}px;'''; | 72 height: ${math.min(_height, _height * _position * 1.5)}px;'''; |
77 } | 73 } |
78 | 74 |
79 void _measureSize() { | 75 void _measureSize() { |
80 setState(() { | 76 setState(() { |
81 var root = getRoot(); | 77 var root = getRoot(); |
82 _width = root.clientWidth; | 78 _width = root.clientWidth; |
83 _height = root.clientHeight; | 79 _height = root.clientHeight; |
84 }); | 80 }); |
85 } | 81 } |
86 | 82 |
87 Node build() { | 83 Node build() { |
88 _ensureItemAnimations(); | 84 int i = 0; |
89 | 85 List<Node> children = new List.from(items.map((List<Node> item) { |
90 List<Node> children = []; | 86 double opacity = _opacityFor(i); |
91 | 87 return new PopupMenuItem(key: i++, children: item, opacity: opacity); |
92 if (controller.isOpen) { | 88 })); |
93 int i = 0; | |
94 items.forEach((List<Node> item) { | |
95 children.add( | |
96 new PopupMenuItem(key: i, children: item, opacity: _opacities[i])); | |
97 ++i; | |
98 }); | |
99 } | |
100 | 89 |
101 return new Material( | 90 return new Material( |
102 style: _style, | 91 style: _style, |
103 inlineStyle: _inlineStyle(), | 92 inlineStyle: _inlineStyle(), |
104 children: children, | 93 children: children, |
105 level: level | 94 level: level); |
106 ); | |
107 } | 95 } |
108 } | 96 } |
OLD | NEW |