Page through a folder's files in the file viewer

Opening a file from the Files panel now passes the folder's previewable
files in their shown order, so the arrows, swipes and Previous/Next step
through them and Escape closes the viewer. Download follows the file on
screen.
This commit is contained in:
2026-11-10 00:14:18 +00:00
parent ec70bee12e
commit 339cce0424
4 changed files with 84 additions and 15 deletions
@@ -63,6 +63,10 @@ class ArtifactViewerPage extends StatefulWidget {
final bool includeExternalDocuments;
final String configuredEndpoint;
/// Where Download fetches the shown file. An artifact whose `raw` carries
/// `download_url` (the Files panel's per-file links) overrides it, so paging
/// through a folder downloads the file on screen.
final String? downloadUrl;
/// The preview rule; the Files panel passes its own (every HTML previews).
@@ -235,9 +239,13 @@ class _ArtifactViewerPageState extends State<ArtifactViewerPage>
Widget buildFrame(BuildContext context, {required bool fullscreen}) {
final page = _page;
final plan = _plan;
final downloadResolved = widget.downloadUrl == null
final pageDownload = page?.artifact.raw['download_url'];
final download = pageDownload is String && pageDownload.isNotEmpty
? pageDownload
: widget.downloadUrl;
final downloadResolved = download == null
? null
: resolveArtifactUrl(widget.configuredEndpoint, widget.downloadUrl!);
: resolveArtifactUrl(widget.configuredEndpoint, download);
final title = page == null
? artifactViewerTitle
: artifactDisplayName(page.artifact);
+6 -1
View File
@@ -217,7 +217,12 @@ class _FilesPanelState extends State<FilesPanel> {
if (widget.onOpen != null) {
widget.onOpen!(entry);
} else {
widget.viewer.open(context, endpoint: _endpoint, entry: entry);
widget.viewer.open(
context,
endpoint: _endpoint,
entry: entry,
siblings: widget.controller.entries,
);
}
}
}
+26 -12
View File
@@ -37,10 +37,14 @@ class FilesViewer {
final ArtifactExternalOpen openExternal;
final FileUrlLauncher launch;
/// Opens [entry]. [siblings] are the folder's entries in the order shown;
/// the viewer then steps through every previewable one with the arrows,
/// swipes and Previous/Next, and Escape closes it.
Future<void> open(
BuildContext context, {
required String endpoint,
required DirectoryEntry entry,
List<DirectoryEntry> siblings = const [],
}) async {
final urls = resolveFileUrls(endpoint, entry.path);
if (urls == null) {
@@ -62,18 +66,28 @@ class FilesViewer {
_showSheet(context, entry, urls);
return;
}
final artifact = ArtifactRecord(
name: entry.name,
url: urls.view.toString(),
contentType: type,
sizeBytes: entry.size ?? 0,
raw: const {},
);
final group = ArtifactGroup(
stageKey: 'Files',
visit: 1,
artifacts: [artifact],
);
ArtifactRecord? record(DirectoryEntry file) {
if (file.isDir || !isPreviewableFile(file.name)) return null;
final links = resolveFileUrls(endpoint, file.path);
if (links == null) return null;
final kind = fileContentType(file.name);
if (artifactKind(kind, file.name) == ArtifactKind.pdf) return null;
return ArtifactRecord(
name: file.name,
url: links.view.toString(),
contentType: kind,
sizeBytes: file.size ?? 0,
raw: {'download_url': links.download.toString()},
);
}
final artifact = record(entry)!;
final pages = [
for (final file in siblings)
if (file.path == entry.path) artifact else ?record(file),
];
if (!pages.contains(artifact)) pages.add(artifact);
final group = ArtifactGroup(stageKey: 'Files', visit: 1, artifacts: pages);
final page = ArtifactViewerPage(
groups: [group],
selectedIdentity: artifactVisitIdentityOf(group, artifact),
@@ -1,5 +1,6 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/cupertino.dart';
import 'package:pipelines_app/features/chat/files/files_preview.dart';
import 'package:pipelines_app/features/artifacts/domain/artifact_kind.dart';
@@ -324,4 +325,45 @@ void main() {
expect(find.text("Couldn't load this folder."), findsOneWidget);
expect(find.textContaining('internal transport'), findsNothing);
});
testWidgets('arrows step through the folder\'s previewable files and '
'Escape closes the viewer', (tester) async {
final fetched = <String>[];
await pump(
tester,
viewer: FilesViewer(
fetch: (url) async {
fetched.add(url.queryParameters['path'] ?? '');
return ArtifactFetchResult(
status: 200,
bytes: url.queryParameters['path']!.endsWith('.png')
? base64Decode(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==',
)
: utf8.encode('A text file preview'),
);
},
),
);
Finder shown(String name) => find.descendant(
of: find.byType(ArtifactViewerPage),
matching: find.text(name),
);
await tester.tap(find.text('notes.txt'));
await tester.pumpAndSettle();
expect(find.byType(ArtifactViewerPage), findsOneWidget);
expect(shown('notes.txt'), findsWidgets);
// The zip and PDF are not previewable, so the next file is the image.
await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight);
await tester.pumpAndSettle();
expect(shown('picture.png'), findsWidgets);
expect(fetched, contains('/home/demo/picture.png'));
await tester.sendKeyEvent(LogicalKeyboardKey.arrowLeft);
await tester.pumpAndSettle();
expect(shown('notes.txt'), findsWidgets);
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.pumpAndSettle();
expect(find.byType(ArtifactViewerPage), findsNothing);
expect(find.text('notes.txt'), findsOneWidget);
});
}