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/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..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 @@ -41,9 +42,13 @@ class _StructureViewState extends State { } /// Decodes [bytes] into [_background]; a new screenshot replaces the old. - Future _loadBackground(Uint8List bytes) async { + /// 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, bool fullPage) 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; @@ -54,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. @@ -77,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) { @@ -90,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); } }); } @@ -159,9 +175,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 +249,7 @@ class _StructureViewState extends State { coloring: _coloring, scheme: scheme, background: _background, - backgroundFullPage: _backgroundFullPage, - backgroundCss: _backgroundFullPage ? fullCss : null), + backgroundFullPage: _backgroundFullPage), ), ), ), 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]}));