Dynamic view sizing (#138648)

Towards https://github.com/flutter/flutter/issues/134501.

This change is based on https://github.com/flutter/engine/pull/48090. It changes the `RenderView` to be dynamically sized based on its content if the `FlutterView` it is configured with allows it (i.e. the `FlutterView` has loose `FlutterView.physicalConstraints`). For that, it uses those `physicalConstraints` as input to the layout algorithm by passing them on to its child (after translating them to logical constraints via the device pixel ratio). The resulting `Size` that the `RenderView` would like to be is then communicated back to the engine by passing it to the `FlutterView.render` call.

Tests will fail until https://github.com/flutter/engine/pull/48090 has rolled into the framework.
This commit is contained in:
Michael Goderbauer
2023-11-29 12:45:18 -08:00
committed by GitHub
parent b417812547
commit 4f8a99147a
16 changed files with 292 additions and 44 deletions

View File

@@ -560,7 +560,7 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
final FlutterView view = renderView.flutterView;
if (_surfaceSize != null && view == platformDispatcher.implicitView) {
return ViewConfiguration(
size: _surfaceSize!,
constraints: ui.ViewConstraints.tight(_surfaceSize!),
devicePixelRatio: view.devicePixelRatio,
);
}
@@ -1831,7 +1831,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)
@@ -2115,9 +2115,10 @@ 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, constraints: ui.ViewConstraints.tight(size));
static Matrix4 _getMatrix(Size size, double devicePixelRatio, ui.FlutterView window) {
final double inverseRatio = devicePixelRatio / window.devicePixelRatio;
@@ -2148,6 +2149,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

@@ -751,6 +751,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
@@ -761,12 +764,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();
}
@@ -875,8 +905,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
@@ -901,6 +930,7 @@ class TestFlutterView implements FlutterView {
resetDisplayFeatures();
resetPadding();
resetPhysicalSize();
// resetPhysicalConstraints is implicitly called by resetPhysicalSize.
resetSystemGestureInsets();
resetViewInsets();
resetViewPadding();
@@ -1637,8 +1667,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();