Files
pipelines/app/lib/features/chat/files/files_rename_dialog.dart
T
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

132 lines
4.2 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../data/directory_listing.dart';
import '../ui/sessions/chat_adaptive.dart';
import 'files_mutations.dart';
/// Asks for [entry]'s new name, prefilled and selected. Resolves to the
/// trimmed name, or null when cancelled. Save stays disabled while the name is
/// invalid ([renameNameError]) or unchanged; the server's refusal of a valid
/// name is reported by the panel, not here.
Future<String?> showRenameEntryDialog(
BuildContext context,
DirectoryEntry entry,
) {
final dialog = _RenameEntryDialog(entry: entry);
return chatControlsStyleOf(context) == ChatControlsStyle.cupertino
? showCupertinoDialog<String>(context: context, builder: (_) => dialog)
: showDialog<String>(context: context, builder: (_) => dialog);
}
class _RenameEntryDialog extends StatefulWidget {
const _RenameEntryDialog({required this.entry});
final DirectoryEntry entry;
@override
State<_RenameEntryDialog> createState() => _RenameEntryDialogState();
}
class _RenameEntryDialogState extends State<_RenameEntryDialog> {
late final TextEditingController _name =
TextEditingController(text: widget.entry.name)
..selection = TextSelection(
baseOffset: 0,
extentOffset: _stemLength(widget.entry),
);
/// A file's selection stops before its extension, so typing replaces the
/// stem only; a folder or a dot-file is selected whole.
static int _stemLength(DirectoryEntry entry) {
final dot = entry.name.lastIndexOf('.');
return entry.isDir || dot <= 0 ? entry.name.length : dot;
}
@override
void dispose() {
_name.dispose();
super.dispose();
}
void _save() {
final name = _name.text.trim();
if (renameNameError(name) != null || name == widget.entry.name) return;
Navigator.pop(context, name);
}
@override
Widget build(BuildContext context) {
final title = widget.entry.isDir ? 'Rename folder' : 'Rename file';
return ValueListenableBuilder<TextEditingValue>(
valueListenable: _name,
builder: (context, value, _) {
final error = renameNameError(value.text);
final canSave = error == null && value.text.trim() != widget.entry.name;
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
return CupertinoAlertDialog(
title: Text(title),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
const SizedBox(height: 8),
CupertinoTextField(
key: const Key('files-rename-field'),
controller: _name,
autofocus: true,
placeholder: 'Name',
onSubmitted: (_) => _save(),
),
if (error != null) ...[
const SizedBox(height: 8),
Text(
error,
style: TextStyle(
color: CupertinoColors.systemRed.resolveFrom(context),
),
),
],
],
),
actions: [
CupertinoDialogAction(
onPressed: () => Navigator.pop(context),
child: const Text('Cancel'),
),
CupertinoDialogAction(
isDefaultAction: true,
onPressed: canSave ? _save : null,
child: const Text('Save'),
),
],
);
}
return AlertDialog(
scrollable: true,
title: Text(title),
content: TextField(
key: const Key('files-rename-field'),
controller: _name,
autofocus: true,
onSubmitted: (_) => _save(),
decoration: InputDecoration(
labelText: 'Name',
errorText: error,
errorMaxLines: 2,
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Cancel'),
),
FilledButton(
onPressed: canSave ? _save : null,
child: const Text('Save'),
),
],
);
},
);
}
}