Port stocks to fn3 and introduce an App component.

This commit is contained in:
Hixie
2015-09-25 16:00:56 -07:00
parent 93f1ba5dac
commit 0a0a92eb45
16 changed files with 372 additions and 287 deletions

View File

@@ -5,6 +5,7 @@
library fn3;
export 'fn3/animated_component.dart';
export 'fn3/app.dart';
export 'fn3/basic.dart';
export 'fn3/binding.dart';
export 'fn3/button_state.dart';

View File

@@ -0,0 +1,83 @@
// 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.
import 'dart:sky' as sky;
import 'package:sky/material.dart';
import 'package:sky/painting.dart';
import 'package:sky/src/fn3/basic.dart';
import 'package:sky/src/fn3/binding.dart';
import 'package:sky/src/fn3/framework.dart';
import 'package:sky/src/fn3/navigator.dart';
import 'package:sky/src/fn3/theme.dart';
import 'package:sky/src/fn3/title.dart';
const TextStyle _errorTextStyle = const TextStyle(
color: const Color(0xD0FF0000),
fontFamily: 'monospace',
fontSize: 48.0,
fontWeight: FontWeight.w900,
textAlign: TextAlign.right,
decoration: underline,
decorationColor: const Color(0xFFFF00),
decorationStyle: TextDecorationStyle.double
);
class App extends StatefulComponent {
App({
Key key,
this.title,
this.theme,
this.routes
}): super(key: key);
final String title;
final ThemeData theme;
final Map<String, RouteBuilder> routes;
AppState createState() => new AppState();
}
class AppState extends State<App> {
GlobalObjectKey _navigator;
void initState(BuildContext context) {
super.initState(context);
_navigator = new GlobalObjectKey(this);
WidgetFlutterBinding.instance.addEventListener(_backHandler);
}
void dispose() {
WidgetFlutterBinding.instance.removeEventListener(_backHandler);
super.dispose();
}
void _backHandler(sky.Event event) {
assert(mounted);
if (event.type == 'back') {
NavigatorState navigator = _navigator.currentState;
assert(navigator != null);
if (navigator.hasPreviousRoute)
navigator.pop();
}
}
Widget build(BuildContext context) {
return new Theme(
data: config.theme,
child: new DefaultTextStyle(
style: _errorTextStyle,
child: new Title(
title: config.title,
child: new Navigator(
key: _navigator,
routes: config.routes
)
)
)
);
}
}

View File

@@ -44,10 +44,11 @@ class Checkbox extends StatelessComponent {
? _kLightUncheckedColor
: _kDarkUncheckedColor;
return new _CheckboxWrapper(
value: value,
onChanged: onChanged,
uncheckedColor: uncheckedColor,
accentColor: themeData.accentColor);
value: value,
onChanged: onChanged,
uncheckedColor: uncheckedColor,
accentColor: themeData.accentColor
);
}
}
@@ -55,9 +56,16 @@ class Checkbox extends StatelessComponent {
// order to get an accent color from a Theme but Components do not know how to
// host RenderObjects.
class _CheckboxWrapper extends LeafRenderObjectWidget {
_CheckboxWrapper({Key key, this.value, this.onChanged, this.uncheckedColor,
this.accentColor})
: super(key: key);
_CheckboxWrapper({
Key key,
this.value,
this.onChanged,
this.uncheckedColor,
this.accentColor
}): super(key: key) {
assert(uncheckedColor != null);
assert(accentColor != null);
}
final bool value;
final ValueChanged onChanged;
@@ -65,7 +73,11 @@ class _CheckboxWrapper extends LeafRenderObjectWidget {
final Color accentColor;
_RenderCheckbox createRenderObject() => new _RenderCheckbox(
value: value, uncheckedColor: uncheckedColor, onChanged: onChanged);
value: value,
accentColor: accentColor,
uncheckedColor: uncheckedColor,
onChanged: onChanged
);
void updateRenderObject(_RenderCheckbox renderObject, _CheckboxWrapper oldWidget) {
renderObject.value = value;
@@ -76,25 +88,38 @@ class _CheckboxWrapper extends LeafRenderObjectWidget {
}
class _RenderCheckbox extends RenderToggleable {
_RenderCheckbox({bool value, Color uncheckedColor, ValueChanged onChanged})
: _uncheckedColor = uncheckedColor,
super(
value: value,
onChanged: onChanged,
size: new Size(_kEdgeSize, _kEdgeSize)) {}
_RenderCheckbox({
bool value,
Color uncheckedColor,
Color accentColor,
ValueChanged onChanged
}): _uncheckedColor = uncheckedColor,
_accentColor = accentColor,
super(
value: value,
onChanged: onChanged,
size: new Size(_kEdgeSize, _kEdgeSize)
) {
assert(uncheckedColor != null);
assert(accentColor != null);
}
Color _uncheckedColor;
Color get uncheckedColor => _uncheckedColor;
void set uncheckedColor(Color value) {
if (value == _uncheckedColor) return;
assert(value != null);
if (value == _uncheckedColor)
return;
_uncheckedColor = value;
markNeedsPaint();
}
Color _accentColor;
void set accentColor(Color value) {
if (value == _accentColor) return;
assert(value != null);
if (value == _accentColor)
return;
_accentColor = value;
markNeedsPaint();
}

View File

@@ -16,7 +16,7 @@ import 'package:sky/src/fn3/scrollable.dart';
import 'package:sky/src/fn3/theme.dart';
import 'package:sky/src/fn3/transitions.dart';
typedef Widget DialogBuilder(Navigator navigator);
typedef Dialog DialogBuilder(NavigatorState navigator);
/// A material design dialog
///
@@ -132,7 +132,7 @@ class Dialog extends StatelessComponent {
const Duration _kTransitionDuration = const Duration(milliseconds: 150);
class DialogRoute extends RouteBase {
class DialogRoute extends Route {
DialogRoute({ this.completer, this.builder });
final Completer completer;

View File

@@ -8,11 +8,11 @@ import 'package:sky/src/fn3/focus.dart';
import 'package:sky/src/fn3/framework.dart';
import 'package:sky/src/fn3/transitions.dart';
typedef Widget RouteBuilder(NavigatorState navigator, RouteBase route);
typedef Widget RouteBuilder(NavigatorState navigator, Route route);
typedef void NotificationCallback();
abstract class RouteBase {
abstract class Route {
AnimationPerformance _performance;
NotificationCallback onDismissed;
NotificationCallback onCompleted;
@@ -57,10 +57,10 @@ abstract class RouteBase {
const Duration _kTransitionDuration = const Duration(milliseconds: 150);
const Point _kTransitionStartPoint = const Point(0.0, 75.0);
class Route extends RouteBase {
Route({ this.name, this.builder });
final String name;
class PageRoute extends Route {
PageRoute(this.builder);
final RouteBuilder builder;
bool get isOpaque => true;
@@ -81,16 +81,16 @@ class Route extends RouteBase {
)
);
}
String toString() => '$runtimeType(name="$name")';
}
class RouteState extends RouteBase {
RouteState({ this.callback, this.route, this.owner });
typedef void RouteStateCallback(RouteState route);
Function callback;
RouteBase route;
class RouteState extends Route {
RouteState({ this.route, this.owner, this.callback });
Route route;
State owner;
RouteStateCallback callback;
bool get isOpaque => false;
@@ -105,99 +105,81 @@ class RouteState extends RouteBase {
Widget build(Key key, NavigatorState navigator, WatchableAnimationPerformance performance) => null;
}
class NavigatorHistory {
NavigatorHistory(List<Route> routes) {
for (Route route in routes) {
if (route.name != null)
namedRoutes[route.name] = route;
}
recents.add(routes[0]);
}
List<RouteBase> recents = new List<RouteBase>();
int index = 0;
Map<String, RouteBase> namedRoutes = new Map<String, RouteBase>();
RouteBase get currentRoute => recents[index];
bool hasPrevious() => index > 0;
void pushNamed(String name) {
Route route = namedRoutes[name];
assert(route != null);
push(route);
}
void push(RouteBase route) {
assert(!_debugCurrentlyHaveRoute(route));
recents.insert(index + 1, route);
index++;
}
void pop([dynamic result]) {
if (index > 0) {
RouteBase route = recents[index];
route.popState(result);
index--;
}
}
bool _debugCurrentlyHaveRoute(RouteBase route) {
return recents.any((candidate) => candidate == route);
}
}
class Navigator extends StatefulComponent {
Navigator(this.history, { Key key }) : super(key: key);
Navigator({ this.routes, Key key }) : super(key: key) {
// To use a navigator, you must at a minimum define the route with the name '/'.
assert(routes.containsKey('/'));
}
final NavigatorHistory history;
final Map<String, RouteBuilder> routes;
NavigatorState createState() => new NavigatorState();
}
class NavigatorState extends State<Navigator> {
RouteBase get currentRoute => config.history.currentRoute;
List<Route> _history = new List<Route>();
int _currentPosition = 0;
Route get currentRoute => _history[_currentPosition];
bool get hasPreviousRoute => _history.length > 1;
void initState(BuildContext context) {
super.initState(context);
PageRoute route = new PageRoute(config.routes['/']);
assert(route != null);
_history.add(route);
}
void pushState(State owner, Function callback) {
RouteBase route = new RouteState(
push(new RouteState(
route: currentRoute,
owner: owner,
callback: callback,
route: currentRoute
);
push(route);
callback: callback
));
}
void pushNamed(String name) {
setState(() {
config.history.pushNamed(name);
});
PageRoute route = new PageRoute(config.routes[name]);
assert(route != null);
push(route);
}
void push(RouteBase route) {
void push(Route route) {
assert(!_debugCurrentlyHaveRoute(route));
_history.insert(_currentPosition + 1, route);
setState(() {
config.history.push(route);
_currentPosition += 1;
});
}
void pop([dynamic result]) {
setState(() {
config.history.pop(result);
});
if (_currentPosition > 0) {
Route route = _history[_currentPosition];
route.popState(result);
setState(() {
_currentPosition -= 1;
});
}
}
bool _debugCurrentlyHaveRoute(Route route) {
return _history.any((candidate) => candidate == route);
}
Widget build(BuildContext context) {
List<Widget> visibleRoutes = new List<Widget>();
for (int i = config.history.recents.length-1; i >= 0; i -= 1) {
RouteBase route = config.history.recents[i];
for (int i = _history.length-1; i >= 0; i -= 1) {
Route route = _history[i];
if (!route.hasContent)
continue;
WatchableAnimationPerformance performance = route.ensurePerformance(
direction: (i <= config.history.index) ? Direction.forward : Direction.reverse
direction: (i <= _currentPosition) ? Direction.forward : Direction.reverse
);
route.onDismissed = () {
setState(() {
assert(config.history.recents.contains(route));
config.history.recents.remove(route);
assert(_history.contains(route));
_history.remove(route);
});
};
Key key = new ObjectKey(route);

View File

@@ -6,8 +6,8 @@ import 'dart:async';
import 'dart:sky' as sky;
import 'package:sky/animation.dart';
import 'package:sky/painting.dart';
import 'package:sky/material.dart';
import 'package:sky/painting.dart';
import 'package:sky/src/fn3/basic.dart';
import 'package:sky/src/fn3/focus.dart';
import 'package:sky/src/fn3/framework.dart';
@@ -15,6 +15,7 @@ import 'package:sky/src/fn3/gesture_detector.dart';
import 'package:sky/src/fn3/navigator.dart';
import 'package:sky/src/fn3/popup_menu_item.dart';
import 'package:sky/src/fn3/scrollable.dart';
import 'package:sky/src/fn3/theme.dart';
import 'package:sky/src/fn3/transitions.dart';
const Duration _kMenuDuration = const Duration(milliseconds: 300);
@@ -26,6 +27,8 @@ const double _kMenuMaxWidth = 5.0 * _kMenuWidthStep;
const double _kMenuHorizontalPadding = 16.0;
const double _kMenuVerticalPadding = 8.0;
typedef List<PopupMenuItem> PopupMenuItemsBuilder(NavigatorState navigator);
class PopupMenu extends StatefulComponent {
PopupMenu({
Key key,
@@ -49,25 +52,10 @@ class PopupMenu extends StatefulComponent {
class PopupMenuState extends State<PopupMenu> {
void initState(BuildContext context) {
super.initState(context);
_updateBoxPainter();
config.performance.addListener(_performanceChanged);
}
BoxPainter _painter;
void _updateBoxPainter() {
_painter = new BoxPainter(
new BoxDecoration(
backgroundColor: Colors.grey[50],
borderRadius: 2.0,
boxShadow: shadows[config.level]
)
);
}
void didUpdateConfig(PopupMenu oldConfig) {
if (config.level != config.level)
_updateBoxPainter();
if (config.performance != oldConfig.performance) {
oldConfig.performance.removeListener(_performanceChanged);
config.performance.addListener(_performanceChanged);
@@ -85,7 +73,19 @@ class PopupMenuState extends State<PopupMenu> {
});
}
BoxPainter _painter;
void _updateBoxPainter(BoxDecoration decoration) {
if (_painter == null || _painter.decoration != decoration)
_painter = new BoxPainter(decoration);
}
Widget build(BuildContext context) {
_updateBoxPainter(new BoxDecoration(
backgroundColor: Theme.of(context).canvasColor,
borderRadius: 2.0,
boxShadow: shadows[config.level]
));
double unit = 1.0 / (config.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 < config.items.length; ++i) {
@@ -151,7 +151,7 @@ class MenuPosition {
final double left;
}
class MenuRoute extends RouteBase {
class MenuRoute extends Route {
MenuRoute({ this.completer, this.position, this.builder, this.level });
final Completer completer;
@@ -194,8 +194,6 @@ class MenuRoute extends RouteBase {
}
}
typedef List<PopupMenuItem> PopupMenuItemsBuilder(NavigatorState navigator);
Future showMenu({ NavigatorState navigator, MenuPosition position, PopupMenuItemsBuilder builder, int level: 4 }) {
Completer completer = new Completer();
navigator.push(new MenuRoute(

View File

@@ -63,6 +63,7 @@ class Typography {
// TODO(abarth): Maybe this should be hard-coded in Scaffold?
static const String typeface = 'font-family: sans-serif';
// TODO(ianh): Remove this when we remove fn2, now that it's hard-coded in App.
static const TextStyle error = const TextStyle(
color: const Color(0xD0FF0000),
fontFamily: 'monospace',

View File

@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// TODO(ianh): rename this file 'binding.dart'
import 'dart:sky' as sky;
import 'package:sky/animation.dart';
@@ -39,6 +41,7 @@ class BindingHitTestEntry extends HitTestEntry {
}
/// The glue between the render tree and the sky engine
// TODO(ianh): rename this class FlutterBinding
class SkyBinding extends HitTestTarget {
SkyBinding({ RenderBox root: null, RenderView renderViewOverride }) {

View File

@@ -50,18 +50,12 @@ void main() {
test('Can navigator navigate to and from a stateful component', () {
WidgetTester tester = new WidgetTester();
final NavigatorHistory routes = new NavigatorHistory([
new Route(
name: '/',
builder: (navigator, route) => new FirstComponent(navigator)
),
new Route(
name: '/second',
builder: (navigator, route) => new SecondComponent(navigator)
)
]);
final Map<String, RouteBuilder> routes = <String, RouteBuilder>{
'/': (navigator, route) => new FirstComponent(navigator),
'/second': (navigator, route) => new SecondComponent(navigator),
};
tester.pumpFrame(new Navigator(routes));
tester.pumpFrame(new Navigator(routes: routes));
expect(tester.findText('X'), isNotNull);
expect(tester.findText('Y'), isNull);