Files
pipelines/app/lib/features/chat/conversation/chat_command_palette.dart
T
alan 63babc597a feat(app): offer the chat slash command palette
A leading slash opens the recorded command palette above the composer,
filtered by name prefix and driven by Up/Down, Enter, tap and Escape.

Slash drafts never reach the prompt path: a recorded command reports
locally that its action is not implemented yet, and an unknown word takes
the web client's no-send report with an explicit Send as message offer.
Enter and the primary control share that route.

Adds the chat commands domain vocabulary, the palette widget, composer
wiring and regression tests; notes/C9-commands.md records behaviour and
the remaining command-action gap.
2026-10-22 15:47:46 +01:00

128 lines
4.5 KiB
Dart

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<ChatCommand> commands;
/// The highlighted row. Callers clamp it to this list.
final int selectedIndex;
final ValueChanged<ChatCommand> 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<String>('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<String>('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),
),
],
),
),
),
),
),
),
),
),
],
),
);
}
}