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 { const AppRouteParser(); @override Future 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 with ChangeNotifier { AppRouterDelegate({ required this.builder, required this.onPopToRuns, required this.onPopToChat, required this.cupertino, AppRoute initial = AppRoute.pipelines, }) : _route = initial; final Widget Function(BuildContext context, AppRoute route) builder; /// 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(); static const _pageKey = ValueKey('pipelines-shell'); AppRoute _route; AppRoute get route => _route; @override AppRoute get currentConfiguration => _route; /// A single-page Navigator supplies the Overlay, overlay portals and route /// context that native controls (action sheets, tooltips) require. The page /// key is stable, so changing the route updates the shell in place instead of /// remounting it and losing the open run. @override Widget build(BuildContext context) { final child = builder(context, _route); return Navigator( key: _navigatorKey, pages: [ if (cupertino) CupertinoPage(key: _pageKey, child: child) else MaterialPage(key: _pageKey, child: child), ], onDidRemovePage: (_) {}, ); } @override Future setInitialRoutePath(AppRoute configuration) async { _route = configuration; } @override Future setNewRoutePath(AppRoute configuration) async { if (configuration == _route) return; _route = configuration; notifyListeners(); } @override Future popRoute() async { 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)); }