Add a home-restricted folder picker to the new-chat dialog
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.
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
import '../../../core/api_gateway.dart';
|
||||
|
||||
/// One name in a `directory_listing` reply. Only directories are selectable;
|
||||
/// files are listed for context.
|
||||
class DirectoryEntry {
|
||||
const DirectoryEntry({
|
||||
required this.name,
|
||||
required this.path,
|
||||
required this.isDir,
|
||||
required this.isGitRepo,
|
||||
required this.hidden,
|
||||
});
|
||||
|
||||
factory DirectoryEntry.fromJson(JsonMap json) => DirectoryEntry(
|
||||
name: '${json['name'] ?? ''}',
|
||||
path: '${json['path'] ?? ''}',
|
||||
isDir: json['is_dir'] == true,
|
||||
isGitRepo: json['is_git_repo'] == true,
|
||||
hidden: json['hidden'] == true,
|
||||
);
|
||||
|
||||
final String name;
|
||||
final String path;
|
||||
final bool isDir;
|
||||
final bool isGitRepo;
|
||||
final bool hidden;
|
||||
}
|
||||
|
||||
/// The server's home-restricted listing of one folder.
|
||||
class DirectoryListing {
|
||||
const DirectoryListing({
|
||||
required this.path,
|
||||
required this.home,
|
||||
required this.parent,
|
||||
required this.entries,
|
||||
this.truncated = false,
|
||||
});
|
||||
|
||||
/// Reads the `directory_listing` body; a reply without one is a protocol
|
||||
/// error, never an empty folder.
|
||||
factory DirectoryListing.fromReply(JsonMap reply) {
|
||||
final body = reply['directory_listing'];
|
||||
if (body is! Map || body['path'] is! String || body['home'] is! String) {
|
||||
throw StateError('Server did not return a directory listing.');
|
||||
}
|
||||
final entries = body['entries'];
|
||||
return DirectoryListing(
|
||||
path: body['path'] as String,
|
||||
home: body['home'] as String,
|
||||
parent: body['parent'] is String ? body['parent'] as String : null,
|
||||
entries: [
|
||||
if (entries is List)
|
||||
for (final entry in entries)
|
||||
if (entry is Map) DirectoryEntry.fromJson(entry.cast()),
|
||||
],
|
||||
truncated: body['truncated'] == true,
|
||||
);
|
||||
}
|
||||
|
||||
final String path;
|
||||
final String home;
|
||||
final String? parent;
|
||||
final List<DirectoryEntry> entries;
|
||||
final bool truncated;
|
||||
}
|
||||
|
||||
/// One breadcrumb: its visible label and the folder it opens.
|
||||
typedef FolderCrumb = ({String label, String path});
|
||||
|
||||
/// The crumbs from home (`~`) down to [path]. A path outside [home] yields
|
||||
/// home alone, because the picker never lists outside it.
|
||||
List<FolderCrumb> folderCrumbs(String home, String path) {
|
||||
final crumbs = <FolderCrumb>[(label: '~', path: home)];
|
||||
if (!isInsideHome(home, path) || path == home) return crumbs;
|
||||
final base = home.endsWith('/') ? home : '$home/';
|
||||
var current = home;
|
||||
for (final part in path.substring(base.length).split('/')) {
|
||||
if (part.isEmpty) continue;
|
||||
current = current.endsWith('/') ? '$current$part' : '$current/$part';
|
||||
crumbs.add((label: part, path: current));
|
||||
}
|
||||
return crumbs;
|
||||
}
|
||||
|
||||
/// Whether [path] is [home] or a folder below it, compared lexically.
|
||||
bool isInsideHome(String home, String path) {
|
||||
if (home.isEmpty) return false;
|
||||
if (path == home) return true;
|
||||
final base = home.endsWith('/') ? home : '$home/';
|
||||
return path.startsWith(base);
|
||||
}
|
||||
|
||||
/// [path] shown relative to home (`~/src/app`), or as sent when outside it.
|
||||
String homeRelativeLabel(String home, String path) {
|
||||
if (path == home) return '~';
|
||||
if (!isInsideHome(home, path)) return path;
|
||||
final base = home.endsWith('/') ? home : '$home/';
|
||||
return '~/${path.substring(base.length)}';
|
||||
}
|
||||
|
||||
/// Sends `list_directory`; an omitted [path] lists the server user's home.
|
||||
Future<DirectoryListing> listDirectory(
|
||||
ApiGateway gateway, [
|
||||
String? path,
|
||||
]) async {
|
||||
final reply = await gateway.call('list_directory', {
|
||||
'list_directory': {
|
||||
if (path != null && path.trim().isNotEmpty) 'path': path.trim(),
|
||||
},
|
||||
});
|
||||
return DirectoryListing.fromReply(reply);
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../../../../core/api_gateway.dart';
|
||||
import '../../data/directory_listing.dart';
|
||||
|
||||
/// The folder picker's state: the last listing, the folder being loaded, an
|
||||
/// inline error, the hidden toggle and the filter. Listing goes through the
|
||||
/// server's home-restricted `list_directory`; nothing is guessed locally.
|
||||
class FolderPickerController extends ChangeNotifier {
|
||||
FolderPickerController({
|
||||
required this.gateway,
|
||||
this.initialPath,
|
||||
this.recentFolders = const [],
|
||||
});
|
||||
|
||||
final ApiGateway gateway;
|
||||
final String? initialPath;
|
||||
|
||||
/// Every recent chat folder the server reported; [recents] filters them.
|
||||
final List<String> recentFolders;
|
||||
|
||||
DirectoryListing? _listing;
|
||||
String? _error;
|
||||
String? _failedPath;
|
||||
bool _loading = false;
|
||||
bool _showHidden = false;
|
||||
String _filter = '';
|
||||
int _request = 0;
|
||||
bool _disposed = false;
|
||||
|
||||
DirectoryListing? get listing => _listing;
|
||||
String? get error => _error;
|
||||
bool get loading => _loading;
|
||||
bool get showHidden => _showHidden;
|
||||
String get filter => _filter;
|
||||
|
||||
/// The home directory once any listing has arrived.
|
||||
String? get home => _listing?.home;
|
||||
|
||||
/// The folder "Start chat here" would use: only a successfully listed one.
|
||||
String? get selectable => _loading || _error != null ? null : _listing?.path;
|
||||
|
||||
bool get atHome => _listing != null && _listing!.parent == null;
|
||||
|
||||
/// Recent chat folders inside home, offered only at the home level.
|
||||
List<String> get recents {
|
||||
final listing = _listing;
|
||||
if (listing == null || listing.parent != null || _error != null) {
|
||||
return const [];
|
||||
}
|
||||
return [
|
||||
for (final path in recentFolders)
|
||||
if (path != listing.home && isInsideHome(listing.home, path)) path,
|
||||
];
|
||||
}
|
||||
|
||||
/// The visible entries: hidden names only when toggled on, then the filter
|
||||
/// (case-insensitive substring). Folders stay ahead of files as sent.
|
||||
List<DirectoryEntry> get entries {
|
||||
final listing = _listing;
|
||||
if (listing == null) return const [];
|
||||
final needle = _filter.trim().toLowerCase();
|
||||
return [
|
||||
for (final entry in listing.entries)
|
||||
if ((_showHidden || !entry.hidden) &&
|
||||
(needle.isEmpty || entry.name.toLowerCase().contains(needle)))
|
||||
entry,
|
||||
];
|
||||
}
|
||||
|
||||
/// How many hidden names the toggle currently conceals.
|
||||
int get hiddenCount =>
|
||||
_showHidden ? 0 : (_listing?.entries.where((e) => e.hidden).length ?? 0);
|
||||
|
||||
Future<void> start() => open(initialPath);
|
||||
|
||||
/// Lists [path] (home when null). A failure keeps the previous listing for
|
||||
/// the breadcrumbs and Back, and shows the server's words inline.
|
||||
Future<void> open(String? path) async {
|
||||
final request = ++_request;
|
||||
_loading = true;
|
||||
_error = null;
|
||||
_failedPath = path;
|
||||
_notify();
|
||||
try {
|
||||
final next = await listDirectory(gateway, path);
|
||||
if (_disposed || request != _request) return;
|
||||
_listing = next;
|
||||
_filter = '';
|
||||
_failedPath = null;
|
||||
} catch (error) {
|
||||
if (_disposed || request != _request) return;
|
||||
_error = '$error';
|
||||
} finally {
|
||||
if (!_disposed && request == _request) {
|
||||
_loading = false;
|
||||
_notify();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> retry() => open(_failedPath ?? _listing?.path ?? initialPath);
|
||||
|
||||
/// Back: dismiss an error first, then go up one folder. Returns false at
|
||||
/// home, where Back leaves the picker.
|
||||
bool up() {
|
||||
if (_error != null && _listing != null) {
|
||||
_error = null;
|
||||
_failedPath = null;
|
||||
_notify();
|
||||
return true;
|
||||
}
|
||||
final parent = _listing?.parent;
|
||||
if (parent == null) return false;
|
||||
open(parent);
|
||||
return true;
|
||||
}
|
||||
|
||||
void setShowHidden(bool value) {
|
||||
_showHidden = value;
|
||||
_notify();
|
||||
}
|
||||
|
||||
void setFilter(String value) {
|
||||
_filter = value;
|
||||
_notify();
|
||||
}
|
||||
|
||||
void _notify() {
|
||||
if (!_disposed) notifyListeners();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_disposed = true;
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../data/directory_listing.dart';
|
||||
import 'folder_picker_controller.dart';
|
||||
import 'folder_picker_status.dart';
|
||||
|
||||
/// The body: loading, the inline error, or recent folders (home level only)
|
||||
/// followed by the current folder's folders and its dimmed, unselectable files.
|
||||
class FolderPickerList extends StatelessWidget {
|
||||
const FolderPickerList({
|
||||
super.key,
|
||||
required this.cupertino,
|
||||
required this.controller,
|
||||
});
|
||||
|
||||
final bool cupertino;
|
||||
final FolderPickerController controller;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final listing = controller.listing;
|
||||
if (controller.error != null) {
|
||||
return FolderPickerError(cupertino: cupertino, controller: controller);
|
||||
}
|
||||
if (listing == null) {
|
||||
return FolderPickerLoading(cupertino: cupertino);
|
||||
}
|
||||
final entries = controller.entries;
|
||||
final recents = controller.filter.trim().isEmpty
|
||||
? controller.recents
|
||||
: const <String>[];
|
||||
final rows = <Widget>[
|
||||
if (controller.loading) FolderPickerLoadingBar(cupertino: cupertino),
|
||||
if (recents.isNotEmpty) ...[
|
||||
FolderSectionHeader(cupertino: cupertino, text: 'Recent folders'),
|
||||
for (final path in recents)
|
||||
_RecentRow(
|
||||
cupertino: cupertino,
|
||||
label: homeRelativeLabel(listing.home, path),
|
||||
onTap: () => controller.open(path),
|
||||
),
|
||||
FolderSectionHeader(
|
||||
cupertino: cupertino,
|
||||
text: 'In ${homeRelativeLabel(listing.home, listing.path)}',
|
||||
),
|
||||
],
|
||||
for (final entry in entries)
|
||||
_EntryRow(
|
||||
cupertino: cupertino,
|
||||
entry: entry,
|
||||
onTap: entry.isDir ? () => controller.open(entry.path) : null,
|
||||
),
|
||||
if (entries.isEmpty)
|
||||
FolderPickerNote(
|
||||
cupertino: cupertino,
|
||||
text: controller.filter.trim().isNotEmpty
|
||||
? 'Nothing here matches “${controller.filter.trim()}”.'
|
||||
: controller.hiddenCount > 0
|
||||
? 'Only hidden items here. Turn on Show hidden to see them.'
|
||||
: 'This folder is empty.',
|
||||
),
|
||||
if (listing.truncated)
|
||||
FolderPickerNote(
|
||||
cupertino: cupertino,
|
||||
text:
|
||||
'This folder is large; only the first entries are shown. '
|
||||
'Use the filter or type the path instead.',
|
||||
),
|
||||
];
|
||||
return ListView(
|
||||
key: ValueKey('folder-list-${listing.path}'),
|
||||
children: rows,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _RecentRow extends StatelessWidget {
|
||||
const _RecentRow({
|
||||
required this.cupertino,
|
||||
required this.label,
|
||||
required this.onTap,
|
||||
});
|
||||
|
||||
final bool cupertino;
|
||||
final String label;
|
||||
final VoidCallback onTap;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (cupertino) {
|
||||
return CupertinoListTile(
|
||||
leading: const Icon(CupertinoIcons.clock),
|
||||
title: Text(label),
|
||||
trailing: const CupertinoListTileChevron(),
|
||||
onTap: onTap,
|
||||
);
|
||||
}
|
||||
return ListTile(
|
||||
leading: const Icon(Icons.history),
|
||||
title: Text(label),
|
||||
trailing: const Icon(Icons.chevron_right),
|
||||
onTap: onTap,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _EntryRow extends StatelessWidget {
|
||||
const _EntryRow({
|
||||
required this.cupertino,
|
||||
required this.entry,
|
||||
required this.onTap,
|
||||
});
|
||||
|
||||
final bool cupertino;
|
||||
final DirectoryEntry entry;
|
||||
final VoidCallback? onTap;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final folder = entry.isDir;
|
||||
final trailing = folder
|
||||
? Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (entry.isGitRepo) FolderGitBadge(cupertino: cupertino),
|
||||
const SizedBox(width: 4),
|
||||
cupertino
|
||||
? const CupertinoListTileChevron()
|
||||
: const Icon(Icons.chevron_right),
|
||||
],
|
||||
)
|
||||
: null;
|
||||
if (cupertino) {
|
||||
final dim = CupertinoColors.tertiaryLabel.resolveFrom(context);
|
||||
return CupertinoListTile(
|
||||
key: ValueKey('folder-entry-${entry.path}'),
|
||||
leading: Icon(
|
||||
folder ? CupertinoIcons.folder_fill : CupertinoIcons.doc,
|
||||
color: folder ? null : dim,
|
||||
),
|
||||
title: Text(entry.name, style: folder ? null : TextStyle(color: dim)),
|
||||
trailing: trailing,
|
||||
onTap: onTap,
|
||||
);
|
||||
}
|
||||
return ListTile(
|
||||
key: ValueKey('folder-entry-${entry.path}'),
|
||||
enabled: folder,
|
||||
leading: Icon(
|
||||
folder ? Icons.folder : Icons.insert_drive_file_outlined,
|
||||
color: folder ? Theme.of(context).colorScheme.primary : null,
|
||||
),
|
||||
title: Text(entry.name),
|
||||
trailing: trailing,
|
||||
onTap: onTap,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// The small "git" mark on a repository folder.
|
||||
class FolderGitBadge extends StatelessWidget {
|
||||
const FolderGitBadge({super.key, required this.cupertino});
|
||||
|
||||
final bool cupertino;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (!cupertino) {
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
return Badge(
|
||||
label: const Text('git'),
|
||||
backgroundColor: scheme.secondaryContainer,
|
||||
textColor: scheme.onSecondaryContainer,
|
||||
);
|
||||
}
|
||||
return DecoratedBox(
|
||||
decoration: BoxDecoration(
|
||||
color: CupertinoColors.systemGrey5.resolveFrom(context),
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 1),
|
||||
child: Text(
|
||||
'git',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: CupertinoColors.secondaryLabel.resolveFrom(context),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../../core/api_gateway.dart';
|
||||
import '../../data/directory_listing.dart';
|
||||
import '../sessions/chat_adaptive.dart';
|
||||
import 'folder_picker_controller.dart';
|
||||
import 'folder_picker_list.dart';
|
||||
import 'folder_picker_parts.dart';
|
||||
|
||||
const String folderPickerTitle = 'Choose folder';
|
||||
const String folderPickerStartLabel = 'Start chat here';
|
||||
|
||||
/// The folder picker: breadcrumbs, a filter and hidden toggle, the current
|
||||
/// folder's entries and a prominent "Start chat here". It pops with the chosen
|
||||
/// absolute path; Back climbs one folder and leaves only at home.
|
||||
class FolderPickerPage extends StatefulWidget {
|
||||
const FolderPickerPage({
|
||||
super.key,
|
||||
required this.gateway,
|
||||
this.initialPath,
|
||||
this.recents = const [],
|
||||
});
|
||||
|
||||
final ApiGateway gateway;
|
||||
final String? initialPath;
|
||||
final List<String> recents;
|
||||
|
||||
@override
|
||||
State<FolderPickerPage> createState() => _FolderPickerPageState();
|
||||
}
|
||||
|
||||
class _FolderPickerPageState extends State<FolderPickerPage> {
|
||||
late final FolderPickerController _controller = FolderPickerController(
|
||||
gateway: widget.gateway,
|
||||
initialPath: widget.initialPath,
|
||||
recentFolders: widget.recents,
|
||||
);
|
||||
final _filter = TextEditingController();
|
||||
String? _shownPath;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_controller.addListener(_changed);
|
||||
_controller.start();
|
||||
}
|
||||
|
||||
void _changed() {
|
||||
final path = _controller.listing?.path;
|
||||
if (path != _shownPath) {
|
||||
_shownPath = path;
|
||||
_filter.clear();
|
||||
}
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.removeListener(_changed);
|
||||
_controller.dispose();
|
||||
_filter.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _back() {
|
||||
if (!_controller.up()) Navigator.pop(context);
|
||||
}
|
||||
|
||||
void _start() {
|
||||
final path = _controller.selectable;
|
||||
if (path != null) Navigator.pop(context, path);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
final body = Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
FolderBreadcrumbs(cupertino: cupertino, controller: _controller),
|
||||
FolderPickerToolbar(
|
||||
cupertino: cupertino,
|
||||
controller: _controller,
|
||||
filter: _filter,
|
||||
),
|
||||
Expanded(
|
||||
child: FolderPickerList(
|
||||
cupertino: cupertino,
|
||||
controller: _controller,
|
||||
),
|
||||
),
|
||||
FolderStartBar(
|
||||
cupertino: cupertino,
|
||||
path: _controller.selectable,
|
||||
home: _controller.home,
|
||||
onStart: _start,
|
||||
),
|
||||
],
|
||||
);
|
||||
return PopScope(
|
||||
canPop: _controller.atHome && _controller.error == null,
|
||||
onPopInvokedWithResult: (didPop, _) {
|
||||
if (!didPop) _back();
|
||||
},
|
||||
child: cupertino ? _cupertino(context, body) : _material(body),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _material(Widget body) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
leading: BackButton(onPressed: _back),
|
||||
title: const Text(folderPickerTitle),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: SafeArea(top: false, child: body),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _cupertino(BuildContext context, Widget body) {
|
||||
final parent = _controller.listing?.parent;
|
||||
final home = _controller.home;
|
||||
return CupertinoPageScaffold(
|
||||
navigationBar: CupertinoNavigationBar(
|
||||
automaticallyImplyLeading: false,
|
||||
leading: CupertinoNavigationBarBackButton(
|
||||
onPressed: _back,
|
||||
previousPageTitle: parent == null || home == null
|
||||
? 'Back'
|
||||
: folderCrumbs(home, parent).last.label,
|
||||
),
|
||||
middle: const Text(folderPickerTitle),
|
||||
trailing: CupertinoButton(
|
||||
padding: EdgeInsets.zero,
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
),
|
||||
child: DefaultTextStyle(
|
||||
style: CupertinoTheme.of(context).textTheme.textStyle,
|
||||
child: SafeArea(child: body),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,268 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../data/directory_listing.dart';
|
||||
import 'folder_picker_controller.dart';
|
||||
|
||||
/// The path from `~` to the current folder; every crumb but the last opens
|
||||
/// that folder. It scrolls so the current folder stays visible.
|
||||
class FolderBreadcrumbs extends StatelessWidget {
|
||||
const FolderBreadcrumbs({
|
||||
super.key,
|
||||
required this.cupertino,
|
||||
required this.controller,
|
||||
});
|
||||
|
||||
final bool cupertino;
|
||||
final FolderPickerController controller;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final listing = controller.listing;
|
||||
if (listing == null) return const SizedBox.shrink();
|
||||
final crumbs = folderCrumbs(listing.home, listing.path);
|
||||
final children = <Widget>[];
|
||||
for (var i = 0; i < crumbs.length; i++) {
|
||||
final crumb = crumbs[i];
|
||||
final last = i == crumbs.length - 1;
|
||||
if (i > 0) {
|
||||
children.add(
|
||||
Icon(
|
||||
cupertino ? CupertinoIcons.chevron_right : Icons.chevron_right,
|
||||
size: 16,
|
||||
),
|
||||
);
|
||||
}
|
||||
children.add(
|
||||
Semantics(
|
||||
label: last ? 'Current folder ${crumb.label}' : 'Open ${crumb.label}',
|
||||
button: !last,
|
||||
excludeSemantics: true,
|
||||
child: _crumb(context, crumb, last),
|
||||
),
|
||||
);
|
||||
}
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8),
|
||||
// Reversed so a deep path keeps the current folder in view; the
|
||||
// viewport-wide minimum keeps a short path aligned to the start.
|
||||
child: LayoutBuilder(
|
||||
builder: (context, constraints) => SingleChildScrollView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
reverse: true,
|
||||
child: ConstrainedBox(
|
||||
constraints: BoxConstraints(minWidth: constraints.maxWidth),
|
||||
child: Row(children: children),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _crumb(BuildContext context, FolderCrumb crumb, bool last) {
|
||||
final text = Text(
|
||||
crumb.label,
|
||||
style: last ? const TextStyle(fontWeight: FontWeight.w600) : null,
|
||||
);
|
||||
final home = crumb.label == '~' && crumb.path == controller.home;
|
||||
final label = home
|
||||
? Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(cupertino ? CupertinoIcons.house : Icons.home_outlined),
|
||||
const SizedBox(width: 4),
|
||||
text,
|
||||
],
|
||||
)
|
||||
: text;
|
||||
final onPressed = last ? null : () => controller.open(crumb.path);
|
||||
if (cupertino) {
|
||||
return CupertinoButton(
|
||||
key: ValueKey('folder-crumb-${crumb.path}'),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 8),
|
||||
onPressed: onPressed,
|
||||
disabledColor: CupertinoColors.transparent,
|
||||
child: last
|
||||
? DefaultTextStyle.merge(
|
||||
style: TextStyle(
|
||||
color: CupertinoColors.label.resolveFrom(context),
|
||||
),
|
||||
child: label,
|
||||
)
|
||||
: label,
|
||||
);
|
||||
}
|
||||
return TextButton(
|
||||
key: ValueKey('folder-crumb-${crumb.path}'),
|
||||
onPressed: onPressed,
|
||||
style: last
|
||||
? TextButton.styleFrom(
|
||||
disabledForegroundColor: Theme.of(context).colorScheme.onSurface,
|
||||
)
|
||||
: null,
|
||||
child: label,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// The filter field for the current folder and the "Show hidden" toggle.
|
||||
class FolderPickerToolbar extends StatelessWidget {
|
||||
const FolderPickerToolbar({
|
||||
super.key,
|
||||
required this.cupertino,
|
||||
required this.controller,
|
||||
required this.filter,
|
||||
});
|
||||
|
||||
final bool cupertino;
|
||||
final FolderPickerController controller;
|
||||
final TextEditingController filter;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final hiddenLabel = controller.hiddenCount > 0
|
||||
? 'Show hidden (${controller.hiddenCount})'
|
||||
: 'Show hidden';
|
||||
final field = cupertino
|
||||
? CupertinoSearchTextField(
|
||||
key: const Key('folder-picker-filter'),
|
||||
controller: filter,
|
||||
placeholder: 'Filter this folder',
|
||||
onChanged: controller.setFilter,
|
||||
)
|
||||
: TextField(
|
||||
key: const Key('folder-picker-filter'),
|
||||
controller: filter,
|
||||
onChanged: controller.setFilter,
|
||||
decoration: InputDecoration(
|
||||
isDense: true,
|
||||
prefixIcon: const Icon(Icons.search),
|
||||
hintText: 'Filter this folder',
|
||||
border: const OutlineInputBorder(),
|
||||
suffixIcon: filter.text.isEmpty
|
||||
? null
|
||||
: IconButton(
|
||||
tooltip: 'Clear filter',
|
||||
icon: const Icon(Icons.clear),
|
||||
onPressed: () {
|
||||
filter.clear();
|
||||
controller.setFilter('');
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
final toggle = cupertino
|
||||
? Semantics(
|
||||
label: 'Show hidden',
|
||||
toggled: controller.showHidden,
|
||||
excludeSemantics: true,
|
||||
child: GestureDetector(
|
||||
onTap: () => controller.setShowHidden(!controller.showHidden),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(hiddenLabel),
|
||||
const SizedBox(width: 6),
|
||||
CupertinoSwitch(
|
||||
key: const Key('folder-picker-hidden'),
|
||||
value: controller.showHidden,
|
||||
onChanged: controller.setShowHidden,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
)
|
||||
: FilterChip(
|
||||
key: const Key('folder-picker-hidden'),
|
||||
label: Text(hiddenLabel),
|
||||
selected: controller.showHidden,
|
||||
onSelected: controller.setShowHidden,
|
||||
);
|
||||
return Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 4, 16, 8),
|
||||
child: Wrap(
|
||||
spacing: 12,
|
||||
runSpacing: 8,
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
children: [
|
||||
ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 420),
|
||||
child: field,
|
||||
),
|
||||
toggle,
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// The prominent confirm: names the folder and starts the chat there. It is
|
||||
/// disabled until a folder is listed (never while loading or after an error).
|
||||
class FolderStartBar extends StatelessWidget {
|
||||
const FolderStartBar({
|
||||
super.key,
|
||||
required this.cupertino,
|
||||
required this.path,
|
||||
required this.home,
|
||||
required this.onStart,
|
||||
});
|
||||
|
||||
final bool cupertino;
|
||||
final String? path;
|
||||
final String? home;
|
||||
final VoidCallback onStart;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final where = path == null || home == null
|
||||
? 'No folder selected'
|
||||
: 'Start in ${homeRelativeLabel(home!, path!)}';
|
||||
final onPressed = path == null ? null : onStart;
|
||||
final button = cupertino
|
||||
? CupertinoButton.filled(
|
||||
key: const Key('folder-picker-start'),
|
||||
onPressed: onPressed,
|
||||
child: const Text('Start chat here'),
|
||||
)
|
||||
: FilledButton.icon(
|
||||
key: const Key('folder-picker-start'),
|
||||
onPressed: onPressed,
|
||||
icon: const Icon(Icons.chat_bubble_outline),
|
||||
label: const Text('Start chat here'),
|
||||
);
|
||||
final content = Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 8, 16, 12),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Text(
|
||||
where,
|
||||
key: const Key('folder-picker-where'),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
button,
|
||||
],
|
||||
),
|
||||
);
|
||||
if (cupertino) {
|
||||
return DecoratedBox(
|
||||
decoration: BoxDecoration(
|
||||
border: Border(
|
||||
top: BorderSide(
|
||||
color: CupertinoColors.separator.resolveFrom(context),
|
||||
width: 0,
|
||||
),
|
||||
),
|
||||
),
|
||||
child: content,
|
||||
);
|
||||
}
|
||||
return Material(
|
||||
color: Theme.of(context).colorScheme.surfaceContainer,
|
||||
child: content,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../../core/api_gateway.dart';
|
||||
import '../sessions/chat_adaptive.dart';
|
||||
import 'folder_picker_page.dart';
|
||||
|
||||
/// At or above this window width the picker is a large dialog over the create
|
||||
/// dialog; below it, a full-screen page.
|
||||
const double folderPickerDialogBreakpoint = 720;
|
||||
|
||||
/// The wide dialog's bounds, kept here so the page itself stays content-sized.
|
||||
const double _dialogMaxWidth = 760;
|
||||
const double _dialogMaxHeight = 900;
|
||||
|
||||
/// Opens the folder picker and resolves to the chosen absolute folder, or
|
||||
/// null when the operator cancels. [initialPath] opens there when the server
|
||||
/// can list it; [recents] are offered as quick picks at the home level.
|
||||
Future<String?> showFolderPicker(
|
||||
BuildContext context, {
|
||||
required ApiGateway gateway,
|
||||
String? initialPath,
|
||||
List<String> recents = const [],
|
||||
}) {
|
||||
final cupertino = chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
final size = MediaQuery.sizeOf(context);
|
||||
final wide = size.width >= folderPickerDialogBreakpoint;
|
||||
final navigator = Navigator.of(context, rootNavigator: true);
|
||||
Widget page(BuildContext _) => FolderPickerPage(
|
||||
gateway: gateway,
|
||||
initialPath: initialPath,
|
||||
recents: recents,
|
||||
);
|
||||
if (!wide) {
|
||||
return navigator.push<String>(
|
||||
cupertino
|
||||
? CupertinoPageRoute(fullscreenDialog: true, builder: page)
|
||||
: MaterialPageRoute(fullscreenDialog: true, builder: page),
|
||||
);
|
||||
}
|
||||
Widget bounded(BuildContext context) {
|
||||
final window = MediaQuery.sizeOf(context);
|
||||
return SizedBox(
|
||||
width: math.min(_dialogMaxWidth, window.width - 64),
|
||||
height: math.min(_dialogMaxHeight, window.height * 0.85),
|
||||
child: page(context),
|
||||
);
|
||||
}
|
||||
|
||||
if (cupertino) {
|
||||
return showCupertinoDialog<String>(
|
||||
context: context,
|
||||
useRootNavigator: true,
|
||||
builder: (context) => Center(
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(14),
|
||||
child: bounded(context),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
return showDialog<String>(
|
||||
context: context,
|
||||
useRootNavigator: true,
|
||||
builder: (context) =>
|
||||
Dialog(clipBehavior: Clip.antiAlias, child: bounded(context)),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../data/directory_listing.dart';
|
||||
import 'folder_picker_controller.dart';
|
||||
|
||||
/// First load: a centred native activity indicator.
|
||||
class FolderPickerLoading extends StatelessWidget {
|
||||
const FolderPickerLoading({super.key, required this.cupertino});
|
||||
|
||||
final bool cupertino;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Center(
|
||||
child: Semantics(
|
||||
label: 'Loading folder',
|
||||
child: cupertino
|
||||
? const CupertinoActivityIndicator()
|
||||
: const CircularProgressIndicator(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// A later load keeps the previous listing on screen under a thin indicator.
|
||||
class FolderPickerLoadingBar extends StatelessWidget {
|
||||
const FolderPickerLoadingBar({super.key, required this.cupertino});
|
||||
|
||||
final bool cupertino;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Semantics(
|
||||
label: 'Loading folder',
|
||||
child: cupertino
|
||||
? const Padding(
|
||||
padding: EdgeInsets.all(8),
|
||||
child: CupertinoActivityIndicator(),
|
||||
)
|
||||
: const LinearProgressIndicator(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// The server's refusal shown in place (outside home, permission denied,
|
||||
/// missing folder), with Retry, Back to the last good folder, and Home.
|
||||
class FolderPickerError extends StatelessWidget {
|
||||
const FolderPickerError({
|
||||
super.key,
|
||||
required this.cupertino,
|
||||
required this.controller,
|
||||
});
|
||||
|
||||
final bool cupertino;
|
||||
final FolderPickerController controller;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final color = cupertino
|
||||
? CupertinoColors.systemRed.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.error;
|
||||
final listing = controller.listing;
|
||||
final actions = <(String, VoidCallback)>[
|
||||
('Retry', controller.retry),
|
||||
if (listing != null)
|
||||
(
|
||||
'Back to ${homeRelativeLabel(listing.home, listing.path)}',
|
||||
controller.up,
|
||||
),
|
||||
if (!controller.atHome || listing == null)
|
||||
('Go to home', () => controller.open(null)),
|
||||
];
|
||||
return SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
cupertino
|
||||
? CupertinoIcons.exclamationmark_triangle
|
||||
: Icons.error_outline,
|
||||
color: color,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
"Can't open this folder",
|
||||
style: const TextStyle(fontWeight: FontWeight.w600),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
controller.error ?? '',
|
||||
key: const Key('folder-picker-error'),
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(color: color),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
alignment: WrapAlignment.center,
|
||||
children: [
|
||||
for (final (label, onPressed) in actions)
|
||||
cupertino
|
||||
? CupertinoButton(onPressed: onPressed, child: Text(label))
|
||||
: TextButton(onPressed: onPressed, child: Text(label)),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// A section label above recent folders and the folder's own entries.
|
||||
class FolderSectionHeader extends StatelessWidget {
|
||||
const FolderSectionHeader({
|
||||
super.key,
|
||||
required this.cupertino,
|
||||
required this.text,
|
||||
});
|
||||
|
||||
final bool cupertino;
|
||||
final String text;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final style = cupertino
|
||||
? TextStyle(
|
||||
fontSize: 13,
|
||||
color: CupertinoColors.secondaryLabel.resolveFrom(context),
|
||||
)
|
||||
: Theme.of(context).textTheme.labelLarge?.copyWith(
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
);
|
||||
return Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 12, 16, 4),
|
||||
child: Text(text, style: style),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// A quiet explanatory line (empty folder, no matches, truncated listing).
|
||||
class FolderPickerNote extends StatelessWidget {
|
||||
const FolderPickerNote({
|
||||
super.key,
|
||||
required this.cupertino,
|
||||
required this.text,
|
||||
});
|
||||
|
||||
final bool cupertino;
|
||||
final String text;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final color = cupertino
|
||||
? CupertinoColors.secondaryLabel.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.onSurfaceVariant;
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Text(text, style: TextStyle(color: color)),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,9 @@ 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 = '~';
|
||||
@@ -49,6 +52,7 @@ class ChatCatalogFields extends StatelessWidget {
|
||||
required this.onDirectory,
|
||||
required this.onBackend,
|
||||
required this.onRetry,
|
||||
this.onBrowse,
|
||||
});
|
||||
|
||||
final bool loading;
|
||||
@@ -61,6 +65,9 @@ class ChatCatalogFields extends StatelessWidget {
|
||||
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 =
|
||||
@@ -85,13 +92,42 @@ class ChatCatalogFields extends StatelessWidget {
|
||||
onSelected: onDirectory,
|
||||
)
|
||||
else
|
||||
MaterialPickerField(
|
||||
fieldKey: const Key('chat-create-server-directory'),
|
||||
label: chatCreatePrompt,
|
||||
value: directory,
|
||||
values: choices,
|
||||
labelOf: directoryLabel,
|
||||
onSelected: onDirectory,
|
||||
// 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)
|
||||
|
||||
@@ -7,7 +7,9 @@ import '../../../../core/api_gateway.dart';
|
||||
import '../../../../core/servers/server_scope.dart';
|
||||
import '../../data/chat_create_prompt_store.dart';
|
||||
import '../../data/chat_repository.dart';
|
||||
import '../../data/directory_listing.dart';
|
||||
import '../../domain/chat_commands.dart';
|
||||
import '../folder_picker/folder_picker_route.dart';
|
||||
import 'chat_adaptive.dart';
|
||||
import 'chat_create_dialog_frame.dart';
|
||||
import 'chat_create_fields.dart';
|
||||
@@ -17,7 +19,8 @@ import 'chat_prompt_chooser.dart';
|
||||
const String chatCreateInstructionsHelper =
|
||||
"Preloaded as this chat's session instructions. The agent still reads the "
|
||||
"repository's AGENTS.md and skills.";
|
||||
const String chatCreateModelNote = "New chats start with the server's default model.";
|
||||
const String chatCreateModelNote =
|
||||
"New chats start with the server's default model.";
|
||||
|
||||
class ChatDraft {
|
||||
const ChatDraft({
|
||||
@@ -211,6 +214,43 @@ class _CreateChatDialogState extends State<CreateChatDialog> {
|
||||
});
|
||||
}
|
||||
|
||||
/// Opens the folder picker at the current folder when it is inside home,
|
||||
/// with the server's recent chat folders as quick picks, and makes the
|
||||
/// chosen folder this chat's directory.
|
||||
Future<void> _browse() async {
|
||||
final home = _directories.isEmpty ? null : _directories.first;
|
||||
final current = _directory;
|
||||
final initial =
|
||||
home != null &&
|
||||
current != null &&
|
||||
current != chatHomeDirectory &&
|
||||
isInsideHome(home, current)
|
||||
? current
|
||||
: null;
|
||||
final chosen = await showFolderPicker(
|
||||
context,
|
||||
gateway: widget.repository.gateway,
|
||||
initialPath: initial,
|
||||
recents: _directories.skip(1).toList(),
|
||||
);
|
||||
if (!mounted || chosen == null || _submitting) return;
|
||||
setState(() {
|
||||
if (chosen == home) {
|
||||
_directory = chatHomeDirectory;
|
||||
} else {
|
||||
if (!_directories.contains(chosen)) {
|
||||
_directories = [
|
||||
..._directories.take(1),
|
||||
chosen,
|
||||
..._directories.skip(1),
|
||||
];
|
||||
}
|
||||
_directory = chosen;
|
||||
}
|
||||
});
|
||||
_choiceChanged();
|
||||
}
|
||||
|
||||
Future<void> _submit() async {
|
||||
if (!_catalogReady || _submitting) return;
|
||||
if (!_promptReady) {
|
||||
@@ -284,6 +324,7 @@ class _CreateChatDialogState extends State<CreateChatDialog> {
|
||||
_choiceChanged();
|
||||
},
|
||||
onRetry: _loadCatalog,
|
||||
onBrowse: _submitting ? null : _browse,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
const Text('Name'),
|
||||
|
||||
Reference in New Issue
Block a user