224 lines
6.8 KiB
Dart
224 lines
6.8 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 '../ui/sessions/chat_adaptive.dart';
|
|
import 'files_controller.dart';
|
|
import 'files_icons.dart';
|
|
import 'files_models.dart';
|
|
import 'files_thumbnail.dart';
|
|
import 'files_viewer.dart';
|
|
import 'files_header.dart';
|
|
import '../../../ui/layout/app_overflow_menu.dart';
|
|
|
|
const double filesPanelMinWidth = 280;
|
|
const double filesPanelMaxWidth = 420;
|
|
const double filesPanelFraction = .30;
|
|
|
|
double filesPanelWidth(double available) => (available * filesPanelFraction)
|
|
.clamp(filesPanelMinWidth, filesPanelMaxWidth);
|
|
|
|
/// The two explicit actions of a file's row or tile menu.
|
|
enum FileAction { open, download }
|
|
|
|
class FilesPanel extends StatefulWidget {
|
|
const FilesPanel({
|
|
super.key,
|
|
required this.controller,
|
|
this.onOpen,
|
|
this.viewer = const FilesViewer(),
|
|
});
|
|
final FilesController controller;
|
|
final ValueChanged<DirectoryEntry>? onOpen;
|
|
|
|
/// Opens and downloads files; tests inject one that never fetches.
|
|
final FilesViewer viewer;
|
|
@override
|
|
State<FilesPanel> createState() => _FilesPanelState();
|
|
}
|
|
|
|
class _FilesPanelState extends State<FilesPanel> {
|
|
late final TextEditingController _filter = TextEditingController();
|
|
FilesController get controller => widget.controller;
|
|
String get _endpoint => configuredGatewayEndpoint(controller.gateway);
|
|
@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: Column(
|
|
children: [
|
|
FilesHeader(controller: controller, filter: _filter),
|
|
if (controller.error != null)
|
|
ChatErrorNotice(
|
|
message: controller.error!,
|
|
onRetry: controller.retry,
|
|
),
|
|
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: controller.view == FilesViewMode.grid
|
|
? _grid(entries)
|
|
: _list(entries),
|
|
),
|
|
if (listing?.truncated == true)
|
|
const Padding(
|
|
padding: EdgeInsets.all(8),
|
|
child: Text('Some entries are not shown.'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _list(List<DirectoryEntry> entries) => ListView.builder(
|
|
itemCount: entries.length,
|
|
itemBuilder: (_, index) {
|
|
final entry = entries[index];
|
|
return ListTile(
|
|
onTap: () => entry.isDir ? controller.open(entry.path) : _open(entry),
|
|
leading: Icon(fileIcon(entry)),
|
|
title: Text(entry.name, maxLines: 2, overflow: TextOverflow.ellipsis),
|
|
subtitle: Text(
|
|
entry.isDir
|
|
? 'Folder'
|
|
: '${fileModifiedLabel(entry.modified)} · ${fileSizeLabel(entry.size)}',
|
|
),
|
|
trailing: entry.isDir
|
|
? null
|
|
: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [Text(fileKind(entry)), _menu(entry)],
|
|
),
|
|
);
|
|
},
|
|
);
|
|
|
|
Widget _grid(List<DirectoryEntry> entries) => GridView.builder(
|
|
padding: const EdgeInsets.all(12),
|
|
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
|
|
maxCrossAxisExtent: 180,
|
|
childAspectRatio: .85,
|
|
),
|
|
itemCount: entries.length,
|
|
itemBuilder: (_, index) {
|
|
final entry = entries[index];
|
|
return InkWell(
|
|
onTap: () => entry.isDir ? controller.open(entry.path) : _open(entry),
|
|
child: Card(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(12),
|
|
child: Column(
|
|
children: [
|
|
Align(
|
|
alignment: Alignment.centerRight,
|
|
child: entry.isDir
|
|
? const SizedBox(height: 48)
|
|
: _menu(entry),
|
|
),
|
|
Expanded(
|
|
child: Center(
|
|
child: FileThumbnail(entry: entry, endpoint: _endpoint),
|
|
),
|
|
),
|
|
const SizedBox(height: 8),
|
|
Text(
|
|
entry.name,
|
|
maxLines: 2,
|
|
overflow: TextOverflow.ellipsis,
|
|
textAlign: TextAlign.center,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
|
|
/// Open and Download for one file; a folder has no menu (nothing to save).
|
|
Widget _menu(DirectoryEntry entry) => AppOverflowMenu<FileAction>(
|
|
label: 'File actions',
|
|
sheetTitle: entry.name,
|
|
onSelected: (_, action) => switch (action) {
|
|
FileAction.open => _open(entry),
|
|
FileAction.download => widget.viewer.download(
|
|
context,
|
|
endpoint: _endpoint,
|
|
entry: entry,
|
|
),
|
|
},
|
|
actions: const [
|
|
AppMenuAction(value: FileAction.open, label: 'Open'),
|
|
AppMenuAction(value: FileAction.download, label: 'Download'),
|
|
],
|
|
);
|
|
|
|
void _open(DirectoryEntry entry) {
|
|
if (widget.onOpen != null) {
|
|
widget.onOpen!(entry);
|
|
} else {
|
|
widget.viewer.open(context, endpoint: _endpoint, entry: entry);
|
|
}
|
|
}
|
|
}
|
|
|
|
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),
|
|
),
|
|
);
|
|
}
|