From 5ed8c549d30d87024b3e8d90169e38fd31dc2356 Mon Sep 17 00:00:00 2001 From: Wouter van Wijk Date: Fri, 20 Nov 2026 12:07:21 +0100 Subject: [PATCH 1/2] Structure tab: draw the full-page screenshot at wireframe size The full-page background was drawn at the capture-time CSS page size times the wireframe scale, while the canvas is the measured layout times the scale. When the two heights disagree (on a phone the page grows during the scroll-through, and the new screenshot can arrive before the re-measured layout), the image was drawn much larger than the wireframe and clipped, so it looked zoomed and misaligned. Draw the background stretched over the whole canvas instead: the canvas is the page, so the screenshot and the blocks always share one coordinate space. Also decode the bitmap downscaled to the wireframe's max width (the wireframe never displays wider), so a full-resolution capture no longer wastes memory. The now-unused fullPageScreenshotCss is removed. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe --- lib/services/html_service.dart | 15 ----------- lib/widgets/structure_painter.dart | 42 ++++++++++++------------------ lib/widgets/structure_view.dart | 12 ++++----- 3 files changed, 22 insertions(+), 47 deletions(-) diff --git a/lib/services/html_service.dart b/lib/services/html_service.dart index db5e23d..13af2a4 100644 --- a/lib/services/html_service.dart +++ b/lib/services/html_service.dart @@ -4542,21 +4542,11 @@ Technical details: $message'''; Observed _fullPageShot = kNoScreenshot; - /// The page's CSS size at the moment the full-page screenshot was taken; - /// null when there is none. The Structure tab draws the image at this size - /// so it lines up with the wireframe even when the layout was measured at - /// a different page height. - ({double width, double height})? _fullPageShotCss; - /// Full-page screenshot of the live browser (Size and Structure tabs); /// Unavailable until the scroll-through capture produced one or when the /// platform gives none. Until then those tabs show the top-of-page shot. Observed get fullPageScreenshot => _fullPageShot; - /// CSS size of [fullPageScreenshot]'s page at capture time. - ({double width, double height})? get fullPageScreenshotCss => - _fullPageShotCss; - @visibleForTesting void debugSetFullPageScreenshot(Observed shot) { _fullPageShot = shot; @@ -4681,10 +4671,6 @@ Technical details: $message'''; if (stale() || bytes == null) return; _fullPageShot = Present(bytes.buffer.asUint8List(), source: DataSource.browser); - // The page's CSS size at capture time, so the Structure tab can draw - // the image at exactly that size (the layout may have been measured - // at a different moment and differ, e.g. after lazy content grew). - _fullPageShotCss = (width: iw, height: pageH); notifyListeners(); } catch (e) { debugPrint('Scroll-through screenshot unavailable: $e'); @@ -4984,7 +4970,6 @@ Technical details: $message'''; _cancelBrowserRefreshes(); final policy = _browserPoll; _fullPageShot = kNoScreenshot; - _fullPageShotCss = null; // Screenshot of the top of the page right away, without scrolling; the // Size and Structure tabs show it until the full-page capture is done. _captureLiveScreenshot(gen).ignore(); diff --git a/lib/widgets/structure_painter.dart b/lib/widgets/structure_painter.dart index 1549dc7..29fb99e 100644 --- a/lib/widgets/structure_painter.dart +++ b/lib/widgets/structure_painter.dart @@ -53,12 +53,13 @@ class StructurePalette { /// Draws the page's blocks in page proportions: [scale] logical px per CSS px. /// When [background] is set (a screenshot of the page), it is drawn opaque -/// behind the blocks — over the whole page when [backgroundFullPage], else -/// over the first screen only. A full-page background is drawn at -/// [backgroundCss] (the page's CSS size at capture time) when known, so the -/// image lines up with the blocks even when the layout was measured at a -/// different page height. The blocks themselves are drawn 75% transparent so -/// the screenshot shows through. +/// behind the blocks — stretched over the whole canvas when +/// [backgroundFullPage], else over the first screen only. The canvas is the +/// page at [model.layout]'s size, so a full-page background drawn at canvas +/// size always shares its coordinate space with the blocks, even when the +/// layout was measured at a different page height than the capture. The +/// blocks themselves are drawn 75% transparent so the screenshot shows +/// through. class StructurePainter extends CustomPainter { final StructureModel model; final double scale; @@ -66,7 +67,6 @@ class StructurePainter extends CustomPainter { final ColorScheme scheme; final ui.Image? background; final bool backgroundFullPage; - final ({double width, double height})? backgroundCss; StructurePainter({ required this.model, @@ -75,7 +75,6 @@ class StructurePainter extends CustomPainter { required this.scheme, this.background, this.backgroundFullPage = false, - this.backgroundCss, }); static const double minLabelWidth = 60; @@ -92,27 +91,19 @@ class StructurePainter extends CustomPainter { ..style = PaintingStyle.stroke ..color = scheme.outlineVariant); - // The screenshot is the background, fully opaque: the whole page when we - // have a full-page capture, else the first screen only. A full-page - // capture is drawn at the page's CSS size at capture time, so it stays - // aligned with the blocks (the layout may have been measured later, at a - // different page height). + // The screenshot is the background, fully opaque: stretched over the + // whole canvas for a full-page capture (the canvas is the page, so the + // image and the blocks share coordinates), else over the first screen + // only. if (background != null) { - final Rect dest; - if (backgroundFullPage && backgroundCss != null) { - dest = Rect.fromLTWH(0, 0, backgroundCss!.width * scale, - backgroundCss!.height * scale); - } else if (backgroundFullPage) { - dest = Rect.fromLTWH(0, 0, size.width, size.height); - } else { - dest = Rect.fromLTWH(0, 0, size.width, - math.min(model.layout.viewportHeight * scale, size.height)); - } + final bgHeight = backgroundFullPage + ? size.height + : math.min(model.layout.viewportHeight * scale, size.height); canvas.drawImageRect( background!, Rect.fromLTWH( 0, 0, background!.width.toDouble(), background!.height.toDouble()), - dest, + Rect.fromLTWH(0, 0, size.width, bgHeight), Paint()..filterQuality = FilterQuality.medium, ); } @@ -191,6 +182,5 @@ class StructurePainter extends CustomPainter { old.coloring != coloring || old.scheme != scheme || old.background != background || - old.backgroundFullPage != backgroundFullPage || - old.backgroundCss != backgroundCss; + old.backgroundFullPage != backgroundFullPage; } diff --git a/lib/widgets/structure_view.dart b/lib/widgets/structure_view.dart index 2a2d513..b73c981 100644 --- a/lib/widgets/structure_view.dart +++ b/lib/widgets/structure_view.dart @@ -41,9 +41,13 @@ class _StructureViewState extends State { } /// Decodes [bytes] into [_background]; a new screenshot replaces the old. + /// The bitmap is decoded downscaled to the wireframe's max width — the + /// wireframe never displays wider than [kMaxWireframeWidth], so a + /// full-resolution full-page shot would only waste memory. Future _loadBackground(Uint8List bytes) async { try { - final codec = await ui.instantiateImageCodec(bytes); + final codec = await ui.instantiateImageCodec(bytes, + targetWidth: kMaxWireframeWidth.round(), allowUpscaling: false); final frame = await codec.getNextFrame(); codec.dispose(); final image = frame.image; @@ -159,9 +163,6 @@ class _StructureViewState extends State { Widget _model(BuildContext context, StructureModel model) { final scheme = Theme.of(context).colorScheme; final palette = StructurePalette(scheme); - final fullCss = - context.select( - (s) => s.fullPageScreenshotCss); return SingleChildScrollView( physics: const AlwaysScrollableScrollPhysics(), padding: const EdgeInsets.all(16), @@ -236,8 +237,7 @@ class _StructureViewState extends State { coloring: _coloring, scheme: scheme, background: _background, - backgroundFullPage: _backgroundFullPage, - backgroundCss: _backgroundFullPage ? fullCss : null), + backgroundFullPage: _backgroundFullPage), ), ), ), From 1857bd8af9ca01db51c1a47dcb314a0a1d371931 Mon Sep 17 00:00:00 2001 From: Wouter van Wijk Date: Fri, 20 Nov 2026 13:07:32 +0100 Subject: [PATCH 2/2] Structure tab: match page-size measurement to the capture; atomic shot swap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The layout script measured only scrollingElement's scroll size while the full-page capture takes max(scrollingElement, body) — on pages where is the taller one the wireframe's page size disagreed with the capture's, so the screenshot and the blocks were drawn in different coordinate spaces. Take the max of both in the layout script too. Also swap the decoded background image and its full-page flag atomically, and drop a stale shot of the other kind immediately, so the old viewport shot is never stretched over the full canvas while the full-page shot decodes. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe --- lib/services/collectors/vsv_layout_js.dart | 7 +++++++ lib/widgets/structure_view.dart | 20 ++++++++++++++++---- test/collectors/vsv_layout_js_test.dart | 12 ++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/lib/services/collectors/vsv_layout_js.dart b/lib/services/collectors/vsv_layout_js.dart index 2174ca7..491f893 100644 --- a/lib/services/collectors/vsv_layout_js.dart +++ b/lib/services/collectors/vsv_layout_js.dart @@ -44,6 +44,13 @@ const String vsvLayoutJs = r''' var body = document.body; var se = document.scrollingElement || document.documentElement; var docW = se ? se.scrollWidth : 0, docH = se ? se.scrollHeight : 0; + // Some pages size beyond the scrolling element (or the reverse). + // Take the max of both, like the full-page screenshot capture does, so + // the wireframe's page size matches the capture's. + if (body) { + if (body.scrollWidth > docW) docW = body.scrollWidth; + if (body.scrollHeight > docH) docH = body.scrollHeight; + } if (!body || !docW || !docH) return JSON.stringify({v: 1, empty: true}); var sx = window.pageXOffset || window.scrollX || 0; var sy = window.pageYOffset || window.scrollY || 0; diff --git a/lib/widgets/structure_view.dart b/lib/widgets/structure_view.dart index b73c981..ee1acc5 100644 --- a/lib/widgets/structure_view.dart +++ b/lib/widgets/structure_view.dart @@ -30,8 +30,9 @@ class _StructureViewState extends State { ui.Image? _background; Uint8List? _backgroundBytes; - /// Whether [_background] covers the whole page (true) or only the first - /// screen (false). + /// Whether the decoded [_background] covers the whole page (true) or only + /// the first screen (false). Swapped together with [_background], so the + /// painter never draws the old shot as full-page while the new one decodes. bool _backgroundFullPage = false; @override @@ -44,7 +45,7 @@ class _StructureViewState extends State { /// The bitmap is decoded downscaled to the wireframe's max width — the /// wireframe never displays wider than [kMaxWireframeWidth], so a /// full-resolution full-page shot would only waste memory. - Future _loadBackground(Uint8List bytes) async { + Future _loadBackground(Uint8List bytes, bool fullPage) async { try { final codec = await ui.instantiateImageCodec(bytes, targetWidth: kMaxWireframeWidth.round(), allowUpscaling: false); @@ -58,6 +59,7 @@ class _StructureViewState extends State { setState(() { _background?.dispose(); _background = image; + _backgroundFullPage = fullPage; }); } catch (_) { // Not a decodable image: draw the wireframe without a background. @@ -81,9 +83,11 @@ class _StructureViewState extends State { final shot = useFull ? fullShot : liveShot; final bytes = shot is Present ? shot.value : null; if (!identical(bytes, _backgroundBytes)) { + final wasFull = _backgroundFullPage; _backgroundBytes = bytes; _backgroundFullPage = useFull; final b = bytes; + final full = useFull; WidgetsBinding.instance.addPostFrameCallback((_) { if (!mounted) return; if (b == null) { @@ -94,7 +98,15 @@ class _StructureViewState extends State { }); } } else { - _loadBackground(b); + // Drop a stale shot of the other kind right away, so it is not + // drawn with the new full-page flag while the new one decodes. + if (_background != null && wasFull != full) { + setState(() { + _background?.dispose(); + _background = null; + }); + } + _loadBackground(b, full); } }); } diff --git a/test/collectors/vsv_layout_js_test.dart b/test/collectors/vsv_layout_js_test.dart index f8eb2f2..5449e7e 100644 --- a/test/collectors/vsv_layout_js_test.dart +++ b/test/collectors/vsv_layout_js_test.dart @@ -133,6 +133,18 @@ add(mk('section', {rect: [0, 5000, 412, 300]})); expect((r['blocks'] as List).length, 2); }); + test('the document size takes the max of scrollingElement and body', + () async { + final r = await layout(r''' +body.scrollWidth = 500; body.scrollHeight = 9000; +document.scrollingElement.scrollWidth = 412; +document.scrollingElement.scrollHeight = 5210; +add(mk('section', {rect: [0, 100, 412, 300]})); +'''); + if (r == null) return markTestSkipped('node not installed'); + expect([r['w'], r['h']], [500, 9000]); + }); + test('scanCapped is set only when the element scan hit its limit', () async { final few = await layout(r''' add(mk('section', {rect: [0, 0, 100, 100]}));