import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../domain/chat_commands.dart'; import '../ui/sessions/chat_adaptive.dart'; /// The command palette above the composer: the recorded commands matching the /// slash word, each with its own `/name` and summary. /// /// Rows are standard host controls (Material's selected `ListTile`, Cupertino's /// own button on a popup surface), so typography, colours and text scaling stay /// the platform's. The list is content-sized and never imposes a fixed height; /// a viewport-bound scroll keeps a tall text scale reachable. class ChatCommandPalette extends StatelessWidget { const ChatCommandPalette({ super.key, required this.commands, required this.selectedIndex, required this.onSelected, }); final List commands; /// The highlighted row. Callers clamp it to this list. final int selectedIndex; final ValueChanged onSelected; @override Widget build(BuildContext context) { if (commands.isEmpty) return const SizedBox.shrink(); final cupertino = chatControlsStyleOf(context) == ChatControlsStyle.cupertino; final list = cupertino ? _cupertinoRows(context) : _materialRows(context); return ConstrainedBox( constraints: BoxConstraints( maxHeight: MediaQuery.sizeOf(context).height * 0.4, ), child: SingleChildScrollView( child: Semantics( container: true, label: 'Command suggestions', child: list, ), ), ); } Widget _materialRows(BuildContext context) { return Card( key: const Key('chat-command-palette'), margin: EdgeInsets.zero, clipBehavior: Clip.antiAlias, child: Column( mainAxisSize: MainAxisSize.min, children: [ for (var index = 0; index < commands.length; index += 1) ListTile( key: ValueKey('chat-command-${commands[index].name}'), selected: index == selectedIndex, title: Text('/${commands[index].name}'), subtitle: Text(commands[index].summary), onTap: () => onSelected(commands[index]), ), ], ), ); } /// Cupertino has no list tile that keeps a wrapping summary, so each row is /// the host's own button, with the name and summary at the native text style /// and the selected row on the host's semantic fill. Widget _cupertinoRows(BuildContext context) { final secondary = CupertinoColors.secondaryLabel.resolveFrom(context); final selectedFill = CupertinoColors.systemFill.resolveFrom(context); final textStyle = CupertinoTheme.of(context).textTheme.textStyle; return CupertinoPopupSurface( key: const Key('chat-command-palette'), child: Column( mainAxisSize: MainAxisSize.min, children: [ for (var index = 0; index < commands.length; index += 1) Semantics( selected: index == selectedIndex, child: SizedBox( width: double.infinity, child: CupertinoButton( key: ValueKey('chat-command-${commands[index].name}'), padding: EdgeInsets.zero, onPressed: () => onSelected(commands[index]), child: SizedBox( width: double.infinity, child: DecoratedBox( decoration: BoxDecoration( color: index == selectedIndex ? selectedFill : null, ), child: Padding( padding: const EdgeInsetsDirectional.fromSTEB( 16, 10, 16, 10, ), child: DefaultTextStyle.merge( style: textStyle, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text('/${commands[index].name}'), Text( commands[index].summary, style: TextStyle(color: secondary), ), ], ), ), ), ), ), ), ), ), ], ), ); } }