240 lines
7.3 KiB
Dart
240 lines
7.3 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_controller.dart';
|
|
import 'files_icons.dart';
|
|
import 'files_models.dart';
|
|
|
|
const double filesPanelMinWidth = 280;
|
|
const double filesPanelMaxWidth = 420;
|
|
const double filesPanelFraction = .30;
|
|
|
|
double filesPanelWidth(double available) => (available * filesPanelFraction)
|
|
.clamp(filesPanelMinWidth, filesPanelMaxWidth);
|
|
|
|
class FilesPanel extends StatefulWidget {
|
|
const FilesPanel({super.key, required this.controller, this.onOpen});
|
|
final FilesController controller;
|
|
final ValueChanged<DirectoryEntry>? onOpen;
|
|
@override
|
|
State<FilesPanel> createState() => _FilesPanelState();
|
|
}
|
|
|
|
class _FilesPanelState extends State<FilesPanel> {
|
|
late final TextEditingController _filter = TextEditingController();
|
|
FilesController get controller => widget.controller;
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
controller.addListener(_changed);
|
|
controller.start();
|
|
}
|
|
|
|
void _changed() {
|
|
if (mounted) setState(() {});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
controller.removeListener(_changed);
|
|
_filter.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final listing = controller.listing;
|
|
if (controller.missingDirectory) {
|
|
return const _FilesMessage('This chat has no working directory.');
|
|
}
|
|
return Material(
|
|
color: Theme.of(context).colorScheme.surface,
|
|
child: Column(
|
|
children: [
|
|
_header(context, listing),
|
|
if (controller.error != null)
|
|
ChatErrorNotice(
|
|
message: controller.error!,
|
|
onRetry: controller.retry,
|
|
),
|
|
if (controller.loading && listing == null)
|
|
const Expanded(child: ChatLoadingIndicator())
|
|
else if (listing != null &&
|
|
controller.entries.isEmpty &&
|
|
!controller.loading)
|
|
const Expanded(child: _FilesMessage('This folder is empty.'))
|
|
else
|
|
Expanded(
|
|
child: controller.view == FilesViewMode.grid ? _grid() : _list(),
|
|
),
|
|
if (listing?.truncated == true)
|
|
const Padding(
|
|
padding: EdgeInsets.all(8),
|
|
child: Text('Some entries are not shown.'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _header(BuildContext context, DirectoryListing? listing) => Padding(
|
|
padding: const EdgeInsets.fromLTRB(12, 8, 8, 4),
|
|
child: Column(
|
|
children: [
|
|
Row(
|
|
children: [
|
|
if (listing?.parent != null)
|
|
IconButton(
|
|
tooltip: 'Back',
|
|
onPressed: controller.loading
|
|
? null
|
|
: () => controller.open(listing!.parent),
|
|
icon: const Icon(Icons.arrow_back),
|
|
),
|
|
Expanded(
|
|
child: Text(
|
|
homeRelativeLabel(
|
|
listing?.home ?? '',
|
|
listing?.path ?? controller.initialPath,
|
|
),
|
|
overflow: TextOverflow.ellipsis,
|
|
),
|
|
),
|
|
IconButton(
|
|
tooltip: 'Refresh',
|
|
onPressed: controller.loading ? null : controller.retry,
|
|
icon: const Icon(Icons.refresh),
|
|
),
|
|
IconButton(
|
|
tooltip: controller.view == FilesViewMode.list
|
|
? 'Grid view'
|
|
: 'List view',
|
|
onPressed: () => controller.setView(
|
|
controller.view == FilesViewMode.list
|
|
? FilesViewMode.grid
|
|
: FilesViewMode.list,
|
|
),
|
|
icon: Icon(
|
|
controller.view == FilesViewMode.list
|
|
? Icons.grid_view
|
|
: Icons.view_list,
|
|
),
|
|
),
|
|
PopupMenuButton<FilesSortKey>(
|
|
initialValue: controller.sort.key,
|
|
tooltip: 'Sort',
|
|
onSelected: (key) =>
|
|
controller.setSort(controller.sort.reverseOr(key)),
|
|
itemBuilder: (_) => [
|
|
for (final key in FilesSortKey.values)
|
|
PopupMenuItem(value: key, child: Text(key.name)),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
child: chatControlsStyleOf(context) == ChatControlsStyle.cupertino
|
|
? CupertinoSearchTextField(
|
|
controller: _filter,
|
|
onChanged: controller.setFilter,
|
|
placeholder: 'Filter files',
|
|
)
|
|
: TextField(
|
|
controller: _filter,
|
|
onChanged: controller.setFilter,
|
|
decoration: const InputDecoration(
|
|
prefixIcon: Icon(Icons.search),
|
|
hintText: 'Filter files',
|
|
isDense: true,
|
|
),
|
|
),
|
|
),
|
|
IconButton(
|
|
tooltip: 'Show hidden files',
|
|
onPressed: () => controller.toggleHidden(!controller.showHidden),
|
|
icon: Icon(
|
|
controller.showHidden ? Icons.visibility : Icons.visibility_off,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
);
|
|
|
|
Widget _list() => ListView.builder(
|
|
itemCount: controller.entries.length,
|
|
itemBuilder: (_, index) {
|
|
final entry = controller.entries[index];
|
|
return ListTile(
|
|
onTap: () => entry.isDir
|
|
? controller.open(entry.path)
|
|
: widget.onOpen?.call(entry),
|
|
leading: Icon(
|
|
fileIcon(entry),
|
|
),
|
|
title: Text(entry.name),
|
|
subtitle: Text(
|
|
entry.isDir
|
|
? 'Folder'
|
|
: '${entry.modified ?? 'Unknown'} · ${fileSizeLabel(entry.size)}',
|
|
),
|
|
trailing: entry.isDir ? null : Text(fileKind(entry)),
|
|
);
|
|
},
|
|
);
|
|
|
|
Widget _grid() => GridView.builder(
|
|
padding: const EdgeInsets.all(12),
|
|
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
|
|
maxCrossAxisExtent: 180,
|
|
childAspectRatio: 1.25,
|
|
),
|
|
itemCount: controller.entries.length,
|
|
itemBuilder: (_, index) {
|
|
final entry = controller.entries[index];
|
|
return InkWell(
|
|
onTap: () => entry.isDir
|
|
? controller.open(entry.path)
|
|
: widget.onOpen?.call(entry),
|
|
child: Card(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(12),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Icon(
|
|
fileIcon(entry),
|
|
size: 32,
|
|
),
|
|
const SizedBox(height: 8),
|
|
Text(
|
|
entry.name,
|
|
maxLines: 2,
|
|
overflow: TextOverflow.ellipsis,
|
|
textAlign: TextAlign.center,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
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),
|
|
),
|
|
);
|
|
}
|