diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 787aec593c..5c55d6239f 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -2672,6 +2672,9 @@ ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/KeyCodeMap_I ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/UIViewController+FlutterScreenAndSceneIfLoaded.h + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/UIViewController+FlutterScreenAndSceneIfLoaded.mm + ../../../flutter/LICENSE +ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/UIViewController_FlutterScreenAndSceneIfLoadedTest.mm + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/VsyncWaiterIosTest.mm + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.mm + ../../../flutter/LICENSE @@ -5395,6 +5398,9 @@ FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/KeyCodeMap_Int FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm +FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/UIViewController+FlutterScreenAndSceneIfLoaded.h +FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/UIViewController+FlutterScreenAndSceneIfLoaded.mm +FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/UIViewController_FlutterScreenAndSceneIfLoadedTest.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/VsyncWaiterIosTest.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.mm diff --git a/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn b/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn index 762d3ca94a..6a62586420 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn +++ b/engine/src/flutter/shell/platform/darwin/ios/BUILD.gn @@ -123,6 +123,8 @@ source_set("flutter_framework_source") { "framework/Source/KeyCodeMap_Internal.h", "framework/Source/SemanticsObject.h", "framework/Source/SemanticsObject.mm", + "framework/Source/UIViewController+FlutterScreenAndSceneIfLoaded.h", + "framework/Source/UIViewController+FlutterScreenAndSceneIfLoaded.mm", "framework/Source/accessibility_bridge.h", "framework/Source/accessibility_bridge.mm", "framework/Source/accessibility_text_entry.h", @@ -298,6 +300,7 @@ shared_library("ios_test_flutter") { "framework/Source/FlutterUndoManagerPluginTest.mm", "framework/Source/FlutterViewControllerTest.mm", "framework/Source/SemanticsObjectTest.mm", + "framework/Source/UIViewController_FlutterScreenAndSceneIfLoadedTest.mm", "framework/Source/connection_collection_test.mm", ] deps = [ diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm index 00637023f3..2680567f79 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm @@ -30,6 +30,7 @@ #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerDelegate.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerPlugin.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h" +#import "flutter/shell/platform/darwin/ios/framework/Source/UIViewController+FlutterScreenAndSceneIfLoaded.h" #import "flutter/shell/platform/darwin/ios/framework/Source/connection_collection.h" #import "flutter/shell/platform/darwin/ios/framework/Source/platform_message_response_darwin.h" #import "flutter/shell/platform/darwin/ios/framework/Source/profiler_metrics_ios.h" @@ -880,7 +881,7 @@ static void SetEntryPoint(flutter::Settings* settings, NSString* entrypoint, NSS #if APPLICATION_EXTENSION_API_ONLY if (@available(iOS 13.0, *)) { - _isGpuDisabled = self.viewController.windowSceneIfViewLoaded.activationState == + _isGpuDisabled = self.viewController.flutterWindowSceneIfViewLoaded.activationState == UISceneActivationStateBackground; } else { // [UIApplication sharedApplication API is not available for app extension. diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm index ab27072567..8a3688cb11 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm @@ -11,6 +11,7 @@ #include "flutter/fml/logging.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h" +#import "flutter/shell/platform/darwin/ios/framework/Source/UIViewController+FlutterScreenAndSceneIfLoaded.h" namespace { @@ -293,7 +294,7 @@ using namespace flutter; #if APPLICATION_EXTENSION_API_ONLY if (@available(iOS 15.0, *)) { rootViewController = - [engineViewController windowSceneIfViewLoaded].keyWindow.rootViewController; + [engineViewController flutterWindowSceneIfViewLoaded].keyWindow.rootViewController; } else { FML_LOG(WARNING) << "rootViewController is not available in application extension prior to iOS 15.0."; diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index 7d9c226d1d..47f6481fe9 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -26,6 +26,7 @@ #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputDelegate.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterView.h" +#import "flutter/shell/platform/darwin/ios/framework/Source/UIViewController+FlutterScreenAndSceneIfLoaded.h" #import "flutter/shell/platform/darwin/ios/framework/Source/platform_message_response_darwin.h" #import "flutter/shell/platform/darwin/ios/framework/Source/vsync_waiter_ios.h" #import "flutter/shell/platform/darwin/ios/platform_view_ios.h" @@ -516,25 +517,6 @@ static UIView* GetViewOrPlaceholder(UIView* existing_view) { return pointer_data; } -- (UIWindowScene*)windowSceneIfViewLoaded { - if (self.viewIfLoaded == nil) { - FML_LOG(WARNING) << "Trying to access the window scene before the view is loaded."; - return nil; - } - return self.viewIfLoaded.window.windowScene; -} - -- (UIScreen*)screenIfViewLoaded { - if (@available(iOS 13.0, *)) { - if (self.viewIfLoaded == nil) { - FML_LOG(WARNING) << "Trying to access the screen before the view is loaded."; - return nil; - } - return [self windowSceneIfViewLoaded].screen; - } - return UIScreen.mainScreen; -} - static void SendFakeTouchEvent(UIScreen* screen, FlutterEngine* engine, CGPoint location, @@ -554,7 +536,7 @@ static void SendFakeTouchEvent(UIScreen* screen, return NO; } CGPoint statusBarPoint = CGPointZero; - UIScreen* screen = [self screenIfViewLoaded]; + UIScreen* screen = [self flutterScreenIfViewLoaded]; if (screen) { SendFakeTouchEvent(screen, _engine.get(), statusBarPoint, flutter::PointerData::Change::kDown); SendFakeTouchEvent(screen, _engine.get(), statusBarPoint, flutter::PointerData::Change::kUp); @@ -869,8 +851,8 @@ static void SendFakeTouchEvent(UIScreen* screen, BOOL stateIsActive = YES; #if APPLICATION_EXTENSION_API_ONLY if (@available(iOS 13.0, *)) { - stateIsActive = - self.windowSceneIfViewLoaded.activationState == UISceneActivationStateForegroundActive; + stateIsActive = self.flutterWindowSceneIfViewLoaded.activationState == + UISceneActivationStateForegroundActive; } #else stateIsActive = UIApplication.sharedApplication.applicationState == UIApplicationStateActive; @@ -1166,7 +1148,7 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) // Activate or pause the correction of delivery frame rate of touch events. [self triggerTouchRateCorrectionIfNeeded:touches]; - const CGFloat scale = [self screenIfViewLoaded].scale; + const CGFloat scale = [self flutterScreenIfViewLoaded].scale; auto packet = std::make_unique(touches.count + touches_to_remove_count); @@ -1381,7 +1363,7 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) - (void)viewDidLayoutSubviews { CGRect viewBounds = self.view.bounds; - CGFloat scale = [self screenIfViewLoaded].scale; + CGFloat scale = [self flutterScreenIfViewLoaded].scale; // Purposefully place this not visible. _scrollView.get().frame = CGRectMake(0.0, 0.0, viewBounds.size.width, 0.0); @@ -1400,8 +1382,8 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) bool applicationOrSceneIsActive = YES; #if APPLICATION_EXTENSION_API_ONLY if (@available(iOS 13.0, *)) { - applicationOrSceneIsActive = - self.windowSceneIfViewLoaded.activationState == UISceneActivationStateForegroundActive; + applicationOrSceneIsActive = self.flutterWindowSceneIfViewLoaded.activationState == + UISceneActivationStateForegroundActive; } #else applicationOrSceneIsActive = @@ -1436,7 +1418,7 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) // Set _viewportMetrics physical size. - (void)setViewportMetricsSize { - UIScreen* screen = [self screenIfViewLoaded]; + UIScreen* screen = [self flutterScreenIfViewLoaded]; if (!screen) { return; } @@ -1450,7 +1432,7 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) // // Viewport paddings represent the iOS safe area insets. - (void)setViewportMetricsPaddings { - UIScreen* screen = [self screenIfViewLoaded]; + UIScreen* screen = [self flutterScreenIfViewLoaded]; if (!screen) { return; } @@ -1616,7 +1598,7 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) return FlutterKeyboardModeHidden; } - CGRect screenRect = [self screenIfViewLoaded].bounds; + CGRect screenRect = [self flutterScreenIfViewLoaded].bounds; CGRect adjustedKeyboardFrame = keyboardFrame; adjustedKeyboardFrame.origin.y += [self calculateMultitaskingAdjustment:screenRect keyboardFrame:keyboardFrame]; @@ -1656,7 +1638,7 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) } CGRect viewRectRelativeToScreen = [self.viewIfLoaded convertRect:self.viewIfLoaded.frame - toCoordinateSpace:[self screenIfViewLoaded].coordinateSpace]; + toCoordinateSpace:[self flutterScreenIfViewLoaded].coordinateSpace]; CGFloat viewBottom = CGRectGetMaxY(viewRectRelativeToScreen); CGFloat offset = screenHeight - viewBottom; if (offset > 0) { @@ -1672,14 +1654,14 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) // Calculate how much of the keyboard intersects with the view. CGRect viewRectRelativeToScreen = [self.viewIfLoaded convertRect:self.viewIfLoaded.frame - toCoordinateSpace:[self screenIfViewLoaded].coordinateSpace]; + toCoordinateSpace:[self flutterScreenIfViewLoaded].coordinateSpace]; CGRect intersection = CGRectIntersection(keyboardFrame, viewRectRelativeToScreen); CGFloat portionOfKeyboardInView = CGRectGetHeight(intersection); // The keyboard is treated as an inset since we want to effectively reduce the window size by // the keyboard height. The Dart side will compute a value accounting for the keyboard-consuming // bottom padding. - CGFloat scale = [self screenIfViewLoaded].scale; + CGFloat scale = [self flutterScreenIfViewLoaded].scale; return portionOfKeyboardInView * scale; } return 0; @@ -1974,8 +1956,9 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) if (@available(iOS 16.0, *)) { NSSet* scenes = #if APPLICATION_EXTENSION_API_ONLY - self.windowSceneIfViewLoaded ? [NSSet setWithObject:self.windowSceneIfViewLoaded] - : [NSSet set]; + self.flutterWindowSceneIfViewLoaded + ? [NSSet setWithObject:self.flutterWindowSceneIfViewLoaded] + : [NSSet set]; #else [UIApplication.sharedApplication.connectedScenes filteredSetUsingPredicate:[NSPredicate predicateWithBlock:^BOOL( @@ -1987,7 +1970,7 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) } else { UIInterfaceOrientationMask currentInterfaceOrientation; if (@available(iOS 13.0, *)) { - UIWindowScene* windowScene = [self windowSceneIfViewLoaded]; + UIWindowScene* windowScene = [self flutterWindowSceneIfViewLoaded]; if (!windowScene) { FML_LOG(WARNING) << "Accessing the interface orientation when the window scene is unavailable."; @@ -2410,7 +2393,7 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) - (void)hoverEvent:(UIPanGestureRecognizer*)recognizer API_AVAILABLE(ios(13.4)) { CGPoint location = [recognizer locationInView:self.view]; - CGFloat scale = [self screenIfViewLoaded].scale; + CGFloat scale = [self flutterScreenIfViewLoaded].scale; CGPoint oldLocation = _mouseState.location; _mouseState.location = {location.x * scale, location.y * scale}; @@ -2467,7 +2450,7 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) - (void)discreteScrollEvent:(UIPanGestureRecognizer*)recognizer API_AVAILABLE(ios(13.4)) { CGPoint translation = [recognizer translationInView:self.view]; - const CGFloat scale = [self screenIfViewLoaded].scale; + const CGFloat scale = [self flutterScreenIfViewLoaded].scale; translation.x *= scale; translation.y *= scale; @@ -2496,7 +2479,7 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) - (void)continuousScrollEvent:(UIPanGestureRecognizer*)recognizer API_AVAILABLE(ios(13.4)) { CGPoint translation = [recognizer translationInView:self.view]; - const CGFloat scale = [self screenIfViewLoaded].scale; + const CGFloat scale = [self flutterScreenIfViewLoaded].scale; flutter::PointerData pointer_data = [self generatePointerDataAtLastMouseLocation]; pointer_data.device = reinterpret_cast(recognizer); diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm index 8723a418d5..d642b2c4ce 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm @@ -16,6 +16,7 @@ #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterFakeKeyEvents.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h" +#import "flutter/shell/platform/darwin/ios/framework/Source/UIViewController+FlutterScreenAndSceneIfLoaded.h" #import "flutter/shell/platform/embedder/embedder.h" #import "flutter/third_party/spring_animation/spring_animation.h" @@ -129,7 +130,6 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; - (void)onUserSettingsChanged:(NSNotification*)notification; - (void)applicationWillTerminate:(NSNotification*)notification; - (void)goToApplicationLifecycle:(nonnull NSString*)state; -- (UIScreen*)screenIfViewLoaded; - (void)handleKeyboardNotification:(NSNotification*)notification; - (CGFloat)calculateKeyboardInset:(CGRect)keyboardFrame keyboardMode:(int)keyboardMode; - (BOOL)shouldIgnoreKeyboardNotification:(NSNotification*)notification; @@ -199,7 +199,7 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; screen:(UIScreen*)screen viewFrame:(CGRect)viewFrame convertedFrame:(CGRect)convertedFrame { - OCMStub([viewControllerMock screenIfViewLoaded]).andReturn(screen); + OCMStub([viewControllerMock flutterScreenIfViewLoaded]).andReturn(screen); id mockView = OCMClassMock([UIView class]); OCMStub([mockView frame]).andReturn(viewFrame); OCMStub([mockView convertRect:viewFrame toCoordinateSpace:[OCMArg any]]) @@ -680,7 +680,7 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; bundle:nil]; FlutterViewController* viewControllerMock = OCMPartialMock(viewController); UIScreen* screen = [self setUpMockScreen]; - OCMStub([viewControllerMock screenIfViewLoaded]).andReturn(screen); + OCMStub([viewControllerMock flutterScreenIfViewLoaded]).andReturn(screen); CGFloat screenWidth = screen.bounds.size.width; CGFloat screenHeight = screen.bounds.size.height; @@ -943,7 +943,7 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; bundle:nil]; FlutterViewController* viewControllerMock = OCMPartialMock(viewController); UIScreen* screen = [self setUpMockScreen]; - OCMStub([viewControllerMock screenIfViewLoaded]).andReturn(screen); + OCMStub([viewControllerMock flutterScreenIfViewLoaded]).andReturn(screen); mockEngine.viewController = viewController; id mockCoordinator = OCMProtocolMock(@protocol(UIViewControllerTransitionCoordinator)); @@ -965,7 +965,7 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; bundle:nil]; FlutterViewController* viewControllerMock = OCMPartialMock(viewController); UIScreen* screen = [self setUpMockScreen]; - OCMStub([viewControllerMock screenIfViewLoaded]).andReturn(screen); + OCMStub([viewControllerMock flutterScreenIfViewLoaded]).andReturn(screen); mockEngine.viewController = viewController; // Mimic the device rotation with non-zero transition duration. @@ -999,7 +999,7 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; bundle:nil]; FlutterViewController* viewControllerMock = OCMPartialMock(viewController); UIScreen* screen = [self setUpMockScreen]; - OCMStub([viewControllerMock screenIfViewLoaded]).andReturn(screen); + OCMStub([viewControllerMock flutterScreenIfViewLoaded]).andReturn(screen); mockEngine.viewController = viewController; // Mimic the device rotation with zero transition duration. @@ -1512,7 +1512,7 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; if (@available(iOS 16.0, *)) { mockWindowScene = OCMClassMock([UIWindowScene class]); mockVC = OCMPartialMock(realVC); - OCMStub([mockVC windowSceneIfViewLoaded]).andReturn(mockWindowScene); + OCMStub([mockVC flutterWindowSceneIfViewLoaded]).andReturn(mockWindowScene); if (realVC.supportedInterfaceOrientations == mask) { OCMReject([mockWindowScene requestGeometryUpdateWithPreferences:[OCMArg any] errorHandler:[OCMArg any]]); @@ -1540,7 +1540,7 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; if (@available(iOS 13.0, *)) { mockWindowScene = OCMClassMock([UIWindowScene class]); mockVC = OCMPartialMock(realVC); - OCMStub([mockVC windowSceneIfViewLoaded]).andReturn(mockWindowScene); + OCMStub([mockVC flutterWindowSceneIfViewLoaded]).andReturn(mockWindowScene); OCMStub(((UIWindowScene*)mockWindowScene).interfaceOrientation) .andReturn(currentOrientation); } else { diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h index 09cb7c4554..657c189dc1 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h @@ -65,7 +65,6 @@ typedef void (^FlutterKeyboardAnimationCallback)(fml::TimePoint); - (void)addInternalPlugins; - (void)deregisterNotifications; - (int32_t)accessibilityFlags; -- (UIWindowScene*)windowSceneIfViewLoaded API_AVAILABLE(ios(13.0)); @end diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/UIViewController+FlutterScreenAndSceneIfLoaded.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/UIViewController+FlutterScreenAndSceneIfLoaded.h new file mode 100644 index 0000000000..6ea4492870 --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/UIViewController+FlutterScreenAndSceneIfLoaded.h @@ -0,0 +1,21 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_UIVIEWCONTROLLER_FLUTTERSCREENANDSCENEIFLOADED_H_ +#define FLUTTER_UIVIEWCONTROLLER_FLUTTERSCREENANDSCENEIFLOADED_H_ + +#import + +@interface UIViewController (FlutterScreenAndSceneIfLoaded) + +/// Returns a UIWindowScene if the UIViewController's view is loaded, and nil otherwise. +- (UIWindowScene*)flutterWindowSceneIfViewLoaded API_AVAILABLE(ios(13.0)); + +/// Before iOS 13, returns the main screen; After iOS 13, returns the screen the UIViewController is +/// attached to if its view is loaded, and nil otherwise. +- (UIScreen*)flutterScreenIfViewLoaded; + +@end + +#endif // FLUTTER_UIVIEWCONTROLLER_FLUTTERSCREENANDSCENEIFLOADED_H_ diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/UIViewController+FlutterScreenAndSceneIfLoaded.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/UIViewController+FlutterScreenAndSceneIfLoaded.mm new file mode 100644 index 0000000000..5eb4f71103 --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/UIViewController+FlutterScreenAndSceneIfLoaded.mm @@ -0,0 +1,30 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "flutter/shell/platform/darwin/ios/framework/Source/UIViewController+FlutterScreenAndSceneIfLoaded.h" + +#include "flutter/fml/logging.h" + +@implementation UIViewController (FlutterScreenAndSceneIfLoaded) + +- (UIWindowScene*)flutterWindowSceneIfViewLoaded { + if (self.viewIfLoaded == nil) { + FML_LOG(WARNING) << "Trying to access the window scene before the view is loaded."; + return nil; + } + return self.viewIfLoaded.window.windowScene; +} + +- (UIScreen*)flutterScreenIfViewLoaded { + if (@available(iOS 13.0, *)) { + if (self.viewIfLoaded == nil) { + FML_LOG(WARNING) << "Trying to access the screen before the view is loaded."; + return nil; + } + return [self flutterWindowSceneIfViewLoaded].screen; + } + return UIScreen.mainScreen; +} + +@end diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/UIViewController_FlutterScreenAndSceneIfLoadedTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/UIViewController_FlutterScreenAndSceneIfLoadedTest.mm new file mode 100644 index 0000000000..c68e22da6b --- /dev/null +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/UIViewController_FlutterScreenAndSceneIfLoadedTest.mm @@ -0,0 +1,81 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import + +#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h" +#import "flutter/shell/platform/darwin/ios/framework/Source/UIViewController+FlutterScreenAndSceneIfLoaded.h" + +FLUTTER_ASSERT_ARC + +@interface UIViewController_FlutterViewAndSceneIfLoadedTest : XCTestCase +@end + +@implementation UIViewController_FlutterViewAndSceneIfLoadedTest + +- (void)testWindowSceneIfViewLoadedReturnsWindowSceneIfViewLoaded { + if (@available(iOS 13.0, *)) { + UIViewController* viewController = [[UIViewController alloc] initWithNibName:nil bundle:nil]; + + NSSet* scenes = UIApplication.sharedApplication.connectedScenes; + XCTAssertEqual(scenes.count, 1UL, @"There must only be 1 scene for test"); + UIScene* scene = scenes.anyObject; + XCTAssert([scene isKindOfClass:[UIWindowScene class]], @"Must be a window scene for test"); + UIWindowScene* windowScene = (UIWindowScene*)scene; + XCTAssert(windowScene.windows.count > 0, @"There must be at least 1 window for test"); + UIWindow* window = windowScene.windows[0]; + [window addSubview:viewController.view]; + + [viewController loadView]; + XCTAssertEqual(viewController.flutterWindowSceneIfViewLoaded, windowScene, + @"Must return the correct window scene when view loaded"); + } +} + +- (void)testWindowSceneIfViewLoadedReturnsNilIfViewNotLoaded { + if (@available(iOS 13.0, *)) { + UIViewController* viewController = [[UIViewController alloc] initWithNibName:nil bundle:nil]; + XCTAssertNil(viewController.flutterWindowSceneIfViewLoaded, + @"Must return nil window scene when view not loaded"); + } +} + +- (void)testScreenIfViewLoadedReturnsMainScreenBeforeIOS13 { + if (@available(iOS 13.0, *)) { + return; + } + + UIViewController* viewController = [[UIViewController alloc] initWithNibName:nil bundle:nil]; + XCTAssertEqual(viewController.flutterScreenIfViewLoaded, UIScreen.mainScreen, + @"Must return UIScreen.mainScreen before iOS 13"); +} + +- (void)testScreenIfViewLoadedReturnsScreenIfViewLoadedAfterIOS13 { + if (@available(iOS 13.0, *)) { + UIViewController* viewController = [[UIViewController alloc] initWithNibName:nil bundle:nil]; + + NSSet* scenes = UIApplication.sharedApplication.connectedScenes; + XCTAssertEqual(scenes.count, 1UL, @"There must only be 1 scene for test"); + UIScene* scene = scenes.anyObject; + XCTAssert([scene isKindOfClass:[UIWindowScene class]], @"Must be a window scene for test"); + UIWindowScene* windowScene = (UIWindowScene*)scene; + XCTAssert(windowScene.windows.count > 0, @"There must be at least 1 window for test"); + UIWindow* window = windowScene.windows[0]; + [window addSubview:viewController.view]; + + [viewController loadView]; + XCTAssertEqual(viewController.flutterScreenIfViewLoaded, windowScene.screen, + @"Must return the correct screen when view loaded"); + } +} + +- (void)testScreenIfViewLoadedReturnsNilIfViewNotLoadedAfterIOS13 { + if (@available(iOS 13.0, *)) { + UIViewController* viewController = [[UIViewController alloc] initWithNibName:nil bundle:nil]; + XCTAssertNil(viewController.flutterScreenIfViewLoaded, + @"Must return nil screen when view not loaded"); + } +} + +@end