// The `/` palette above a composer, shared by the chat and the new-session // draft: commands and skills of one directory, filtered as the name is typed. import 'package:flutter/material.dart'; import '../state.dart'; import '../l10n.dart'; const _slashLabels = { 'compact': '压缩', 'init': '初始化', 'review': '审查', 'goal': '目标', 'new-extension': '新建扩展', 'permission': '权限', 'plan': '计划', 'feedback': '反馈', }; String? slashLabel(SlashItem item) => item.skill ? null : _slashLabels[item.name]; /// Rows for a draft, or null when the palette should stay closed. It stays /// up while only the command name is typed and closes once an argument /// starts, unless the same text was dismissed with Esc. List? matchSlashItems({ required List items, required String text, String? dismissed, }) { if (dismissed == text) return null; final trimmed = text.trimLeft(); if (!trimmed.startsWith('/')) return null; if (trimmed.length > 1 && RegExp(r'\s').hasMatch(trimmed.substring(1))) { return null; } final query = trimmed.substring(1).toLowerCase().trim(); if (query.isEmpty) return items; bool hit(SlashItem item) { final label = slashLabel(item) ?? ''; return item.name.toLowerCase().contains(query) || label.toLowerCase().contains(query) || item.description.toLowerCase().contains(query); } return items.where(hit).toList() ..sort((a, b) { final aPrefix = a.name.toLowerCase().startsWith(query) ? 0 : 1; final bPrefix = b.name.toLowerCase().startsWith(query) ? 0 : 1; if (aPrefix != bPrefix) return aPrefix - bPrefix; if (a.skill != b.skill) return a.skill ? 1 : -1; return a.name.compareTo(b.name); }); } /// The panel itself: rows with an icon, the command and its description. class SlashPalette extends StatelessWidget { final List items; final int index; final bool loading; final ValueChanged onPick; const SlashPalette({ super.key, required this.items, required this.index, required this.loading, required this.onPick, }); @override Widget build(BuildContext context) { final scheme = Theme.of(context).colorScheme; final selected = items.isEmpty ? 0 : index.clamp(0, items.length - 1); Widget row(IconData icon, String title, String subtitle) => Padding( padding: const EdgeInsets.fromLTRB(14, 14, 14, 14), child: Row( children: [ SizedBox( width: 32, child: Icon(icon, size: 20, color: scheme.onSurfaceVariant), ), const SizedBox(width: 8), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ AppText(title, style: TextStyle(fontSize: 14, color: scheme.onSurface)), if (subtitle.isNotEmpty) AppText(subtitle, maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle( fontSize: 12, color: scheme.onSurfaceVariant)), ], ), ), ], ), ); return Container( constraints: const BoxConstraints(maxHeight: 56 * 5.5), clipBehavior: Clip.antiAlias, decoration: BoxDecoration( color: scheme.surfaceContainerHigh, borderRadius: BorderRadius.circular(20), border: Border.all(color: scheme.outlineVariant.withValues(alpha: 0.6)), ), child: items.isEmpty ? (loading ? const Padding( padding: EdgeInsets.all(18), child: Center( child: SizedBox( width: 20, height: 20, child: CircularProgressIndicator(strokeWidth: 2)), ), ) : row(Icons.search_off, '没有可用的指令', '这个目录还没有注册指令或技能')) : ListView.builder( shrinkWrap: true, padding: const EdgeInsets.symmetric(vertical: 6, horizontal: 6), itemCount: items.length, itemBuilder: (context, i) { final item = items[i]; final label = slashLabel(item); final isSelected = i == selected; return InkWell( borderRadius: BorderRadius.circular(14), onTap: () => onPick(item), child: Container( height: 56, padding: const EdgeInsets.symmetric(horizontal: 8), decoration: BoxDecoration( color: isSelected ? scheme.primary.withValues(alpha: 0.10) : null, borderRadius: BorderRadius.circular(14), ), child: Row( children: [ SizedBox( width: 32, child: Icon( item.skill ? Icons.extension_outlined : Icons.terminal, size: 20, color: scheme.onSurfaceVariant, ), ), const SizedBox(width: 8), Expanded( child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ AppText('/${item.name}', style: TextStyle( fontSize: 14, fontWeight: FontWeight.w600, color: scheme.onSurface)), if (label != null) Padding( padding: const EdgeInsets.only(left: 6), child: AppText(label, style: TextStyle( fontSize: 12, color: scheme.onSurfaceVariant)), ), ], ), if (item.description.isNotEmpty) AppText(item.description, maxLines: 1, overflow: TextOverflow.ellipsis, style: TextStyle( fontSize: 12, color: scheme.onSurfaceVariant)), ], ), ), ], ), ), ); }, ), ); } }