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, this.searchBuilder, AppRoute initial = AppRoute.pipelines, }) : _route = initial, _searchReturn = initial.kind == AppRouteKind.search ? AppRoute.pipelines : initial; final Widget Function(BuildContext context, AppRoute route) builder; /// 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(); static const _pageKey = ValueKey('pipelines-shell'); static const _searchPageKey = ValueKey('app-search'); AppRoute _route; /// The destination that was open before Search, restored on close. AppRoute _searchReturn; AppRoute get route => _route; bool get searchOpen => _route.kind == AppRouteKind.search; @override AppRoute get currentConfiguration => _route; Page _page(LocalKey key, Widget child, {bool dialog = false}) => cupertino ? CupertinoPage(key: key, child: child, fullscreenDialog: dialog) : MaterialPage(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 shellRoute = searching ? _searchReturn : _route; return Navigator( key: _navigatorKey, pages: [ _page(_pageKey, builder(context, shellRoute)), // 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(); }, ); } /// 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 setInitialRoutePath(AppRoute configuration) async { _route = configuration; } @override Future setNewRoutePath(AppRoute configuration) async { if (configuration == _route) return; _route = configuration; notifyListeners(); } @override Future popRoute() async { if (searchOpen) { closeSearch(); 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)); }