Files
pipelines/app/lib/application/app_router.dart
T

227 lines
7.5 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'app_route.dart';
/// Parses the browser (or platform) location into the app's routes.
class AppRouteParser extends RouteInformationParser<AppRoute> {
const AppRouteParser();
@override
Future<AppRoute> parseRouteInformation(
RouteInformation routeInformation,
) async => AppRoute.parse(routeInformation.uri.path);
@override
RouteInformation restoreRouteInformation(AppRoute configuration) =>
RouteInformation(uri: Uri(path: configuration.path));
}
/// Owns the app's current route and builds the shell for it.
///
/// Route changes made by the operator go through [goTo]; the Router reports
/// them as browser history entries. Browser Back/Forward and deep links arrive
/// through [setNewRoutePath] and update the same route.
class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
AppRouterDelegate({
required this.builder,
required this.onPopToRuns,
required this.onPopToChat,
required this.cupertino,
this.searchBuilder,
this.templatesBuilder,
this.schedulesBuilder,
AppRoute initial = AppRoute.pipelines,
}) : _route = initial,
_searchReturn = initial.kind == AppRouteKind.search
? AppRoute.pipelines
: initial,
_templatesReturn = initial.kind == AppRouteKind.templates
|| initial.kind == AppRouteKind.schedules
? AppRoute.settings
: initial;
final Widget Function(BuildContext context, AppRoute route) builder;
/// The Templates page above the shell. Templates belongs to Settings, so the
/// shell keeps the Settings destination mounted underneath and its state is
/// restored when this page closes.
final Widget Function(BuildContext context)? templatesBuilder;
final Widget Function(BuildContext context)? schedulesBuilder;
/// The Search page above the shell. Search is transient chrome, not a fifth
/// tab: the shell stays mounted underneath so closing restores its loaded
/// state and selection, and the standard navigation bar supplies Back.
final Widget Function(BuildContext context)? searchBuilder;
/// Called when a system Back belongs to the Runs detail, not the app stack.
final VoidCallback onPopToRuns;
/// Called when a system Back belongs to one chat session.
final VoidCallback onPopToChat;
final bool cupertino;
final _navigatorKey = GlobalKey<NavigatorState>();
static const _pageKey = ValueKey<String>('pipelines-shell');
static const _searchPageKey = ValueKey<String>('app-search');
static const _templatesPageKey = ValueKey<String>('app-templates');
static const _schedulesPageKey = ValueKey<String>('app-schedules');
AppRoute _route;
/// The destination that was open before Search, restored on close.
AppRoute _searchReturn;
/// The Settings route the Templates page closes back to.
AppRoute _templatesReturn;
AppRoute get route => _route;
bool get searchOpen => _route.kind == AppRouteKind.search;
bool get templatesOpen => _route.kind == AppRouteKind.templates;
/// The saved-schedule list page, opened above Settings.
bool get schedulesOpen => _route.kind == AppRouteKind.schedules;
@override
AppRoute get currentConfiguration => _route;
Page<void> _page(LocalKey key, Widget child, {bool dialog = false}) =>
cupertino
? CupertinoPage<void>(key: key, child: child, fullscreenDialog: dialog)
: MaterialPage<void>(key: key, child: child, fullscreenDialog: dialog);
/// A page-based Navigator supplies the Overlay, overlay portals and route
/// context that native controls (action sheets, tooltips) require. The shell
/// page key is stable, so changing the route updates it in place instead of
/// remounting it and losing the open run. Search is the only extra page, so
/// its native Close action pops just that page.
@override
Widget build(BuildContext context) {
final searching = searchOpen && searchBuilder != null;
final templates = templatesOpen && templatesBuilder != null;
final schedules = schedulesOpen && schedulesBuilder != null;
final shellRoute = searching
? _searchReturn
: templates || schedules
? _templatesReturn
: _route;
return Navigator(
key: _navigatorKey,
pages: [
_page(_pageKey, builder(context, shellRoute)),
// Templates is a Settings sub-page: the shell stays mounted underneath,
// so closing restores Settings exactly as it was left.
if (templates) _page(_templatesPageKey, templatesBuilder!(context)),
if (schedules) _page(_schedulesPageKey, schedulesBuilder!(context)),
// Search is a transient full-screen mode, so a full-screen dialog gives
// the standard Close/Cancel action instead of a Back chevron.
if (searching)
_page(_searchPageKey, searchBuilder!(context), dialog: true),
],
onDidRemovePage: (page) {
if (page.key == _searchPageKey) closeSearch();
if (page.key == _templatesPageKey) closeTemplates();
if (page.key == _schedulesPageKey) closeSchedules();
},
);
}
/// Open the Templates gallery above Settings. The browser history entry is
/// `/templates`; Back returns to the same Settings screen instance.
void openTemplates() {
if (templatesOpen) return;
_templatesReturn = AppRoute.settings;
goTo(AppRoute.templates);
}
void closeTemplates() {
if (!templatesOpen) return;
goTo(AppRoute.settings);
}
/// Open the saved-schedule list above Settings. Back restores the same
/// Settings screen instance, like Templates.
void openSchedules() {
if (schedulesOpen) return;
_templatesReturn = AppRoute.settings;
goTo(AppRoute.schedules);
}
void closeSchedules() {
if (!schedulesOpen) return;
goTo(AppRoute.settings);
}
/// Open Search above the current destination; its route carries the browser
/// history entry, and close/back returns to what was open before.
void openSearch() {
if (searchOpen) return;
_searchReturn = _route;
goTo(AppRoute.search);
}
void closeSearch() {
if (!searchOpen) return;
goTo(_searchReturn);
}
@override
Future<void> setInitialRoutePath(AppRoute configuration) async {
_route = configuration;
}
@override
Future<void> setNewRoutePath(AppRoute configuration) async {
if (configuration == _route) return;
_route = configuration;
notifyListeners();
}
@override
Future<bool> popRoute() async {
if (searchOpen) {
closeSearch();
return true;
}
if (schedulesOpen) {
closeSchedules();
return true;
}
if (templatesOpen) {
closeTemplates();
return true;
}
if (_route.runId != null) {
onPopToRuns();
return true;
}
if (_route.chatSessionId != null) {
onPopToChat();
return true;
}
return false;
}
/// Records a user move; the Router turns the new configuration into a
/// history entry (or a replacement when it already matches the location).
void goTo(AppRoute route) {
if (route == _route) return;
_route = route;
notifyListeners();
}
}
/// The location a web launch starts at, or the Pipelines list elsewhere.
///
/// Only the path is meaningful to [AppRouteParser]; the origin stays out of
/// route configuration so a serialised route is always app-relative.
RouteInformation initialRouteInformation() {
if (!kIsWeb) return RouteInformation(uri: Uri(path: '/pipelines'));
final path = Uri.base.path;
return RouteInformation(uri: Uri(path: path.isEmpty ? '/' : path));
}