Reapply "Dynamic view sizing" (#140165) (#140918)

This reverts commit
d24c01bd0c.

The original change was reverted because it caused some apps to get
stuck on the splash screen on some phones.

An investigation determined that this was due to a rounding error.
Example: The device reports a physical size of 1008.0 x 2198.0 with a
dpr of 1.912500023841858. Flutter would translate that to a logical size
of 527.0588169589221 x 1149.2810314243163 and use that as the input for
its layout algorithm. Since the constraints here are tight, the layout
algorithm would determine that the resulting logical size of the root
render object must be 527.0588169589221 x 1149.2810314243163.
Translating this back to physical pixels by applying the dpr resulted in
a physical size of 1007.9999999999999 x 2198.0 for the frame. Android
now rejected that frame because it didn't match the expected size of
1008.0 x 2198.0 and since no frame had been rendered would never take
down the splash screen.

Prior to dynamically sized views, this wasn't an issue because we would
hard-code the frame size to whatever the requested size was.

Changes in this PR over the original PR:

* The issue has been fixed now by constraining the calculated physical
size to the input physical constraints which makes sure that we always
end up with a size that is acceptable to the operating system.
* The `ViewConfiguration` was refactored to use the slightly more
convenient `BoxConstraints` over the `ViewConstraints` to represent
constraints. Both essentially represent the same thing, but
`BoxConstraints` are more powerful and we avoid a couple of translations
between the two by translating the` ViewConstraints` from the
`FlutterView` to `BoxConstraints` directly when the `ViewConfiguration`
is created.

All changes over the original PR are contained in the second commit of
this PR.

Fixes b/316813075
Part of https://github.com/flutter/flutter/issues/134501.
This commit is contained in:
Michael Goderbauer
2024-01-09 14:10:43 -08:00
committed by GitHub
parent 0c40f21fc5
commit 4534a24c09
18 changed files with 371 additions and 51 deletions

View File

@@ -559,8 +559,10 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
}
final FlutterView view = renderView.flutterView;
if (_surfaceSize != null && view == platformDispatcher.implicitView) {
final BoxConstraints constraints = BoxConstraints.tight(_surfaceSize!);
return ViewConfiguration(
size: _surfaceSize!,
logicalConstraints: constraints,
physicalConstraints: constraints * view.devicePixelRatio,
devicePixelRatio: view.devicePixelRatio,
);
}
@@ -1832,7 +1834,7 @@ class LiveTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
final Map<int, _LiveTestPointerRecord>? pointerIdToRecord = _renderViewToPointerIdToPointerRecord[renderView];
if (pointerIdToRecord != null && pointerIdToRecord.isNotEmpty) {
final double radius = renderView.configuration.size.shortestSide * 0.05;
final double radius = renderView.size.shortestSide * 0.05;
final Path path = Path()
..addOval(Rect.fromCircle(center: Offset.zero, radius: radius))
..moveTo(0.0, -radius * 2.0)
@@ -2116,9 +2118,14 @@ class TestViewConfiguration extends ViewConfiguration {
/// Creates a [TestViewConfiguration] with the given size and view.
///
/// The [size] defaults to 800x600.
TestViewConfiguration.fromView({required ui.FlutterView view, super.size = _kDefaultTestViewportSize})
TestViewConfiguration.fromView({required ui.FlutterView view, Size size = _kDefaultTestViewportSize})
: _paintMatrix = _getMatrix(size, view.devicePixelRatio, view),
super(devicePixelRatio: view.devicePixelRatio);
_physicalSize = view.physicalSize,
super(
devicePixelRatio: view.devicePixelRatio,
logicalConstraints: BoxConstraints.tight(size),
physicalConstraints: BoxConstraints.tight(size) * view.devicePixelRatio,
);
static Matrix4 _getMatrix(Size size, double devicePixelRatio, ui.FlutterView window) {
final double inverseRatio = devicePixelRatio / window.devicePixelRatio;
@@ -2149,6 +2156,11 @@ class TestViewConfiguration extends ViewConfiguration {
@override
Matrix4 toMatrix() => _paintMatrix.clone();
final Size _physicalSize;
@override
Size toPhysicalSize(Size logicalSize) => _physicalSize;
@override
String toString() => 'TestViewConfiguration';
}

View File

@@ -750,6 +750,9 @@ class TestFlutterView implements FlutterView {
/// can only be set in a test environment to emulate different view
/// configurations. A standard [FlutterView] is not mutable from the framework.
///
/// Setting this value also sets [physicalConstraints] to tight constraints
/// based on the given size.
///
/// See also:
///
/// * [FlutterView.physicalSize] for the standard implementation
@@ -760,12 +763,39 @@ class TestFlutterView implements FlutterView {
Size? _physicalSize;
set physicalSize(Size value) {
_physicalSize = value;
// For backwards compatibility the constraints are set based on the provided size.
physicalConstraints = ViewConstraints.tight(value);
}
/// Resets [physicalSize] (and implicitly also the [physicalConstraints]) to
/// the default value for this view.
void resetPhysicalSize() {
_physicalSize = null;
resetPhysicalConstraints();
}
/// The physical constraints to use for this test.
///
/// Defaults to the value provided by [FlutterView.physicalConstraints]. This
/// can only be set in a test environment to emulate different view
/// configurations. A standard [FlutterView] is not mutable from the framework.
///
/// See also:
///
/// * [FlutterView.physicalConstraints] for the standard implementation
/// * [physicalConstraints] to reset this value specifically
/// * [reset] to reset all test values for this view
@override
ViewConstraints get physicalConstraints => _physicalConstraints ?? _view.physicalConstraints;
ViewConstraints? _physicalConstraints;
set physicalConstraints(ViewConstraints value) {
_physicalConstraints = value;
platformDispatcher.onMetricsChanged?.call();
}
/// Resets [physicalSize] to the default value for this view.
void resetPhysicalSize() {
_physicalSize = null;
/// Resets [physicalConstraints] to the default value for this view.
void resetPhysicalConstraints() {
_physicalConstraints = null;
platformDispatcher.onMetricsChanged?.call();
}
@@ -874,8 +904,7 @@ class TestFlutterView implements FlutterView {
@override
void render(Scene scene, {Size? size}) {
// TODO(goderbauer): Wire through size after https://github.com/flutter/engine/pull/48090 rolled in.
_view.render(scene);
_view.render(scene, size: size);
}
@override
@@ -900,6 +929,7 @@ class TestFlutterView implements FlutterView {
resetDisplayFeatures();
resetPadding();
resetPhysicalSize();
// resetPhysicalConstraints is implicitly called by resetPhysicalSize.
resetSystemGestureInsets();
resetViewInsets();
resetViewPadding();
@@ -1636,8 +1666,7 @@ class TestWindow implements SingletonFlutterWindow {
)
@override
void render(Scene scene, {Size? size}) {
// TODO(goderbauer): Wire through size after https://github.com/flutter/engine/pull/48090 rolled in.
_view.render(scene);
_view.render(scene, size: size);
}
@Deprecated(

View File

@@ -124,6 +124,19 @@ void main() {
);
});
testWidgets('faking physicalSize fakes physicalConstraints', (WidgetTester tester) async {
const Size fakeSize = Size(50, 50);
verifyPropertyFaked<ViewConstraints>(
tester: tester,
realValue: trueImplicitView().physicalConstraints,
fakeValue: ViewConstraints.tight(fakeSize),
propertyRetriever: () => boundImplicitView().physicalConstraints,
propertyFaker: (_, __) {
tester.view.physicalSize = fakeSize;
},
);
});
testWidgets('can reset physicalSize', (WidgetTester tester) async {
verifyPropertyReset<Size>(
tester: tester,
@@ -138,6 +151,47 @@ void main() {
);
});
testWidgets('resetting physicalSize resets physicalConstraints', (WidgetTester tester) async {
const Size fakeSize = Size(50, 50);
verifyPropertyReset<ViewConstraints>(
tester: tester,
fakeValue: ViewConstraints.tight(fakeSize),
propertyRetriever: () => boundImplicitView().physicalConstraints,
propertyResetter: () {
tester.view.resetPhysicalSize();
},
propertyFaker: (_) {
tester.view.physicalSize = fakeSize;
},
);
});
testWidgets('can fake physicalConstraints', (WidgetTester tester) async {
verifyPropertyFaked<ViewConstraints>(
tester: tester,
realValue: trueImplicitView().physicalConstraints,
fakeValue: const ViewConstraints(minWidth: 1, maxWidth: 2, minHeight: 3, maxHeight: 4),
propertyRetriever: () => boundImplicitView().physicalConstraints,
propertyFaker: (_, ViewConstraints fakeValue) {
tester.view.physicalConstraints = fakeValue;
},
);
});
testWidgets('can reset physicalConstraints', (WidgetTester tester) async {
verifyPropertyReset<ViewConstraints>(
tester: tester,
fakeValue: const ViewConstraints(minWidth: 1, maxWidth: 2, minHeight: 3, maxHeight: 4),
propertyRetriever: () => boundImplicitView().physicalConstraints,
propertyResetter: () {
tester.view.resetPhysicalConstraints();
},
propertyFaker: (ViewConstraints fakeValue) {
tester.view.physicalConstraints = fakeValue;
},
);
});
testWidgets('can fake systemGestureInsets', (WidgetTester tester) async {
verifyPropertyFaked<ViewPadding>(
tester: tester,

View File

@@ -83,7 +83,7 @@ No widgets found at Offset(1.0, 1.0).
),
);
final Size originalSize = tester.binding.createViewConfigurationFor(tester.binding.renderView).size; // ignore: deprecated_member_use
final Size originalSize = tester.binding.renderView.size; // ignore: deprecated_member_use
await tester.binding.setSurfaceSize(const Size(2000, 1800));
try {
await tester.pump();