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, ), ]; }