Files
pipelines/app/lib/features/chat/ui/sessions/chat_create_fields.dart
T

253 lines
7.4 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'chat_adaptive.dart';
/// The web's directory prompt, also the directory picker's label.
const String chatCreatePrompt = 'Where should this chat live?';
/// The home choice: shown as `Home (~)` and sent as the `~` the server
/// documents.
const String chatHomeDirectory = '~';
const String chatHomeLabel = 'Home ($chatHomeDirectory)';
/// The web's directory choices: home first (the server's first directory is
/// its path), then the remaining server directories as sent.
List<String> chatDirectoryChoices(List<String> directories) => [
chatHomeDirectory,
...directories.skip(1),
];
/// The visible label of a directory choice; home names its resolved path.
String chatDirectoryLabel(String choice, List<String> directories) {
if (choice != chatHomeDirectory) return choice;
final home = directories.isEmpty ? chatHomeDirectory : directories.first;
return '$chatHomeLabel — $home';
}
/// The web's names for the chat harnesses; an unknown one shows as sent.
String chatBackendLabel(String backend) => switch (backend) {
'codex' => 'Codex',
'claude' => 'Claude',
'pi' => 'Pi',
'opencode' => 'OpenCode',
_ => backend,
};
/// The create dialog's server catalog area: loading, error with retry, the
/// directory choices and, only when the server offers more than one, the
/// harness choice.
class ChatCatalogFields extends StatelessWidget {
const ChatCatalogFields({
super.key,
required this.loading,
required this.error,
required this.directories,
required this.backends,
required this.directory,
required this.backend,
required this.onDirectory,
required this.onBackend,
required this.onRetry,
});
final bool loading;
final String? error;
final List<String> directories;
final List<String> backends;
final String? directory;
final String? backend;
final ValueChanged<String> onDirectory;
final ValueChanged<String> onBackend;
final VoidCallback onRetry;
@override
Widget build(BuildContext context) {
final cupertino =
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
if (loading) return const Text('Loading directories…');
if (error != null) {
return _message(cupertino, 'Unable to load chat options: $error');
}
final choices = chatDirectoryChoices(directories);
String directoryLabel(String choice) =>
chatDirectoryLabel(choice, directories);
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
if (cupertino)
CupertinoPickerField(
fieldKey: const Key('chat-create-server-directory'),
label: chatCreatePrompt,
value: directory,
values: choices,
labelOf: directoryLabel,
onSelected: onDirectory,
)
else
MaterialPickerField(
fieldKey: const Key('chat-create-server-directory'),
label: chatCreatePrompt,
value: directory,
values: choices,
labelOf: directoryLabel,
onSelected: onDirectory,
),
if (backends.length > 1)
if (cupertino)
CupertinoPickerField(
fieldKey: const Key('chat-create-backend'),
label: 'Chat harness',
value: backend,
values: backends,
labelOf: chatBackendLabel,
onSelected: onBackend,
)
else
MaterialPickerField(
fieldKey: const Key('chat-create-backend'),
label: 'Chat harness',
value: backend,
values: backends,
labelOf: chatBackendLabel,
onSelected: onBackend,
),
],
);
}
Widget _message(bool cupertino, String text) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(text),
Align(
alignment: AlignmentDirectional.centerStart,
child: cupertino
? CupertinoButton(
padding: EdgeInsets.zero,
onPressed: onRetry,
child: const Text('Retry'),
)
: TextButton(onPressed: onRetry, child: const Text('Retry')),
),
],
);
}
}
class MaterialPickerField extends StatelessWidget {
const MaterialPickerField({
super.key,
required this.fieldKey,
required this.label,
required this.value,
required this.values,
required this.onSelected,
this.labelOf,
});
final String label;
final String? value;
final List<String> values;
final ValueChanged<String> onSelected;
final Key fieldKey;
/// The visible text of a value; the value itself is what is sent.
final String Function(String value)? labelOf;
String _text(String value) => labelOf?.call(value) ?? value;
@override
Widget build(BuildContext context) {
return DropdownButtonFormField<String>(
key: fieldKey,
initialValue: value,
isExpanded: true,
// Intrinsic item height: a 2x label must not be clipped by the 48px
// default, and a real path must ellipsize instead of overflowing.
itemHeight: null,
decoration: InputDecoration(labelText: label),
selectedItemBuilder: (context) => [
for (final item in values) _fieldLabel(item),
],
items: [
for (final item in values)
DropdownMenuItem(value: item, child: _fieldLabel(item)),
],
onChanged: (item) {
if (item != null) onSelected(item);
},
);
}
Widget _fieldLabel(String item) {
return Text(_text(item), maxLines: 1, overflow: TextOverflow.ellipsis);
}
}
class CupertinoPickerField extends StatelessWidget {
const CupertinoPickerField({
super.key,
required this.fieldKey,
required this.label,
required this.value,
required this.values,
required this.onSelected,
this.labelOf,
});
final String label;
final String? value;
final List<String> values;
final ValueChanged<String> onSelected;
final Key fieldKey;
/// The visible text of a value; the value itself is what is sent.
final String Function(String value)? labelOf;
String _text(String value) => labelOf?.call(value) ?? value;
Future<void> _choose(BuildContext context) async {
final choice = await showCupertinoModalPopup<String>(
context: context,
builder: (sheetContext) => CupertinoActionSheet(
title: Text(label),
actions: [
for (final item in values)
CupertinoActionSheetAction(
onPressed: () => Navigator.pop(sheetContext, item),
child: Text(_text(item)),
),
],
cancelButton: CupertinoActionSheetAction(
onPressed: () => Navigator.pop(sheetContext),
child: const Text('Cancel'),
),
),
);
if (choice != null) onSelected(choice);
}
@override
Widget build(BuildContext context) {
return Semantics(
label: '$label: ${value == null ? '' : _text(value!)}',
button: true,
child: CupertinoButton(
key: fieldKey,
padding: const EdgeInsets.symmetric(vertical: 4),
onPressed: () => _choose(context),
child: Row(
children: [
Expanded(
child: Text('$label: ${value == null ? '' : _text(value!)}'),
),
const Icon(CupertinoIcons.chevron_down),
],
),
),
);
}
}