import 'package:cutdex_ui/icons.dart'; import 'package:cutdex_ui/theme.dart'; import 'package:cutdex_ui/widgets.dart'; import 'package:flutter/material.dart'; import '../../l10n/app_localizations.dart'; /// Desktop conversation navigation, independent of project and agent state. class ChatHeader extends StatelessWidget { const ChatHeader({ super.key, required this.title, required this.historyBuilder, required this.onNew, required this.actions, this.busy = false, this.panelActions = const [], }); final String title; final Widget Function(BuildContext context, VoidCallback close) historyBuilder; final VoidCallback? onNew; final List actions; final bool busy; final List panelActions; @override Widget build(BuildContext context) { final l10n = AppLocalizations.of(context)!; final iconStyle = IconButton.styleFrom( fixedSize: const Size.square(26), minimumSize: const Size.square(26), maximumSize: const Size.square(26), padding: EdgeInsets.zero, iconSize: 16, tapTargetSize: MaterialTapTargetSize.shrinkWrap, ); return SizedBox( key: const Key('project-chat-header'), height: AppDesktopMetrics.panelHeader, child: Padding( padding: const EdgeInsets.symmetric(horizontal: 10), child: Row( children: [ Expanded( child: AppPopover( enabled: !busy, width: 300, surfaceKey: const Key('project-chat-history-menu'), child: Builder( builder: (menuContext) { final menu = MenuController.maybeOf(menuContext); return historyBuilder(menuContext, () { if (menu?.isOpen == true) menu!.close(); }); }, ), builder: (context, toggle) => Tooltip( message: '$title\n${l10n.agentConversations}', child: TextButton( key: const Key('project-chat-history'), onPressed: busy ? null : toggle, style: AppTheme.toolbarButtonStyle(context).copyWith( minimumSize: const WidgetStatePropertyAll(Size(0, 26)), maximumSize: const WidgetStatePropertyAll( Size(double.infinity, 26), ), tapTargetSize: MaterialTapTargetSize.shrinkWrap, padding: const WidgetStatePropertyAll( EdgeInsets.symmetric(horizontal: 6), ), ), child: Row( children: [ Expanded( child: Text( title, key: const Key('project-chat-title'), maxLines: 1, overflow: TextOverflow.ellipsis, style: AppTheme.toolbarTextStyle(context) .copyWith(fontWeight: FontWeight.w500), ), ), const SizedBox(width: 4), const Icon(AppIcons.expandMore, size: 14), ], ), ), ), ), ), const SizedBox(width: 4), IconButton( key: const Key('project-chat-new'), tooltip: l10n.agentNewConversation, style: iconStyle, onPressed: busy ? null : onNew, icon: const Icon(AppIcons.edit), ), const SizedBox(width: 4), AppDropdownMenu( enabled: (!busy && actions.isNotEmpty) || panelActions.isNotEmpty, items: [ if (!busy) ...actions, if (!busy && actions.isNotEmpty && panelActions.isNotEmpty) const AppMenuSeparator(), ...panelActions, ], builder: (context, toggle, focus) => IconButton( key: const Key('project-chat-more'), tooltip: l10n.agentConversationActions, style: iconStyle, focusNode: focus, onPressed: toggle, icon: const Icon(AppIcons.more), ), ), ], ), ), ); } } typedef ChatHistoryEntry = ({String id, String title}); /// Search remains local to the currently open project's conversation list. class ChatHistoryMenu extends StatefulWidget { const ChatHistoryMenu({ super.key, required this.entries, required this.onSelect, this.selectedId, this.itemBuilder, this.footer, this.error, }); static const contentInset = 8.0; static const sectionPadding = EdgeInsets.symmetric( horizontal: contentInset, vertical: 8, ); final List entries; final String? selectedId; final ValueChanged onSelect; final Widget Function(BuildContext, String id, VoidCallback onSelect)? itemBuilder; final Widget? footer; final String? error; @override State createState() => _ChatHistoryMenuState(); } class _ChatHistoryMenuState extends State { String _query = ''; @override void didUpdateWidget(ChatHistoryMenu oldWidget) { super.didUpdateWidget(oldWidget); if (widget.entries.isEmpty) _query = ''; } @override Widget build(BuildContext context) { final l10n = AppLocalizations.of(context)!; final searchStyle = AppTheme.inputTextStyle( context, style: AppTheme.toolbarTextStyle(context), ); final entries = widget.entries.where( (entry) => entry.title.toLowerCase().contains(_query.trim().toLowerCase()), ); return SizedBox( height: widget.entries.isEmpty && widget.footer == null && widget.error == null ? 88 : 360, child: Padding( padding: const EdgeInsets.all(8), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ if (widget.entries.isNotEmpty) TextField( key: const Key('project-chat-search'), autofocus: true, style: searchStyle, cursorHeight: AppTheme.inputCursorHeight( context, style: searchStyle, ), textAlignVertical: TextAlignVertical.center, decoration: InputDecoration( isDense: true, constraints: const BoxConstraints(minHeight: 28), contentPadding: const EdgeInsets.symmetric( horizontal: 8, vertical: 5, ), hintText: l10n.searchOptions(l10n.agentConversations), hintStyle: AppTheme.inputHintStyle( context, style: searchStyle, ), prefixIcon: const Icon(AppIcons.search, size: 14), prefixIconConstraints: const BoxConstraints( minWidth: 30, minHeight: 28, ), ), onChanged: (value) => setState(() => _query = value), ), Padding( padding: ChatHistoryMenu.sectionPadding, child: Text( key: const Key('project-chat-group'), l10n.agentConversations, maxLines: 1, overflow: TextOverflow.ellipsis, style: Theme.of(context).textTheme.bodySmall, ), ), Expanded( child: ListView( primary: false, padding: EdgeInsets.zero, children: [ if (entries.isEmpty) Padding( padding: ChatHistoryMenu.sectionPadding, child: Text( _query.trim().isEmpty ? l10n.agentNoConversations : l10n.noMatchingOptions, ), ), for (final entry in entries) widget.itemBuilder?.call( context, entry.id, () => widget.onSelect(entry.id), ) ?? AppMenuItem( key: ValueKey('chat-history-${entry.id}'), label: entry.title, selectOption: true, selected: entry.id == widget.selectedId, onPressed: () => widget.onSelect(entry.id), ), if (widget.error != null) Padding( padding: ChatHistoryMenu.sectionPadding, child: Text(widget.error!), ), if (widget.footer != null) widget.footer!, ], ), ), ], ), ), ); } }