Merge: artifact full screen; HTML scrolls, pans and zooms; links open safely

This commit is contained in:
2026-11-03 18:51:15 +00:00
26 changed files with 1305 additions and 319 deletions
@@ -0,0 +1,50 @@
/// What following one link inside a recorded HTML document does.
enum ArtifactHtmlLinkAction {
/// A place in the same document (`#section`): the view scrolls to it.
inDocument,
/// An `http`, `https` or `mailto` address: it opens outside the artifact
/// (a new browser tab on the web, the system's handler on a phone), never
/// inside the document's view.
external,
/// Anything else (`javascript:`, `file:`, `data:`, a relative path the
/// document has no base for): the link stays inert.
blocked,
}
const _externalSchemes = {'http', 'https', 'mailto'};
/// The documents the views load the recorded HTML as: a web view's
/// `about:blank` and an iframe's `about:srcdoc`.
const _documentUrls = {'about:blank', 'about:srcdoc'};
/// Decides what a link in a recorded HTML document does, from the raw `href`
/// (the web view rewrites anchors before showing the document) or the url a
/// mobile web view resolved it to.
///
/// The document is loaded from a string, so it has no address of its own: a
/// relative link cannot resolve to the artifact's neighbours and stays inert
/// rather than reaching the app's own origin.
ArtifactHtmlLinkAction artifactHtmlLinkAction(String href) {
final link = href.trim();
if (link.isEmpty) return ArtifactHtmlLinkAction.blocked;
if (link.startsWith('#')) return ArtifactHtmlLinkAction.inDocument;
final uri = Uri.tryParse(link);
if (uri == null || !uri.hasScheme) return ArtifactHtmlLinkAction.blocked;
final scheme = uri.scheme.toLowerCase();
if (_externalSchemes.contains(scheme)) {
// `http:foo` with no host is not an address anyone can open.
if (scheme != 'mailto' && uri.host.isEmpty) {
return ArtifactHtmlLinkAction.blocked;
}
return ArtifactHtmlLinkAction.external;
}
if (scheme == 'about') {
final document = link.split('#').first.toLowerCase();
if (_documentUrls.contains(document)) {
return ArtifactHtmlLinkAction.inDocument;
}
}
return ArtifactHtmlLinkAction.blocked;
}
@@ -0,0 +1,116 @@
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../ui/adaptive/host.dart';
/// Open, Copy and the file actions float over the top of the content: shown
/// when the page opens and whenever the content is touched, hidden again after
/// a few seconds without use, so nothing needs scrolling to reach them.
class ArtifactControlsOverlay extends StatefulWidget {
const ArtifactControlsOverlay({
super.key,
required this.controls,
required this.child,
this.hideAfter = const Duration(seconds: 3),
});
final List<Widget> controls;
final Widget child;
final Duration hideAfter;
@override
State<ArtifactControlsOverlay> createState() =>
_ArtifactControlsOverlayState();
}
class _ArtifactControlsOverlayState extends State<ArtifactControlsOverlay> {
bool _shown = true;
Timer? _hide;
@override
void initState() {
super.initState();
_schedule();
}
@override
void dispose() {
_hide?.cancel();
super.dispose();
}
void _schedule() {
_hide?.cancel();
_hide = Timer(widget.hideAfter, () {
if (mounted) setState(() => _shown = false);
});
}
void _show() {
if (!_shown) setState(() => _shown = true);
_schedule();
}
@override
Widget build(BuildContext context) {
final cupertino = usesCupertinoHost(context);
final surface = cupertino
? CupertinoColors.systemBackground
.resolveFrom(context)
.withValues(alpha: 0.92)
: Theme.of(
context,
).colorScheme.surfaceContainerHigh.withValues(alpha: 0.94);
return Stack(
children: [
// Any touch on the content brings the controls back.
Positioned.fill(
child: Listener(
behavior: HitTestBehavior.translucent,
onPointerDown: (_) => _show(),
child: widget.child,
),
),
Positioned(
left: 8,
right: 8,
top: 8,
child: IgnorePointer(
ignoring: !_shown,
child: AnimatedOpacity(
key: const Key('artifact-viewer-controls'),
opacity: _shown ? 1 : 0,
duration: const Duration(milliseconds: 200),
child: Listener(
// Using a control keeps the bar up.
onPointerDown: (_) => _schedule(),
child: DecoratedBox(
decoration: BoxDecoration(
color: surface,
borderRadius: BorderRadius.circular(12),
boxShadow: const [
BoxShadow(color: Color(0x22000000), blurRadius: 6),
],
),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 4,
vertical: 2,
),
child: Wrap(
alignment: WrapAlignment.center,
crossAxisAlignment: WrapCrossAlignment.center,
children: widget.controls,
),
),
),
),
),
),
),
],
);
}
}
@@ -0,0 +1,116 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import '../../../ui/adaptive/segmented_bar_overflow.dart';
import '../actions/artifact_file_actions.dart';
/// Marks a subtree as the artifact viewer's full-screen view, where an HTML
/// document takes every gesture over it (scroll, pan, pinch) instead of
/// sharing them with the viewer's page swipe, and follows [htmlZoom].
class ArtifactFullscreenScope extends InheritedWidget {
const ArtifactFullscreenScope({
super.key,
required this.htmlZoom,
required super.child,
});
/// The HTML document's zoom factor (1 is the document's own size), set by
/// [ArtifactHtmlZoomActions].
final ValueListenable<double> htmlZoom;
static ArtifactFullscreenScope? maybeOf(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<ArtifactFullscreenScope>();
@override
bool updateShouldNotify(ArtifactFullscreenScope oldWidget) =>
htmlZoom != oldWidget.htmlZoom;
}
/// The full-screen view's route, pushed on the root navigator so it covers
/// the shell's bars and list pane. A full-screen dialog route: its bar shows
/// the platform's Close, and the system back gesture leaves it.
Route<void> artifactFullscreenRoute({
required bool cupertino,
required WidgetBuilder builder,
}) => cupertino
? CupertinoPageRoute<void>(fullscreenDialog: true, builder: builder)
: MaterialPageRoute<void>(fullscreenDialog: true, builder: builder);
/// The zoom factors Zoom in and Zoom out step through.
const List<double> artifactHtmlZoomSteps = [
0.5, 0.67, 0.8, 0.9, 1, 1.1, 1.25, 1.5, 1.75, 2, 2.5, 3, 4, //
];
/// The next zoom step from [current] in [direction] (+1 in, -1 out), held at
/// both ends.
double artifactHtmlZoomStep(double current, int direction) {
if (direction > 0) {
return artifactHtmlZoomSteps.firstWhere(
(step) => step > current + 0.001,
orElse: () => artifactHtmlZoomSteps.last,
);
}
return artifactHtmlZoomSteps.lastWhere(
(step) => step < current - 0.001,
orElse: () => artifactHtmlZoomSteps.first,
);
}
const artifactZoomOutKey = ValueKey<String>('artifact-zoom-out');
const artifactZoomResetKey = ValueKey<String>('artifact-zoom-reset');
const artifactZoomInKey = ValueKey<String>('artifact-zoom-in');
/// Zoom out, actual size and zoom in for an HTML document, as top-bar
/// actions (a narrow bar moves them into its More menu). Used where the
/// document cannot be pinched (the web's sandboxed frame): the frame keeps
/// scrolling the live document at the chosen size.
List<Widget> artifactHtmlZoomActions(ValueNotifier<double> zoom) {
void step(int direction) =>
zoom.value = artifactHtmlZoomStep(zoom.value, direction);
final percent = '${(zoom.value * 100).round()}%';
Widget action(
Key key,
String label,
IconData icon,
IconData cupertinoIcon,
VoidCallback? onPressed,
) => SegmentedOverflowAction(
key: key,
overflowEntry: SegmentedOverflowEntry(
label: label,
icon: icon,
onPressed: onPressed,
),
child: ArtifactIconAction(
label: label,
icon: icon,
cupertinoIcon: cupertinoIcon,
onPressed: onPressed,
),
);
final value = zoom.value;
return [
action(
artifactZoomOutKey,
'Zoom out',
Icons.zoom_out,
CupertinoIcons.zoom_out,
value > artifactHtmlZoomSteps.first + 0.001 ? () => step(-1) : null,
),
action(
artifactZoomResetKey,
'Actual size ($percent)',
Icons.restart_alt,
CupertinoIcons.arrow_counterclockwise,
(value - 1).abs() > 0.001 ? () => zoom.value = 1 : null,
),
action(
artifactZoomInKey,
'Zoom in',
Icons.zoom_in,
CupertinoIcons.zoom_in,
value < artifactHtmlZoomSteps.last - 0.001 ? () => step(1) : null,
),
];
}
@@ -0,0 +1,58 @@
import 'package:flutter/widgets.dart';
import '../../../ui/adaptive/host.dart';
import 'artifact_fullscreen.dart';
/// Lets a viewer page open its own content as the full-screen view.
///
/// The full-screen route lives on the root navigator, outside the page's
/// subtree, yet draws from the page's state: [buildFrame] builds either
/// presentation, and every [setState] redraws the full-screen route too, so
/// paging, loading and status messages show in both.
mixin ArtifactFullscreenHost<T extends StatefulWidget> on State<T> {
final ValueNotifier<int> _revision = ValueNotifier<int>(0);
/// The full-screen view's HTML zoom; each full-screen visit starts at 1.
final ValueNotifier<double> htmlZoom = ValueNotifier<double>(1);
Route<void>? _fullscreen;
/// The page's scaffold, inline or as the full-screen view.
Widget buildFrame(BuildContext context, {required bool fullscreen});
@override
void setState(VoidCallback fn) {
super.setState(fn);
_revision.value++;
}
@override
void dispose() {
// The full-screen route closes with the page, once the tree unlocks.
final route = _fullscreen;
if (route != null) {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (route.isActive) route.navigator?.removeRoute(route);
});
}
super.dispose();
}
/// Opens the full-screen view; it returns when the view closes.
Future<void> enterFullscreen() async {
if (_fullscreen != null) return;
htmlZoom.value = 1;
final route = artifactFullscreenRoute(
cupertino: usesCupertinoHost(context),
builder: (_) => ArtifactFullscreenScope(
htmlZoom: htmlZoom,
child: ListenableBuilder(
listenable: Listenable.merge([_revision, htmlZoom]),
builder: (context, _) => buildFrame(context, fullscreen: true),
),
),
);
_fullscreen = route;
await Navigator.of(context, rootNavigator: true).push(route);
_fullscreen = null;
}
}
@@ -370,7 +370,7 @@ class _ArtifactGalleryPageState extends State<ArtifactGalleryPage> {
CupertinoDialogAction(
isDefaultAction: true,
onPressed: () => _open(resolved, dialogContext),
child: const Text('Open'),
child: const Text('Open in browser'),
),
],
),
@@ -394,7 +394,7 @@ class _ArtifactGalleryPageState extends State<ArtifactGalleryPage> {
if (resolved != null)
TextButton(
onPressed: () => _open(resolved, dialogContext),
child: const Text('Open'),
child: const Text('Open in browser'),
),
],
),
@@ -0,0 +1,52 @@
import 'dart:js_interop';
import 'package:web/web.dart' as web;
import '../domain/artifact_html_links.dart';
/// The iframe's sandbox: no script, no same-origin access, no forms and no
/// navigation of the app, but a link may open a new tab that is itself
/// unsandboxed (an ordinary website, as if typed into the address bar).
const String artifactHtmlSandbox =
'allow-popups allow-popups-to-escape-sandbox';
/// [html] with every link's behaviour fixed before the frame shows it
/// ([artifactHtmlLinkAction]): a `#place` stays in the frame and scrolls
/// there, an http(s)/mailto address opens a new tab (no opener, no
/// referrer), and every other link loses its `href`, so it is inert text.
/// A document's own `<base>` is dropped: the frame is not at the artifact's
/// address, and a relative link must not reach the app's origin.
///
/// The browser's `DOMParser` builds an inert document: nothing in it runs or
/// loads while it is rewritten.
String artifactHtmlWithInertLinks(String html) {
final doc = web.DOMParser().parseFromString(html.toJS, 'text/html');
final bases = doc.querySelectorAll('base');
for (var i = bases.length - 1; i >= 0; i--) {
(bases.item(i) as web.Element?)?.remove();
}
final links = doc.querySelectorAll('a[href], area[href]');
for (var i = 0; i < links.length; i++) {
final link = links.item(i) as web.Element?;
if (link == null) continue;
final href = link.getAttribute('href') ?? '';
switch (artifactHtmlLinkAction(href)) {
case ArtifactHtmlLinkAction.inDocument:
// A srcdoc document's base is the app page, so a bare `#place` would
// navigate the frame to the app; the document's own address keeps it
// a same-document jump.
final hash = href.indexOf('#');
final place = hash < 0 ? '' : href.substring(hash);
link.setAttribute('href', 'about:srcdoc$place');
link.setAttribute('target', '_self');
case ArtifactHtmlLinkAction.external:
link.setAttribute('target', '_blank');
link.setAttribute('rel', 'noopener noreferrer');
case ArtifactHtmlLinkAction.blocked:
link.removeAttribute('href');
link.removeAttribute('target');
}
}
final doctype = doc.doctype == null ? '' : '<!DOCTYPE html>';
return '$doctype${doc.documentElement?.outerHTML ?? ''}';
}
@@ -1,5 +1,6 @@
import 'package:flutter/widgets.dart';
import 'artifact_fullscreen.dart';
import 'artifact_html_view_native.dart'
if (dart.library.js_interop) 'artifact_html_view_web.dart'
as impl;
@@ -9,6 +10,12 @@ import 'artifact_html_view_native.dart'
typedef ArtifactHtmlBuilder =
Widget Function(BuildContext context, String html);
/// Whether the full-screen view offers zoom buttons for HTML: on the web the
/// sandboxed frame cannot be pinched (the page's viewport is not scalable and
/// a frame's document has no pinch zoom of its own), so the buttons scale the
/// live frame. The mobile web views pinch natively and offer none.
const bool artifactHtmlZoomButtons = impl.inertHtmlZoomButtons;
/// Renders a recorded HTML document inertly: on the web in an iframe whose
/// empty `sandbox` blocks scripts, forms, popups and navigation; on Android and
/// iOS in a web view with JavaScript disabled and navigation refused. The
@@ -18,13 +25,22 @@ typedef ArtifactHtmlBuilder =
/// the web the iframe is a platform view that takes every click over its
/// area, even where a bar is drawn on top, which left the viewer's actions
/// and pager unclickable.
///
/// Inside an [ArtifactFullscreenScope] the document takes every gesture over
/// it and follows the scope's zoom; inline it shares them with the viewer's
/// page swipe.
Widget defaultArtifactHtml(BuildContext context, String html) {
final padding = MediaQuery.paddingOf(context);
final fullscreen = ArtifactFullscreenScope.maybeOf(context);
return Padding(
padding: EdgeInsets.only(top: padding.top, bottom: padding.bottom),
child: SizedBox.expand(
key: const Key('artifact-html-preview'),
child: impl.InertHtmlView(html: html),
child: impl.InertHtmlView(
html: html,
interactive: fullscreen != null,
zoom: fullscreen?.htmlZoom,
),
),
);
}
@@ -1,36 +1,105 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/widgets.dart';
import 'package:webview_flutter/webview_flutter.dart';
import '../domain/artifact_html_links.dart';
import 'artifact_external_open.dart';
/// The mobile web views pinch-zoom natively (Android's built-in zoom, which
/// the plugin turns on with its display controls hidden; WKWebView's own), so
/// the full-screen view needs no zoom buttons.
const bool inertHtmlZoomButtons = false;
/// A web view with JavaScript disabled that refuses every navigation after the
/// recorded document itself loads.
///
/// A platform view only receives the pointers of gestures that its
/// [WebViewWidget.gestureRecognizers] win in Flutter's gesture arena. With
/// none it got a drag only after every other recognizer gave up, which the
/// viewer's page swipe never does before the finger lifts: the document never
/// scrolled or pinched. Inline it claims vertical drags (scrolling the
/// document) and leaves horizontal ones to the page swipe; [interactive] (the
/// full-screen view) claims every pointer at once, so scroll, pan and pinch
/// all reach the document.
class InertHtmlView extends StatefulWidget {
const InertHtmlView({super.key, required this.html});
const InertHtmlView({
super.key,
required this.html,
this.interactive = false,
this.zoom,
this.openExternal = openArtifactExternally,
});
final String html;
/// Opens a link's http(s)/mailto address outside the artifact.
final ArtifactExternalOpen openExternal;
final bool interactive;
/// Unused: the mobile web views pinch instead ([inertHtmlZoomButtons]).
final ValueListenable<double>? zoom;
@override
State<InertHtmlView> createState() => _InertHtmlViewState();
}
final _inlineGestures = <Factory<OneSequenceGestureRecognizer>>{
Factory<VerticalDragGestureRecognizer>(VerticalDragGestureRecognizer.new),
};
final _allGestures = <Factory<OneSequenceGestureRecognizer>>{
Factory<EagerGestureRecognizer>(EagerGestureRecognizer.new),
};
class _InertHtmlViewState extends State<InertHtmlView> {
/// Until the recorded document finishes loading, the view may load it (an
/// `about:` or `data:` document); afterwards only links are followed.
bool _loaded = false;
late final WebViewController _controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.disabled)
..enableZoom(true)
..setNavigationDelegate(
NavigationDelegate(
onNavigationRequest: (request) =>
request.url.startsWith('about:') || request.url.startsWith('data:')
? NavigationDecision.navigate
: NavigationDecision.prevent,
onPageFinished: (_) => _loaded = true,
onNavigationRequest: _decide,
),
)
..loadHtmlString(widget.html);
@override
void didUpdateWidget(covariant InertHtmlView oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.html != widget.html) _controller.loadHtmlString(widget.html);
/// Links: a place in the document scrolls there; an http(s)/mailto address
/// opens in the system's handler, never inside this view; anything else
/// (scripts, files, other schemes, relative paths) stays inert.
NavigationDecision _decide(NavigationRequest request) {
if (!_loaded &&
(request.url.startsWith('about:') || request.url.startsWith('data:'))) {
return NavigationDecision.navigate;
}
switch (artifactHtmlLinkAction(request.url)) {
case ArtifactHtmlLinkAction.inDocument:
return NavigationDecision.navigate;
case ArtifactHtmlLinkAction.external:
final uri = Uri.tryParse(request.url.trim());
if (uri != null) widget.openExternal(uri);
return NavigationDecision.prevent;
case ArtifactHtmlLinkAction.blocked:
return NavigationDecision.prevent;
}
}
@override
Widget build(BuildContext context) => WebViewWidget(controller: _controller);
void didUpdateWidget(covariant InertHtmlView oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.html != widget.html) {
_loaded = false;
_controller.loadHtmlString(widget.html);
}
}
@override
Widget build(BuildContext context) => WebViewWidget(
controller: _controller,
gestureRecognizers: widget.interactive ? _allGestures : _inlineGestures,
);
}
@@ -1,28 +1,109 @@
import 'dart:js_interop';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:web/web.dart' as web;
/// A sandboxed iframe: the empty `sandbox` attribute applies every
/// restriction, so the document cannot run script or leave the frame.
class InertHtmlView extends StatelessWidget {
const InertHtmlView({super.key, required this.html});
import 'artifact_html_links_web.dart';
/// The frame's document cannot be pinched: a pinch over it is a page zoom,
/// which the app's viewport (`user-scalable=no`) refuses, and the sandbox
/// keeps the document out of the app's reach. The full-screen view zooms
/// the live frame with buttons instead.
const bool inertHtmlZoomButtons = true;
/// A sandboxed iframe ([artifactHtmlSandbox]): the document cannot run
/// script, reach the app's origin, submit forms or navigate the app. Its
/// links are fixed first ([artifactHtmlWithInertLinks]): a `#place` scrolls
/// the frame, a web or mail address opens a new tab, anything else is inert.
///
/// The frame scrolls its document itself, by wheel, trackpad and touch.
/// [zoom] scales the live frame as a browser's page zoom would: the frame is
/// laid out 1/zoom as wide and tall and scaled back to fill its box, so the
/// document reflows and still scrolls inside it. In the app, Chrome returns
/// the frame's document to its top when a zoom step resizes it.
///
/// While another route covers this one (a menu, a sheet) the frame stops
/// taking pointers: a platform view takes every click over its area, even
/// where Flutter draws that menu on top.
class InertHtmlView extends StatefulWidget {
const InertHtmlView({
super.key,
required this.html,
this.interactive = false,
this.zoom,
});
final String html;
/// The full-screen view's frame. Pointers already reach the frame directly
/// on the web, inline as in full screen.
final bool interactive;
final ValueListenable<double>? zoom;
@override
Widget build(BuildContext context) => HtmlElementView.fromTagName(
key: ValueKey<int>(html.hashCode),
tagName: 'iframe',
onElementCreated: (element) {
final frame = element as web.HTMLIFrameElement;
frame.setAttribute('sandbox', '');
frame.srcdoc = html.toJS;
frame.style
..border = 'none'
..width = '100%'
..height = '100%'
..backgroundColor = 'white';
},
);
State<InertHtmlView> createState() => _InertHtmlViewState();
}
class _InertHtmlViewState extends State<InertHtmlView> {
web.HTMLIFrameElement? _frame;
bool _covered = false;
@override
void initState() {
super.initState();
widget.zoom?.addListener(_style);
}
@override
void didUpdateWidget(covariant InertHtmlView oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.zoom != widget.zoom) {
oldWidget.zoom?.removeListener(_style);
widget.zoom?.addListener(_style);
_style();
}
}
@override
void dispose() {
widget.zoom?.removeListener(_style);
super.dispose();
}
void _style() {
final frame = _frame;
if (frame == null) return;
final zoom = widget.zoom?.value ?? 1;
final size = '${100 / zoom}%';
frame.style
..width = size
..height = size
..transformOrigin = '0 0'
..transform = zoom == 1 ? '' : 'scale($zoom)'
..pointerEvents = _covered ? 'none' : '';
}
@override
Widget build(BuildContext context) {
final covered = !(ModalRoute.of(context)?.isCurrent ?? true);
if (covered != _covered) {
_covered = covered;
_style();
}
return HtmlElementView.fromTagName(
key: ValueKey<int>(widget.html.hashCode),
tagName: 'iframe',
onElementCreated: (element) {
final frame = element as web.HTMLIFrameElement;
frame.setAttribute('sandbox', artifactHtmlSandbox);
frame.srcdoc = artifactHtmlWithInertLinks(widget.html).toJS;
frame.style
..border = 'none'
..backgroundColor = 'white';
_frame = frame;
_style();
},
);
}
}
@@ -0,0 +1,158 @@
import 'dart:math' as math;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../ui/adaptive/glass_bar.dart';
import '../../../ui/adaptive/host.dart';
import '../../../ui/navigation/chevron_pager.dart';
import '../actions/artifact_file_actions.dart';
import '../actions/artifact_file_bridge.dart';
import '../domain/artifact_url.dart';
import '../domain/gallery_navigation.dart';
import 'artifact_viewer_fetch.dart';
const artifactPreviousKey = ValueKey<String>('artifact-previous');
const artifactNextKey = ValueKey<String>('artifact-next');
const artifactFullscreenKey = ValueKey<String>('artifact-fullscreen');
/// The viewer's bottom controls: file transfer, Open, Copy link, Full screen
/// and the pager, in one glass bar above the bottom safe area.
///
/// The inline viewer and the full-screen view draw this same bar; in full
/// screen the Full screen control reads Exit full screen and leaves it.
class ArtifactViewerBar extends StatelessWidget {
const ArtifactViewerBar({
super.key,
required this.page,
required this.resolved,
required this.index,
required this.count,
required this.fetch,
required this.fileBridge,
required this.onOpen,
required this.onCopy,
required this.onPrevious,
required this.onNext,
this.fullscreen = false,
this.onFullscreen,
});
final PagedArtifact page;
final ResolvedArtifactUrl? resolved;
final int index;
final int count;
final ArtifactFetch fetch;
final ArtifactFileBridge fileBridge;
final VoidCallback onOpen;
final VoidCallback onCopy;
final VoidCallback onPrevious;
final VoidCallback onNext;
/// Whether this bar is the full-screen view's.
final bool fullscreen;
/// Enters (inline) or leaves (in full screen) the full-screen view; absent,
/// the control is not drawn.
final VoidCallback? onFullscreen;
@override
Widget build(BuildContext context) {
final cupertino = usesCupertinoHost(context);
final actions = _actions(resolved, onFullscreen);
final pagerMin = count > 1
? 2 * GlassBarMetrics.controlExtent(cupertino)
: 0.0;
final pager = ChevronPager(
index: index,
count: count,
previousLabel: 'Previous artifact',
nextLabel: 'Next artifact',
previousKey: artifactPreviousKey,
nextKey: artifactNextKey,
onPrevious: pagedIndex(index, -1, count) != index ? onPrevious : null,
onNext: pagedIndex(index, 1, count) != index ? onNext : null,
);
return SafeArea(
// The SafeArea already lifts the bar by the inherited bottom inset (a
// shell bar below the pane); adding that clearance again floated the
// bar far above it.
top: false,
minimum: const EdgeInsets.all(8),
// Without a pager the bar is as wide as its actions, so its last
// control keeps the same gap from the bar's end as the first.
child: Align(
alignment: AlignmentDirectional.centerStart,
heightFactor: 1,
child: GlassControlBar(
cupertino: cupertino,
child: LayoutBuilder(
builder: (context, constraints) => Row(
key: const Key('artifact-viewer-actions'),
mainAxisSize: count > 1 ? MainAxisSize.max : MainAxisSize.min,
children: [
// On a narrow window or at large text the actions scroll
// sideways, so the chevrons beside them stay whole.
ConstrainedBox(
constraints: BoxConstraints(
maxWidth: math.max(0, constraints.maxWidth - pagerMin),
),
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
mainAxisSize: MainAxisSize.min,
children: actions,
),
),
),
if (count > 1) Expanded(child: pager),
],
),
),
),
),
);
}
List<Widget> _actions(ResolvedArtifactUrl? resolved, VoidCallback? toggle) =>
[
if (resolved != null && !resolved.external)
ArtifactFileActions(
key: ValueKey<String>(
'viewer-files-${page.group.stageKey}\u0000'
'${page.group.visit}\u0000${page.artifact.url}',
),
artifact: page.artifact,
resolved: resolved,
fetch: fetch,
bridge: fileBridge,
compact: true,
),
if (resolved != null) ...[
ArtifactIconAction(
key: const Key('artifact-open'),
label: 'Open in browser',
icon: Icons.open_in_new,
cupertinoIcon: CupertinoIcons.arrow_up_right_square,
onPressed: onOpen,
),
ArtifactIconAction(
key: const Key('artifact-copy-link'),
label: 'Copy link',
icon: Icons.link,
cupertinoIcon: CupertinoIcons.link,
onPressed: onCopy,
),
],
if (toggle != null)
ArtifactIconAction(
key: artifactFullscreenKey,
label: fullscreen ? 'Exit full screen' : 'Full screen',
icon: fullscreen ? Icons.fullscreen_exit : Icons.fullscreen,
cupertinoIcon: fullscreen
? CupertinoIcons.fullscreen_exit
: CupertinoIcons.fullscreen,
onPressed: toggle,
),
];
}
@@ -1,5 +1,3 @@
import 'dart:async';
import '../../../ui/navigation/chevron_pager.dart';
import 'dart:typed_data';
import 'package:flutter/cupertino.dart';
@@ -7,15 +5,19 @@ import 'package:flutter/material.dart';
import '../../../ui/adaptive/host.dart';
import '../../../ui/adaptive/floating_bar_layout.dart';
import '../../../ui/adaptive/glass_bar.dart';
import '../actions/artifact_file_actions.dart';
import '../actions/artifact_file_bridge.dart';
import '../domain/artifact_preview.dart';
import '../domain/artifact_url.dart';
import '../domain/gallery_navigation.dart';
import 'artifact_html_view.dart';
import 'artifact_viewer_bar.dart';
import 'artifact_viewer_content.dart';
import 'artifact_viewer_fetch.dart';
export 'artifact_controls_overlay.dart';
export 'artifact_viewer_bar.dart'
show artifactFullscreenKey, artifactNextKey, artifactPreviousKey;
/// One viewer page: the recorded facts, position, live status, preview surface,
/// file transfer actions, external actions and paging controls.
///
@@ -42,8 +44,20 @@ class ArtifactViewerBody extends StatelessWidget {
this.error,
this.status,
this.onRetry,
this.htmlBuilder = defaultArtifactHtml,
this.fullscreen = false,
this.onFullscreen,
});
/// The inert HTML renderer; tests inject a plain stand-in.
final ArtifactHtmlBuilder htmlBuilder;
/// Whether this body is the full-screen view's ([ArtifactViewerBar]).
final bool fullscreen;
/// Enters or leaves the full-screen view.
final VoidCallback? onFullscreen;
/// Loads the current page's preview again after a failure.
final VoidCallback? onRetry;
@@ -79,6 +93,7 @@ class ArtifactViewerBody extends StatelessWidget {
bytes: bytes,
error: error,
onRetry: onRetry,
htmlBuilder: htmlBuilder,
),
),
],
@@ -116,233 +131,25 @@ class ArtifactViewerBody extends StatelessWidget {
// no empty glass bar, and no bottom clearance reserved for one.
if (resolved == null && count <= 1) return swipe;
return FloatingBarOverlay(
// The SafeArea already lifts the bar by the inherited bottom inset (a
// shell bar below the pane); adding that clearance again floated the
// bar far above it.
bar: SafeArea(
top: false,
minimum: const EdgeInsets.all(8),
// Without a pager the bar is as wide as its actions, so its last
// control keeps the same gap from the bar's end as the first.
child: Align(
alignment: AlignmentDirectional.centerStart,
heightFactor: 1,
child: GlassControlBar(
cupertino: usesCupertinoHost(context),
child: Row(
key: const Key('artifact-viewer-actions'),
mainAxisSize: count > 1 ? MainAxisSize.max : MainAxisSize.min,
children: [
if (resolved != null && !resolved.external)
ArtifactFileActions(
key: ValueKey<String>(
'viewer-files-${page.group.stageKey}\u0000'
'${page.group.visit}\u0000${page.artifact.url}',
),
artifact: page.artifact,
resolved: resolved,
fetch: fetch,
bridge: fileBridge,
compact: true,
),
if (resolved != null)
_Actions(resolved: resolved, onOpen: onOpen, onCopy: onCopy),
if (count > 1)
Expanded(
child: _Pager(
index: index,
count: count,
onPrevious: onPrevious,
onNext: onNext,
),
),
],
),
),
),
bar: ArtifactViewerBar(
page: page,
resolved: resolved,
index: index,
count: count,
fetch: fetch,
fileBridge: fileBridge,
onOpen: onOpen,
onCopy: onCopy,
onPrevious: onPrevious,
onNext: onNext,
fullscreen: fullscreen,
onFullscreen: onFullscreen,
),
child: swipe,
);
}
}
/// Open, Copy and the file actions float over the top of the content: shown
/// when the page opens and whenever the content is touched, hidden again after
/// a few seconds without use, so nothing needs scrolling to reach them.
class ArtifactControlsOverlay extends StatefulWidget {
const ArtifactControlsOverlay({
super.key,
required this.controls,
required this.child,
this.hideAfter = const Duration(seconds: 3),
});
final List<Widget> controls;
final Widget child;
final Duration hideAfter;
@override
State<ArtifactControlsOverlay> createState() =>
_ArtifactControlsOverlayState();
}
class _ArtifactControlsOverlayState extends State<ArtifactControlsOverlay> {
bool _shown = true;
Timer? _hide;
@override
void initState() {
super.initState();
_schedule();
}
@override
void dispose() {
_hide?.cancel();
super.dispose();
}
void _schedule() {
_hide?.cancel();
_hide = Timer(widget.hideAfter, () {
if (mounted) setState(() => _shown = false);
});
}
void _show() {
if (!_shown) setState(() => _shown = true);
_schedule();
}
@override
Widget build(BuildContext context) {
final cupertino = usesCupertinoHost(context);
final surface = cupertino
? CupertinoColors.systemBackground
.resolveFrom(context)
.withValues(alpha: 0.92)
: Theme.of(
context,
).colorScheme.surfaceContainerHigh.withValues(alpha: 0.94);
return Stack(
children: [
// Any touch on the content brings the controls back.
Positioned.fill(
child: Listener(
behavior: HitTestBehavior.translucent,
onPointerDown: (_) => _show(),
child: widget.child,
),
),
Positioned(
left: 8,
right: 8,
top: 8,
child: IgnorePointer(
ignoring: !_shown,
child: AnimatedOpacity(
key: const Key('artifact-viewer-controls'),
opacity: _shown ? 1 : 0,
duration: const Duration(milliseconds: 200),
child: Listener(
// Using a control keeps the bar up.
onPointerDown: (_) => _schedule(),
child: DecoratedBox(
decoration: BoxDecoration(
color: surface,
borderRadius: BorderRadius.circular(12),
boxShadow: const [
BoxShadow(color: Color(0x22000000), blurRadius: 6),
],
),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 4,
vertical: 2,
),
child: Wrap(
alignment: WrapAlignment.center,
crossAxisAlignment: WrapCrossAlignment.center,
children: widget.controls,
),
),
),
),
),
),
),
],
);
}
}
class _Actions extends StatelessWidget {
const _Actions({
required this.resolved,
required this.onOpen,
required this.onCopy,
});
final ResolvedArtifactUrl resolved;
final VoidCallback onOpen;
final VoidCallback onCopy;
@override
Widget build(BuildContext context) => Row(
mainAxisSize: MainAxisSize.min,
children: [
ArtifactIconAction(
key: const Key('artifact-open'),
label: 'Open',
icon: Icons.open_in_new,
cupertinoIcon: CupertinoIcons.arrow_up_right_square,
onPressed: onOpen,
),
ArtifactIconAction(
key: const Key('artifact-copy-link'),
label: 'Copy link',
icon: Icons.link,
cupertinoIcon: CupertinoIcons.link,
onPressed: onCopy,
),
],
);
}
const artifactPreviousKey = ValueKey<String>('artifact-previous');
const artifactNextKey = ValueKey<String>('artifact-next');
class _Pager extends StatelessWidget {
const _Pager({
required this.index,
required this.count,
required this.onPrevious,
required this.onNext,
});
final int index;
final int count;
final VoidCallback onPrevious;
final VoidCallback onNext;
@override
Widget build(BuildContext context) {
if (count <= 1) return const SizedBox.shrink();
final hasPrevious = pagedIndex(index, -1, count) != index;
final hasNext = pagedIndex(index, 1, count) != index;
return ChevronPager(
index: index,
count: count,
previousLabel: 'Previous artifact',
nextLabel: 'Next artifact',
previousKey: artifactPreviousKey,
nextKey: artifactNextKey,
onPrevious: hasPrevious ? onPrevious : null,
onNext: hasNext ? onNext : null,
);
}
}
/// The viewer's top edge: where this artifact sits in the list, and an
/// information button for its recorded facts.
class ArtifactViewerInfoAction extends StatelessWidget {
@@ -1,13 +1,9 @@
import '../../../ui/adaptive/segmented_bar.dart';
import '../../../ui/theme/app_theme.dart';
import '../../../ui/input/arrow_key_pager.dart';
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../../../ui/adaptive/host.dart';
import '../../../ui/input/arrow_key_pager.dart';
import 'artifact_selection_facts.dart';
import '../actions/artifact_file_bridge.dart';
import '../domain/artifact_groups.dart';
@@ -16,9 +12,13 @@ import '../domain/artifact_preview_failure.dart';
import '../domain/artifact_url.dart';
import '../domain/gallery_navigation.dart';
import 'artifact_external_open.dart';
import 'artifact_fullscreen.dart';
import 'artifact_fullscreen_host.dart';
import 'artifact_html_view.dart';
import 'artifact_viewer_body.dart';
import 'artifact_viewer_content.dart';
import 'artifact_viewer_fetch.dart';
import 'artifact_viewer_scaffold.dart';
import 'artifact_visit_list.dart';
/// The full-screen viewer's title, matching the web client's wording.
@@ -47,6 +47,7 @@ class ArtifactViewerPage extends StatefulWidget {
this.markdownBuilder = defaultArtifactMarkdown,
this.openExternal = openArtifactExternally,
this.fileBridge = const ArtifactFileBridge(),
this.htmlBuilder = defaultArtifactHtml,
});
final List<ArtifactGroup> groups;
@@ -64,6 +65,9 @@ class ArtifactViewerPage extends StatefulWidget {
final ArtifactExternalOpen openExternal;
final ArtifactFileBridge fileBridge;
/// The inert HTML renderer; tests inject a plain stand-in.
final ArtifactHtmlBuilder htmlBuilder;
@override
State<ArtifactViewerPage> createState() => _ArtifactViewerPageState();
}
@@ -75,7 +79,8 @@ class _FetchedPreview {
final Uint8List? bytes;
}
class _ArtifactViewerPageState extends State<ArtifactViewerPage> {
class _ArtifactViewerPageState extends State<ArtifactViewerPage>
with ArtifactFullscreenHost<ArtifactViewerPage> {
late final List<PagedArtifact> _pages = pagedArtifacts(
widget.groups,
includeExternalDocuments: widget.includeExternalDocuments,
@@ -117,47 +122,31 @@ class _ArtifactViewerPageState extends State<ArtifactViewerPage> {
void _beginLoad({bool initial = false}) {
final generation = ++_generation;
final plan = _plan;
// The first load runs in initState, before there is anything to redraw.
void update(VoidCallback change) => initial ? change() : setState(change);
if (plan == null || !plan.canFetch) {
void clear() {
update(() {
_loading = false;
_error = null;
_fetched = null;
}
if (initial) {
clear();
} else {
setState(clear);
}
});
return;
}
final key = plan.resolved!.url;
final cached = _cache[key];
if (cached != null) {
void apply() {
update(() {
_loading = false;
_error = null;
_fetched = cached;
}
if (initial) {
apply();
} else {
setState(apply);
}
});
return;
}
void start() {
update(() {
_loading = true;
_error = null;
_fetched = null;
}
if (initial) {
start();
} else {
setState(start);
}
});
widget
.fetch(plan.resolved!.uri)
.then(
@@ -226,7 +215,13 @@ class _ArtifactViewerPageState extends State<ArtifactViewerPage> {
}
@override
Widget build(BuildContext context) {
Widget build(BuildContext context) => buildFrame(context, fullscreen: false);
/// The viewer's scaffold, inline or as the full-screen view: the same top
/// bar, preview and bottom actions; full screen adds HTML zoom on the web
/// and gives the preview every gesture over it.
@override
Widget buildFrame(BuildContext context, {required bool fullscreen}) {
final page = _page;
final plan = _plan;
final title = page == null
@@ -242,6 +237,11 @@ class _ArtifactViewerPageState extends State<ArtifactViewerPage> {
final info = page == null || plan == null
? const <Widget>[]
: <Widget>[
if (fullscreen &&
artifactHtmlZoomButtons &&
plan.kind == ArtifactPreviewKind.html &&
_fetched?.text != null)
...artifactHtmlZoomActions(htmlZoom),
ArtifactViewerInfoAction(
facts: ArtifactSelectionFacts(
group: page.group,
@@ -251,7 +251,7 @@ class _ArtifactViewerPageState extends State<ArtifactViewerPage> {
),
),
];
Widget body = page == null || plan == null
final body = page == null || plan == null
? const Center(child: Text('No previewable artifacts recorded.'))
: ArtifactViewerBody(
page: page,
@@ -272,36 +272,26 @@ class _ArtifactViewerPageState extends State<ArtifactViewerPage> {
onRetry: _beginLoad,
onOpen: plan.resolved == null ? () {} : () => _open(plan.resolved!),
onCopy: plan.resolved == null ? () {} : () => _copy(plan.resolved!),
htmlBuilder: widget.htmlBuilder,
fullscreen: fullscreen,
onFullscreen: fullscreen
? () => Navigator.of(context).maybePop()
: enterFullscreen,
);
// Left/right arrows walk the same list as the Previous/Next buttons.
body = ArrowKeyPager(
// Left/right arrows walk the same list as the Previous/Next buttons,
// wherever focus is on the page, its top bar's controls included.
return ArrowKeyPager(
onPrevious: () => _step(-1),
onNext: () => _step(1),
// Escape closes the viewer, like the web's overlay.
// Escape closes the viewer (or leaves full screen), like the web's
// overlay.
onEscape: () => Navigator.of(context).maybePop(),
child: body,
);
if (usesCupertinoHost(context)) {
return CupertinoPageScaffold(
backgroundColor: AppColors.groupedBackground,
navigationBar: SegmentedBar(
cupertino: true,
title: title,
subtitle: subtitle,
actions: info,
),
child: body,
);
}
return Scaffold(
extendBodyBehindAppBar: true,
appBar: SegmentedBar(
cupertino: false,
child: ArtifactViewerScaffold(
title: title,
subtitle: subtitle,
actions: info,
body: body,
),
body: body,
);
}
}
@@ -0,0 +1,42 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../ui/adaptive/host.dart';
import '../../../ui/adaptive/segmented_bar.dart';
import '../../../ui/theme/app_theme.dart';
/// The viewer's page structure on either host: its [SegmentedBar] (the
/// platform's Back, or Close on the full-screen route) over [body].
class ArtifactViewerScaffold extends StatelessWidget {
const ArtifactViewerScaffold({
super.key,
required this.title,
required this.subtitle,
required this.actions,
required this.body,
});
final String title;
final String? subtitle;
final List<Widget> actions;
final Widget body;
@override
Widget build(BuildContext context) {
final cupertino = usesCupertinoHost(context);
final bar = SegmentedBar(
cupertino: cupertino,
title: title,
subtitle: subtitle,
actions: actions,
);
if (cupertino) {
return CupertinoPageScaffold(
backgroundColor: AppColors.groupedBackground,
navigationBar: bar,
child: body,
);
}
return Scaffold(extendBodyBehindAppBar: true, appBar: bar, body: body);
}
}
+92
View File
@@ -0,0 +1,92 @@
# Artifact viewer: full screen, HTML scroll/pan/zoom, links
## What changed
- The viewer's bottom bar has a **Full screen** control (Material
`Icons.fullscreen` / `CupertinoIcons.fullscreen`). It pushes a full-screen
dialog route on the root navigator, covering the shell's tab bar and list
pane. The route draws the same viewer frame (`ArtifactFullscreenHost`
mixin + `buildFrame`), so the same top bar, preview and bottom bar
(`ArtifactViewerBar`, shared, not duplicated) appear, and paging, loading
and status follow the page. Exit: the bar's **Exit full screen**, the
route's Close (Material X / Cupertino Cancel), Escape, system back.
- Full screen renders the bytes the viewer already fetched with the app's
session. It never calls `openArtifactExternally`/`launchUrl` for the
artifact. The iPad report (HTML opening in Chrome, then a sign-in prompt)
came from the viewer's existing **Open** action
(`openArtifactExternally` → `launchUrl(externalApplication)`): it is now
labelled **Open in browser** in the viewer bar and the gallery dialog.
- Images, text, tables and video keep their own behaviour in full screen.
## Why HTML would not scroll or pinch
- **Android/iOS app:** a platform view only gets pointers for gestures its
`gestureRecognizers` win in Flutter's gesture arena. `WebViewWidget` had
none, and the viewer's page-swipe `GestureDetector` (horizontal drag)
stays in the arena until the finger lifts, so the web view got a drag only
after release: no live scroll, no pinch. Now inline the web view claims
vertical drags (the document scrolls, a horizontal swipe still pages), and
full screen claims every pointer (`EagerGestureRecognizer`): scroll, pan
and pinch all reach the web view. Zoom is on (`enableZoom(true)`; the
Android plugin already sets built-in zoom with hidden controls and ignores
viewport meta, so no viewport injection is needed).
- **Web:** wheel, trackpad and touch scrolling inside the iframe already
worked (captured below). Pinch cannot work: a pinch over a frame is a page
zoom, which the Flutter page's viewport (`user-scalable=no`) refuses, and
the sandbox (no `allow-same-origin`) keeps the document out of the app's
reach. Full screen therefore adds **Zoom out / Actual size / Zoom in**
(50%–400%) that scale the live frame like a browser page zoom (frame laid
out at 1/zoom and scaled back), so the document reflows and keeps
scrolling. Not a screenshot.
## Links
- Decision: `lib/features/artifacts/domain/artifact_html_links.dart`
(`artifactHtmlLinkAction`): `#place` / `about:blank|srcdoc#place` stay in
the document; `http(s)` with a host and `mailto` open outside; everything
else (`javascript:`, `file:`, `data:`, `tel:`, `intent:`, relative paths,
`//host`) is inert.
- Web: the iframe sandbox is `allow-popups allow-popups-to-escape-sandbox`
(still no scripts, no same-origin, no forms, no top navigation). Before
showing, the document is rewritten through the browser's inert
`DOMParser`: `<base>` removed, external links get `target=_blank
rel="noopener noreferrer"`, in-document links become `about:srcdoc#place`
with `target=_self` (a bare `#place` resolves against the app page in a
srcdoc frame and blanked the frame), blocked links lose `href`.
- Native: the `NavigationDelegate` allows the initial `about:`/`data:` load
until the page finishes, then applies the same decision; external links go
to the system handler, nothing else navigates the view.
- Relative links cannot resolve (the document is loaded from a string with
no address) and stay inert. A link to the Pipelines server itself opens in
the browser without the app's session.
- Applies inline and in full screen.
## Checks
- `flutter analyze`: no issues. `tool/memwatch/full.sh test`: 2778 pass;
`test/features/artifacts`: 240 pass (new `ui/artifact_fullscreen_test.dart`,
`domain/artifact_html_links_test.dart`).
- Real Chrome (capture kit, `--params html=1`, fixture
`tool/capture/fixtures/release-dashboard.html`), captures in
`/tmp/afs/final/`:
- `desktop/` 1280x900 Material: inline, full screen, wheel scroll, zoom
125%, Details sheet over the frame, Escape back to the inline viewer.
- `phone/` 560x1000 touch: full screen, swipe up scrolls, swipe left pans
the wide table; a pinch does not zoom (web limit above).
- `ipad/` 820x1180 Cupertino touch: full screen, swipe scrolls, Exit full
screen returns.
- `links/`: "Jump to section 5" scrolls the frame; example.com opens a new
tab (`05-tabs-after-external.json`) with the frame unchanged; script and
relative links are plain text and open nothing.
- The capture kit gained `--touch true`, `swipe=`, `pinch=` and `tabs=`.
## Not verified / limits
- Android and iOS behaviour (web view gestures, pinch, link hand-off) is
**unverified**: no device or simulator was used.
- Web: no pinch zoom (buttons instead). A zoom step returns the document to
its top: Chrome resets the frame's scroll when the app resizes it (a plain
page keeps it; wrapping the frame in a box did not help).
- Web: Escape while focus is inside the frame goes to the frame; use the
close button or click outside first.
- Narrow windows: the bar's actions scroll sideways so the pager stays whole.
@@ -244,7 +244,7 @@ void main() {
expect(find.byType(AlertDialog), findsOneWidget);
expect(find.text('Download'), findsOneWidget);
expect(find.text('Share'), findsOneWidget);
expect(find.text('Open'), findsOneWidget);
expect(find.text('Open in browser'), findsOneWidget);
expect(find.text('Copy link'), findsOneWidget);
await tester.tap(find.text('Download'));
@@ -0,0 +1,42 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/artifacts/domain/artifact_html_links.dart';
void main() {
const inDocument = ArtifactHtmlLinkAction.inDocument;
const external = ArtifactHtmlLinkAction.external;
const blocked = ArtifactHtmlLinkAction.blocked;
final cases = <String, ArtifactHtmlLinkAction>{
'#section-2': inDocument,
' #top ': inDocument,
'about:blank': inDocument,
'about:blank#section-2': inDocument,
'about:srcdoc#top': inDocument,
'https://example.com/report': external,
'HTTP://Example.com': external,
'http://127.0.0.1:8080/x': external,
'mailto:ops@example.com': external,
'javascript:alert(1)': blocked,
' JavaScript:alert(1)': blocked,
'file:///etc/passwd': blocked,
'data:text/html,<b>x</b>': blocked,
'about:config': blocked,
'intent://scan#Intent;end': blocked,
'tel:+15551234': blocked,
'report.html': blocked,
'../7/2/other.html': blocked,
'/api/runs': blocked,
'//example.com/x': blocked,
'http:no-host': blocked,
'': blocked,
};
for (final entry in cases.entries) {
test(
'${entry.key.isEmpty ? '(empty)' : entry.key} -> ${entry.value.name}',
() {
expect(artifactHtmlLinkAction(entry.key), entry.value);
},
);
}
}
@@ -0,0 +1,180 @@
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/artifacts/domain/artifact_groups.dart';
import 'package:pipelines_app/features/artifacts/domain/artifact_record.dart';
import 'package:pipelines_app/features/artifacts/ui/artifact_fullscreen.dart';
import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_body.dart';
import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_fetch.dart';
import 'package:pipelines_app/features/artifacts/ui/artifact_viewer_page.dart';
import 'package:pipelines_app/features/artifacts/ui/artifact_visit_list.dart';
import 'artifact_visit_list_fixture.dart';
const _endpoint = 'ws://127.0.0.1:8081/connect';
const _shellBar = Key('shell-tab-bar');
final ArtifactRecord _report = recordedArtifact(
'report.html',
url: '/artifacts/1/1/report.html',
contentType: 'text/html',
);
final ArtifactRecord _log = recordedArtifact(
'build.log',
url: '/artifacts/1/1/build.log',
contentType: 'text/plain',
);
final ArtifactGroup _group = visitGroup(artifacts: [_report, _log]);
/// Each rendered HTML document, with whether it rendered in full screen.
typedef _Rendered = List<(String, bool)>;
Future<({_Rendered rendered, List<Uri> opened})> _pump(
WidgetTester tester, {
bool cupertino = false,
}) async {
final rendered = <(String, bool)>[];
final opened = <Uri>[];
final page = ArtifactViewerPage(
groups: [_group],
selectedIdentity: artifactVisitIdentity(_group, _report),
configuredEndpoint: _endpoint,
fetch: (uri) async => ArtifactFetchResult(
status: 200,
bytes: utf8.encode(uri.path.endsWith('.html') ? '<h1>Hi</h1>' : 'ok'),
),
openExternal: (uri) async {
opened.add(uri);
return true;
},
htmlBuilder: (context, html) {
rendered.add((html, ArtifactFullscreenScope.maybeOf(context) != null));
return Text('html:$html');
},
);
// A stand-in for the shell: a nested navigator holding the viewer above a
// tab bar the full-screen view must cover.
final shell = Column(
children: [
Expanded(
child: Navigator(
onGenerateRoute: (_) => cupertino
? CupertinoPageRoute<void>(builder: (_) => page)
: MaterialPageRoute<void>(builder: (_) => page),
),
),
const SizedBox(key: _shellBar, height: 56, child: Text('Tabs')),
],
);
await tester.pumpWidget(
cupertino ? CupertinoApp(home: shell) : MaterialApp(home: shell),
);
await tester.pumpAndSettle();
return (rendered: rendered, opened: opened);
}
Future<void> _enter(WidgetTester tester) async {
await tester.tap(find.byKey(artifactFullscreenKey));
await tester.pumpAndSettle();
}
/// A bar control by its accessible name: the Material button's tooltip, the
/// Cupertino button's semantics label.
Finder _named(bool cupertino, String name) =>
cupertino ? find.bySemanticsLabel(name) : find.byTooltip(name);
void main() {
for (final cupertino in [false, true]) {
final host = cupertino ? 'Cupertino' : 'Material';
testWidgets('$host: the bottom actions offer Full screen', (tester) async {
final result = await _pump(tester, cupertino: cupertino);
final bar = find.byKey(const Key('artifact-viewer-actions'));
expect(
find.descendant(of: bar, matching: find.byKey(artifactFullscreenKey)),
findsOneWidget,
);
expect(_named(cupertino, 'Full screen'), findsOneWidget);
expect(result.rendered.single, ('<h1>Hi</h1>', false));
});
testWidgets('$host: full screen covers the shell, keeps the actions and '
'renders the fetched document in the app', (tester) async {
final result = await _pump(tester, cupertino: cupertino);
await _enter(tester);
expect(find.byKey(_shellBar), findsNothing);
// The same document, fetched once with the app's session, now in the
// full-screen scope; nothing was handed to the system browser.
expect(result.rendered.last, ('<h1>Hi</h1>', true));
expect(result.opened, isEmpty);
final bar = find.byKey(const Key('artifact-viewer-actions'));
expect(bar, findsOneWidget);
for (final key in const [
Key('artifact-open'),
Key('artifact-copy-link'),
artifactPreviousKey,
artifactNextKey,
]) {
expect(find.descendant(of: bar, matching: find.byKey(key)), findsOne);
}
expect(_named(cupertino, 'Exit full screen'), findsOneWidget);
expect(_named(cupertino, 'Open in browser'), findsOneWidget);
});
testWidgets('$host: Exit full screen returns to the viewer', (
tester,
) async {
await _pump(tester, cupertino: cupertino);
await _enter(tester);
await tester.tap(find.byKey(artifactFullscreenKey));
await tester.pumpAndSettle();
expect(find.byKey(_shellBar), findsOneWidget);
expect(_named(cupertino, 'Full screen'), findsOneWidget);
});
}
testWidgets('Escape and the system back leave full screen', (tester) async {
await _pump(tester);
await _enter(tester);
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.pumpAndSettle();
expect(find.byKey(_shellBar), findsOneWidget);
expect(find.text('html:<h1>Hi</h1>'), findsOneWidget);
await _enter(tester);
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
expect(find.byKey(_shellBar), findsOneWidget);
// Back left full screen only, not the viewer as well.
expect(find.text('html:<h1>Hi</h1>'), findsOneWidget);
});
testWidgets('paging in full screen follows the viewer, any kind', (
tester,
) async {
await _pump(tester);
await _enter(tester);
await tester.tap(find.byKey(artifactNextKey));
await tester.pumpAndSettle();
expect(find.text('build.log'), findsOneWidget);
expect(find.byKey(const Key('artifact-text-preview')), findsOneWidget);
expect(find.byKey(_shellBar), findsNothing);
await tester.tap(find.byKey(artifactFullscreenKey));
await tester.pumpAndSettle();
expect(find.text('build.log'), findsOneWidget);
expect(find.byKey(_shellBar), findsOneWidget);
});
test('zoom steps walk the list and hold at both ends', () {
expect(artifactHtmlZoomStep(1, 1), 1.1);
expect(artifactHtmlZoomStep(1, -1), 0.9);
expect(artifactHtmlZoomStep(4, 1), 4);
expect(artifactHtmlZoomStep(0.5, -1), 0.5);
expect(artifactHtmlZoomStep(1.3, -1), 1.25);
});
}
@@ -188,7 +188,7 @@ void main() {
expect(find.byType(ArtifactViewerPage), findsNothing);
expect(_dialog(platform, 'session.html'), findsOneWidget);
expect(_dialog(platform, 'text/plain · 512\u00a0B'), findsOneWidget);
await tester.tap(_dialog(platform, 'Open'));
await tester.tap(_dialog(platform, 'Open in browser'));
await tester.pumpAndSettle();
expect(find.byType(ArtifactViewerPage), findsNothing);
expect(opened, [
@@ -214,7 +214,7 @@ void main() {
);
await tester.tap(find.text('result.zip'));
await tester.pumpAndSettle();
await tester.tap(_inDialog(_Platform.material, 'Open'));
await tester.tap(_inDialog(_Platform.material, 'Open in browser'));
await tester.pumpAndSettle();
expect(opened, [
@@ -243,7 +243,7 @@ void main() {
),
findsOneWidget,
);
expect(_inDialog(_Platform.material, 'Open'), findsNothing);
expect(_inDialog(_Platform.material, 'Open in browser'), findsNothing);
expect(_inDialog(_Platform.material, 'Copy link'), findsNothing);
});
@@ -270,7 +270,7 @@ void main() {
_inDialog(_Platform.material, 'No artifact URL recorded.'),
findsOneWidget,
);
expect(_inDialog(_Platform.material, 'Open'), findsNothing);
expect(_inDialog(_Platform.material, 'Open in browser'), findsNothing);
});
testWidgets('Copy link copies the resolved url, separate from Open', (
@@ -504,7 +504,7 @@ void main() {
}
expect(orderOf('Close'), lessThan(orderOf('Copy link')));
expect(orderOf('Copy link'), lessThan(orderOf('Open')));
expect(orderOf('Copy link'), lessThan(orderOf('Open in browser')));
},
);
}
+7
View File
@@ -53,6 +53,9 @@ In JSON, a step can also set `wait` (ms), `timeout`, `name`, `ms` and `frames`.
| `labels=name` | write the visible semantics labels, keys and rects to JSON |
| `film=<target>` | screencast a click for `--film-ms` (900) and write the frames plus a contact sheet |
| `wait=ms` | sleep |
| `swipe=<target>>dx,dy` | one-finger touch swipe (needs `--touch true`, which emulates a touch screen once semantics is on) |
| `pinch=<target>>scale` | two-finger touch pinch, spreading (>1) or closing (<1) (needs `--touch true`) |
| `tabs=name` | write the open tabs (`{url, type}`) to JSON, e.g. to see a link open a new tab |
| `eval=<js>` | evaluate an expression in the page and log/record its value |
| `expect=<js>` | fail the run (status 1) unless the expression is truthy; JSON `name` labels the check |
@@ -69,6 +72,10 @@ Targets:
When a target is missing, the step fails. The kit lists the visible labels,
saves `NN-FAILED.png` and exits with status 1.
An HTML artifact: `--params html=1` adds `release-dashboard.html` to run 7.
Its bytes are served from the build, so copy the fixture in after building:
`mkdir -p build/probe-app/artifacts/7/2 && cp tool/capture/fixtures/release-dashboard.html build/probe-app/artifacts/7/2/`.
## Recipes (run from `app/` after building `build/probe-app`)
Capture a page at phone and fold widths, in Material and in Cupertino. Use 560
+3
View File
@@ -91,6 +91,9 @@ export async function openBrowser({ cdp, chromePath = process.env.CHROME_PATH ??
listeners.set(method, [...(listeners.get(method) ?? []), fn]);
return () => listeners.set(method, listeners.get(method).filter((f) => f !== fn));
},
/** Every open page target (tabs a link opened included): `{ url, type }`. */
targets: async () => (await raw('Target.getTargets')).targetInfos
.filter((t) => t.type === 'page').map((t) => ({ url: t.url, type: t.type })),
newPage: async () => {
const page = await raw('Target.createTarget', { url: 'about:blank' });
const { sessionId: sid } = await raw('Target.attachToTarget', { targetId: page.targetId, flatten: true });
+23 -2
View File
@@ -20,10 +20,12 @@ options: --script <steps.json> --size WxH (880x1000) --platform material|cuperti
--theme light|dark --q <search> --wait <ms after actions, 1000>
--film-ms <ms, 900> --cdp http://127.0.0.1:<port> --probe true|false
--params <query, e.g. chats=2&rich=1, added to every open>
--touch true (emulate a touch screen: mobile metrics, touch input)
steps: open=<route>[@WxH] wait=<ms> shot=<name> click=<target> waitfor=<target>
type=<text> press=<Enter|Escape|Tab|...> scroll=<dy> size=WxH
film=<target> labels=<name> eval=<js> expect=<js> hover=<target>
film=<target> labels=<name> tabs=<name> eval=<js> expect=<js> hover=<target>
drag=<target>><dx>,<dy> (JSON: drag, dx, dy, steps, gap, hold)
swipe=<target>><dx>,<dy> pinch=<target>><scale> (with --touch true)
target: <label> | label:<label> | key:<semantics identifier> | x,y`;
const opts = { wait: '1000', 'film-ms': '900' };
@@ -86,7 +88,7 @@ let exitCode = 0;
try {
const base = opts.build ? (server = await serveBuild(opts.build)).origin : opts.url;
browser = await openBrowser({ cdp: opts.cdp });
page = flutterPage(browser, log);
page = flutterPage(browser, log, { touch: opts.touch === 'true' });
for (const step of steps) {
const [verb] = Object.keys(step);
const value = step[verb];
@@ -137,6 +139,24 @@ try {
await delay(step.wait ?? settle);
break;
}
case 'swipe': {
const [target, by] = String(value).split('>');
const [bx, by2] = (by ?? '').split(',').map(Number);
const moved = await page.swipe(target, {
dx: Number(step.dx ?? bx ?? 0) || 0, dy: Number(step.dy ?? by2 ?? 0) || 0,
steps: Number(step.steps ?? 12), gap: Number(step.gap ?? 16),
});
log(` swiped ${Math.round(moved.from.x)},${Math.round(moved.from.y)} -> ${Math.round(moved.to.x)},${Math.round(moved.to.y)}`);
await delay(step.wait ?? settle);
break;
}
case 'pinch': {
const [target, scale] = String(value).split('>');
const at = await page.pinch(target, Number(step.scale ?? scale ?? 2));
log(` pinched at ${Math.round(at.x)},${Math.round(at.y)}`);
await delay(step.wait ?? settle);
break;
}
case 'eval': {
const result = await page.evaluate(String(value));
log(` = ${JSON.stringify(result)}`);
@@ -150,6 +170,7 @@ try {
log(` ok ${step.name ?? ''}`);
break;
}
case 'tabs': await save(file(value || 'tabs', 'json'), JSON.stringify(await browser.targets(), null, 2)); break;
case 'labels': await save(file(value || 'labels', 'json'), JSON.stringify(await page.labels(), null, 2)); break;
case 'film': {
const name = step.name ?? `film-${describe(parseTarget(value)).replace(/\W+/g, '-').replace(/^-|-$/g, '')}`;
@@ -0,0 +1,32 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Release dashboard</title>
<style>
body { font-family: sans-serif; margin: 16px; color: #222; }
h1 { color: #0b5394; }
.wide { width: 1800px; border-collapse: collapse; }
.wide td { border: 1px solid #999; padding: 6px 10px; white-space: nowrap; }
.section { padding: 24px; margin: 16px 0; background: #eef3fb; border-left: 6px solid #0b5394; }
</style>
</head>
<body>
<h1>Release dashboard (top)</h1>
<p id="links">Links: <a href="#section-5">Jump to section 5</a> · <a href="https://example.com/">example.com</a> · <a href="javascript:alert(1)">script link</a> · <a href="other.html">relative link</a></p>
<p>Fixture document for the artifact viewer captures: taller and wider than the screen, no viewport meta.</p>
<table class="wide">
<tr><td>Column A</td><td>Column B</td><td>Column C</td><td>Column D</td><td>Column E</td><td>Column F</td><td>Column G</td><td>Column H</td><td>Column I</td><td>Column J</td><td>Right edge column</td></tr>
<tr><td>a1</td><td>b1</td><td>c1</td><td>d1</td><td>e1</td><td>f1</td><td>g1</td><td>h1</td><td>i1</td><td>j1</td><td>RIGHT EDGE</td></tr>
</table>
<div class="section" id="section-1"><h2>Section 1 of 8</h2><p>Build green across all targets.</p></div>
<div class="section" id="section-2"><h2>Section 2 of 8</h2><p>Integration suites passed.</p></div>
<div class="section" id="section-3"><h2>Section 3 of 8</h2><p>Release notes drafted.</p></div>
<div class="section" id="section-4"><h2>Section 4 of 8</h2><p>Migration checked on staging.</p></div>
<div class="section" id="section-5"><h2>Section 5 of 8</h2><p>Rollback plan recorded.</p></div>
<div class="section" id="section-6"><h2>Section 6 of 8</h2><p>Owners notified.</p></div>
<div class="section" id="section-7"><h2>Section 7 of 8</h2><p>Artifacts signed.</p></div>
<div class="section" id="section-8"><h2>Section 8 of 8</h2><p>Bottom of the document reached.</p></div>
<h1>Release dashboard (bottom)</h1>
</body>
</html>
+9 -1
View File
@@ -2,6 +2,7 @@
// enable semantics, find targets by semantics label / identifier / point,
// click, type, screenshot and film.
import { setTimeout as delay } from 'node:timers/promises';
import { enableTouch, pinch, swipe } from './touch.mjs';
/** Parses `label:Text`, `key:id`, `xy:10,20`, `10,20` or bare `Text` (a label). */
export function parseTarget(target) {
@@ -45,7 +46,7 @@ const LABELS = `(() => [...document.querySelectorAll('flt-semantics, [aria-label
rect: [r.left, r.top, r.width, r.height].map(Math.round) }; })
.filter((n) => (n.label || n.key) && n.rect[2] > 0 && n.rect[3] > 0))()`;
export function flutterPage(browser, log) {
export function flutterPage(browser, log, { touch = false } = {}) {
const { send } = browser;
let viewport = { width: 880, height: 1000 };
const errors = [];
@@ -110,6 +111,9 @@ export function flutterPage(browser, log) {
await until(() => evaluate("Boolean(document.querySelector('flt-semantics-placeholder'))"), 'Flutter to start', 30000);
await evaluate("document.querySelector('flt-semantics-placeholder')?.click()");
await until(() => evaluate("document.querySelectorAll('flt-semantics').length > 0"), 'the semantics tree', 15000);
// Touch starts once semantics is on: the placeholder's click does not
// turn semantics on under touch emulation.
if (touch) await enableTouch(send, viewport);
await delay(800);
},
async click(target, timeout) {
@@ -181,6 +185,10 @@ export function flutterPage(browser, log) {
if (!hold) await mouse('mouseReleased', at.x + dx, at.y + dy, { buttons: 0, clickCount: 1 });
return { from: at, to: { x: at.x + dx, y: at.y + dy } };
},
/** One-finger touch swipe from [target] by dx/dy (needs `--touch true`). */
swipe: async (target, moves) => swipe(send, await locate(target), moves),
/** Two-finger touch pinch at [target] by [scale] (needs `--touch true`). */
pinch: async (target, scale) => pinch(send, await locate(target), scale),
evaluate,
/** Screencasts [ms] after clicking [target]; frames are `{ data, ms }` relative to the click. */
async film(target, ms) {
+33
View File
@@ -0,0 +1,33 @@
// Touch input over one DevTools page session, for phone/tablet review:
// a touch-screen emulation switch, one-finger swipes and two-finger pinches.
// The events are trusted browser input, so they reach whatever the page puts
// under the finger (a Flutter canvas or an iframe platform view alike).
import { setTimeout as delay } from 'node:timers/promises';
/** Emulates a touch screen (mobile metrics, 5 touch points). */
export async function enableTouch(send, { width, height }) {
await send('Emulation.setDeviceMetricsOverride', { width, height, deviceScaleFactor: 1, mobile: true });
await send('Emulation.setTouchEmulationEnabled', { enabled: true, maxTouchPoints: 5 });
}
/** One finger down at [at], moved by dx/dy in [steps] moves [gap] ms apart, lifted. */
export async function swipe(send, at, { dx = 0, dy = 0, steps = 12, gap = 16 } = {}) {
const touch = (type, x, y) => send('Input.dispatchTouchEvent', {
type, touchPoints: type === 'touchEnd' ? [] : [{ x, y, id: 1 }],
});
await touch('touchStart', at.x, at.y);
for (let i = 1; i <= steps; i++) {
await delay(gap);
await touch('touchMove', at.x + (dx * i) / steps, at.y + (dy * i) / steps);
}
await touch('touchEnd');
return { from: at, to: { x: at.x + dx, y: at.y + dy } };
}
/** Two fingers spread (scale > 1) or closed (< 1) around [at]. */
export async function pinch(send, at, scale) {
await send('Input.synthesizePinchGesture', {
x: at.x, y: at.y, scaleFactor: scale, relativeSpeed: 300, gestureSourceType: 'touch',
});
return at;
}
@@ -72,6 +72,13 @@ JsonMap probeRunDetail() => {
'size_bytes': 12,
'url': '/artifacts/7/2/release-report.txt',
},
if (_htmlArtifact)
{
'name': 'release-dashboard.html',
'content_type': 'text/html',
'size_bytes': 4096,
'url': '/artifacts/7/2/release-dashboard.html',
},
],
'file_changes': _probeChanges([
'lib/release/train.dart',
@@ -123,6 +130,10 @@ JsonMap _probeChanges(List<String> paths) => {
int _fillerRunCount = 0;
int _fillerStepCount = 0;
/// `?html=1` adds `release-dashboard.html` to run 7's build visit; the capture
/// serves its bytes from the build directory (see notes/artifact-fullscreen.md).
bool _htmlArtifact = false;
List<JsonMap> _fillerSteps() => [
for (var i = 0; i < _fillerStepCount; i++)
{
@@ -583,6 +594,7 @@ class _FixtureSink implements WebSocketSink {
/// `?route=/runs/7/stage/build%3Fvisit%3D1` opens that app location;
/// `?runs=200` / `?steps=60` append filler runs / run-7 steps for scroll
/// performance captures;
/// `?html=1` adds an HTML artifact to run 7 (`release-dashboard.html`);
/// `?q=release` types that query into Search once its field is shown;
/// `?platform=cupertino` renders the Cupertino host (`material`, the
/// default, renders Material as browsers do);
@@ -606,6 +618,7 @@ void main() {
configurePerfFixture(params);
_fillerRunCount = int.tryParse(params['runs'] ?? '') ?? 0;
_fillerStepCount = int.tryParse(params['steps'] ?? '') ?? 0;
_htmlArtifact = params['html'] == '1';
final cupertino = params['platform'] == 'cupertino';
final route = params['route'];
final opensRoute = route != null && route.startsWith('/');