Files
alan b8fb4d350c Upload files into the shown folder from the chat Files panel
The header gains an Upload button that uses the host file picker (disabled
with its reason where no picker exists), and the panel body becomes a drop
target: on web, document drag events are observed through a dart:js_interop
seam and a file drag over the panel shows a highlight naming the folder.
Each file is posted to POST /files?path=<folder> through an injectable
transport; refusals (exists, sign in, too large) stay visible until dismissed
and the folder reloads afterwards. The list/grid builders move to
files_entries.dart to keep the panel small.
2026-11-09 00:09:01 +00:00

80 lines
2.8 KiB
Dart

import 'dart:ui' show Offset;
import '../domain/chat_attachment_picker.dart';
import 'files_drop_stub.dart'
if (dart.library.js_interop) 'files_drop_web.dart'
as impl;
/// Stops observing the host's drag events for one drop area.
typedef FilesDropUnsubscribe = void Function();
/// Registers [listener] with the host and returns its cancellation.
typedef FilesDropSubscribe =
FilesDropUnsubscribe Function(FilesDropListener listener);
/// One drop area's view of a host drag that carries files.
///
/// Positions are logical pixels in the Flutter view's coordinate space, so a
/// listener compares them with its own render box. The host asks [accepts]
/// before it takes over the drag; a drag the area does not accept keeps the
/// host's default behaviour.
class FilesDropListener {
const FilesDropListener({
required this.accepts,
required this.onHover,
required this.onLeave,
required this.onDrop,
required this.onError,
});
/// Whether a drop at [position] would land on this area right now.
final bool Function(Offset position) accepts;
/// A file drag is over the area at [position].
final void Function(Offset position) onHover;
/// The drag left the area, ended or was dropped.
final void Function() onLeave;
/// Files were dropped on the area, in the host's order.
final void Function(List<ChatPickedFile> files) onDrop;
/// One dropped item whose bytes could not be read (a folder, for example).
final void Function(Object error) onError;
}
/// The file drag-and-drop seam for one host.
///
/// The web host observes the browser's own drag events on the document and
/// reads dropped files before reporting them as [ChatPickedFile] rows. A host
/// without file drops constructs [FilesDrop.unsupported] so the panel offers
/// the Upload button alone instead of pretending to listen. Tests inject a
/// fake [subscribe] and drive the listener directly.
class FilesDrop {
const FilesDrop({required FilesDropSubscribe this.subscribe})
: unsupportedMessage = null;
const FilesDrop.unsupported(String message)
: subscribe = null,
unsupportedMessage = message;
/// The host's drag listener registration, or null when unsupported.
final FilesDropSubscribe? subscribe;
/// Why this host cannot observe file drops, when [subscribe] is null.
final String? unsupportedMessage;
bool get isSupported => subscribe != null;
/// Starts observing the host's drag events. A host without the capability
/// throws [UnsupportedError] with [unsupportedMessage].
FilesDropUnsubscribe listen(FilesDropListener listener) {
final subscribe = this.subscribe;
if (subscribe == null) throw UnsupportedError(unsupportedMessage!);
return subscribe(listener);
}
}
/// The drop seam for the running host.
FilesDrop get hostFilesDrop => impl.hostFilesDrop;