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:
2026-11-01 21:51:48 +00:00
parent eadd017bdc
commit eed3806efb
25 changed files with 2333 additions and 9 deletions
@@ -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);
}