import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../core/api_gateway.dart'; import '../../ui/adaptive/host.dart'; import 'data/search_repository.dart'; import 'domain/search_controller.dart'; import 'domain/search_models.dart'; import 'ui/cupertino_search_body.dart'; import 'ui/search_result_view.dart'; import 'ui/material_search_body.dart'; /// Native omni-search over the Pipelines server: a standard search field, an /// explicit kind selector, and a flat result list whose rows open the server's /// own target through [onOpen]. The screen never mutates server state and never /// guesses a route for an unknown or targetless hit. class SearchScreen extends StatefulWidget { const SearchScreen({ super.key, required this.gateway, required this.onOpen, this.cupertino, this.embedded = false, }); final ApiGateway gateway; final ValueChanged onOpen; /// Lets the shell state the surface it composed. When omitted, iOS/macOS use /// Cupertino controls and every other platform uses Material. final bool? cupertino; /// True as the Search tab: the shell's bar names it, so no page bar. final bool embedded; @override State createState() => _SearchScreenState(); } class _SearchScreenState extends State { late final OmniSearchController _controller; late final TextEditingController _text; @override void initState() { super.initState(); _controller = OmniSearchController( repository: SearchRepository(widget.gateway), ); _text = TextEditingController(); _controller.addListener(_onState); } void _onState() { final query = _controller.query; if (_text.text != query) { _text.value = TextEditingValue( text: query, selection: TextSelection.collapsed(offset: query.length), ); } if (mounted) setState(() {}); } void _clear() { _text.clear(); _controller.updateQuery(''); } void _selectKind(SearchKind kind) => _controller.selectKind(kind); bool get _showHint { final body = _controller.bodyKind; return body == SearchBodyKind.idle || body == SearchBodyKind.empty; } Future _pickKind(BuildContext context) async { final selected = await showCupertinoModalPopup( context: context, builder: (context) => CupertinoActionSheet( title: const Text('Kind'), actions: [ for (final kind in SearchKind.values) CupertinoActionSheetAction( onPressed: () => Navigator.pop(context, kind), isDefaultAction: kind == _controller.parsed.kind, child: Text(kind.label), ), ], cancelButton: CupertinoActionSheetAction( onPressed: () => Navigator.pop(context), child: const Text('Cancel'), ), ), ); if (selected != null) _selectKind(selected); } @override void dispose() { _controller.removeListener(_onState); _controller.dispose(); _text.dispose(); super.dispose(); } @override Widget build(BuildContext context) { // Follow the actual host app class first: a CupertinoApp supplies no // Material theme, so reading only the platform would pick Material controls // inside a Cupertino host on macOS/iOS. final cupertino = widget.cupertino ?? (usesCupertinoHost(context) || Theme.of(context).platform == TargetPlatform.iOS || Theme.of(context).platform == TargetPlatform.macOS); return cupertino ? _cupertinoScreen() : _materialScreen(); } Widget _materialScreen() { final body = SafeArea( child: SearchArtifactScope( gateway: widget.gateway, onOpen: widget.onOpen, child: MaterialSearchBody( controller: _controller, query: _controller.parsed.query, onOpen: widget.onOpen, header: _materialHeader(), ), ), ); if (widget.embedded) return body; return Scaffold( appBar: AppBar(title: const Text('Search')), body: body, ); } Widget _materialHeader() => Padding( padding: const EdgeInsets.fromLTRB(16, 12, 16, 8), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, spacing: 8, children: [ SearchBar( key: const ValueKey('search.field'), controller: _text, autoFocus: true, hintText: 'Search pipelines, runs and artifacts', leading: const Icon(Icons.search), trailing: [ if (_text.text.isNotEmpty) IconButton( key: const ValueKey('search.clear'), onPressed: _clear, tooltip: 'Clear search', icon: const Icon(Icons.close), ), ], textInputAction: TextInputAction.search, onChanged: _controller.updateQuery, onSubmitted: (_) => _controller.submit(), ), InputDecorator( decoration: const InputDecoration( labelText: 'Kind', border: OutlineInputBorder(), ), child: DropdownButtonHideUnderline( child: DropdownButton( key: const ValueKey('search.kind'), value: _controller.parsed.kind, isExpanded: true, onChanged: (kind) { if (kind != null) _selectKind(kind); }, items: [ for (final kind in SearchKind.values) DropdownMenuItem(value: kind, child: Text(kind.label)), ], ), ), ), if (_showHint) const Text(_tokenHint), if (_controller.boundsText case final bounds?) Text(bounds), ], ), ); Widget _cupertinoScreen() { final body = SafeArea( child: SearchArtifactScope( gateway: widget.gateway, onOpen: widget.onOpen, child: CupertinoSearchBody( controller: _controller, query: _controller.parsed.query, onOpen: widget.onOpen, header: _cupertinoHeader(context), ), ), ); if (widget.embedded) return body; return CupertinoPageScaffold( navigationBar: const CupertinoNavigationBar(middle: Text('Search')), child: body, ); } Widget _cupertinoHeader(BuildContext context) => Padding( padding: const EdgeInsets.fromLTRB(16, 12, 16, 8), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, spacing: 8, children: [ CupertinoSearchTextField( key: const ValueKey('search.field'), controller: _text, autofocus: true, placeholder: 'Search pipelines, runs and artifacts', onChanged: _controller.updateQuery, onSubmitted: (_) => _controller.submit(), onSuffixTap: _clear, ), Align( alignment: AlignmentDirectional.centerStart, child: CupertinoButton( key: const ValueKey('search.kind'), padding: EdgeInsets.zero, onPressed: () => _pickKind(context), child: Row( mainAxisSize: MainAxisSize.max, spacing: 6, children: [ Expanded(child: Text('Kind: ${_controller.parsed.kind.label}')), const Icon(CupertinoIcons.chevron_down), ], ), ), ), if (_showHint) const Text(_tokenHint), if (_controller.boundsText case final bounds?) Text(bounds), ], ), ); } const _tokenHint = 'Filter with t:pipeline or type:artifact (pipeline, run, step, field, ' 'artifact, version). Last type wins; choosing Kind replaces it. Quote ' 'tokens to search literally.';