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