From dc9d7f6f04f74e296ddfaddc3b3262f619466d5c Mon Sep 17 00:00:00 2001 From: fatduckling Date: Sat, 6 Aug 2022 07:43:04 +1000 Subject: [PATCH] =?UTF-8?q?Fix=20#77084:=20flutter=20website=20doesn't=20r?= =?UTF-8?q?esize=20properly=20when=20zoomed=20in/ou=E2=80=A6=20(flutter/en?= =?UTF-8?q?gine#33695)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/src/engine/canvaskit/surface.dart | 1 + .../web_ui/test/canvaskit/surface_test.dart | 48 ++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/surface.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/surface.dart index 189a06158d..144aab63eb 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/surface.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/surface.dart @@ -161,6 +161,7 @@ class Surface { // The existing surface is still reusable. if (window.devicePixelRatio != _currentDevicePixelRatio) { _updateLogicalHtmlCanvasSize(); + _translateCanvas(); } return _surface!; } diff --git a/engine/src/flutter/lib/web_ui/test/canvaskit/surface_test.dart b/engine/src/flutter/lib/web_ui/test/canvaskit/surface_test.dart index 74038d52a4..7253381aaf 100644 --- a/engine/src/flutter/lib/web_ui/test/canvaskit/surface_test.dart +++ b/engine/src/flutter/lib/web_ui/test/canvaskit/surface_test.dart @@ -31,16 +31,18 @@ void testMain() { expect(original.height, 19); expect(original.style.width, '9px'); expect(original.style.height, '19px'); + expect(original.style.transform, _isTranslate(0, 0)); expect(originalSurface.width(), 9); expect(originalSurface.height(), 19); - // Shrinking reuses the existing canvas straight-up. + // Shrinking reuses the existing canvas but translates it so Skia renders into the visible area. final CkSurface shrunkSurface = surface.acquireFrame(const ui.Size(5, 15)).skiaSurface; final DomCanvasElement shrunk = surface.htmlCanvas!; expect(shrunk, same(original)); expect(shrunk.style.width, '9px'); expect(shrunk.style.height, '19px'); + expect(shrunk.style.transform, _isTranslate(0, -4)); expect(shrunkSurface, isNot(same(original))); expect(shrunkSurface.width(), 5); expect(shrunkSurface.height(), 15); @@ -58,6 +60,7 @@ void testMain() { expect(firstIncrease.height, 28); expect(firstIncrease.style.width, '14px'); expect(firstIncrease.style.height, '28px'); + expect(firstIncrease.style.transform, _isTranslate(0, -8)); expect(firstIncreaseSurface.width(), 10); expect(firstIncreaseSurface.height(), 20); @@ -66,6 +69,7 @@ void testMain() { surface.acquireFrame(const ui.Size(11, 22)).skiaSurface; final DomCanvasElement secondIncrease = surface.htmlCanvas!; expect(secondIncrease, same(firstIncrease)); + expect(secondIncrease.style.transform, _isTranslate(0, -6)); expect(secondIncreaseSurface, isNot(same(firstIncreaseSurface))); expect(secondIncreaseSurface.width(), 11); expect(secondIncreaseSurface.height(), 22); @@ -81,6 +85,7 @@ void testMain() { expect(huge.height, 56); expect(huge.style.width, '28px'); expect(huge.style.height, '56px'); + expect(huge.style.transform, _isTranslate(0, -16)); expect(hugeSurface.width(), 20); expect(hugeSurface.height(), 40); @@ -89,9 +94,27 @@ void testMain() { surface.acquireFrame(const ui.Size(5, 15)).skiaSurface; final DomCanvasElement shrunk2 = surface.htmlCanvas!; expect(shrunk2, same(huge)); + expect(shrunk2.style.width, '28px'); + expect(shrunk2.style.height, '56px'); + expect(shrunk2.style.transform, _isTranslate(0, -41)); expect(shrunkSurface2, isNot(same(hugeSurface))); expect(shrunkSurface2.width(), 5); expect(shrunkSurface2.height(), 15); + + // Doubling the DPR should halve the CSS width, height, and translation of the canvas. + // This tests https://github.com/flutter/flutter/issues/77084 + window.debugOverrideDevicePixelRatio(2.0); + final CkSurface dpr2Surface2 = + surface.acquireFrame(const ui.Size(5, 15)).skiaSurface; + final DomCanvasElement dpr2Canvas = surface.htmlCanvas!; + expect(dpr2Canvas, same(huge)); + expect(dpr2Canvas.style.width, '14px'); + expect(dpr2Canvas.style.height, '28px'); + expect(dpr2Canvas.style.transform, _isTranslate(0, -20.5)); + expect(dpr2Surface2, isNot(same(hugeSurface))); + expect(dpr2Surface2.width(), 5); + expect(dpr2Surface2.height(), 15); + // Skipping on Firefox for now since Firefox headless doesn't support WebGL // This causes issues in the test since we create a Canvas-backed surface, // which cannot be a different size from the canvas. @@ -156,6 +179,7 @@ void testMain() { expect(original.height(), 16); expect(surface.htmlCanvas!.style.width, '10px'); expect(surface.htmlCanvas!.style.height, '16px'); + expect(surface.htmlCanvas!.style.transform, _isTranslate(0, 0)); // Increase device-pixel ratio: this makes CSS pixels bigger, so we need // fewer of them to cover the browser window. @@ -166,6 +190,7 @@ void testMain() { expect(highDpr.height(), 16); expect(surface.htmlCanvas!.style.width, '5px'); expect(surface.htmlCanvas!.style.height, '8px'); + expect(surface.htmlCanvas!.style.transform, _isTranslate(0, 0)); // Decrease device-pixel ratio: this makes CSS pixels smaller, so we need // more of them to cover the browser window. @@ -176,6 +201,7 @@ void testMain() { expect(lowDpr.height(), 16); expect(surface.htmlCanvas!.style.width, '20px'); expect(surface.htmlCanvas!.style.height, '32px'); + expect(surface.htmlCanvas!.style.transform, _isTranslate(0, 0)); // See https://github.com/flutter/flutter/issues/77084#issuecomment-1120151172 window.debugOverrideDevicePixelRatio(2.0); @@ -185,6 +211,26 @@ void testMain() { expect(changeRatioAndSize.height(), 16); expect(surface.htmlCanvas!.style.width, '5px'); expect(surface.htmlCanvas!.style.height, '8px'); + expect(surface.htmlCanvas!.style.transform, _isTranslate(0, 0)); }); }); } + +/// Checks that the CSS 'transform' property is a translation in a cross-browser way. +/// +/// Assumes that the `x` and `y` values are round enough for their `toString` values +/// to match the stringified CSS length value. +Matcher _isTranslate(double x, double y) { + // When the y coordinate is zero, Firefox omits it, e.g.: + // Chrome/Safari/Edge: translate(0px, 0px) + // Firefox: translate(0px) + final String fullFormat = 'translate(${x}px, ${y}px)'; + if (y != 0) { + return equals(fullFormat); + } else { + return anyOf( + fullFormat, // Non-Firefox browsers use this format. + 'translate(${x}px)', // Firefox omits y when it's zero. + ); + } +}