Skip to content

Commit 4ca3f02

Browse files
committed
Update the screen in didUpdateEffectiveGeometry
1 parent ce8318b commit 4ca3f02

5 files changed

Lines changed: 118 additions & 0 deletions

File tree

‎engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterSceneLifeCycle.h‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,31 @@ API_AVAILABLE(ios(13.0))
258258
performActionForShortcutItem:(UIApplicationShortcutItem*)shortcutItem
259259
completionHandler:(void (^)(BOOL succeeded))completionHandler;
260260

261+
#pragma mark - Responding to geometry changes
262+
263+
/**
264+
* Updates the display information of the Flutter engines associated with `windowScene`.
265+
*
266+
* Call this from `-[UIWindowSceneDelegate windowScene:didUpdateEffectiveGeometry:]`. That callback
267+
* is always called when a `UIWindowScene` moves between screens (e.g. between the outer and inner
268+
* displays of a device with multiple built-in displays), even if no Flutter view's size changes.
269+
*
270+
* UIKit declares `previousEffectiveGeometry` as nonnull, but passes `nil` for it on the initial
271+
* call while the scene is being created. This is a UIKit bug. If Apple fixes it by marking the
272+
* parameter nullable, that is a breaking change for every Swift implementer of
273+
* `UIWindowSceneDelegate`, and this method will change to match.
274+
*
275+
* This parameter is still intentionally nonnull to match UIKit, rather than nullable:
276+
* - This method then only changes when UIKit's does, so Flutter never introduces a breaking change
277+
* here on its own. If it were nullable, Swift callers could pass an optional, and changing it to
278+
* nonnull later (e.g. if Apple stops passing `nil`) would break them.
279+
* - Like the other methods of this class, it mirrors the UIKit callback exactly, so forwarding is
280+
* a direct pass-through with matching types.
281+
*/
282+
- (void)windowScene:(UIWindowScene*)windowScene
283+
didUpdateEffectiveGeometry:(UIWindowSceneGeometry*)previousEffectiveGeometry
284+
API_AVAILABLE(ios(26.0));
285+
261286
@end
262287

263288
/**

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterSceneDelegate.mm‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ - (void)windowScene:(UIWindowScene*)windowScene
102102
completionHandler:completionHandler];
103103
}
104104

105+
#pragma mark - Responding to geometry changes
106+
107+
- (void)windowScene:(UIWindowScene*)windowScene
108+
didUpdateEffectiveGeometry:(UIWindowSceneGeometry*)previousEffectiveGeometry
109+
API_AVAILABLE(ios(26.0)) {
110+
[self.sceneLifeCycleDelegate windowScene:windowScene
111+
didUpdateEffectiveGeometry:previousEffectiveGeometry];
112+
}
113+
105114
#pragma mark - Helpers
106115

107116
- (BOOL)registerSceneLifeCycleWithFlutterEngine:(FlutterEngine*)engine scene:(UIScene*)scene {

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterSceneDelegateTest.m‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,28 @@ - (void)testWindowScenePerformActionForShortcutItem {
272272
completionHandler:[OCMArg any]]);
273273
}
274274

275+
- (void)testWindowSceneDidUpdateEffectiveGeometry {
276+
if (@available(iOS 26.0, *)) {
277+
[self setupMockApplication];
278+
279+
FlutterSceneDelegate* sceneDelegate = [[FlutterSceneDelegate alloc] init];
280+
id mockSceneDelegate = OCMPartialMock(sceneDelegate);
281+
282+
FlutterPluginSceneLifeCycleDelegate* mockLifecycleDelegate =
283+
OCMClassMock([FlutterPluginSceneLifeCycleDelegate class]);
284+
OCMStub([mockSceneDelegate sceneLifeCycleDelegate]).andReturn(mockLifecycleDelegate);
285+
286+
id scene = OCMClassMock([UIWindowScene class]);
287+
id geometry = OCMClassMock([UIWindowSceneGeometry class]);
288+
289+
[((FlutterSceneDelegate*)mockSceneDelegate) windowScene:scene
290+
didUpdateEffectiveGeometry:geometry];
291+
292+
OCMVerify(times(1), [mockLifecycleDelegate windowScene:scene
293+
didUpdateEffectiveGeometry:geometry]);
294+
}
295+
}
296+
275297
- (void)testRegisterSceneLifeCycleWithFlutterEngine {
276298
[self setupMockApplication];
277299

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterSceneLifeCycle.mm‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,18 @@ - (void)windowScene:(UIWindowScene*)windowScene
510510
}
511511
}
512512

513+
#pragma mark - Responding to geometry changes
514+
515+
- (void)windowScene:(UIWindowScene*)windowScene
516+
didUpdateEffectiveGeometry:(UIWindowSceneGeometry*)previousEffectiveGeometry {
517+
// The scene may have moved to a different screen. Unlike `viewDidLayoutSubviews`, this is called
518+
// even if the Flutter view's size did not change (e.g. a fixed-size view in an add-to-app host).
519+
// `updateDisplays` skips the update if the display information has not changed.
520+
for (FlutterEngine* engine in [self searchFlutterEnginesWithinScene:windowScene]) {
521+
[engine updateDisplays];
522+
}
523+
}
524+
513525
#pragma mark - Helpers
514526

515527
- (void)handleDeeplinkingForEngine:(FlutterEngine*)engine

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterSceneLifeCycleTest.mm‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,56 @@ - (void)testWindowScenePerformActionForShortcutItemHandledByNoPlugin {
15141514
completionHandler:handler]);
15151515
}
15161516

1517+
- (void)testWindowSceneDidUpdateEffectiveGeometryUpdatesDisplaysOfEnginesInScene {
1518+
if (@available(iOS 26.0, *)) {
1519+
FlutterPluginSceneLifeCycleDelegate* delegate =
1520+
[[FlutterPluginSceneLifeCycleDelegate alloc] init];
1521+
1522+
id mocks = [self mocksForEvents];
1523+
id mockScene = mocks[@"mockScene"];
1524+
// An engine whose view controller is in the scene, e.g. a fixed-size FlutterViewController
1525+
// embedded as a child view controller in an add-to-app host.
1526+
id viewControllerEngine = mocks[@"mockEngine"];
1527+
id mockWindow = OCMClassMock([UIWindow class]);
1528+
id mockHostViewController = OCMClassMock([UIViewController class]);
1529+
id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
1530+
OCMStub([mockScene windows]).andReturn(@[ mockWindow ]);
1531+
OCMStub([mockWindow rootViewController]).andReturn(mockHostViewController);
1532+
OCMStub([mockHostViewController childViewControllers]).andReturn(@[
1533+
mockFlutterViewController
1534+
]);
1535+
OCMStub([mockFlutterViewController engine]).andReturn(viewControllerEngine);
1536+
// An engine registered with the scene by the developer.
1537+
id registeredEngine = OCMClassMock([FlutterEngine class]);
1538+
[delegate registerSceneLifeCycleWithFlutterEngine:registeredEngine scene:mockScene];
1539+
XCTAssertEqual([delegate searchFlutterEnginesWithinScene:mockScene].count, 2.0);
1540+
1541+
id geometry = OCMClassMock([UIWindowSceneGeometry class]);
1542+
[delegate windowScene:mockScene didUpdateEffectiveGeometry:geometry];
1543+
1544+
OCMVerify(times(1), [viewControllerEngine updateDisplays]);
1545+
OCMVerify(times(1), [registeredEngine updateDisplays]);
1546+
}
1547+
}
1548+
1549+
- (void)testWindowSceneDidUpdateEffectiveGeometryDoesNotUpdateDisplaysOfEnginesInOtherScenes {
1550+
if (@available(iOS 26.0, *)) {
1551+
FlutterPluginSceneLifeCycleDelegate* delegate =
1552+
[[FlutterPluginSceneLifeCycleDelegate alloc] init];
1553+
1554+
id mocks = [self mocksForEvents];
1555+
id mockEngine = mocks[@"mockEngine"];
1556+
id mockScene = mocks[@"mockScene"];
1557+
id otherScene = OCMClassMock([UIWindowScene class]);
1558+
[delegate registerSceneLifeCycleWithFlutterEngine:mockEngine scene:otherScene];
1559+
1560+
[delegate windowScene:mockScene
1561+
didUpdateEffectiveGeometry:OCMClassMock([UIWindowSceneGeometry class])];
1562+
1563+
OCMVerify(never(), [mockEngine updateDisplays]);
1564+
}
1565+
}
1566+
15171567
- (NSDictionary*)mocksForEvents {
15181568
id mockEngine = OCMClassMock([FlutterEngine class]);
15191569
id mockLifecycleDelegate = OCMClassMock([FlutterEnginePluginSceneLifeCycleDelegate class]);

0 commit comments

Comments
 (0)