diff --git a/app/lib/features/artifacts/domain/artifact_html_links.dart b/app/lib/features/artifacts/domain/artifact_html_links.dart new file mode 100644 index 00000000..7f2f7414 --- /dev/null +++ b/app/lib/features/artifacts/domain/artifact_html_links.dart @@ -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; +} diff --git a/app/lib/features/artifacts/ui/artifact_controls_overlay.dart b/app/lib/features/artifacts/ui/artifact_controls_overlay.dart new file mode 100644 index 00000000..bc58f4c4 --- /dev/null +++ b/app/lib/features/artifacts/ui/artifact_controls_overlay.dart @@ -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 controls; + final Widget child; + final Duration hideAfter; + + @override + State createState() => + _ArtifactControlsOverlayState(); +} + +class _ArtifactControlsOverlayState extends State { + 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, + ), + ), + ), + ), + ), + ), + ), + ], + ); + } +} diff --git a/app/lib/features/artifacts/ui/artifact_fullscreen.dart b/app/lib/features/artifacts/ui/artifact_fullscreen.dart new file mode 100644 index 00000000..dd31aa15 --- /dev/null +++ b/app/lib/features/artifacts/ui/artifact_fullscreen.dart @@ -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 htmlZoom; + + static ArtifactFullscreenScope? maybeOf(BuildContext context) => + context.dependOnInheritedWidgetOfExactType(); + + @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 artifactFullscreenRoute({ + required bool cupertino, + required WidgetBuilder builder, +}) => cupertino + ? CupertinoPageRoute(fullscreenDialog: true, builder: builder) + : MaterialPageRoute(fullscreenDialog: true, builder: builder); + +/// The zoom factors Zoom in and Zoom out step through. +const List 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('artifact-zoom-out'); +const artifactZoomResetKey = ValueKey('artifact-zoom-reset'); +const artifactZoomInKey = ValueKey('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 artifactHtmlZoomActions(ValueNotifier 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, + ), + ]; +} diff --git a/app/lib/features/artifacts/ui/artifact_fullscreen_host.dart b/app/lib/features/artifacts/ui/artifact_fullscreen_host.dart new file mode 100644 index 00000000..5593e8a7 --- /dev/null +++ b/app/lib/features/artifacts/ui/artifact_fullscreen_host.dart @@ -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 on State { + final ValueNotifier _revision = ValueNotifier(0); + + /// The full-screen view's HTML zoom; each full-screen visit starts at 1. + final ValueNotifier htmlZoom = ValueNotifier(1); + Route? _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 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; + } +} diff --git a/app/lib/features/artifacts/ui/artifact_gallery_page.dart b/app/lib/features/artifacts/ui/artifact_gallery_page.dart index a4e47d06..12ede23b 100644 --- a/app/lib/features/artifacts/ui/artifact_gallery_page.dart +++ b/app/lib/features/artifacts/ui/artifact_gallery_page.dart @@ -370,7 +370,7 @@ class _ArtifactGalleryPageState extends State { 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 { if (resolved != null) TextButton( onPressed: () => _open(resolved, dialogContext), - child: const Text('Open'), + child: const Text('Open in browser'), ), ], ), diff --git a/app/lib/features/artifacts/ui/artifact_html_links_web.dart b/app/lib/features/artifacts/ui/artifact_html_links_web.dart new file mode 100644 index 00000000..a7716d57 --- /dev/null +++ b/app/lib/features/artifacts/ui/artifact_html_links_web.dart @@ -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 `` 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 ? '' : ''; + return '$doctype${doc.documentElement?.outerHTML ?? ''}'; +} diff --git a/app/lib/features/artifacts/ui/artifact_html_view.dart b/app/lib/features/artifacts/ui/artifact_html_view.dart index b4d2abb7..0210379f 100644 --- a/app/lib/features/artifacts/ui/artifact_html_view.dart +++ b/app/lib/features/artifacts/ui/artifact_html_view.dart @@ -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, + ), ), ); } diff --git a/app/lib/features/artifacts/ui/artifact_html_view_native.dart b/app/lib/features/artifacts/ui/artifact_html_view_native.dart index 40085c84..23275528 100644 --- a/app/lib/features/artifacts/ui/artifact_html_view_native.dart +++ b/app/lib/features/artifacts/ui/artifact_html_view_native.dart @@ -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? zoom; + @override State createState() => _InertHtmlViewState(); } +final _inlineGestures = >{ + Factory(VerticalDragGestureRecognizer.new), +}; + +final _allGestures = >{ + Factory(EagerGestureRecognizer.new), +}; + class _InertHtmlViewState extends State { + /// 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, + ); } diff --git a/app/lib/features/artifacts/ui/artifact_html_view_web.dart b/app/lib/features/artifacts/ui/artifact_html_view_web.dart index ae8056b1..1cceabc2 100644 --- a/app/lib/features/artifacts/ui/artifact_html_view_web.dart +++ b/app/lib/features/artifacts/ui/artifact_html_view_web.dart @@ -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? zoom; + @override - Widget build(BuildContext context) => HtmlElementView.fromTagName( - key: ValueKey(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 createState() => _InertHtmlViewState(); +} + +class _InertHtmlViewState extends State { + 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(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(); + }, + ); + } } diff --git a/app/lib/features/artifacts/ui/artifact_viewer_bar.dart b/app/lib/features/artifacts/ui/artifact_viewer_bar.dart new file mode 100644 index 00000000..57ffeefb --- /dev/null +++ b/app/lib/features/artifacts/ui/artifact_viewer_bar.dart @@ -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('artifact-previous'); +const artifactNextKey = ValueKey('artifact-next'); +const artifactFullscreenKey = ValueKey('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 _actions(ResolvedArtifactUrl? resolved, VoidCallback? toggle) => + [ + if (resolved != null && !resolved.external) + ArtifactFileActions( + key: ValueKey( + '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, + ), + ]; +} diff --git a/app/lib/features/artifacts/ui/artifact_viewer_body.dart b/app/lib/features/artifacts/ui/artifact_viewer_body.dart index ed4b5420..48f6b9da 100644 --- a/app/lib/features/artifacts/ui/artifact_viewer_body.dart +++ b/app/lib/features/artifacts/ui/artifact_viewer_body.dart @@ -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( - '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 controls; - final Widget child; - final Duration hideAfter; - - @override - State createState() => - _ArtifactControlsOverlayState(); -} - -class _ArtifactControlsOverlayState extends State { - 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('artifact-previous'); -const artifactNextKey = ValueKey('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 { diff --git a/app/lib/features/artifacts/ui/artifact_viewer_page.dart b/app/lib/features/artifacts/ui/artifact_viewer_page.dart index 88d6c4d8..519224ef 100644 --- a/app/lib/features/artifacts/ui/artifact_viewer_page.dart +++ b/app/lib/features/artifacts/ui/artifact_viewer_page.dart @@ -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 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 createState() => _ArtifactViewerPageState(); } @@ -75,7 +79,8 @@ class _FetchedPreview { final Uint8List? bytes; } -class _ArtifactViewerPageState extends State { +class _ArtifactViewerPageState extends State + with ArtifactFullscreenHost { late final List _pages = pagedArtifacts( widget.groups, includeExternalDocuments: widget.includeExternalDocuments, @@ -117,47 +122,31 @@ class _ArtifactViewerPageState extends State { 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 { } @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 { final info = page == null || plan == null ? const [] : [ + 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 { ), ), ]; - 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 { 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, ); } } diff --git a/app/lib/features/artifacts/ui/artifact_viewer_scaffold.dart b/app/lib/features/artifacts/ui/artifact_viewer_scaffold.dart new file mode 100644 index 00000000..e1a17a3b --- /dev/null +++ b/app/lib/features/artifacts/ui/artifact_viewer_scaffold.dart @@ -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 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); + } +} diff --git a/app/notes/artifact-fullscreen.md b/app/notes/artifact-fullscreen.md new file mode 100644 index 00000000..8ff62a65 --- /dev/null +++ b/app/notes/artifact-fullscreen.md @@ -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`: `` 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. diff --git a/app/test/features/artifacts/actions/artifact_file_actions_test.dart b/app/test/features/artifacts/actions/artifact_file_actions_test.dart index 83360640..7532a6a3 100644 --- a/app/test/features/artifacts/actions/artifact_file_actions_test.dart +++ b/app/test/features/artifacts/actions/artifact_file_actions_test.dart @@ -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')); diff --git a/app/test/features/artifacts/domain/artifact_html_links_test.dart b/app/test/features/artifacts/domain/artifact_html_links_test.dart new file mode 100644 index 00000000..0c5b7e34 --- /dev/null +++ b/app/test/features/artifacts/domain/artifact_html_links_test.dart @@ -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 = { + '#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,x': 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); + }, + ); + } +} diff --git a/app/test/features/artifacts/ui/artifact_fullscreen_test.dart b/app/test/features/artifacts/ui/artifact_fullscreen_test.dart new file mode 100644 index 00000000..2a06125c --- /dev/null +++ b/app/test/features/artifacts/ui/artifact_fullscreen_test.dart @@ -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 opened})> _pump( + WidgetTester tester, { + bool cupertino = false, +}) async { + final rendered = <(String, bool)>[]; + final opened = []; + final page = ArtifactViewerPage( + groups: [_group], + selectedIdentity: artifactVisitIdentity(_group, _report), + configuredEndpoint: _endpoint, + fetch: (uri) async => ArtifactFetchResult( + status: 200, + bytes: utf8.encode(uri.path.endsWith('.html') ? '

Hi

' : '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(builder: (_) => page) + : MaterialPageRoute(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 _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, ('

Hi

', 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, ('

Hi

', 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:

Hi

'), 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:

Hi

'), 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); + }); +} diff --git a/app/test/features/artifacts/ui/artifact_gallery_documents_test.dart b/app/test/features/artifacts/ui/artifact_gallery_documents_test.dart index f8e229cc..4be4546c 100644 --- a/app/test/features/artifacts/ui/artifact_gallery_documents_test.dart +++ b/app/test/features/artifacts/ui/artifact_gallery_documents_test.dart @@ -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, [ diff --git a/app/test/features/artifacts/ui/artifact_gallery_page_test.dart b/app/test/features/artifacts/ui/artifact_gallery_page_test.dart index 4cdb0030..1415326b 100644 --- a/app/test/features/artifacts/ui/artifact_gallery_page_test.dart +++ b/app/test/features/artifacts/ui/artifact_gallery_page_test.dart @@ -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'))); }, ); } diff --git a/app/tool/capture/README.md b/app/tool/capture/README.md index 6fe79be0..ce1fd3c8 100644 --- a/app/tool/capture/README.md +++ b/app/tool/capture/README.md @@ -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=` | screencast a click for `--film-ms` (900) and write the frames plus a contact sheet | | `wait=ms` | sleep | +| `swipe=>dx,dy` | one-finger touch swipe (needs `--touch true`, which emulates a touch screen once semantics is on) | +| `pinch=>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=` | evaluate an expression in the page and log/record its value | | `expect=` | 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 diff --git a/app/tool/capture/browser.mjs b/app/tool/capture/browser.mjs index fdfd6ea3..3a5531ee 100644 --- a/app/tool/capture/browser.mjs +++ b/app/tool/capture/browser.mjs @@ -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 }); diff --git a/app/tool/capture/capture.mjs b/app/tool/capture/capture.mjs index d0e24026..3d1b2d85 100644 --- a/app/tool/capture/capture.mjs +++ b/app/tool/capture/capture.mjs @@ -20,10 +20,12 @@ options: --script --size WxH (880x1000) --platform material|cuperti --theme light|dark --q --wait --film-ms --cdp http://127.0.0.1: --probe true|false --params + --touch true (emulate a touch screen: mobile metrics, touch input) steps: open=[@WxH] wait= shot= click= waitfor= type= press= scroll= size=WxH - film= labels= eval= expect= hover= + film= labels= tabs= eval= expect= hover= drag=>, (JSON: drag, dx, dy, steps, gap, hold) + swipe=>, pinch=> (with --touch true) target: