App: dark-aware HTML views, run cards, changed/missing file notes, one card width, dense diff rows

show_html documents that declare dark support follow the app theme; others
stay light. Tool description and skill docs say so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-02 19:54:58 +00:00
parent d204ce3187
commit f02ff56dfe
27 changed files with 1086 additions and 61 deletions
@@ -0,0 +1,63 @@
/// How a recorded HTML document follows the app's light or dark theme.
///
/// A document opts into dark mode by saying it supports it: a
/// `prefers-color-scheme` media query, a `color-scheme` declaration (CSS or
/// `<meta>`) or the `light-dark()` colour function. Such a document is shown
/// in the app's scheme; any other document keeps the light scheme on a white
/// page, because its own colours were chosen for that and may be unreadable
/// on a dark canvas.
///
/// The frame's browser does not reliably pass the app's preference into a
/// sandboxed document (and a phone's web view knows only the system's), so
/// the scheme is written into the document itself: a leading
/// `<meta name="color-scheme">` naming only the chosen scheme (the browser
/// paints its default canvas and text, form controls and `light-dark()` in
/// it), and each `prefers-color-scheme` query is replaced by one that is
/// always true or always false for that scheme.
library;
final RegExp _darkAware = RegExp(
r'prefers-color-scheme|color-scheme|light-dark\(',
caseSensitive: false,
);
final RegExp _schemeQuery = RegExp(
r'\(\s*prefers-color-scheme\s*:\s*(light|dark)\s*\)',
caseSensitive: false,
);
final RegExp _headOpen = RegExp(r'<head(\s[^>]*)?>', caseSensitive: false);
/// A media feature that always holds, and one that never does (a negative
/// length makes the query invalid, which evaluates to false).
const String _always = '(min-width: 0px)';
const String _never = '(max-width: -1px)';
/// Whether [html] declares that it styles itself for dark mode.
bool artifactHtmlSupportsDark(String html) => _darkAware.hasMatch(html);
/// The colour scheme [html] is shown in: `dark` only when the app is dark
/// ([dark]) and the document supports it, else `light`.
String artifactHtmlScheme(String html, {required bool dark}) =>
dark && artifactHtmlSupportsDark(html) ? 'dark' : 'light';
/// [html] fixed to its [artifactHtmlScheme] (see the library comment).
String artifactHtmlForScheme(String html, {required bool dark}) {
final scheme = artifactHtmlScheme(html, dark: dark);
final queried = html.replaceAllMapped(
_schemeQuery,
(match) => match[1]!.toLowerCase() == scheme ? _always : _never,
);
final meta = '<meta name="color-scheme" content="$scheme">';
final head = _headOpen.firstMatch(queried);
if (head != null) {
return queried.replaceRange(head.end, head.end, meta);
}
// Before everything else, but after a doctype: content ahead of the
// doctype would put the document in quirks mode.
final doctype = _doctype.firstMatch(queried);
final at = doctype?.end ?? 0;
return queried.replaceRange(at, at, meta);
}
final RegExp _doctype = RegExp(r'^\s*<!doctype[^>]*>', caseSensitive: false);
@@ -1,5 +1,6 @@
import 'package:flutter/widgets.dart';
import 'package:flutter/material.dart';
import '../domain/artifact_html_theme.dart';
import 'artifact_fullscreen.dart';
import 'artifact_html_view_native.dart'
if (dart.library.js_interop) 'artifact_html_view_web.dart'
@@ -16,11 +17,19 @@ typedef ArtifactHtmlBuilder =
/// live frame. The mobile web views pinch natively and offer none.
const bool artifactHtmlZoomButtons = impl.inertHtmlZoomButtons;
/// Whether a recorded HTML document at [context] is shown in the dark scheme
/// (when it supports one): the app's own theme, not the system's.
bool artifactHtmlDark(BuildContext context) =>
Theme.of(context).brightness == Brightness.dark;
/// 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
/// document never runs code with the app's origin or credentials.
///
/// A document that supports dark mode follows the app's theme
/// ([artifactHtmlForScheme]); any other keeps a white page.
///
/// The preview stops at the floating bars instead of passing behind them: on
/// 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
@@ -40,6 +49,7 @@ Widget defaultArtifactHtml(BuildContext context, String html) {
html: html,
interactive: fullscreen != null,
zoom: fullscreen?.htmlZoom,
dark: artifactHtmlDark(context),
),
),
);
@@ -4,6 +4,7 @@ import 'package:flutter/widgets.dart';
import 'package:webview_flutter/webview_flutter.dart';
import '../domain/artifact_html_links.dart';
import '../domain/artifact_html_theme.dart';
import 'artifact_external_open.dart';
/// The mobile web views pinch-zoom natively (Android's built-in zoom, which
@@ -29,10 +30,15 @@ class InertHtmlView extends StatefulWidget {
this.interactive = false,
this.zoom,
this.openExternal = openArtifactExternally,
this.dark = false,
});
final String html;
/// Whether the app is in its dark theme: a document that supports dark mode
/// is loaded in it ([artifactHtmlForScheme]).
final bool dark;
/// Opens a link's http(s)/mailto address outside the artifact.
final ArtifactExternalOpen openExternal;
final bool interactive;
@@ -66,7 +72,9 @@ class _InertHtmlViewState extends State<InertHtmlView> {
onNavigationRequest: _decide,
),
)
..loadHtmlString(widget.html);
..loadHtmlString(_document);
String get _document => artifactHtmlForScheme(widget.html, dark: widget.dark);
/// 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
@@ -91,9 +99,9 @@ class _InertHtmlViewState extends State<InertHtmlView> {
@override
void didUpdateWidget(covariant InertHtmlView oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.html != widget.html) {
if (oldWidget.html != widget.html || oldWidget.dark != widget.dark) {
_loaded = false;
_controller.loadHtmlString(widget.html);
_controller.loadHtmlString(_document);
}
}
@@ -4,6 +4,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:web/web.dart' as web;
import '../domain/artifact_html_theme.dart';
import 'artifact_html_links_web.dart';
/// The frame's document cannot be pinched: a pinch over it is a page zoom,
@@ -23,6 +24,10 @@ const bool inertHtmlZoomButtons = true;
/// 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.
///
/// The document follows the app's theme ([dark]) when it supports dark mode
/// ([artifactHtmlForScheme]); the frame's own `color-scheme` matches, so the
/// browser paints the page's canvas in that scheme rather than always white.
///
/// 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.
@@ -32,10 +37,14 @@ class InertHtmlView extends StatefulWidget {
required this.html,
this.interactive = false,
this.zoom,
this.dark = false,
});
final String html;
/// Whether the app is in its dark theme.
final bool dark;
/// The full-screen view's frame. Pointers already reach the frame directly
/// on the web, inline as in full screen.
final bool interactive;
@@ -91,16 +100,20 @@ class _InertHtmlViewState extends State<InertHtmlView> {
_covered = covered;
_style();
}
final scheme = artifactHtmlScheme(widget.html, dark: widget.dark);
return HtmlElementView.fromTagName(
key: ValueKey<int>(widget.html.hashCode),
key: ValueKey<(int, String)>((widget.html.hashCode, scheme)),
tagName: 'iframe',
onElementCreated: (element) {
final frame = element as web.HTMLIFrameElement;
frame.setAttribute('sandbox', artifactHtmlSandbox);
frame.srcdoc = artifactHtmlWithInertLinks(widget.html).toJS;
frame.srcdoc = artifactHtmlWithInertLinks(
artifactHtmlForScheme(widget.html, dark: widget.dark),
).toJS;
frame.style
..border = 'none'
..backgroundColor = 'white';
..colorScheme = scheme
..backgroundColor = scheme == 'dark' ? 'Canvas' : 'white';
_frame = frame;
_style();
},