Files
alan 4733d4b615 Rename, move and delete entries from the chat Files panel
Every row and tile menu gains Rename…, Move… and Delete… (folders now
have a menu too). Rename prefills the name with the file's stem selected
and applies the server's name rules inline. Move reuses the chat folder
picker through a FolderPickerPurpose: titled for the entry, confirmed
with Move here, never offering the entry's current folder and keeping a
moved folder's own subtree out of reach. Delete confirms, and when the
server reports a folder with contents asks once more before deleting
recursively. Holding an entry and dropping it on a folder row or tile
moves it there; the target highlights and refuses itself and ancestors.

FilesController performs each request through files_mutations.dart,
shows Renaming…/Moving…/Deleting… progress, reloads the shown folder
afterwards and keeps a refusal in the server's words in the panel's
notice until dismissed.
2026-11-09 01:46:28 +00:00

76 lines
2.5 KiB
Dart

import '../data/directory_listing.dart';
import 'files_preview.dart';
import '../../artifacts/domain/artifact_kind.dart';
enum FilesViewMode { list, grid }
enum FilesSortKey { name, modified, size, kind }
class FilesSort {
const FilesSort(this.key, {this.ascending = true});
final FilesSortKey key;
final bool ascending;
FilesSort reverseOr(FilesSortKey next) =>
FilesSort(next, ascending: next == key ? !ascending : true);
}
List<DirectoryEntry> sortFileEntries(
Iterable<DirectoryEntry> entries,
FilesSort sort,
) {
final result = entries.toList();
result.sort((a, b) {
// Keep directories above files regardless of the selected direction.
if (a.isDir != b.isDir) return a.isDir ? -1 : 1;
int compare;
switch (sort.key) {
case FilesSortKey.name:
compare = a.name.toLowerCase().compareTo(b.name.toLowerCase());
case FilesSortKey.modified:
compare = (a.modified ?? '').compareTo(b.modified ?? '');
case FilesSortKey.size:
compare = (a.size ?? -1).compareTo(b.size ?? -1);
case FilesSortKey.kind:
compare = fileKind(a).compareTo(fileKind(b));
}
if (compare == 0) compare = a.name.compareTo(b.name);
return sort.ascending ? compare : -compare;
});
return result;
}
/// Whether dragging [dragged] onto [folder] may move it there: only onto a
/// folder, never onto itself, and a folder never into its own subtree. The
/// server repeats these checks and also refuses a same-named target.
bool canDropEntryOn(DirectoryEntry dragged, DirectoryEntry folder) =>
folder.isDir &&
dragged.path != folder.path &&
!(dragged.isDir && isInsideHome(dragged.path, folder.path));
String fileKind(DirectoryEntry entry) => entry.isDir
? 'Folder'
: artifactKind(fileContentType(entry.name), entry.name).name;
String fileSizeLabel(int? size) {
if (size == null) return '—';
if (size < 1024) return '$size B';
final units = ['KiB', 'MiB', 'GiB', 'TiB'];
var value = size / 1024;
var unit = 0;
while (value >= 1024 && unit < units.length - 1) {
value /= 1024;
unit++;
}
return '${value.toStringAsFixed(value >= 10 ? 0 : 1)} ${units[unit]}';
}
/// A compact local timestamp; keep missing or malformed values unobtrusive.
String fileModifiedLabel(String? value) {
final date = DateTime.tryParse(value ?? '')?.toLocal();
if (date == null) return 'Unknown';
String two(int n) => n.toString().padLeft(2, '0');
return '${date.year}-${two(date.month)}-${two(date.day)} '
'${two(date.hour)}:${two(date.minute)}';
}