popup_menu.dart 6.38 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:async';
6 7
import 'dart:sky' as sky;

8
import 'package:sky/animation.dart';
9
import 'package:sky/painting.dart';
10
import 'package:sky/material.dart';
11
import 'package:sky/src/widgets/basic.dart';
12
import 'package:sky/src/widgets/focus.dart';
13 14 15 16 17
import 'package:sky/src/widgets/framework.dart';
import 'package:sky/src/widgets/navigator.dart';
import 'package:sky/src/widgets/popup_menu_item.dart';
import 'package:sky/src/widgets/scrollable.dart';
import 'package:sky/src/widgets/transitions.dart';
18 19

const Duration _kMenuDuration = const Duration(milliseconds: 300);
20
const double _kMenuCloseIntervalEnd = 2.0 / 3.0;
21 22 23 24 25 26 27
const double _kMenuWidthStep = 56.0;
const double _kMenuMargin = 16.0; // 24.0 on tablet
const double _kMenuMinWidth = 2.0 * _kMenuWidthStep;
const double _kMenuMaxWidth = 5.0 * _kMenuWidthStep;
const double _kMenuHorizontalPadding = 16.0;
const double _kMenuVerticalPadding = 8.0;

28
class PopupMenu extends StatefulComponent {
29 30

  PopupMenu({
31
    Key key,
32
    this.items,
33 34 35 36 37 38 39
    this.level: 4,
    this.navigator,
    this.performance
  }) : super(key: key) {
    assert(items != null);
    assert(performance != null);
  }
40 41 42 43

  List<PopupMenuItem> items;
  int level;
  Navigator navigator;
44
  WatchableAnimationPerformance performance;
45

46
  BoxPainter _painter;
47 48 49

  void initState() {
    _updateBoxPainter();
50
  }
51

52 53 54 55 56 57 58 59
  void _updateBoxPainter() {
    _painter = new BoxPainter(
      new BoxDecoration(
        backgroundColor: Colors.grey[50],
        borderRadius: 2.0,
        boxShadow: shadows[level]
      )
    );
60 61
  }

62
  void syncConstructorArguments(PopupMenu source) {
63
    items = source.items;
64 65 66 67 68
    if (level != source.level) {
      level = source.level;
      _updateBoxPainter();
    }
    navigator = source.navigator;
69 70 71 72 73
    if (mounted)
      performance.removeListener(_performanceChanged);
    performance = source.performance;
    if (mounted)
      performance.addListener(_performanceChanged);
74 75
  }

76 77 78
  void didMount() {
    performance.addListener(_performanceChanged);
    super.didMount();
79 80
  }

81 82 83
  void didUnmount() {
    performance.removeListener(_performanceChanged);
    super.didMount();
84 85
  }

86 87 88 89
  void _performanceChanged() {
    setState(() {
      // the performance changed, and our state is tied up with the performance
    });
90 91
  }

92 93 94
  void itemPressed(PopupMenuItem item) {
    if (navigator != null)
      navigator.pop(item.value);
95 96 97
  }

  Widget build() {
98 99 100 101 102 103
    double unit = 1.0 / (items.length + 1.5); // 1.0 for the width and 0.5 for the last item's fade.
    List<Widget> children = [];
    for (int i = 0; i < items.length; ++i) {
      double start = (i + 1) * unit;
      double end = (start + 1.5 * unit).clamp(0.0, 1.0);
      children.add(new FadeTransition(
104
        performance: performance,
105
        opacity: new AnimatedValue<double>(0.0, end: 1.0, interval: new Interval(start, end)),
106 107
        child: items[i])
      );
108 109 110 111
    }
    final width = new AnimatedValue<double>(0.0, end: 1.0, interval: new Interval(0.0, unit));
    final height = new AnimatedValue<double>(0.0, end: 1.0, interval: new Interval(0.0, unit * items.length));
    return new FadeTransition(
112
      performance: performance,
113
      opacity: new AnimatedValue<double>(0.0, end: 1.0, interval: new Interval(0.0, 1.0 / 3.0)),
114 115
      child: new Container(
        margin: new EdgeDims.all(_kMenuMargin),
116
        child: new BuilderTransition(
117
          performance: performance,
118 119 120 121 122 123 124 125 126 127 128 129 130
          variables: [width, height],
          builder: () {
            return new CustomPaint(
              callback: (sky.Canvas canvas, Size size) {
                double widthValue = width.value * size.width;
                double heightValue = height.value * size.height;
                _painter.paint(canvas, new Rect.fromLTWH(size.width - widthValue, 0.0, widthValue, heightValue));
              },
              child: new ConstrainedBox(
                constraints: new BoxConstraints(
                  minWidth: _kMenuMinWidth,
                  maxWidth: _kMenuMaxWidth
                ),
131
                child: new IntrinsicWidth(
132 133 134 135 136 137 138
                  stepWidth: _kMenuWidthStep,
                  child: new ScrollableViewport(
                    child: new Container(
                      padding: const EdgeDims.symmetric(
                        horizontal: _kMenuHorizontalPadding,
                        vertical: _kMenuVerticalPadding
                      ),
139
                      child: new BlockBody(children)
140 141
                    )
                  )
142 143
                )
              )
144 145
            );
          }
146 147 148 149 150 151
        )
      )
    );
  }

}
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

class MenuPosition {
  const MenuPosition({ this.top, this.right, this.bottom, this.left });
  final double top;
  final double right;
  final double bottom;
  final double left;
}

class MenuRoute extends RouteBase {
  MenuRoute({ this.completer, this.position, this.builder, this.level });

  final Completer completer;
  final MenuPosition position;
  final PopupMenuItemsBuilder builder;
  final int level;

  AnimationPerformance createPerformance() {
    AnimationPerformance result = super.createPerformance();
    AnimationTiming timing = new AnimationTiming();
    timing.reverseInterval = new Interval(0.0, _kMenuCloseIntervalEnd);
    result.timing = timing;
    return result;
  }

  Duration get transitionDuration => _kMenuDuration;
  bool get isOpaque => false;
  Widget build(Key key, Navigator navigator, WatchableAnimationPerformance performance) {
    return new Positioned(
      top: position?.top,
      right: position?.right,
      bottom: position?.bottom,
      left: position?.left,
      child: new Focus(
        key: new GlobalObjectKey(this),
        autofocus: true,
        child: new PopupMenu(
          key: key,
          items: builder != null ? builder(navigator) : const <PopupMenuItem>[],
          level: level,
          navigator: navigator,
          performance: performance
        )
      )
    );
  }

  void popState([dynamic result]) {
    completer.complete(result);
  }
}

typedef List<PopupMenuItem> PopupMenuItemsBuilder(Navigator navigator);

Future showMenu({ Navigator navigator, MenuPosition position, PopupMenuItemsBuilder builder, int level: 4 }) {
  Completer completer = new Completer();
  navigator.push(new MenuRoute(
    completer: completer,
    position: position,
    builder: builder,
    level: level
  ));
  return completer.future;
}