Files
alan 9551eceaed Complete Material-only APIs and fixtures
Remove obsolete control-family flags and icon metadata across all callers, dead alternate themes and host helpers, obsolete test setup, and the unused icon-font dependency. Preserve native Material Back transitions. Make navigation fixtures independent of update plugins and answer auxiliary shell snapshots so the complete application suite can run deterministically.
2026-11-11 21:55:39 +00:00

264 lines
9.9 KiB
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(routeLocation(routeInformation.uri));
@override
RouteInformation restoreRouteInformation(AppRoute configuration) =>
// The route's path already carries its query (`?visit=N`, `?action=…`).
// Parsing it keeps that query a query instead of percent-encoding the
// `?` into the path, which would make a reload read one bogus key.
RouteInformation(uri: Uri.parse(configuration.path));
}
/// The app-relative location of [uri]: its path plus query, without origin.
///
/// [AppRoute.parse] understands this form, so a browser location such as
/// `/runs/7/stage/plan?visit=1` keeps the query instead of losing it to
/// `Uri.path`. A fragment is not part of an app route.
///
/// The native notification links the server publishes as a notification's
/// click target (and the apps register): `pipelines://run/<id>` is the run
/// route `/runs/<id>` and `pipelines://chat/<id>` the chat route `/chat/<id>`.
String routeLocation(Uri uri) {
final native = uri.scheme == 'pipelines';
final path = native && uri.host == 'run'
? '/runs${uri.path}'
: native && uri.host == 'chat'
? '/chat${uri.path}'
: uri.path;
return uri.hasQuery ? '$path?${uri.query}' : 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,
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;
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;
/// Whether a dialog, popup, or other route is covering the shell page.
bool get hasTransientRoute => _navigatorKey.currentState?.canPop() ?? false;
/// Context for app-level native dialogs owned by the mounted navigator.
BuildContext? get navigatorContext => _navigatorKey.currentContext;
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;
/// The route the shell renders underneath the transient Search, Templates
/// and Schedules pages. Those pages keep the shell mounted, so the route the
/// operator last opened there is still the shell's own destination.
AppRoute get shellRoute => searchOpen && searchBuilder != null
? _searchReturn
: templatesOpen || schedulesOpen
? _templatesReturn
: _route;
@override
AppRoute get currentConfiguration => _route;
Page<void> _page(LocalKey key, Widget child, {bool dialog = false}) =>
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 {
_rememberParent(configuration);
_route = configuration;
}
@override
Future<void> setNewRoutePath(AppRoute configuration) async {
if (configuration == _route) return;
_rememberParent(configuration);
_route = configuration;
notifyListeners();
}
/// System Back is the Navigator's own: it pops the page on top (a pushed
/// detail, Search, Templates, Schedules or a dialog) and otherwise leaves
/// Back to the platform. The app adds no Back behavior of its own.
@override
Future<bool> popRoute() async =>
await _navigatorKey.currentState?.maybePop() ?? false;
void _rememberParent(AppRoute route) {
if (route.kind == AppRouteKind.templates ||
route.kind == AppRouteKind.schedules) {
_templatesReturn = AppRoute.settings;
}
}
/// 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;
_rememberParent(route);
_route = route;
notifyListeners();
}
}
/// The location a launch starts at: the browser location on the web, the
/// deep link a native app was opened with, or the Pipelines list.
///
/// The origin stays out of route configuration so a serialised route is always
/// app-relative, but the path and its query are both part of the route: a
/// launch at `/runs/7/stage/plan?visit=1` must open exactly that visit.
RouteInformation initialRouteInformation() {
if (!kIsWeb) {
// A cold start from a deep link carries the full link as the platform's
// initial route; an ordinary launch opens the Pipelines list.
final launched =
WidgetsBinding.instance.platformDispatcher.defaultRouteName;
return RouteInformation(
uri: launched.isEmpty || launched == '/'
? Uri(path: '/pipelines')
: Uri.parse(launched),
);
}
return routeInformationForLocation(Uri.base);
}
/// The app-relative route information for a browser [base] location.
///
/// Exposed for the boot test: [Uri.base] cannot be changed inside a widget
/// test, so the web launch seam is exercised through this function.
@visibleForTesting
RouteInformation routeInformationForLocation(Uri base) {
final path = base.path.isEmpty ? '/' : base.path;
final query = base.hasQuery ? '?${base.query}' : '';
return RouteInformation(uri: Uri.parse('$path$query'));
}