Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/services/collectors/vsv_layout_js.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 <body> 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;
Expand Down
15 changes: 0 additions & 15 deletions lib/services/html_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4542,21 +4542,11 @@ Technical details: $message''';

Observed<Uint8List> _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<Uint8List> get fullPageScreenshot => _fullPageShot;

/// CSS size of [fullPageScreenshot]'s page at capture time.
({double width, double height})? get fullPageScreenshotCss =>
_fullPageShotCss;

@visibleForTesting
void debugSetFullPageScreenshot(Observed<Uint8List> shot) {
_fullPageShot = shot;
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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();
Expand Down
42 changes: 16 additions & 26 deletions lib/widgets/structure_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,20 @@ 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;
final StructureColoring coloring;
final ColorScheme scheme;
final ui.Image? background;
final bool backgroundFullPage;
final ({double width, double height})? backgroundCss;

StructurePainter({
required this.model,
Expand All @@ -75,7 +75,6 @@ class StructurePainter extends CustomPainter {
required this.scheme,
this.background,
this.backgroundFullPage = false,
this.backgroundCss,
});

static const double minLabelWidth = 60;
Expand All @@ -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,
);
}
Expand Down Expand Up @@ -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;
}
32 changes: 22 additions & 10 deletions lib/widgets/structure_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ class _StructureViewState extends State<StructureView> {
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
Expand All @@ -41,9 +42,13 @@ class _StructureViewState extends State<StructureView> {
}

/// Decodes [bytes] into [_background]; a new screenshot replaces the old.
Future<void> _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<void> _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;
Expand All @@ -54,6 +59,7 @@ class _StructureViewState extends State<StructureView> {
setState(() {
_background?.dispose();
_background = image;
_backgroundFullPage = fullPage;
});
} catch (_) {
// Not a decodable image: draw the wireframe without a background.
Expand All @@ -77,9 +83,11 @@ class _StructureViewState extends State<StructureView> {
final shot = useFull ? fullShot : liveShot;
final bytes = shot is Present<Uint8List> ? 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) {
Expand All @@ -90,7 +98,15 @@ class _StructureViewState extends State<StructureView> {
});
}
} 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);
}
});
}
Expand Down Expand Up @@ -159,9 +175,6 @@ class _StructureViewState extends State<StructureView> {
Widget _model(BuildContext context, StructureModel model) {
final scheme = Theme.of(context).colorScheme;
final palette = StructurePalette(scheme);
final fullCss =
context.select<HtmlService, ({double width, double height})?>(
(s) => s.fullPageScreenshotCss);
return SingleChildScrollView(
physics: const AlwaysScrollableScrollPhysics(),
padding: const EdgeInsets.all(16),
Expand Down Expand Up @@ -236,8 +249,7 @@ class _StructureViewState extends State<StructureView> {
coloring: _coloring,
scheme: scheme,
background: _background,
backgroundFullPage: _backgroundFullPage,
backgroundCss: _backgroundFullPage ? fullCss : null),
backgroundFullPage: _backgroundFullPage),
),
),
),
Expand Down
12 changes: 12 additions & 0 deletions test/collectors/vsv_layout_js_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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]}));
Expand Down
Loading