42a2378ecb
Use Material popup menus for file actions and anchor Runs sorting choices to the sort button while retaining the phone fallback. Update callers, tests, and implementation notes to match the desktop behavior.
240 lines
7.7 KiB
Dart
240 lines
7.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import '../../../ui/adaptive/host.dart';
|
|
import '../../../core/transport/pipelines_client.dart';
|
|
import '../data/directory_listing.dart';
|
|
import '../domain/chat_attachment_picker.dart';
|
|
import '../ui/sessions/chat_adaptive.dart';
|
|
import 'files_controller.dart';
|
|
import 'files_drop.dart';
|
|
import 'files_drop_area.dart';
|
|
import 'files_entries.dart';
|
|
import 'files_entry_actions.dart';
|
|
import 'files_upload_notice.dart';
|
|
import 'files_viewer.dart';
|
|
import 'files_header.dart';
|
|
import '../../../ui/layout/app_overflow_menu.dart';
|
|
import '../../terminal/terminal_open_action.dart';
|
|
import '../../terminal/terminal_scope.dart';
|
|
|
|
const double filesPanelMinWidth = 280;
|
|
const double filesPanelMaxWidth = 420;
|
|
const double filesPanelFraction = .30;
|
|
|
|
double filesPanelWidth(double available) => (available * filesPanelFraction)
|
|
.clamp(filesPanelMinWidth, filesPanelMaxWidth);
|
|
|
|
/// The actions of an entry's row or tile menu. Open and Download are for
|
|
/// files, Open in terminal for folders (where the app has a terminal);
|
|
/// Rename, Move and Delete apply to files and folders alike.
|
|
enum FileAction { open, download, terminal, rename, move, delete }
|
|
|
|
class FilesPanel extends StatefulWidget {
|
|
const FilesPanel({
|
|
super.key,
|
|
required this.controller,
|
|
this.onOpen,
|
|
this.viewer = const FilesViewer(),
|
|
this.picker,
|
|
this.drop,
|
|
});
|
|
final FilesController controller;
|
|
final ValueChanged<DirectoryEntry>? onOpen;
|
|
|
|
/// Opens and downloads files; tests inject one that never fetches.
|
|
final FilesViewer viewer;
|
|
|
|
/// The Upload button's file picker and the host drag-and-drop seam; null
|
|
/// means the running host's. Tests inject fakes.
|
|
final ChatAttachmentPicker? picker;
|
|
final FilesDrop? drop;
|
|
@override
|
|
State<FilesPanel> createState() => _FilesPanelState();
|
|
}
|
|
|
|
class _FilesPanelState extends State<FilesPanel> {
|
|
late final TextEditingController _filter = TextEditingController();
|
|
late final ChatAttachmentPicker _picker =
|
|
widget.picker ?? hostChatAttachmentPicker;
|
|
late final FilesDrop _drop = widget.drop ?? hostFilesDrop;
|
|
FilesController get controller => widget.controller;
|
|
String get _endpoint => configuredGatewayEndpoint(controller.gateway);
|
|
|
|
Future<void> _pickAndUpload() async {
|
|
final List<ChatPickedFile>? files;
|
|
try {
|
|
files = await _picker.pick();
|
|
} catch (error) {
|
|
controller.reportUploadError('$error');
|
|
return;
|
|
}
|
|
if (files == null || files.isEmpty) return;
|
|
await controller.upload(files);
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
controller.addListener(_changed);
|
|
controller.start();
|
|
}
|
|
|
|
void _changed() {
|
|
if (mounted) {
|
|
if (_filter.text != controller.filter) _filter.text = controller.filter;
|
|
setState(() {});
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
controller.removeListener(_changed);
|
|
_filter.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final listing = controller.listing;
|
|
final entries = controller.entries;
|
|
if (controller.missingDirectory) {
|
|
return const _FilesMessage('This chat has no working directory.');
|
|
}
|
|
return Theme(
|
|
data: usesCupertinoHost(context)
|
|
? ThemeData(
|
|
brightness: CupertinoTheme.brightnessOf(context),
|
|
colorSchemeSeed: CupertinoTheme.of(context).primaryColor,
|
|
)
|
|
: Theme.of(context),
|
|
child: Builder(
|
|
builder: (context) => Material(
|
|
color: Theme.of(context).colorScheme.surface,
|
|
child: FilesDropArea(
|
|
drop: _drop,
|
|
folderLabel: listing == null
|
|
? null
|
|
: homeRelativeLabel(listing.home, listing.path),
|
|
onFiles: controller.upload,
|
|
onError: (error) => controller.reportUploadError(
|
|
'A dropped item could not be read: $error',
|
|
),
|
|
child: Column(
|
|
children: [
|
|
FilesHeader(
|
|
controller: controller,
|
|
filter: _filter,
|
|
picker: _picker,
|
|
onUpload: _pickAndUpload,
|
|
),
|
|
if (controller.error != null)
|
|
ChatErrorNotice(
|
|
message: controller.error!,
|
|
onRetry: controller.retry,
|
|
),
|
|
FilesUploadNotice(controller: controller),
|
|
if (controller.loading && listing == null)
|
|
const Expanded(child: ChatLoadingIndicator())
|
|
else if (listing != null &&
|
|
entries.isEmpty &&
|
|
!controller.loading)
|
|
Expanded(
|
|
child: _FilesMessage(
|
|
controller.filter.isEmpty
|
|
? 'This folder is empty.'
|
|
: 'No files match your filter.',
|
|
),
|
|
)
|
|
else
|
|
Expanded(
|
|
child: FilesEntries(
|
|
entries: entries,
|
|
view: controller.view,
|
|
endpoint: _endpoint,
|
|
onTap: (entry) => entry.isDir
|
|
? controller.open(entry.path)
|
|
: _open(entry),
|
|
menu: _menu,
|
|
onDrop: (dragged, folder) =>
|
|
controller.move(dragged, folder.path),
|
|
),
|
|
),
|
|
if (listing?.truncated == true)
|
|
const Padding(
|
|
padding: EdgeInsets.all(8),
|
|
child: Text('Some entries are not shown.'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/// Open and Download for a file, then Rename, Move and Delete for any entry.
|
|
Widget _menu(DirectoryEntry entry) => AppOverflowMenu<FileAction>(
|
|
label: entry.isDir ? 'Folder actions' : 'File actions',
|
|
onSelected: (_, action) => switch (action) {
|
|
FileAction.open => _open(entry),
|
|
FileAction.download => widget.viewer.download(
|
|
context,
|
|
endpoint: _endpoint,
|
|
entry: entry,
|
|
),
|
|
// A refusal shows in the terminal the surface opens.
|
|
FileAction.terminal => openInTerminal(
|
|
context,
|
|
controller.gateway,
|
|
entry.path,
|
|
),
|
|
FileAction.rename => renameEntry(context, controller, entry),
|
|
FileAction.move => moveEntry(context, controller, entry),
|
|
FileAction.delete => deleteEntry(context, controller, entry),
|
|
},
|
|
actions: [
|
|
if (!entry.isDir) ...const [
|
|
AppMenuAction(value: FileAction.open, label: 'Open'),
|
|
AppMenuAction(value: FileAction.download, label: 'Download'),
|
|
],
|
|
if (entry.isDir && TerminalScope.maybeOf(context) != null)
|
|
const AppMenuAction(
|
|
value: FileAction.terminal,
|
|
label: terminalOpenActionLabel,
|
|
),
|
|
const AppMenuAction(value: FileAction.rename, label: 'Rename…'),
|
|
const AppMenuAction(value: FileAction.move, label: 'Move…'),
|
|
const AppMenuAction(
|
|
value: FileAction.delete,
|
|
label: 'Delete…',
|
|
destructive: true,
|
|
),
|
|
],
|
|
);
|
|
|
|
void _open(DirectoryEntry entry) {
|
|
if (widget.onOpen != null) {
|
|
widget.onOpen!(entry);
|
|
} else {
|
|
widget.viewer.open(
|
|
context,
|
|
endpoint: _endpoint,
|
|
entry: entry,
|
|
siblings: widget.controller.entries,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
class _FilesMessage extends StatelessWidget {
|
|
const _FilesMessage(this.text);
|
|
final String text;
|
|
@override
|
|
Widget build(BuildContext context) => Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(24),
|
|
child: Text(text, textAlign: TextAlign.center),
|
|
),
|
|
);
|
|
}
|