Files
pipelines/app/lib/features/chat/ui/sessions/chat_create_fields.dart
T
alan ae208699d8 Remove Cupertino chat session branches
Keep the Material-only session UI and narrow its tests to the supported host. Preserve public Cupertino parameters for callers while removing unreachable Cupertino widgets and variants.
2026-11-11 14:18:04 +00:00

179 lines
5.4 KiB
Dart

import 'package:flutter/material.dart';
/// The web's directory prompt, also the directory picker's label.
const String chatCreatePrompt = 'Where should this chat live?';
/// Opens the folder picker from the create dialog.
const String chatChooseFolderLabel = 'Choose folder…';
/// 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,
this.onBrowse,
});
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;
/// Opens the folder picker; null hides the "Choose folder" action.
final VoidCallback? onBrowse;
@override
Widget build(BuildContext context) {
if (loading) return const Text('Loading directories…');
if (error != null) {
return _message('Unable to load chat options: $error');
}
final choices = chatDirectoryChoices(directories);
String directoryLabel(String choice) =>
chatDirectoryLabel(choice, directories);
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Keyed by the value so a folder chosen in the picker replaces the
// dropdown's retained initial value.
KeyedSubtree(
key: ValueKey('chat-create-directory-$directory'),
child: MaterialPickerField(
fieldKey: const Key('chat-create-server-directory'),
label: chatCreatePrompt,
value: directory,
values: choices,
labelOf: directoryLabel,
onSelected: onDirectory,
),
),
if (onBrowse != null)
Align(
alignment: AlignmentDirectional.centerStart,
child: TextButton.icon(
key: const Key('chat-create-choose-folder'),
onPressed: onBrowse,
icon: const Icon(Icons.folder_open),
label: const Text(chatChooseFolderLabel),
),
),
if (backends.length > 1)
MaterialPickerField(
fieldKey: const Key('chat-create-backend'),
label: 'Chat harness',
value: backend,
values: backends,
labelOf: chatBackendLabel,
onSelected: onBackend,
),
],
);
}
Widget _message(String text) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(text),
Align(
alignment: AlignmentDirectional.centerStart,
child: 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);
}
}