@@ -477,10 +477,30 @@ - (void)loadView {
477477 return pointer_data;
478478}
479479
480- static void SendFakeTouchEvent (FlutterEngine* engine,
480+ - (UIWindowScene*)windowSceneIfViewLoaded {
481+ if (self.viewIfLoaded == nil ) {
482+ FML_LOG (WARNING) << " Trying to access the window scene before the view is loaded." ;
483+ return nil ;
484+ }
485+ return self.viewIfLoaded .window .windowScene ;
486+ }
487+
488+ - (UIScreen*)screenIfViewLoaded {
489+ if (@available (iOS 13.0 , *)) {
490+ if (self.viewIfLoaded == nil ) {
491+ FML_LOG (WARNING) << " Trying to access the screen before the view is loaded." ;
492+ return nil ;
493+ }
494+ return [self windowSceneIfViewLoaded ].screen ;
495+ }
496+ return UIScreen.mainScreen ;
497+ }
498+
499+ static void SendFakeTouchEvent (UIScreen* screen,
500+ FlutterEngine* engine,
481501 CGPoint location,
482502 flutter::PointerData::Change change) {
483- const CGFloat scale = [UIScreen mainScreen ] .scale ;
503+ const CGFloat scale = screen .scale ;
484504 flutter::PointerData pointer_data = [[engine viewController ] generatePointerDataForFake ];
485505 pointer_data.physical_x = location.x * scale;
486506 pointer_data.physical_y = location.y * scale;
@@ -495,8 +515,11 @@ - (BOOL)scrollViewShouldScrollToTop:(UIScrollView*)scrollView {
495515 return NO ;
496516 }
497517 CGPoint statusBarPoint = CGPointZero;
498- SendFakeTouchEvent (_engine.get (), statusBarPoint, flutter::PointerData::Change::kDown );
499- SendFakeTouchEvent (_engine.get (), statusBarPoint, flutter::PointerData::Change::kUp );
518+ UIScreen* screen = [self screenIfViewLoaded ];
519+ if (screen) {
520+ SendFakeTouchEvent (screen, _engine.get (), statusBarPoint, flutter::PointerData::Change::kDown );
521+ SendFakeTouchEvent (screen, _engine.get (), statusBarPoint, flutter::PointerData::Change::kUp );
522+ }
500523 return NO ;
501524}
502525
@@ -613,24 +636,6 @@ - (SpringAnimation*)keyboardSpringAnimation {
613636 return _keyboardSpringAnimation.get ();
614637}
615638
616- - (UIScreen*)mainScreenIfViewLoaded {
617- if (@available (iOS 13.0 , *)) {
618- if (self.viewIfLoaded == nil ) {
619- FML_LOG (WARNING) << " Trying to access the view before it is loaded." ;
620- }
621- return [self windowSceneIfViewLoaded ].screen ;
622- }
623- return UIScreen.mainScreen ;
624- }
625-
626- - (UIWindowScene*)windowSceneIfViewLoaded {
627- if (self.viewIfLoaded == nil ) {
628- FML_LOG (WARNING) << " Trying to access the view before it is loaded." ;
629- return nil ;
630- }
631- return self.viewIfLoaded .window .windowScene ;
632- }
633-
634639- (BOOL )loadDefaultSplashScreenView {
635640 NSString * launchscreenName =
636641 [[[NSBundle mainBundle ] infoDictionary ] objectForKey: @" UILaunchStoryboardName" ];
@@ -1065,7 +1070,7 @@ - (void)dispatchTouches:(NSSet*)touches
10651070 // Activate or pause the correction of delivery frame rate of touch events.
10661071 [self triggerTouchRateCorrectionIfNeeded: touches];
10671072
1068- const CGFloat scale = [UIScreen mainScreen ].scale ;
1073+ const CGFloat scale = [self screenIfViewLoaded ].scale ;
10691074 auto packet =
10701075 std::make_unique<flutter::PointerDataPacket>(touches.count + touches_to_remove_count);
10711076
@@ -1280,7 +1285,7 @@ - (void)updateViewportMetricsIfNeeded {
12801285
12811286- (void )viewDidLayoutSubviews {
12821287 CGRect viewBounds = self.view .bounds ;
1283- CGFloat scale = [UIScreen mainScreen ].scale ;
1288+ CGFloat scale = [self screenIfViewLoaded ].scale ;
12841289
12851290 // Purposefully place this not visible.
12861291 _scrollView.get ().frame = CGRectMake (0.0 , 0.0 , viewBounds.size .width , 0.0 );
@@ -1327,12 +1332,12 @@ - (void)viewSafeAreaInsetsDidChange {
13271332
13281333// Set _viewportMetrics physical size.
13291334- (void )setViewportMetricsSize {
1330- UIScreen* mainScreen = [self mainScreenIfViewLoaded ];
1331- if (!mainScreen ) {
1335+ UIScreen* screen = [self screenIfViewLoaded ];
1336+ if (!screen ) {
13321337 return ;
13331338 }
13341339
1335- CGFloat scale = mainScreen .scale ;
1340+ CGFloat scale = screen .scale ;
13361341 _viewportMetrics.physical_width = self.view .bounds .size .width * scale;
13371342 _viewportMetrics.physical_height = self.view .bounds .size .height * scale;
13381343}
@@ -1341,12 +1346,12 @@ - (void)setViewportMetricsSize {
13411346//
13421347// Viewport paddings represent the iOS safe area insets.
13431348- (void )setViewportMetricsPaddings {
1344- UIScreen* mainScreen = [self mainScreenIfViewLoaded ];
1345- if (!mainScreen ) {
1349+ UIScreen* screen = [self screenIfViewLoaded ];
1350+ if (!screen ) {
13461351 return ;
13471352 }
13481353
1349- CGFloat scale = mainScreen .scale ;
1354+ CGFloat scale = screen .scale ;
13501355 _viewportMetrics.physical_padding_top = self.view .safeAreaInsets .top * scale;
13511356 _viewportMetrics.physical_padding_left = self.view .safeAreaInsets .left * scale;
13521357 _viewportMetrics.physical_padding_right = self.view .safeAreaInsets .right * scale;
@@ -1507,7 +1512,7 @@ - (FlutterKeyboardMode)calculateKeyboardAttachMode:(NSNotification*)notification
15071512 return FlutterKeyboardModeHidden;
15081513 }
15091514
1510- CGRect screenRect = [self mainScreenIfViewLoaded ].bounds ;
1515+ CGRect screenRect = [self screenIfViewLoaded ].bounds ;
15111516 CGRect adjustedKeyboardFrame = keyboardFrame;
15121517 adjustedKeyboardFrame.origin .y += [self calculateMultitaskingAdjustment: screenRect
15131518 keyboardFrame: keyboardFrame];
@@ -1547,7 +1552,7 @@ - (CGFloat)calculateMultitaskingAdjustment:(CGRect)screenRect keyboardFrame:(CGR
15471552 }
15481553 CGRect viewRectRelativeToScreen =
15491554 [self .viewIfLoaded convertRect: self .viewIfLoaded.frame
1550- toCoordinateSpace: [self mainScreenIfViewLoaded ].coordinateSpace];
1555+ toCoordinateSpace: [self screenIfViewLoaded ].coordinateSpace];
15511556 CGFloat viewBottom = CGRectGetMaxY (viewRectRelativeToScreen);
15521557 CGFloat offset = screenHeight - viewBottom;
15531558 if (offset > 0 ) {
@@ -1563,14 +1568,14 @@ - (CGFloat)calculateKeyboardInset:(CGRect)keyboardFrame keyboardMode:(NSInteger)
15631568 // Calculate how much of the keyboard intersects with the view.
15641569 CGRect viewRectRelativeToScreen =
15651570 [self .viewIfLoaded convertRect: self .viewIfLoaded.frame
1566- toCoordinateSpace: [self mainScreenIfViewLoaded ].coordinateSpace];
1571+ toCoordinateSpace: [self screenIfViewLoaded ].coordinateSpace];
15671572 CGRect intersection = CGRectIntersection (keyboardFrame, viewRectRelativeToScreen);
15681573 CGFloat portionOfKeyboardInView = CGRectGetHeight (intersection);
15691574
15701575 // The keyboard is treated as an inset since we want to effectively reduce the window size by
15711576 // the keyboard height. The Dart side will compute a value accounting for the keyboard-consuming
15721577 // bottom padding.
1573- CGFloat scale = [self mainScreenIfViewLoaded ].scale ;
1578+ CGFloat scale = [self screenIfViewLoaded ].scale ;
15741579 return portionOfKeyboardInView * scale;
15751580 }
15761581 return 0 ;
@@ -1869,9 +1874,8 @@ - (void)performOrientationUpdate:(UIInterfaceOrientationMask)new_preferences {
18691874 if (@available (iOS 13.0 , *)) {
18701875 UIWindowScene* windowScene = [self windowSceneIfViewLoaded ];
18711876 if (!windowScene) {
1872- // When the view is not loaded, it does not make sense to access the interface
1873- // orientation, bail.
1874- FML_LOG (WARNING) << " Trying to access the window scene before the view is loaded." ;
1877+ FML_LOG (WARNING)
1878+ << " Accessing the interface orientation when the window scene is unavailable." ;
18751879 return ;
18761880 }
18771881 currentInterfaceOrientation = 1 << windowScene.interfaceOrientation ;
@@ -2291,7 +2295,7 @@ - (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer
22912295
22922296- (void )hoverEvent : (UIPanGestureRecognizer*)recognizer API_AVAILABLE(ios(13.4 )) {
22932297 CGPoint location = [recognizer locationInView: self .view];
2294- CGFloat scale = [UIScreen mainScreen ].scale ;
2298+ CGFloat scale = [self screenIfViewLoaded ].scale ;
22952299 CGPoint oldLocation = _mouseState.location ;
22962300 _mouseState.location = {location.x * scale, location.y * scale};
22972301
@@ -2348,7 +2352,7 @@ - (void)hoverEvent:(UIPanGestureRecognizer*)recognizer API_AVAILABLE(ios(13.4))
23482352
23492353- (void )discreteScrollEvent : (UIPanGestureRecognizer*)recognizer API_AVAILABLE(ios(13.4 )) {
23502354 CGPoint translation = [recognizer translationInView: self .view];
2351- const CGFloat scale = [UIScreen mainScreen ].scale ;
2355+ const CGFloat scale = [self screenIfViewLoaded ].scale ;
23522356
23532357 translation.x *= scale;
23542358 translation.y *= scale;
@@ -2377,7 +2381,7 @@ - (void)discreteScrollEvent:(UIPanGestureRecognizer*)recognizer API_AVAILABLE(io
23772381
23782382- (void )continuousScrollEvent : (UIPanGestureRecognizer*)recognizer API_AVAILABLE(ios(13.4 )) {
23792383 CGPoint translation = [recognizer translationInView: self .view];
2380- const CGFloat scale = [UIScreen mainScreen ].scale ;
2384+ const CGFloat scale = [self screenIfViewLoaded ].scale ;
23812385
23822386 flutter::PointerData pointer_data = [self generatePointerDataAtLastMouseLocation ];
23832387 pointer_data.device = reinterpret_cast <int64_t >(recognizer);
0 commit comments