Fit long Files breadcrumb labels to the panel
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.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../../ui/layout/app_overflow_menu.dart';
|
||||
import '../data/directory_listing.dart';
|
||||
@@ -204,14 +206,16 @@ class _FilesBreadcrumbsState extends State<_FilesBreadcrumbs> {
|
||||
child: Row(
|
||||
children: [
|
||||
for (var index = 0; index < crumbs.length; index++)
|
||||
TextButton(
|
||||
_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),
|
||||
child: Text(
|
||||
_middleEllipsis(crumbs[index].label),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -238,10 +242,82 @@ class _FilesBreadcrumbsState extends State<_FilesBreadcrumbs> {
|
||||
}
|
||||
}
|
||||
|
||||
String _middleEllipsis(String label, {int maxCharacters = 36}) {
|
||||
if (label.length <= maxCharacters) return label;
|
||||
final visible = maxCharacters - 1;
|
||||
final leading = (visible + 1) ~/ 2;
|
||||
final trailing = visible - leading;
|
||||
return '${label.substring(0, leading)}…${label.substring(label.length - trailing)}';
|
||||
/// 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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user