diff --git a/app/fonts/README.md b/app/fonts/README.md new file mode 100644 index 00000000..3e76f7fc --- /dev/null +++ b/app/fonts/README.md @@ -0,0 +1,8 @@ +# Bundled fonts + +`RobotoMono-Regular.ttf` and `RobotoMono-Bold.ttf` are Roboto Mono by +Christian Robertson, licensed under the Apache License, Version 2.0 +(https://www.apache.org/licenses/LICENSE-2.0). The files are copied unchanged +from the Flutter SDK's DevTools assets. They give monospace text (artifact text +previews, background task output) a real fixed-width face on every platform: +Flutter web does not resolve the generic `monospace` family. diff --git a/app/fonts/RobotoMono-Bold.ttf b/app/fonts/RobotoMono-Bold.ttf new file mode 100644 index 00000000..482f028a Binary files /dev/null and b/app/fonts/RobotoMono-Bold.ttf differ diff --git a/app/fonts/RobotoMono-Regular.ttf b/app/fonts/RobotoMono-Regular.ttf new file mode 100644 index 00000000..5919b5d1 Binary files /dev/null and b/app/fonts/RobotoMono-Regular.ttf differ diff --git a/app/lib/features/artifacts/domain/artifact_kind.dart b/app/lib/features/artifacts/domain/artifact_kind.dart index 3cf24f0c..340b9d37 100644 --- a/app/lib/features/artifacts/domain/artifact_kind.dart +++ b/app/lib/features/artifacts/domain/artifact_kind.dart @@ -196,21 +196,38 @@ bool isMarkdownArtifact(String contentType, String name) { return _markdownExtensions.contains(_extensionOf(name)); } -/// Whether an artifact opens inside the viewer: images, videos, markdown/text -/// documents and any recorded `text/plain` file. The viewer draws text/plain as -/// selectable text; a CSV, source file or spreadsheet with its own type stays an -/// external file because it is evidence, not prose. Videos are pageable but the -/// viewer reports that it cannot draw them rather than faking a player. -bool isViewerArtifact(String contentType, String name) { - if (isImageContentType(contentType) || isVideoContentType(contentType)) { - return true; - } +/// How the viewer presents an artifact in-app, or [none] when it only offers +/// Open/Download. +enum ArtifactPreviewFormat { image, video, markdown, text, none } + +/// The one rule for in-app previews, shared by the gallery's paging and the +/// viewer's fetch plan. Images and videos come from the content type; markdown +/// from `text/markdown` or an md/markdown name; plain text, logs, JSON and +/// source/config files are shown as selectable monospace text. +ArtifactPreviewFormat artifactPreviewFormat(String contentType, String name) { + if (isImageContentType(contentType)) return ArtifactPreviewFormat.image; + if (isVideoContentType(contentType)) return ArtifactPreviewFormat.video; final mime = contentType.split(';').first.trim().toLowerCase(); - if (mime == 'text/plain') return true; - return artifactKind(contentType, name) == ArtifactKind.document && - isMarkdownArtifact(contentType, name); + final ext = _extensionOf(name); + if (mime == 'text/markdown' || ext == 'md' || ext == 'markdown') { + return ArtifactPreviewFormat.markdown; + } + final kind = artifactKind(contentType, name); + if (kind == ArtifactKind.json || + kind == ArtifactKind.log || + kind == ArtifactKind.code || + mime == 'text/plain' || + ext == 'txt' || + ext == 'rst') { + return ArtifactPreviewFormat.text; + } + return ArtifactPreviewFormat.none; } +/// Whether an artifact opens inside the viewer (see [artifactPreviewFormat]). +bool isViewerArtifact(String contentType, String name) => + artifactPreviewFormat(contentType, name) != ArtifactPreviewFormat.none; + /// Human-readable byte size, matching the web client: whole bytes under 1 KiB, /// otherwise one decimal with a binary unit (KiB/MiB/GiB/TiB/PiB/EiB). String humanBytes(num size) { diff --git a/app/lib/features/artifacts/domain/artifact_preview.dart b/app/lib/features/artifacts/domain/artifact_preview.dart index 728efe12..243c5c2c 100644 --- a/app/lib/features/artifacts/domain/artifact_preview.dart +++ b/app/lib/features/artifacts/domain/artifact_preview.dart @@ -56,9 +56,10 @@ const String artifactPreviewExternalUrl = /// Classifies [artifact] against the configured server origin. /// -/// Images are drawn from the resolved bytes; a video plays from the resolved -/// url; `text/plain` is selectable text; a `.md`/`.markdown` name or -/// `text/markdown` is handed to the injected markdown builder. Everything else is explicitly unsupported rather than faked. +/// The format comes from [artifactPreviewFormat]: images are drawn from the +/// resolved bytes, a video plays from the resolved url, markdown goes to the +/// injected builder and the text family is selectable monospace text. +/// Everything else is explicitly unsupported rather than faked. ArtifactPreviewPlan planArtifactPreview({ required ArtifactRecord artifact, required String configuredEndpoint, @@ -85,34 +86,18 @@ ArtifactPreviewPlan planArtifactPreview({ message: artifactPreviewExternalUrl, ); } - final mime = artifact.contentType.split(';').first.trim().toLowerCase(); - if (isImageContentType(mime)) { - return ArtifactPreviewPlan( - kind: ArtifactPreviewKind.image, - resolved: resolved, - message: '', - ); - } - if (isVideoContentType(mime)) { - return ArtifactPreviewPlan( - kind: ArtifactPreviewKind.video, - resolved: resolved, - message: '', - ); - } - if (mime == 'text/markdown' || _isMarkdownName(artifact.name)) { - return ArtifactPreviewPlan( - kind: ArtifactPreviewKind.markdown, - resolved: resolved, - message: '', - ); - } - if (mime == 'text/plain') { - return ArtifactPreviewPlan( - kind: ArtifactPreviewKind.text, - resolved: resolved, - message: '', - ); + final kind = switch (artifactPreviewFormat( + artifact.contentType, + artifact.name, + )) { + ArtifactPreviewFormat.image => ArtifactPreviewKind.image, + ArtifactPreviewFormat.video => ArtifactPreviewKind.video, + ArtifactPreviewFormat.markdown => ArtifactPreviewKind.markdown, + ArtifactPreviewFormat.text => ArtifactPreviewKind.text, + ArtifactPreviewFormat.none => null, + }; + if (kind != null) { + return ArtifactPreviewPlan(kind: kind, resolved: resolved, message: ''); } return ArtifactPreviewPlan( kind: ArtifactPreviewKind.unsupported, @@ -120,10 +105,3 @@ ArtifactPreviewPlan planArtifactPreview({ message: 'This file type cannot be previewed here.', ); } - -bool _isMarkdownName(String name) { - final dot = name.lastIndexOf('.'); - if (dot == -1) return false; - final extension = name.substring(dot + 1).toLowerCase(); - return extension == 'md' || extension == 'markdown'; -} diff --git a/app/lib/features/artifacts/ui/artifact_viewer_content.dart b/app/lib/features/artifacts/ui/artifact_viewer_content.dart index 3ac0a122..f4ee50b8 100644 --- a/app/lib/features/artifacts/ui/artifact_viewer_content.dart +++ b/app/lib/features/artifacts/ui/artifact_viewer_content.dart @@ -4,6 +4,7 @@ import 'package:flutter/material.dart'; import '../domain/artifact_preview.dart'; import 'artifact_video_player.dart'; +import '../../../ui/text/monospace.dart'; /// Renders fetched markdown text for the viewer. It takes the build context so /// a themed markdown renderer can be supplied; the viewer's own default stays @@ -68,7 +69,14 @@ class ArtifactPreviewBody extends StatelessWidget { ), ); case ArtifactPreviewKind.text: - return _PreviewPadding(child: SelectableText(text ?? '')); + // Logs, JSON and source keep their columns: a monospace body. + return _PreviewPadding( + child: SelectableText( + text ?? '', + key: const Key('artifact-text-preview'), + style: const TextStyle(fontFamily: appMonospaceFamily), + ), + ); case ArtifactPreviewKind.markdown: return _PreviewPadding(child: markdownBuilder(context, text ?? '')); case ArtifactPreviewKind.video: diff --git a/app/lib/features/chat/conversation/chat_background_task_detail.dart b/app/lib/features/chat/conversation/chat_background_task_detail.dart index df2f2552..46cb9704 100644 --- a/app/lib/features/chat/conversation/chat_background_task_detail.dart +++ b/app/lib/features/chat/conversation/chat_background_task_detail.dart @@ -6,6 +6,7 @@ import 'package:flutter/material.dart'; import '../../../core/api_gateway.dart'; import '../domain/chat_shells.dart'; import '../ui/sessions/chat_adaptive.dart'; +import '../../../ui/text/monospace.dart'; /// One bounded output page: the web's 220 px pane is twelve 18 px monospace /// lines (`web/chat.css`), so the bound is read from the host's own output type @@ -65,7 +66,7 @@ class ChatBackgroundTaskDetail extends StatelessWidget { _label(context, cupertino, shellOutputTaskIdentityLabel), Text( shellIdOf(shell), - style: const TextStyle(fontFamily: 'monospace'), + style: const TextStyle(fontFamily: appMonospaceFamily), ), Text(shellStatusRunningLabel), _outputArea(context, cupertino), @@ -101,7 +102,7 @@ class ChatBackgroundTaskDetail extends StatelessWidget { ? CupertinoTheme.of(context).textTheme.textStyle : Theme.of(context).textTheme.bodySmall ?? const TextStyle()) .copyWith( - fontFamily: 'monospace', + fontFamily: appMonospaceFamily, height: _outputLineHeight, // Cupertino's style is not inheriting; a selectable body needs a // baseline from its own style. diff --git a/app/lib/features/runs/ui/run_detail_tab_bodies.dart b/app/lib/features/runs/ui/run_detail_tab_bodies.dart index 9ba30ac6..44dca4ef 100644 --- a/app/lib/features/runs/ui/run_detail_tab_bodies.dart +++ b/app/lib/features/runs/ui/run_detail_tab_bodies.dart @@ -5,6 +5,7 @@ import '../../../core/api_gateway.dart'; import 'run_detail_content.dart'; import 'run_detail_summary_fields.dart'; import 'run_detail_visit_facts.dart'; +import '../../../ui/text/monospace.dart'; /// The Summary tab: the run's identity, the recorded facts as key/value rows, /// the stored original prompt with its copy control and the collapsible Input @@ -159,7 +160,9 @@ Widget _materialRow(BuildContext context, DetailSummaryRow row) { ), subtitle: SelectableText( row.value, - style: row.monospace ? base?.copyWith(fontFamily: 'monospace') : base, + style: row.monospace + ? base?.copyWith(fontFamily: appMonospaceFamily) + : base, ), ); } @@ -188,7 +191,9 @@ Widget _materialBlock(BuildContext context, DetailSummaryRow row) { ), child: SelectableText( row.value, - style: theme.textTheme.bodySmall?.copyWith(fontFamily: 'monospace'), + style: theme.textTheme.bodySmall?.copyWith( + fontFamily: appMonospaceFamily, + ), ), ), ], @@ -279,7 +284,9 @@ Widget _cupertinoRow(BuildContext context, DetailSummaryRow row) { title: Text(row.label), subtitle: Text( row.value, - style: row.monospace ? style.copyWith(fontFamily: 'monospace') : null, + style: row.monospace + ? style.copyWith(fontFamily: appMonospaceFamily) + : null, ), ); } @@ -308,7 +315,9 @@ Widget _cupertinoBlock(BuildContext context, DetailSummaryRow row) { ), child: Text( row.value, - style: theme.textTheme.textStyle.copyWith(fontFamily: 'monospace'), + style: theme.textTheme.textStyle.copyWith( + fontFamily: appMonospaceFamily, + ), ), ), ], diff --git a/app/lib/ui/text/monospace.dart b/app/lib/ui/text/monospace.dart new file mode 100644 index 00000000..def0fd77 --- /dev/null +++ b/app/lib/ui/text/monospace.dart @@ -0,0 +1,3 @@ +/// The bundled fixed-width family (fonts/README.md). Flutter web does not +/// resolve the generic `monospace` family, so monospace text names this one. +const String appMonospaceFamily = 'RobotoMono'; diff --git a/app/pubspec.yaml b/app/pubspec.yaml index 37da3eb2..84605e0a 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -63,6 +63,13 @@ flutter: # the material Icons class. uses-material-design: true + fonts: + - family: RobotoMono + fonts: + - asset: fonts/RobotoMono-Regular.ttf + - asset: fonts/RobotoMono-Bold.ttf + weight: 700 + # To add assets to your application, add an assets section, like this: # assets: # - images/a_dot_burr.jpeg diff --git a/app/test/features/artifacts/domain/artifact_kind_test.dart b/app/test/features/artifacts/domain/artifact_kind_test.dart index 98ffb2eb..8cf63f19 100644 --- a/app/test/features/artifacts/domain/artifact_kind_test.dart +++ b/app/test/features/artifacts/domain/artifact_kind_test.dart @@ -93,14 +93,26 @@ void main() { ('text/plain', 'memory.txt'), ('text/plain', 'run.log'), ('', 'output.md'), + // The text family previews as selectable text, not an external file. + ('application/json', 'payload.json'), + ('text/x-python', 'main.py'), + ('application/octet-stream', 'config.yaml'), + ('', 'build.log'), ]) { expect(isViewerArtifact(contentType, name), isTrue, reason: name); } + expect( + artifactPreviewFormat('application/json', 'payload.json'), + ArtifactPreviewFormat.text, + ); + expect( + artifactPreviewFormat('text/markdown', 'plan'), + ArtifactPreviewFormat.markdown, + ); for (final (contentType, name) in <(String, String)>[ ('text/html', 'session.html'), ('text/csv', 'rows.csv'), - ('application/json', 'payload.json'), - ('text/x-python', 'main.py'), + ('application/zip', 'bundle.zip'), ('application/octet-stream', 'blob.bin'), ('', ''), ]) { diff --git a/app/test/features/artifacts/domain/gallery_navigation_test.dart b/app/test/features/artifacts/domain/gallery_navigation_test.dart index d642dba1..56daf5f3 100644 --- a/app/test/features/artifacts/domain/gallery_navigation_test.dart +++ b/app/test/features/artifacts/domain/gallery_navigation_test.dart @@ -39,6 +39,7 @@ void main() { 'input.md', 'run.log', 'shot.png', + 'main.py', 'clip.mp4', 'memory.txt', 'output.md', @@ -161,7 +162,7 @@ void main() { sequence: 1, artifacts: >[ artifact('session.html', contentType: 'text/html'), - artifact('main.py', contentType: 'text/x-python'), + artifact('bundle.zip', contentType: 'application/zip'), artifact('result.csv', contentType: 'text/csv'), ], ), diff --git a/app/test/features/artifacts/ui/artifact_text_preview_test.dart b/app/test/features/artifacts/ui/artifact_text_preview_test.dart new file mode 100644 index 00000000..1c77402c --- /dev/null +++ b/app/test/features/artifacts/ui/artifact_text_preview_test.dart @@ -0,0 +1,37 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/artifacts/domain/artifact_preview.dart'; +import 'package:pipelines_app/features/artifacts/domain/artifact_url.dart'; +import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_content.dart'; +import 'package:pipelines_app/ui/text/monospace.dart'; + +void main() { + testWidgets('a text-family preview is selectable bundled monospace', ( + tester, + ) async { + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: ArtifactPreviewBody( + plan: ArtifactPreviewPlan( + kind: ArtifactPreviewKind.text, + resolved: ResolvedArtifactUrl( + uri: Uri.parse('http://127.0.0.1:8080/artifacts/1/main.py'), + external: false, + ), + message: '', + ), + loading: false, + markdownBuilder: defaultArtifactMarkdown, + text: 'def main():\n return 0\n', + ), + ), + ), + ); + final text = tester.widget( + find.byKey(const Key('artifact-text-preview')), + ); + expect(text.data, 'def main():\n return 0\n'); + expect(text.style?.fontFamily, appMonospaceFamily); + }); +} diff --git a/app/test/features/runs/ui/run_detail_summary_fields_test.dart b/app/test/features/runs/ui/run_detail_summary_fields_test.dart index 795e427d..4ede97f0 100644 --- a/app/test/features/runs/ui/run_detail_summary_fields_test.dart +++ b/app/test/features/runs/ui/run_detail_summary_fields_test.dart @@ -6,6 +6,7 @@ import 'package:pipelines_app/features/pipelines/pipeline_format.dart'; import 'package:pipelines_app/features/runs/ui/run_detail_summary_fields.dart'; import 'run_detail_graph_support.dart'; +import 'package:pipelines_app/ui/text/monospace.dart'; /// A run whose recorded facts fill every Summary row. JsonMap recordedRunFixture() => { @@ -156,12 +157,12 @@ void main() { final hash = tester.widget( find.widgetWithText(SelectableText, 'abc123def456'), ); - expect(hash.style?.fontFamily, 'monospace'); + expect(hash.style?.fontFamily, appMonospaceFamily); } else { final tile = tester.widget( find.byKey(const ValueKey('run-detail-summary-content-hash')), ); - expect((tile.subtitle! as Text).style?.fontFamily, 'monospace'); + expect((tile.subtitle! as Text).style?.fontFamily, appMonospaceFamily); } });