a440859108
Each folder in the path gets a maximum width (the current folder the whole row) and its label is shortened in the middle to what actually fits, measured with the label's style, so a long name never pushes the current folder out of a narrow panel.
324 lines
9.7 KiB
Dart
324 lines
9.7 KiB
Dart
import 'dart:math' as math;
|
|
|
|
import 'package:flutter/material.dart';
|
|
import '../../../ui/layout/app_overflow_menu.dart';
|
|
import '../data/directory_listing.dart';
|
|
import '../domain/chat_attachment_picker.dart';
|
|
import 'files_controller.dart';
|
|
import 'files_models.dart';
|
|
|
|
class FilesHeader extends StatelessWidget {
|
|
const FilesHeader({
|
|
super.key,
|
|
required this.controller,
|
|
required this.filter,
|
|
required this.picker,
|
|
required this.onUpload,
|
|
});
|
|
final FilesController controller;
|
|
final TextEditingController filter;
|
|
|
|
/// The host's file picker; an unsupported host disables Upload with its
|
|
/// reason in the tooltip.
|
|
final ChatAttachmentPicker picker;
|
|
final VoidCallback onUpload;
|
|
|
|
bool get _canUpload =>
|
|
picker.isSupported &&
|
|
controller.listing != null &&
|
|
!controller.loading &&
|
|
controller.uploading == 0;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final listing = controller.listing;
|
|
return 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: picker.isSupported
|
|
? 'Upload files'
|
|
: 'Upload files: ${picker.unsupportedMessage}',
|
|
onPressed: _canUpload ? onUpload : null,
|
|
icon: const Icon(Icons.upload_file),
|
|
),
|
|
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,
|
|
),
|
|
),
|
|
AppOverflowMenu<FilesSortKey>(
|
|
label: 'Sort',
|
|
icon: const Icon(Icons.sort),
|
|
onSelected: (_, key) =>
|
|
controller.setSort(controller.sort.reverseOr(key)),
|
|
actions: [
|
|
for (final key in FilesSortKey.values)
|
|
AppMenuAction(
|
|
value: key,
|
|
label:
|
|
'${key.name}${controller.sort.key == key ? (controller.sort.ascending ? ' ↑' : ' ↓') : ''}',
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
if (listing != null)
|
|
_FilesBreadcrumbs(
|
|
home: listing.home,
|
|
path: listing.path,
|
|
loading: controller.loading,
|
|
onOpen: controller.open,
|
|
),
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
child: TextField(
|
|
controller: filter,
|
|
autofocus: true,
|
|
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,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _FilesBreadcrumbs extends StatefulWidget {
|
|
const _FilesBreadcrumbs({
|
|
required this.home,
|
|
required this.path,
|
|
required this.loading,
|
|
required this.onOpen,
|
|
});
|
|
|
|
final String home;
|
|
final String path;
|
|
final bool loading;
|
|
final ValueChanged<String> onOpen;
|
|
|
|
@override
|
|
State<_FilesBreadcrumbs> createState() => _FilesBreadcrumbsState();
|
|
}
|
|
|
|
class _FilesBreadcrumbsState extends State<_FilesBreadcrumbs> {
|
|
final ScrollController _scroll = ScrollController();
|
|
double? _lastWidth;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_pinToEnd();
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(covariant _FilesBreadcrumbs oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (oldWidget.path != widget.path) _pinToEnd();
|
|
}
|
|
|
|
void _pinToEnd() {
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (mounted && _scroll.hasClients) {
|
|
_scroll.jumpTo(_scroll.position.maxScrollExtent);
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_scroll.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final crumbs = folderCrumbs(widget.home, widget.path);
|
|
return LayoutBuilder(
|
|
builder: (context, constraints) {
|
|
final surface = Theme.of(context).colorScheme.surface;
|
|
if (_lastWidth != constraints.maxWidth) {
|
|
_lastWidth = constraints.maxWidth;
|
|
_pinToEnd();
|
|
}
|
|
return SizedBox(
|
|
width: constraints.maxWidth,
|
|
height: kMinInteractiveDimension,
|
|
child: Stack(
|
|
children: [
|
|
SingleChildScrollView(
|
|
key: const Key('files-breadcrumb-scroll'),
|
|
controller: _scroll,
|
|
scrollDirection: Axis.horizontal,
|
|
child: Row(
|
|
children: [
|
|
for (var index = 0; index < crumbs.length; index++)
|
|
_Crumb(
|
|
label: crumbs[index].label,
|
|
// The current folder may use the whole row; earlier
|
|
// folders stay short so the path reads at a glance.
|
|
maxWidth: index == crumbs.length - 1
|
|
? constraints.maxWidth
|
|
: math.min(160, constraints.maxWidth / 2),
|
|
onPressed: widget.loading
|
|
? null
|
|
: () => widget.onOpen(crumbs[index].path),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
IgnorePointer(
|
|
child: Align(
|
|
alignment: Alignment.centerLeft,
|
|
child: Container(
|
|
width: 20,
|
|
height: double.infinity,
|
|
decoration: BoxDecoration(
|
|
gradient: LinearGradient(
|
|
colors: [surface, surface.withValues(alpha: 0)],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|
|
|
|
/// One folder of the path: a button whose label is shortened in the middle
|
|
/// ("very-long-…-name") to fit [maxWidth], so a long name never pushes the
|
|
/// current folder out of view.
|
|
class _Crumb extends StatelessWidget {
|
|
const _Crumb({
|
|
required this.label,
|
|
required this.maxWidth,
|
|
required this.onPressed,
|
|
});
|
|
|
|
final String label;
|
|
final double maxWidth;
|
|
final VoidCallback? onPressed;
|
|
|
|
/// TextButton's own horizontal padding, which the label must leave room for.
|
|
static const double _padding = 24;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final style = DefaultTextStyle.of(
|
|
context,
|
|
).style.merge(Theme.of(context).textTheme.labelLarge);
|
|
final text = middleEllipsis(
|
|
label,
|
|
maxWidth: math.max(0, maxWidth - _padding),
|
|
style: style,
|
|
textScaler: MediaQuery.textScalerOf(context),
|
|
textDirection: Directionality.of(context),
|
|
);
|
|
return ConstrainedBox(
|
|
constraints: BoxConstraints(maxWidth: maxWidth),
|
|
child: TextButton(
|
|
onPressed: onPressed,
|
|
child: Text(text, maxLines: 1, softWrap: false),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// [label] shortened in the middle with an ellipsis so it fits [maxWidth]
|
|
/// when drawn in [style]; unchanged when it already fits.
|
|
String middleEllipsis(
|
|
String label, {
|
|
required double maxWidth,
|
|
required TextStyle? style,
|
|
TextScaler textScaler = TextScaler.noScaling,
|
|
TextDirection textDirection = TextDirection.ltr,
|
|
}) {
|
|
double width(String text) {
|
|
final painter = TextPainter(
|
|
text: TextSpan(text: text, style: style),
|
|
textDirection: textDirection,
|
|
textScaler: textScaler,
|
|
maxLines: 1,
|
|
)..layout();
|
|
final result = painter.width;
|
|
painter.dispose();
|
|
return result;
|
|
}
|
|
|
|
if (width(label) <= maxWidth) return label;
|
|
String shortened(int keep) {
|
|
final leading = (keep + 1) ~/ 2;
|
|
return '${label.substring(0, leading)}…'
|
|
'${label.substring(label.length - (keep - leading))}';
|
|
}
|
|
|
|
// The longest middle-shortened label that still fits.
|
|
var low = 0, high = label.length - 1;
|
|
while (low < high) {
|
|
final mid = (low + high + 1) ~/ 2;
|
|
if (width(shortened(mid)) <= maxWidth) {
|
|
low = mid;
|
|
} else {
|
|
high = mid - 1;
|
|
}
|
|
}
|
|
return shortened(low);
|
|
}
|