OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 import 'dart:async'; | |
6 import 'dart:math' as math; | |
7 import 'dart:sky' as sky; | |
8 | |
9 import '../animation/animated_value.dart'; | |
10 import '../painting/box_painter.dart'; | |
11 import '../theme2/colors.dart'; | |
12 import '../theme2/shadows.dart'; | |
13 import 'animated_component.dart'; | |
14 import 'popup_menu_item.dart'; | |
15 import 'basic.dart'; | |
16 | |
17 const double _kMenuOpenDuration = 300.0; | |
18 const double _kMenuCloseDuration = 200.0; | |
19 const double _kMenuCloseDelay = 100.0; | |
20 | |
21 enum MenuState { hidden, opening, open, closing } | |
22 | |
23 class PopupMenuController { | |
24 AnimatedValue position = new AnimatedValue(0.0); | |
25 MenuState _state = MenuState.hidden; | |
26 MenuState get state => _state; | |
27 | |
28 bool get canReact => (_state == MenuState.opening) || (_state == MenuState.ope
n); | |
29 | |
30 open() async { | |
31 if (_state != MenuState.hidden) | |
32 return; | |
33 _state = MenuState.opening; | |
34 if (await position.animateTo(1.0, _kMenuOpenDuration) == 1.0) | |
35 _state = MenuState.open; | |
36 } | |
37 | |
38 Future _closeState; | |
39 close() async { | |
40 var result = new Completer(); | |
41 _closeState = result.future; | |
42 if ((_state == MenuState.opening) || (_state == MenuState.open)) { | |
43 _state = MenuState.closing; | |
44 await position.animateTo(0.0, _kMenuCloseDuration, initialDelay: _kMenuClo
seDelay); | |
45 _state = MenuState.hidden; | |
46 _closeState = null; | |
47 result.complete(); | |
48 return result.future; | |
49 } | |
50 assert(_closeState != null); | |
51 return _closeState; | |
52 } | |
53 } | |
54 | |
55 class PopupMenu extends AnimatedComponent { | |
56 | |
57 PopupMenu({ Object key, this.controller, this.items, this.level }) | |
58 : super(key: key) { | |
59 _painter = new BoxPainter(new BoxDecoration( | |
60 backgroundColor: Grey[50], | |
61 borderRadius: 2.0, | |
62 boxShadow: shadows[level])); | |
63 | |
64 animate(controller.position, (double value) { | |
65 _position = value; | |
66 }); | |
67 } | |
68 | |
69 PopupMenuController controller; | |
70 List<List<UINode>> items; | |
71 int level; | |
72 | |
73 void syncFields(PopupMenu source) { | |
74 controller = source.controller; | |
75 items = source.items; | |
76 level = source.level; | |
77 _painter = source._painter; | |
78 super.syncFields(source); | |
79 } | |
80 | |
81 double _position; | |
82 BoxPainter _painter; | |
83 | |
84 double _opacityFor(int i) { | |
85 if (_position == null || _position == 1.0) | |
86 return 1.0; | |
87 double unit = 1.0 / items.length; | |
88 double duration = 1.5 * unit; | |
89 double start = i * unit; | |
90 return math.max(0.0, math.min(1.0, (_position - start) / duration)); | |
91 } | |
92 | |
93 UINode build() { | |
94 int i = 0; | |
95 List<UINode> children = new List.from(items.map((List<UINode> item) { | |
96 double opacity = _opacityFor(i); | |
97 return new PopupMenuItem(key: i++, children: item, opacity: opacity); | |
98 })); | |
99 | |
100 return new Opacity( | |
101 opacity: math.min(1.0, _position * 3.0), | |
102 child: new ShrinkWrapWidth( | |
103 child: new CustomPaint( | |
104 callback: (sky.Canvas canvas, Size size) { | |
105 double width = math.min(size.width, size.width * (0.5 + _position *
2.0)); | |
106 double height = math.min(size.height, size.height * _position * 1.5)
; | |
107 _painter.paint(canvas, new Rect.fromLTRB(size.width - width, 0.0, wi
dth, height)); | |
108 }, | |
109 child: new Container( | |
110 padding: const EdgeDims.all(8.0), | |
111 child: new Block(children) | |
112 ) | |
113 ) | |
114 ) | |
115 ); | |
116 } | |
117 | |
118 } | |
OLD | NEW |