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, this.embedded = false, }); final AppSession session; final ServerEndpoint server; final ValueChanged onOpen; /// True as the shell's Search tab: no page chrome of its own. final bool embedded; @override State createState() => _SearchDestinationState(); } class _SearchDestinationState extends State { Animation? _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) { if (widget.embedded) { return _entered ? ConnectionGate( session: widget.session, server: widget.server, cupertino: cupertino, ) : const SizedBox.shrink(); } 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, embedded: widget.embedded, ); }, ); } /// 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; }