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'; |
| 6 import 'dart:math' as math; |
| 7 import 'dart:sky' as sky; |
| 8 |
5 import '../animation/animated_value.dart'; | 9 import '../animation/animated_value.dart'; |
6 import '../fn2.dart'; | 10 import '../fn2.dart'; |
| 11 import '../painting/box_painter.dart'; |
7 import '../theme2/colors.dart'; | 12 import '../theme2/colors.dart'; |
8 import '../theme2/shadows.dart'; | 13 import '../theme2/shadows.dart'; |
9 import 'animated_component.dart'; | 14 import 'animated_component.dart'; |
10 import 'dart:async'; | |
11 import 'dart:math' as math; | |
12 import 'material.dart'; | 15 import 'material.dart'; |
13 import 'popup_menu_item.dart'; | 16 import 'popup_menu_item.dart'; |
14 | 17 |
15 const double _kMenuOpenDuration = 300.0; | 18 const double _kMenuOpenDuration = 300.0; |
16 const double _kMenuCloseDuration = 200.0; | 19 const double _kMenuCloseDuration = 200.0; |
17 const double _kMenuCloseDelay = 100.0; | 20 const double _kMenuCloseDelay = 100.0; |
18 | 21 |
19 enum MenuState { hidden, opening, open, closing } | 22 enum MenuState { hidden, opening, open, closing } |
20 | 23 |
21 class PopupMenuController { | 24 class PopupMenuController { |
(...skipping 25 matching lines...) Expand all Loading... |
47 } | 50 } |
48 assert(_closeState != null); | 51 assert(_closeState != null); |
49 return _closeState; | 52 return _closeState; |
50 } | 53 } |
51 } | 54 } |
52 | 55 |
53 class PopupMenu extends AnimatedComponent { | 56 class PopupMenu extends AnimatedComponent { |
54 | 57 |
55 PopupMenu({ Object key, this.controller, this.items, this.level }) | 58 PopupMenu({ Object key, this.controller, this.items, this.level }) |
56 : super(key: key) { | 59 : super(key: key) { |
| 60 _painter = new BoxPainter(new BoxDecoration( |
| 61 backgroundColor: Grey[50], |
| 62 borderRadius: 2.0, |
| 63 boxShadow: Shadow[level])); |
| 64 |
57 animate(controller.position, (double value) { | 65 animate(controller.position, (double value) { |
58 _position = value; | 66 _position = value; |
59 }); | 67 }); |
60 } | 68 } |
61 | 69 |
62 PopupMenuController controller; | 70 PopupMenuController controller; |
63 List<List<UINode>> items; | 71 List<List<UINode>> items; |
64 int level; | 72 int level; |
65 | 73 |
66 void syncFields(PopupMenu source) { | 74 void syncFields(PopupMenu source) { |
67 controller = source.controller; | 75 controller = source.controller; |
68 items = source.items; | 76 items = source.items; |
69 level = source.level; | 77 level = source.level; |
| 78 _painter = source._painter; |
70 super.syncFields(source); | 79 super.syncFields(source); |
71 } | 80 } |
72 | 81 |
73 double _position; | 82 double _position; |
74 // int _width; | 83 BoxPainter _painter; |
75 // int _height; | |
76 | 84 |
77 double _opacityFor(int i) { | 85 double _opacityFor(int i) { |
78 if (_position == null || _position == 1.0) | 86 if (_position == null || _position == 1.0) |
79 return null; | 87 return 1.0; |
80 double unit = 1.0 / items.length; | 88 double unit = 1.0 / items.length; |
81 double duration = 1.5 * unit; | 89 double duration = 1.5 * unit; |
82 double start = i * unit; | 90 double start = i * unit; |
83 return math.max(0.0, math.min(1.0, (_position - start) / duration)); | 91 return math.max(0.0, math.min(1.0, (_position - start) / duration)); |
84 } | 92 } |
85 | 93 |
86 // String _inlineStyle() { | |
87 // if (_position == null || _position == 1.0 || | |
88 // _height == null || _width == null) | |
89 // return null; | |
90 // return ''' | |
91 // opacity: ${math.min(1.0, _position * 3.0)}; | |
92 // width: ${math.min(_width, _width * (0.5 + _position * 2.0))}px; | |
93 // height: ${math.min(_height, _height * _position * 1.5)}px;'''; | |
94 // } | |
95 | |
96 // void didMount() { | |
97 // _measureSize(); | |
98 // super.didMount(); | |
99 // } | |
100 | |
101 // void _measureSize() { | |
102 // setState(() { | |
103 // var root = getRoot(); | |
104 // _width = root.width.round(); | |
105 // _height = root.height.round(); | |
106 // }); | |
107 // } | |
108 | |
109 UINode build() { | 94 UINode build() { |
110 int i = 0; | 95 int i = 0; |
111 List<UINode> children = new List.from(items.map((List<UINode> item) { | 96 List<UINode> children = new List.from(items.map((List<UINode> item) { |
112 double opacity = _opacityFor(i); | 97 double opacity = _opacityFor(i); |
113 return new PopupMenuItem(key: i++, children: item, opacity: opacity); | 98 return new PopupMenuItem(key: i++, children: item, opacity: opacity); |
114 })); | 99 })); |
115 | 100 |
116 // inlineStyle: _inlineStyle(), | |
117 return new Opacity( | 101 return new Opacity( |
118 opacity: math.min(1.0, _position * 3.0), | 102 opacity: math.min(1.0, _position * 3.0), |
119 child: new ShrinkWrapWidth( | 103 child: new ShrinkWrapWidth( |
120 child: new Container( | 104 child: new CustomPaint( |
121 padding: const EdgeDims.all(8.0), | 105 callback: (sky.Canvas canvas, Size size) { |
122 decoration: new BoxDecoration( | 106 double width = math.min(size.width, size.width * (0.5 + _position *
2.0)); |
123 backgroundColor: Grey[50], | 107 double height = math.min(size.height, size.height * _position * 1.5)
; |
124 borderRadius: 2.0, | 108 _painter.paint(canvas, new Rect.fromLTRB(size.width - width, 0.0, wi
dth, height)); |
125 boxShadow: Shadow[level]), | 109 }, |
126 child: new Block(children) | 110 child: new Container( |
| 111 padding: const EdgeDims.all(8.0), |
| 112 child: new Block(children) |
| 113 ) |
127 ) | 114 ) |
128 ) | 115 ) |
129 ); | 116 ); |
130 } | 117 } |
131 | 118 |
132 } | 119 } |
OLD | NEW |