import 'package:flutter/material.dart'; import '../../l10n/l10n.dart'; import '../../shared/layout/composer_layout.dart'; import '../../shared/theme/composer_style.dart'; import 'package:cutdex_ui/theme.dart'; import '../../workspace/project_state.dart'; class ComposerProjectSelector extends StatefulWidget { const ComposerProjectSelector({ super.key, required this.projects, required this.selected, required this.busy, required this.onSelect, required this.onCreate, required this.backgroundColor, }); final List projects; final RecentProject? selected; final bool busy; final ValueChanged onSelect; final VoidCallback? onCreate; final Color backgroundColor; @override State createState() => _ComposerProjectSelectorState(); } class _ComposerProjectSelectorState extends State { final _menu = MenuController(); final _search = TextEditingController(); @override void dispose() { _search.dispose(); super.dispose(); } Widget _item({ required Key key, required String label, required IconData icon, bool? selected, bool secondary = false, VoidCallback? onPressed, }) { final colors = Theme.of(context).colorScheme; return MenuItemButton( key: key, onPressed: onPressed, style: AppComposerStyle.projectMenuItem(context, secondary: secondary), child: Semantics( selected: selected, child: Row( children: [ Icon( icon, size: ComposerLayout.menuIconSize, color: onPressed == null ? Theme.of(context).disabledColor : colors.onSurfaceVariant, ), const SizedBox(width: ComposerLayout.menuIconGap), Expanded( child: Text(label, maxLines: 1, overflow: TextOverflow.ellipsis), ), if (selected == true) ...[ const SizedBox(width: ComposerLayout.menuIconGap), Icon( Icons.check, size: ComposerLayout.menuIconSize, color: colors.onSurfaceVariant, ), ], ], ), ), ); } Widget _separator() => const Padding( padding: AppComposerStyle.menuSeparatorPadding, child: Divider( height: 1, thickness: AppComposerStyle.menuSeparatorThickness, ), ); @override Widget build(BuildContext context) { final query = _search.text.trim().toLowerCase(); final projects = widget.projects .where((project) => project.name.toLowerCase().contains(query)) .toList(); return Material( key: const Key('composer-project-bar'), color: widget.backgroundColor, borderRadius: const BorderRadius.vertical( top: Radius.circular(ComposerLayout.utilityRadius), ), child: Padding( padding: AppComposerStyle.utilityPadding, child: Align( alignment: AlignmentDirectional.centerStart, child: MenuAnchor( controller: _menu, consumeOutsideTap: false, onClose: () => setState(_search.clear), style: AppComposerStyle.projectMenu(context), menuChildren: [ SizedBox( width: ComposerLayout.menuContentWidth, child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ SizedBox( height: ComposerLayout.menuRowHeight, child: TextField( key: const Key('composer-project-search'), controller: _search, autofocus: true, style: AppTheme.composerTextStyle(context), onChanged: (_) => setState(() {}), decoration: AppComposerStyle.projectSearchDecoration( context, hintText: context.l10n.searchProjects, ), ), ), _separator(), ConstrainedBox( constraints: const BoxConstraints( maxHeight: ComposerLayout.menuListMaxHeight, ), child: SingleChildScrollView( primary: false, child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ if (projects.isEmpty) Padding( padding: AppComposerStyle.menuEmptyPadding, child: Text( query.isEmpty ? context.l10n.projectsNone : context.l10n.noMatchingProjects, style: Theme.of(context).textTheme.bodySmall, ), ), for (final project in projects) _item( key: ValueKey('composer-project-${project.id}'), label: project.name, icon: Icons.folder_outlined, selected: project.id == widget.selected?.id, onPressed: widget.busy || !project.availability.canOpen ? null : () { _menu.close(); widget.onSelect(project); }, ), ], ), ), ), _separator(), _item( key: const Key('composer-new-project'), secondary: true, label: context.l10n.newProject, icon: Icons.add, onPressed: widget.busy || widget.onCreate == null ? null : () { _menu.close(); widget.onCreate!(); }, ), ], ), ), ], builder: (context, controller, _) => TextButton.icon( key: const Key('composer-project-trigger'), onPressed: widget.busy ? null : () => controller.isOpen ? controller.close() : controller.open(), style: AppComposerStyle.projectTrigger(context), icon: const Icon( Icons.folder_outlined, size: ComposerLayout.menuIconSize, ), label: Text( widget.selected?.name ?? context.l10n.selectProject, maxLines: 1, overflow: TextOverflow.ellipsis, ), ), ), ), ), ); } }