eed3806efb
Server: list_directory returns a directory_listing (path, home, parent, entries with is_dir/is_git_repo/hidden, truncated) confined to the server user's home subtree after cleaning and resolving symlinks, refusing pseudo filesystems and capping entries at 2000. Documented in docs/cli.md. App: "Choose folder…" in the create dialog opens a picker (full-screen page on phones, large dialog on wide windows, Material and Cupertino) with breadcrumbs, filter, Show hidden, git badges, dimmed files, recent folders, inline errors and "Start chat here". The probe app gains a folder fixture for captures.
289 lines
8.9 KiB
Dart
289 lines
8.9 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?';
|
|
|
|
/// 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) {
|
|
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
|
|
// 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: cupertino
|
|
? CupertinoButton(
|
|
key: const Key('chat-create-choose-folder'),
|
|
padding: const EdgeInsets.symmetric(vertical: 4),
|
|
onPressed: onBrowse,
|
|
child: const Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Icon(CupertinoIcons.folder),
|
|
SizedBox(width: 6),
|
|
Flexible(child: Text(chatChooseFolderLabel)),
|
|
],
|
|
),
|
|
)
|
|
: 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)
|
|
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),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|