Commit 1211dd23 authored by Ian Hickson's avatar Ian Hickson

Merge pull request #539 from Hixie/clean-up-modal

Clean up the term "modal" in the navigator
parents 377c74f7 c51260ac
...@@ -24,7 +24,7 @@ class HeroController extends NavigatorObserver { ...@@ -24,7 +24,7 @@ class HeroController extends NavigatorObserver {
final List<OverlayEntry> _overlayEntries = new List<OverlayEntry>(); final List<OverlayEntry> _overlayEntries = new List<OverlayEntry>();
void didPushModal(Route route, Route previousRoute) { void didPush(Route route, Route previousRoute) {
assert(navigator != null); assert(navigator != null);
assert(route != null); assert(route != null);
if (route is ModalRoute) { // as opposed to StateRoute, say if (route is ModalRoute) { // as opposed to StateRoute, say
...@@ -37,7 +37,7 @@ class HeroController extends NavigatorObserver { ...@@ -37,7 +37,7 @@ class HeroController extends NavigatorObserver {
} }
} }
void didPopModal(Route route, Route previousRoute) { void didPop(Route route, Route previousRoute) {
assert(navigator != null); assert(navigator != null);
assert(route != null); assert(route != null);
if (route is ModalRoute) { // as opposed to StateRoute, say if (route is ModalRoute) { // as opposed to StateRoute, say
......
...@@ -13,13 +13,13 @@ abstract class Route<T> { ...@@ -13,13 +13,13 @@ abstract class Route<T> {
/// The given route has been pushed onto the navigator after this route. /// The given route has been pushed onto the navigator after this route.
/// Return true if the route before this one should be notified also. The /// Return true if the route before this one should be notified also. The
/// first route to return false will be the one passed to the /// first route to return false will be the one passed to the
/// NavigatorObserver's didPushModal() as the previousRoute. /// NavigatorObserver's didPush() as the previousRoute.
bool willPushNext(Route nextRoute) => false; bool willPushNext(Route nextRoute) => false;
/// The given route, which came after this one, has been popped off the /// The given route, which came after this one, has been popped off the
/// navigator. Return true if the route before this one should be notified /// navigator. Return true if the route before this one should be notified
/// also. The first route to return false will be the one passed to the /// also. The first route to return false will be the one passed to the
/// NavigatorObserver's didPushModal() as the previousRoute. /// NavigatorObserver's didPush() as the previousRoute.
bool didPopNext(Route nextRoute) => false; bool didPopNext(Route nextRoute) => false;
} }
...@@ -34,8 +34,8 @@ typedef Route RouteFactory(NamedRouteSettings settings); ...@@ -34,8 +34,8 @@ typedef Route RouteFactory(NamedRouteSettings settings);
class NavigatorObserver { class NavigatorObserver {
NavigatorState _navigator; NavigatorState _navigator;
NavigatorState get navigator => _navigator; NavigatorState get navigator => _navigator;
void didPushModal(Route route, Route previousRoute) { } void didPush(Route route, Route previousRoute) { }
void didPopModal(Route route, Route previousRoute) { } void didPop(Route route, Route previousRoute) { }
} }
class Navigator extends StatefulComponent { class Navigator extends StatefulComponent {
...@@ -61,8 +61,7 @@ class Navigator extends StatefulComponent { ...@@ -61,8 +61,7 @@ class Navigator extends StatefulComponent {
class NavigatorState extends State<Navigator> { class NavigatorState extends State<Navigator> {
final GlobalKey<OverlayState> _overlayKey = new GlobalKey<OverlayState>(); final GlobalKey<OverlayState> _overlayKey = new GlobalKey<OverlayState>();
// TODO(ianh): Rename _modal to _history or some such final List<Route> _history = new List<Route>();
final List<Route> _modal = new List<Route>();
void initState() { void initState() {
super.initState(); super.initState();
...@@ -84,11 +83,11 @@ class NavigatorState extends State<Navigator> { ...@@ -84,11 +83,11 @@ class NavigatorState extends State<Navigator> {
super.dispose(); super.dispose();
} }
bool get hasPreviousRoute => _modal.length > 1; bool get hasPreviousRoute => _history.length > 1;
OverlayState get overlay => _overlayKey.currentState; OverlayState get overlay => _overlayKey.currentState;
OverlayEntry get _currentOverlay { OverlayEntry get _currentOverlay {
for (Route route in _modal.reversed) { for (Route route in _history.reversed) {
if (route.overlayEntries.isNotEmpty) if (route.overlayEntries.isNotEmpty)
return route.overlayEntries.last; return route.overlayEntries.last;
} }
...@@ -106,12 +105,12 @@ class NavigatorState extends State<Navigator> { ...@@ -106,12 +105,12 @@ class NavigatorState extends State<Navigator> {
void push(Route route, { Set<Key> mostValuableKeys }) { void push(Route route, { Set<Key> mostValuableKeys }) {
setState(() { setState(() {
int index = _modal.length-1; int index = _history.length-1;
while (index >= 0 && _modal[index].willPushNext(route)) while (index >= 0 && _history[index].willPushNext(route))
index -= 1; index -= 1;
route.didPush(overlay, _currentOverlay); route.didPush(overlay, _currentOverlay);
config.observer?.didPushModal(route, index >= 0 ? _modal[index] : null); config.observer?.didPush(route, index >= 0 ? _history[index] : null);
_modal.add(route); _history.add(route);
}); });
} }
...@@ -126,13 +125,13 @@ class NavigatorState extends State<Navigator> { ...@@ -126,13 +125,13 @@ class NavigatorState extends State<Navigator> {
/// The type of the result argument, if provided, must match the type argument /// The type of the result argument, if provided, must match the type argument
/// of the class of the given route. (In practice, this is usually "dynamic".) /// of the class of the given route. (In practice, this is usually "dynamic".)
void remove(Route route, [dynamic result]) { void remove(Route route, [dynamic result]) {
assert(_modal.contains(route)); assert(_history.contains(route));
assert(route.overlayEntries.isEmpty); assert(route.overlayEntries.isEmpty);
if (_modal.last == route) { if (_history.last == route) {
pop(result); pop(result);
} else { } else {
setState(() { setState(() {
_modal.remove(route); _history.remove(route);
route.didPop(result); route.didPop(result);
}); });
} }
...@@ -149,21 +148,21 @@ class NavigatorState extends State<Navigator> { ...@@ -149,21 +148,21 @@ class NavigatorState extends State<Navigator> {
// We use setState to guarantee that we'll rebuild, since the routes can't // We use setState to guarantee that we'll rebuild, since the routes can't
// do that for themselves, even if they have changed their own state (e.g. // do that for themselves, even if they have changed their own state (e.g.
// ModalScope.isCurrent). // ModalScope.isCurrent).
assert(_modal.length > 1); assert(_history.length > 1);
Route route = _modal.removeLast(); Route route = _history.removeLast();
route.didPop(result); route.didPop(result);
int index = _modal.length-1; int index = _history.length-1;
while (index >= 0 && _modal[index].didPopNext(route)) while (index >= 0 && _history[index].didPopNext(route))
index -= 1; index -= 1;
config.observer?.didPopModal(route, index >= 0 ? _modal[index] : null); config.observer?.didPop(route, index >= 0 ? _history[index] : null);
}); });
} }
Widget build(BuildContext context) { Widget build(BuildContext context) {
assert(_modal.isNotEmpty); assert(_history.isNotEmpty);
return new Overlay( return new Overlay(
key: _overlayKey, key: _overlayKey,
initialEntries: _modal.first.overlayEntries initialEntries: _history.first.overlayEntries
); );
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment