From 4fecb346449739cbb4303caf576f356157035517 Mon Sep 17 00:00:00 2001 From: Chris Yang Date: Wed, 14 Jun 2023 10:18:22 -0700 Subject: [PATCH 1/6] use interfaceOrientation on window scene for iOS 13 and above --- .../darwin/ios/framework/Source/FlutterViewController.mm | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index c88366b669cc3..8df5bb85ee6f5 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -1857,8 +1857,13 @@ - (void)performOrientationUpdate:(UIInterfaceOrientationMask)new_preferences { [self setNeedsUpdateOfSupportedInterfaceOrientations]; } } else { - UIInterfaceOrientationMask currentInterfaceOrientation = + UIInterfaceOrientationMask currentInterfaceOrientation; + if (@available(iOS 13.0, *)) { + currentInterfaceOrientation = 1 << self.view.window.windowScene.interfaceOrientation; + } else { + currentInterfaceOrientation = 1 << [[UIApplication sharedApplication] statusBarOrientation]; + } if (!(_orientationPreferences & currentInterfaceOrientation)) { [UIViewController attemptRotationToDeviceOrientation]; // Force orientation switch if the current orientation is not allowed From a1165e83077355b248d40fb1864d3e8337f965b3 Mon Sep 17 00:00:00 2001 From: Chris Yang Date: Wed, 14 Jun 2023 10:30:29 -0700 Subject: [PATCH 2/6] test --- .../ios/framework/Source/FlutterViewController.mm | 3 +-- .../ios/framework/Source/FlutterViewControllerTest.mm | 11 ++++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index 8df5bb85ee6f5..13a93346c90be 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -1861,8 +1861,7 @@ - (void)performOrientationUpdate:(UIInterfaceOrientationMask)new_preferences { if (@available(iOS 13.0, *)) { currentInterfaceOrientation = 1 << self.view.window.windowScene.interfaceOrientation; } else { - currentInterfaceOrientation = - 1 << [[UIApplication sharedApplication] statusBarOrientation]; + currentInterfaceOrientation = 1 << [[UIApplication sharedApplication] statusBarOrientation]; } if (!(_orientationPreferences & currentInterfaceOrientation)) { [UIViewController attemptRotationToDeviceOrientation]; diff --git a/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm b/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm index e7b6880c56536..f20e2280ce16c 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm @@ -1464,9 +1464,14 @@ - (void)orientationTestWithOrientationUpdate:(UIInterfaceOrientationMask)mask } else { OCMExpect([deviceMock setValue:@(resultingOrientation) forKey:@"orientation"]); } - - OCMStub([mockApplication sharedApplication]).andReturn(mockApplication); - OCMStub([mockApplication statusBarOrientation]).andReturn(currentOrientation); + if (@available(iOS 13.0, *)) { + mockWindowScene = OCMPartialMock(realVC.view.window.windowScene); + OCMStub(((UIWindowScene*)mockWindowScene).interfaceOrientation) + .andReturn(currentOrientation); + } else { + OCMStub([mockApplication sharedApplication]).andReturn(mockApplication); + OCMStub([mockApplication statusBarOrientation]).andReturn(currentOrientation); + } } [realVC performOrientationUpdate:mask]; From 739dbae02ea9cb2e0da57f88c3a10e88337e5ffe Mon Sep 17 00:00:00 2001 From: Chris Yang Date: Wed, 14 Jun 2023 10:50:51 -0700 Subject: [PATCH 3/6] bail if window scene does not exsit --- .../framework/Source/FlutterViewController.mm | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index 13a93346c90be..be4573b18d964 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -617,11 +617,19 @@ - (UIScreen*)mainScreenIfViewLoaded { if (self.viewIfLoaded == nil) { FML_LOG(WARNING) << "Trying to access the view before it is loaded."; } - return self.viewIfLoaded.window.windowScene.screen; + return [self windowSceneIfViewLoaded].screen; } return UIScreen.mainScreen; } +- (UIWindowScene*)windowSceneIfViewLoaded API_AVAILABLE(ios(13.0)) { + if (self.viewIfLoaded == nil) { + return nil; + FML_LOG(WARNING) << "Trying to access the view before it is loaded."; + } + return self.viewIfLoaded.window.windowScene; +} + - (BOOL)loadDefaultSplashScreenView { NSString* launchscreenName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UILaunchStoryboardName"]; @@ -1859,7 +1867,12 @@ - (void)performOrientationUpdate:(UIInterfaceOrientationMask)new_preferences { } else { UIInterfaceOrientationMask currentInterfaceOrientation; if (@available(iOS 13.0, *)) { - currentInterfaceOrientation = 1 << self.view.window.windowScene.interfaceOrientation; + UIWindowScene* windowScene = [self windowSceneIfViewLoaded]; + if (!windowScene) { + FML_LOG(WARNING) << "Trying to access the window scene before the view is loaded."; + return; + } + currentInterfaceOrientation = 1 << windowScene.interfaceOrientation; } else { currentInterfaceOrientation = 1 << [[UIApplication sharedApplication] statusBarOrientation]; } From f21f2587927d23a00250373b5869cd37674bdae6 Mon Sep 17 00:00:00 2001 From: Chris Yang Date: Wed, 14 Jun 2023 11:17:26 -0700 Subject: [PATCH 4/6] move interface to .h file --- .../darwin/ios/framework/Source/FlutterViewController.mm | 2 +- .../ios/framework/Source/FlutterViewController_Internal.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index be4573b18d964..6f8f7e6942656 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -622,7 +622,7 @@ - (UIScreen*)mainScreenIfViewLoaded { return UIScreen.mainScreen; } -- (UIWindowScene*)windowSceneIfViewLoaded API_AVAILABLE(ios(13.0)) { +- (UIWindowScene*)windowSceneIfViewLoaded { if (self.viewIfLoaded == nil) { return nil; FML_LOG(WARNING) << "Trying to access the view before it is loaded."; diff --git a/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h b/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h index 5f9fa58418b1b..2a0471ae0f15a 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h +++ b/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h @@ -55,6 +55,7 @@ typedef void (^FlutterKeyboardAnimationCallback)(fml::TimePoint); - (void)addInternalPlugins; - (void)deregisterNotifications; - (int32_t)accessibilityFlags; +- (UIWindowScene*)windowSceneIfViewLoaded API_AVAILABLE(ios(13.0)); @end #endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTERVIEWCONTROLLER_INTERNAL_H_ From 5b0b723df1e3286cf2da27cbca6ac464de0651d1 Mon Sep 17 00:00:00 2001 From: Chris Yang Date: Wed, 14 Jun 2023 12:56:17 -0700 Subject: [PATCH 5/6] review --- .../darwin/ios/framework/Source/FlutterViewController.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index 6f8f7e6942656..43efec59dd94c 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -624,8 +624,8 @@ - (UIScreen*)mainScreenIfViewLoaded { - (UIWindowScene*)windowSceneIfViewLoaded { if (self.viewIfLoaded == nil) { - return nil; FML_LOG(WARNING) << "Trying to access the view before it is loaded."; + return nil; } return self.viewIfLoaded.window.windowScene; } From 1130d200d948057d431741cd4b0b2d4b90070ee0 Mon Sep 17 00:00:00 2001 From: Chris Yang Date: Wed, 14 Jun 2023 14:24:59 -0700 Subject: [PATCH 6/6] add comment --- .../darwin/ios/framework/Source/FlutterViewController.mm | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index 0b58fee0d7f23..2828ea16814ce 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -1870,6 +1870,8 @@ - (void)performOrientationUpdate:(UIInterfaceOrientationMask)new_preferences { if (@available(iOS 13.0, *)) { UIWindowScene* windowScene = [self windowSceneIfViewLoaded]; if (!windowScene) { + // When the view is not loaded, it does not make sense to access the interface + // orientation, bail. FML_LOG(WARNING) << "Trying to access the window scene before the view is loaded."; return; }