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:
2026-11-12 23:13:08 +00:00
parent 6ce67dc491
commit a440859108
3 changed files with 150 additions and 23 deletions
+87 -11
View File
@@ -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);
}
+4 -3
View File
@@ -12,6 +12,7 @@ phone and desktop Material layouts and does not depend on theme brightness.
Focused regression: `files_panel_test.dart` verifies the deep leaf remains
fully visible at the trailing edge, deeper navigation pins the new leaf,
middle-ellipsizes long labels, and tapping an earlier segment requests that
folder. `tool/memwatch/full.sh 'test/features/chat/files/files_panel_test.dart'`
passes (17 tests); `tool/analyze.sh` is clean. The capture fixture now starts at
a deep path and includes a long folder entry for rendered review.
folder. `tool/memwatch/full.sh 'test/features/chat/files/files_panel_test.dart test/features/chat/files/files_controller_persistence_test.dart'` passes (21 tests); `tool/analyze.sh` is clean. The capture fixture now starts at a deep
path and includes a long folder entry for rendered review. The fixture also
retains the remembered-folder missing-path switch from the concurrent Files
folder persistence change.
@@ -4,6 +4,7 @@ import 'package:flutter/services.dart';
import 'package:pipelines_app/features/chat/files/files_preview.dart';
import 'package:pipelines_app/features/artifacts/domain/artifact_kind.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/files/files_header.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/core/transport/pipelines_client.dart';
import 'package:pipelines_app/features/artifacts/actions/artifact_file_bridge.dart';
@@ -159,18 +160,67 @@ void main() {
expect(gateway.requestedPaths.last, '/home/alpha/beta/gamma/delta/leaf');
});
testWidgets('long breadcrumb labels use middle ellipsis', (tester) async {
await pump(
tester,
initialPath:
'/home/a-folder-name-that-is-long-enough-to-ellipsize-in-the-middle',
testWidgets('a long current folder is shortened in the middle to fit a '
'narrow panel and stays fully visible', (tester) async {
const name = 'a-folder-name-that-is-long-enough-to-ellipsize-in-the-middle';
final controller = FilesController(
gateway: Gateway(),
initialPath: '/home/projects/$name',
);
addTearDown(controller.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Align(
alignment: Alignment.topLeft,
// The narrowest Files panel.
child: SizedBox(
width: 280,
child: FilesPanel(
controller: controller,
viewer: const FilesViewer(),
),
),
),
),
),
);
await tester.pumpAndSettle();
expect(find.text('a-folder-name-that…ize-in-the-middle'), findsOneWidget);
expect(
find.text('a-folder-name-that-is-long-enough-to-ellipsize-in-the-middle'),
findsNothing,
final viewport = tester.getRect(
find.byKey(const Key('files-breadcrumb-scroll')),
);
final leaf = find.descendant(
of: find.byKey(const Key('files-breadcrumb-scroll')),
matching: find.byWidgetPredicate(
(widget) =>
widget is Text &&
(widget.data ?? '').startsWith('a-folder') &&
(widget.data ?? '').endsWith('middle'),
),
);
expect(leaf, findsOneWidget);
final label = tester.widget<Text>(leaf).data!;
expect(label, contains('…'));
expect(label.length, lessThan(name.length));
final rect = tester.getRect(leaf);
expect(rect.left, greaterThanOrEqualTo(viewport.left - 0.5));
expect(rect.right, lessThanOrEqualTo(viewport.right + 0.5));
expect(tester.takeException(), isNull);
});
test('middleEllipsis keeps a label that fits and shortens one that does '
'not', () {
const style = TextStyle(fontSize: 10);
expect(middleEllipsis('short', maxWidth: 500, style: style), 'short');
final cut = middleEllipsis(
'abcdefghijklmnopqrstuvwxyz',
maxWidth: 100,
style: style,
);
expect(cut, contains('…'));
expect(cut.startsWith('a'), isTrue);
expect(cut.endsWith('z'), isTrue);
});
testWidgets('file sorting uses a sort icon and preserves direction', (