Merge: artifact full screen; HTML scrolls, pans and zooms; links open safely
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user