From 351ef7a0cec66f69006cfc9920cee03f177d351b Mon Sep 17 00:00:00 2001 From: Ann Marie Mossman <233583+mossmana@users.noreply.github.com> Date: Mon, 24 Jul 2023 13:29:27 -0700 Subject: [PATCH] Replace deprecated [UIScreen mainScreen] in FlutterViewController.mm and FlutterViewControllerTest.mm (flutter/engine#43690) Issue: https://github.com/flutter/flutter/issues/128260 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide] and the [C++, Objective-C, Java style guides]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test-exempt. See [testing the engine] for instructions on writing and running engine tests. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I signed the [CLA]. - [x] All existing and new tests are passing. --- .../framework/Source/FlutterViewController.mm | 84 +++++++------ .../Source/FlutterViewControllerTest.mm | 119 ++++++++++++------ 2 files changed, 123 insertions(+), 80 deletions(-) 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 dbb7a7ab57..0cd16905d3 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 @@ -477,10 +477,30 @@ static UIView* GetViewOrPlaceholder(UIView* existing_view) { return pointer_data; } -static void SendFakeTouchEvent(FlutterEngine* engine, +- (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, flutter::PointerData::Change change) { - const CGFloat scale = [UIScreen mainScreen].scale; + const CGFloat scale = screen.scale; flutter::PointerData pointer_data = [[engine viewController] generatePointerDataForFake]; pointer_data.physical_x = location.x * scale; pointer_data.physical_y = location.y * scale; @@ -495,8 +515,11 @@ static void SendFakeTouchEvent(FlutterEngine* engine, return NO; } CGPoint statusBarPoint = CGPointZero; - SendFakeTouchEvent(_engine.get(), statusBarPoint, flutter::PointerData::Change::kDown); - SendFakeTouchEvent(_engine.get(), statusBarPoint, flutter::PointerData::Change::kUp); + UIScreen* screen = [self screenIfViewLoaded]; + if (screen) { + SendFakeTouchEvent(screen, _engine.get(), statusBarPoint, flutter::PointerData::Change::kDown); + SendFakeTouchEvent(screen, _engine.get(), statusBarPoint, flutter::PointerData::Change::kUp); + } return NO; } @@ -613,24 +636,6 @@ static void SendFakeTouchEvent(FlutterEngine* engine, return _keyboardSpringAnimation.get(); } -- (UIScreen*)mainScreenIfViewLoaded { - if (@available(iOS 13.0, *)) { - if (self.viewIfLoaded == nil) { - FML_LOG(WARNING) << "Trying to access the view before it is loaded."; - } - return [self windowSceneIfViewLoaded].screen; - } - return UIScreen.mainScreen; -} - -- (UIWindowScene*)windowSceneIfViewLoaded { - if (self.viewIfLoaded == nil) { - FML_LOG(WARNING) << "Trying to access the view before it is loaded."; - return nil; - } - return self.viewIfLoaded.window.windowScene; -} - - (BOOL)loadDefaultSplashScreenView { NSString* launchscreenName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UILaunchStoryboardName"]; @@ -1065,7 +1070,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 = [UIScreen mainScreen].scale; + const CGFloat scale = [self screenIfViewLoaded].scale; auto packet = std::make_unique(touches.count + touches_to_remove_count); @@ -1280,7 +1285,7 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) - (void)viewDidLayoutSubviews { CGRect viewBounds = self.view.bounds; - CGFloat scale = [UIScreen mainScreen].scale; + CGFloat scale = [self screenIfViewLoaded].scale; // Purposefully place this not visible. _scrollView.get().frame = CGRectMake(0.0, 0.0, viewBounds.size.width, 0.0); @@ -1327,12 +1332,12 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) // Set _viewportMetrics physical size. - (void)setViewportMetricsSize { - UIScreen* mainScreen = [self mainScreenIfViewLoaded]; - if (!mainScreen) { + UIScreen* screen = [self screenIfViewLoaded]; + if (!screen) { return; } - CGFloat scale = mainScreen.scale; + CGFloat scale = screen.scale; _viewportMetrics.physical_width = self.view.bounds.size.width * scale; _viewportMetrics.physical_height = self.view.bounds.size.height * scale; } @@ -1341,12 +1346,12 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) // // Viewport paddings represent the iOS safe area insets. - (void)setViewportMetricsPaddings { - UIScreen* mainScreen = [self mainScreenIfViewLoaded]; - if (!mainScreen) { + UIScreen* screen = [self screenIfViewLoaded]; + if (!screen) { return; } - CGFloat scale = mainScreen.scale; + CGFloat scale = screen.scale; _viewportMetrics.physical_padding_top = self.view.safeAreaInsets.top * scale; _viewportMetrics.physical_padding_left = self.view.safeAreaInsets.left * scale; _viewportMetrics.physical_padding_right = self.view.safeAreaInsets.right * scale; @@ -1507,7 +1512,7 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) return FlutterKeyboardModeHidden; } - CGRect screenRect = [self mainScreenIfViewLoaded].bounds; + CGRect screenRect = [self screenIfViewLoaded].bounds; CGRect adjustedKeyboardFrame = keyboardFrame; adjustedKeyboardFrame.origin.y += [self calculateMultitaskingAdjustment:screenRect keyboardFrame:keyboardFrame]; @@ -1547,7 +1552,7 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) } CGRect viewRectRelativeToScreen = [self.viewIfLoaded convertRect:self.viewIfLoaded.frame - toCoordinateSpace:[self mainScreenIfViewLoaded].coordinateSpace]; + toCoordinateSpace:[self screenIfViewLoaded].coordinateSpace]; CGFloat viewBottom = CGRectGetMaxY(viewRectRelativeToScreen); CGFloat offset = screenHeight - viewBottom; if (offset > 0) { @@ -1563,14 +1568,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 mainScreenIfViewLoaded].coordinateSpace]; + toCoordinateSpace:[self screenIfViewLoaded].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 mainScreenIfViewLoaded].scale; + CGFloat scale = [self screenIfViewLoaded].scale; return portionOfKeyboardInView * scale; } return 0; @@ -1869,9 +1874,8 @@ static flutter::PointerData::DeviceKind DeviceKindFromTouchType(UITouch* touch) 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."; + FML_LOG(WARNING) + << "Accessing the interface orientation when the window scene is unavailable."; return; } currentInterfaceOrientation = 1 << windowScene.interfaceOrientation; @@ -2291,7 +2295,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 = [UIScreen mainScreen].scale; + CGFloat scale = [self screenIfViewLoaded].scale; CGPoint oldLocation = _mouseState.location; _mouseState.location = {location.x * scale, location.y * scale}; @@ -2348,7 +2352,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 = [UIScreen mainScreen].scale; + const CGFloat scale = [self screenIfViewLoaded].scale; translation.x *= scale; translation.y *= scale; @@ -2377,7 +2381,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 = [UIScreen mainScreen].scale; + const CGFloat scale = [self screenIfViewLoaded].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 982db2d986..63e3658700 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 @@ -129,7 +129,7 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; - (void)onUserSettingsChanged:(NSNotification*)notification; - (void)applicationWillTerminate:(NSNotification*)notification; - (void)goToApplicationLifecycle:(nonnull NSString*)state; -- (UIScreen*)mainScreenIfViewLoaded; +- (UIScreen*)screenIfViewLoaded; - (void)handleKeyboardNotification:(NSNotification*)notification; - (CGFloat)calculateKeyboardInset:(CGRect)keyboardFrame keyboardMode:(int)keyboardMode; - (BOOL)shouldIgnoreKeyboardNotification:(NSNotification*)notification; @@ -174,10 +174,22 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; self.messageSent = nil; } -- (id)setUpMockMainScreenAndView:(FlutterViewController*)viewControllerMock - viewFrame:(CGRect)viewFrame - convertedFrame:(CGRect)convertedFrame { - OCMStub([viewControllerMock mainScreenIfViewLoaded]).andReturn(UIScreen.mainScreen); +- (id)setUpMockScreen { + UIScreen* mockScreen = OCMClassMock([UIScreen class]); + // iPhone 14 pixels + CGRect screenBounds = CGRectMake(0, 0, 1170, 2532); + OCMStub([mockScreen bounds]).andReturn(screenBounds); + CGFloat screenScale = 1; + OCMStub([mockScreen scale]).andReturn(screenScale); + + return mockScreen; +} + +- (id)setUpMockView:(FlutterViewController*)viewControllerMock + screen:(UIScreen*)screen + viewFrame:(CGRect)viewFrame + convertedFrame:(CGRect)convertedFrame { + OCMStub([viewControllerMock screenIfViewLoaded]).andReturn(screen); id mockView = OCMClassMock([UIView class]); OCMStub([mockView frame]).andReturn(viewFrame); OCMStub([mockView convertRect:viewFrame toCoordinateSpace:[OCMArg any]]) @@ -222,8 +234,12 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; nibName:nil bundle:nil]; FlutterViewController* viewControllerMock = OCMPartialMock(viewController); - CGRect viewFrame = UIScreen.mainScreen.bounds; - [self setUpMockMainScreenAndView:viewControllerMock viewFrame:viewFrame convertedFrame:viewFrame]; + UIScreen* screen = [self setUpMockScreen]; + CGRect viewFrame = screen.bounds; + [self setUpMockView:viewControllerMock + screen:screen + viewFrame:viewFrame + convertedFrame:viewFrame]; // Null check. [viewControllerMock setUpKeyboardSpringAnimationIfNeeded:nil]; @@ -261,12 +277,16 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; nibName:nil bundle:nil]; FlutterViewController* viewControllerMock = OCMPartialMock(viewController); - CGRect viewFrame = UIScreen.mainScreen.bounds; - [self setUpMockMainScreenAndView:viewControllerMock viewFrame:viewFrame convertedFrame:viewFrame]; + UIScreen* screen = [self setUpMockScreen]; + CGRect viewFrame = screen.bounds; + [self setUpMockView:viewControllerMock + screen:screen + viewFrame:viewFrame + convertedFrame:viewFrame]; BOOL isLocal = YES; - CGFloat screenHeight = UIScreen.mainScreen.bounds.size.height; - CGFloat screenWidth = UIScreen.mainScreen.bounds.size.height; + CGFloat screenHeight = screen.bounds.size.height; + CGFloat screenWidth = screen.bounds.size.height; // Start show keyboard animation. CGRect initialShowKeyboardBeginFrame = CGRectMake(0, screenHeight, screenWidth, 250); @@ -347,11 +367,15 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; nibName:nil bundle:nil]; FlutterViewController* viewControllerMock = OCMPartialMock(viewController); - CGRect viewFrame = UIScreen.mainScreen.bounds; - [self setUpMockMainScreenAndView:viewControllerMock viewFrame:viewFrame convertedFrame:viewFrame]; + UIScreen* screen = [self setUpMockScreen]; + CGRect viewFrame = screen.bounds; + [self setUpMockView:viewControllerMock + screen:screen + viewFrame:viewFrame + convertedFrame:viewFrame]; - CGFloat screenWidth = UIScreen.mainScreen.bounds.size.width; - CGFloat screenHeight = UIScreen.mainScreen.bounds.size.height; + CGFloat screenWidth = screen.bounds.size.width; + CGFloat screenHeight = screen.bounds.size.height; CGRect emptyKeyboard = CGRectZero; CGRect zeroHeightKeyboard = CGRectMake(0, 0, screenWidth, 0); CGRect validKeyboardEndFrame = CGRectMake(0, screenHeight - 320, screenWidth, 320); @@ -487,11 +511,15 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; bundle:nil]; FlutterViewController* viewControllerMock = OCMPartialMock(viewController); - CGRect viewFrame = UIScreen.mainScreen.bounds; - [self setUpMockMainScreenAndView:viewControllerMock viewFrame:viewFrame convertedFrame:viewFrame]; + UIScreen* screen = [self setUpMockScreen]; + CGRect viewFrame = screen.bounds; + [self setUpMockView:viewControllerMock + screen:screen + viewFrame:viewFrame + convertedFrame:viewFrame]; - CGFloat screenWidth = UIScreen.mainScreen.bounds.size.width; - CGFloat screenHeight = UIScreen.mainScreen.bounds.size.height; + CGFloat screenWidth = screen.bounds.size.width; + CGFloat screenHeight = screen.bounds.size.height; // hide notification CGRect keyboardFrame = CGRectZero; @@ -612,15 +640,17 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; bundle:nil]; FlutterViewController* viewControllerMock = OCMPartialMock(viewController); - CGFloat screenWidth = UIScreen.mainScreen.bounds.size.width; - CGFloat screenHeight = UIScreen.mainScreen.bounds.size.height; - CGRect screenRect = UIScreen.mainScreen.bounds; + UIScreen* screen = [self setUpMockScreen]; + CGFloat screenWidth = screen.bounds.size.width; + CGFloat screenHeight = screen.bounds.size.height; + CGRect screenRect = screen.bounds; CGRect viewOrigFrame = CGRectMake(0, 0, 320, screenHeight - 40); CGRect convertedViewFrame = CGRectMake(20, 20, 320, screenHeight - 40); CGRect keyboardFrame = CGRectMake(20, screenHeight - 320, screenWidth, 300); - id mockView = [self setUpMockMainScreenAndView:viewControllerMock - viewFrame:viewOrigFrame - convertedFrame:convertedViewFrame]; + id mockView = [self setUpMockView:viewControllerMock + screen:screen + viewFrame:viewOrigFrame + convertedFrame:convertedViewFrame]; id mockTraitCollection = OCMClassMock([UITraitCollection class]); OCMStub([mockTraitCollection userInterfaceIdiom]).andReturn(UIUserInterfaceIdiomPad); OCMStub([mockTraitCollection horizontalSizeClass]).andReturn(UIUserInterfaceSizeClassCompact); @@ -639,21 +669,23 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; nibName:nil bundle:nil]; FlutterViewController* viewControllerMock = OCMPartialMock(viewController); - OCMStub([viewControllerMock mainScreenIfViewLoaded]).andReturn(UIScreen.mainScreen); + UIScreen* screen = [self setUpMockScreen]; + OCMStub([viewControllerMock screenIfViewLoaded]).andReturn(screen); - CGFloat screenWidth = UIScreen.mainScreen.bounds.size.width; - CGFloat screenHeight = UIScreen.mainScreen.bounds.size.height; + CGFloat screenWidth = screen.bounds.size.width; + CGFloat screenHeight = screen.bounds.size.height; CGRect viewOrigFrame = CGRectMake(0, 0, 320, screenHeight - 40); CGRect convertedViewFrame = CGRectMake(20, 20, 320, screenHeight - 40); CGRect keyboardFrame = CGRectMake(20, screenHeight - 320, screenWidth, 300); - [self setUpMockMainScreenAndView:viewControllerMock - viewFrame:viewOrigFrame - convertedFrame:convertedViewFrame]; + [self setUpMockView:viewControllerMock + screen:screen + viewFrame:viewOrigFrame + convertedFrame:convertedViewFrame]; CGFloat inset = [viewControllerMock calculateKeyboardInset:keyboardFrame keyboardMode:FlutterKeyboardModeDocked]; - XCTAssertTrue(inset == 300 * UIScreen.mainScreen.scale); + XCTAssertTrue(inset == 300 * screen.scale); } - (void)testHandleKeyboardNotification { @@ -663,10 +695,11 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; nibName:nil bundle:nil]; // keyboard is empty - CGFloat screenWidth = UIScreen.mainScreen.bounds.size.width; - CGFloat screenHeight = UIScreen.mainScreen.bounds.size.height; + UIScreen* screen = [self setUpMockScreen]; + CGFloat screenWidth = screen.bounds.size.width; + CGFloat screenHeight = screen.bounds.size.height; CGRect keyboardFrame = CGRectMake(0, screenHeight - 320, screenWidth, 320); - CGRect viewFrame = UIScreen.mainScreen.bounds; + CGRect viewFrame = screen.bounds; BOOL isLocal = YES; NSNotification* notification = [NSNotification notificationWithName:UIKeyboardWillShowNotification @@ -677,7 +710,10 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; @"UIKeyboardIsLocalUserInfoKey" : @(isLocal) }]; FlutterViewController* viewControllerMock = OCMPartialMock(viewController); - [self setUpMockMainScreenAndView:viewControllerMock viewFrame:viewFrame convertedFrame:viewFrame]; + [self setUpMockView:viewControllerMock + screen:screen + viewFrame:viewFrame + convertedFrame:viewFrame]; viewControllerMock.targetViewInsetBottom = 0; XCTestExpectation* expectation = [self expectationWithDescription:@"update viewport"]; OCMStub([viewControllerMock updateViewportMetricsIfNeeded]).andDo(^(NSInvocation* invocation) { @@ -685,7 +721,7 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; }); [viewControllerMock handleKeyboardNotification:notification]; - XCTAssertTrue(viewControllerMock.targetViewInsetBottom == 320 * UIScreen.mainScreen.scale); + XCTAssertTrue(viewControllerMock.targetViewInsetBottom == 320 * screen.scale); OCMVerify([viewControllerMock startKeyBoardAnimation:0.25]); [self waitForExpectationsWithTimeout:5.0 handler:nil]; } @@ -896,7 +932,8 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; nibName:nil bundle:nil]; FlutterViewController* viewControllerMock = OCMPartialMock(viewController); - OCMStub([viewControllerMock mainScreenIfViewLoaded]).andReturn(UIScreen.mainScreen); + UIScreen* screen = [self setUpMockScreen]; + OCMStub([viewControllerMock screenIfViewLoaded]).andReturn(screen); mockEngine.viewController = viewController; id mockCoordinator = OCMProtocolMock(@protocol(UIViewControllerTransitionCoordinator)); @@ -917,7 +954,8 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; nibName:nil bundle:nil]; FlutterViewController* viewControllerMock = OCMPartialMock(viewController); - OCMStub([viewControllerMock mainScreenIfViewLoaded]).andReturn(UIScreen.mainScreen); + UIScreen* screen = [self setUpMockScreen]; + OCMStub([viewControllerMock screenIfViewLoaded]).andReturn(screen); mockEngine.viewController = viewController; // Mimic the device rotation with non-zero transition duration. @@ -950,7 +988,8 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; nibName:nil bundle:nil]; FlutterViewController* viewControllerMock = OCMPartialMock(viewController); - OCMStub([viewControllerMock mainScreenIfViewLoaded]).andReturn(UIScreen.mainScreen); + UIScreen* screen = [self setUpMockScreen]; + OCMStub([viewControllerMock screenIfViewLoaded]).andReturn(screen); mockEngine.viewController = viewController; // Mimic the device rotation with zero transition duration.