feat(app): open Search from the shell over the shared session

This commit is contained in:
2026-10-21 23:42:41 +01:00
parent a897065fec
commit fd380ef2f4
10 changed files with 1215 additions and 14 deletions
+4 -1
View File
@@ -2,7 +2,7 @@
///
/// The set mirrors [AppDestination] but stays free of widget imports so the
/// parser and serialiser can be exercised without a Flutter host.
enum AppRouteKind { pipelines, runs, chat, settings }
enum AppRouteKind { pipelines, runs, chat, search, settings }
/// One page route: a destination plus the run it opens, if any.
///
@@ -15,6 +15,7 @@ class AppRoute {
static const pipelines = AppRoute(AppRouteKind.pipelines);
static const runs = AppRoute(AppRouteKind.runs);
static const chat = AppRoute(AppRouteKind.chat);
static const search = AppRoute(AppRouteKind.search);
static const settings = AppRoute(AppRouteKind.settings);
static const run = _RunRouteFactory();
@@ -40,6 +41,7 @@ class AppRoute {
chatSessionId == null || !isChatSessionId(chatSessionId!)
? '/chat'
: '/chat/${Uri.encodeComponent(chatSessionId!)}',
AppRouteKind.search => '/search',
AppRouteKind.settings => '/settings',
};
@@ -77,6 +79,7 @@ class AppRoute {
'runs' when segments.length == 2 => _runRoute(segments[1]),
'chat' when segments.length == 1 => chat,
'chat' when segments.length == 2 => _chatRoute(segments[1]),
'search' when segments.length == 1 => search,
'settings' when segments.length == 1 => settings,
_ => pipelines,
};
+53 -11
View File
@@ -29,11 +29,20 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
required this.onPopToRuns,
required this.onPopToChat,
required this.cupertino,
this.searchBuilder,
AppRoute initial = AppRoute.pipelines,
}) : _route = initial;
}) : _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;
@@ -44,33 +53,62 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
final _navigatorKey = GlobalKey<NavigatorState>();
static const _pageKey = ValueKey<String>('pipelines-shell');
static const _searchPageKey = ValueKey<String>('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;
/// 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.
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 child = builder(context, _route);
final searching = searchOpen && searchBuilder != null;
final shellRoute = searching ? _searchReturn : _route;
return Navigator(
key: _navigatorKey,
pages: [
if (cupertino)
CupertinoPage<void>(key: _pageKey, child: child)
else
MaterialPage<void>(key: _pageKey, child: child),
_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: (_) {},
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<void> setInitialRoutePath(AppRoute configuration) async {
_route = configuration;
@@ -85,6 +123,10 @@ class AppRouterDelegate extends RouterDelegate<AppRoute> with ChangeNotifier {
@override
Future<bool> popRoute() async {
if (searchOpen) {
closeSearch();
return true;
}
if (_route.runId != null) {
onPopToRuns();
return true;
+28
View File
@@ -1,8 +1,10 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import '../core/api_gateway.dart';
import '../core/transport/pipelines_client.dart';
import '../shell/adaptive_shell.dart';
import '../shell/destination.dart';
@@ -14,6 +16,7 @@ import 'app_session.dart';
import 'chat_destination.dart';
import 'pipelines_destination.dart';
import 'runs_destination.dart';
import 'search_destination.dart';
import 'server_endpoint.dart';
/// Composes the live application around one [AppSession] and one [Router].
@@ -73,6 +76,7 @@ class _PipelinesAppState extends State<PipelinesApp> {
cupertino: _cupertino,
onPopToRuns: () => _delegate.goTo(AppRoute.runs),
onPopToChat: () => _delegate.goTo(AppRoute.chat),
searchBuilder: _buildSearch,
builder: _buildShell,
);
_config = RouterConfig<AppRoute>(
@@ -97,11 +101,33 @@ class _PipelinesAppState extends State<PipelinesApp> {
Widget build(BuildContext context) =>
MyApp.router(cupertino: _cupertino, routerConfig: _config);
/// The transient Search page. Escape matches the browser's panel shortcut;
/// the native Back button and browser Back come from the router and page.
Widget _buildSearch(BuildContext context) => CallbackShortcuts(
bindings: {
const SingleActivator(LogicalKeyboardKey.escape): _delegate.closeSearch,
},
child: SearchDestination(
session: _session,
server: widget.server,
onOpen: _openSearchResult,
),
);
/// Open a result through an existing app route only. A run target uses the
/// route the Runs destination already owns; the hit map itself is unchanged,
/// and pipeline/version hits have no route yet, so they are not guessed.
void _openSearchResult(JsonMap hit) {
final runId = searchHitRunId(hit);
if (runId != null) _delegate.goTo(AppRoute.run(runId));
}
Widget _buildShell(BuildContext context, AppRoute route) => AdaptiveShell(
cupertino: _cupertino,
destination: _destinationOf(route.kind),
onDestinationSelected: (destination) =>
_delegate.goTo(_routeOf(destination)),
onSearch: _delegate.openSearch,
destinationBodies: {
AppDestination.pipelines: (_) =>
PipelinesDestination(session: _session, server: widget.server),
@@ -131,6 +157,8 @@ class _PipelinesAppState extends State<PipelinesApp> {
AppRouteKind.pipelines => AppDestination.pipelines,
AppRouteKind.runs => AppDestination.runs,
AppRouteKind.chat => AppDestination.chat,
// Search is a transient page above the shell; it never becomes a tab.
AppRouteKind.search => AppDestination.pipelines,
AppRouteKind.settings => AppDestination.settings,
};
+138
View File
@@ -0,0 +1,138 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../core/api_gateway.dart';
import '../features/search/search_screen.dart';
import '../ui/adaptive/host.dart';
import 'app_session.dart';
import 'connection_status.dart';
import 'server_endpoint.dart';
/// The Search page above the shell: the landed screen over the shared gateway.
///
/// Before the first successful connection there is nothing to search, so the
/// native connection gate is shown inside the same title bar and Close still
/// returns to the previous destination. In a Cupertino host the screen is
/// mounted once the route's entrance has completed: during that transition a
/// focus request is dropped, so mounting earlier would open Search with no
/// keyboard or caret. Material mounts it immediately as usual.
class SearchDestination extends StatefulWidget {
const SearchDestination({
super.key,
required this.session,
required this.server,
required this.onOpen,
});
final AppSession session;
final ServerEndpoint server;
final ValueChanged<JsonMap> onOpen;
@override
State<SearchDestination> createState() => _SearchDestinationState();
}
class _SearchDestinationState extends State<SearchDestination> {
Animation<double>? _animation;
bool _entered = false;
@override
void didChangeDependencies() {
super.didChangeDependencies();
// Material's page transition keeps the autofocus request; only the
// Cupertino push needs to wait for the route to become current.
if (!usesCupertinoHost(context)) {
_entered = true;
return;
}
final animation = ModalRoute.of(context)?.animation;
if (identical(animation, _animation)) return;
_animation?.removeStatusListener(_onStatus);
_animation = animation;
if (animation == null || animation.status == AnimationStatus.completed) {
_entered = true;
} else {
animation.addStatusListener(_onStatus);
}
}
void _onStatus(AnimationStatus status) {
if (status != AnimationStatus.completed) return;
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted) setState(() => _entered = true);
});
}
@override
void dispose() {
_animation?.removeStatusListener(_onStatus);
super.dispose();
}
@override
Widget build(BuildContext context) => AnimatedBuilder(
animation: widget.session,
builder: (context, _) {
final cupertino = usesCupertinoHost(context);
if (!widget.session.everConnected || !_entered) {
return _SearchPage(
cupertino: cupertino,
child: _entered
? ConnectionGate(
session: widget.session,
server: widget.server,
cupertino: cupertino,
)
: const SizedBox.shrink(),
);
}
return SearchScreen(
gateway: widget.session.gateway,
onOpen: widget.onOpen,
cupertino: cupertino,
);
},
);
}
/// The native title bar that owns Back while Search has no screen yet.
class _SearchPage extends StatelessWidget {
const _SearchPage({required this.cupertino, required this.child});
final bool cupertino;
final Widget child;
@override
Widget build(BuildContext context) => cupertino
? CupertinoPageScaffold(
navigationBar: const CupertinoNavigationBar(middle: Text('Search')),
child: SafeArea(child: child),
)
: Scaffold(
appBar: AppBar(title: const Text('Search')),
body: SafeArea(child: child),
);
}
/// The run a result names, taken only from the server's own payload document.
///
/// The browser opens run, step, field and artifact hits at their recorded
/// sequence/field; this app has only the run route, so the run id is the one
/// target it can honour. Pipeline/version hits return null: there is no
/// pipeline-detail route yet and a guessed one would be invented navigation.
int? searchHitRunId(JsonMap hit) {
final run = _positiveId(hit['run'], 'id');
if (run != null) return run;
for (final key in const ['step', 'field', 'artifact']) {
final id = _positiveId(hit[key], 'run_id');
if (id != null) return id;
}
return null;
}
int? _positiveId(Object? payload, String key) {
if (payload is! Map) return null;
final value = payload[key];
final id = value is int ? value : int.tryParse('$value');
return id != null && id > 0 ? id : null;
}