Fix #77084: flutter website doesn't resize properly when zoomed in/ou… (flutter/engine#33695)

This commit is contained in:
fatduckling
2022-08-06 07:43:04 +10:00
committed by GitHub
parent 0b3c46ef9f
commit dc9d7f6f04
2 changed files with 48 additions and 1 deletions

View File

@@ -161,6 +161,7 @@ class Surface {
// The existing surface is still reusable.
if (window.devicePixelRatio != _currentDevicePixelRatio) {
_updateLogicalHtmlCanvasSize();
_translateCanvas();
}
return _surface!;
}

View File

@@ -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.
);
}
}