@@ -477,10 +477,30 @@ - (void)loadView {
477
477
return pointer_data;
478
478
}
479
479
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,
481
501
CGPoint location,
482
502
flutter::PointerData::Change change) {
483
- const CGFloat scale = [UIScreen mainScreen ] .scale ;
503
+ const CGFloat scale = screen .scale ;
484
504
flutter::PointerData pointer_data = [[engine viewController ] generatePointerDataForFake ];
485
505
pointer_data.physical_x = location.x * scale;
486
506
pointer_data.physical_y = location.y * scale;
@@ -495,8 +515,11 @@ - (BOOL)scrollViewShouldScrollToTop:(UIScrollView*)scrollView {
495
515
return NO ;
496
516
}
497
517
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
+ }
500
523
return NO ;
501
524
}
502
525
@@ -613,24 +636,6 @@ - (SpringAnimation*)keyboardSpringAnimation {
613
636
return _keyboardSpringAnimation.get ();
614
637
}
615
638
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
-
634
639
- (BOOL )loadDefaultSplashScreenView {
635
640
NSString * launchscreenName =
636
641
[[[NSBundle mainBundle ] infoDictionary ] objectForKey: @" UILaunchStoryboardName" ];
@@ -1065,7 +1070,7 @@ - (void)dispatchTouches:(NSSet*)touches
1065
1070
// Activate or pause the correction of delivery frame rate of touch events.
1066
1071
[self triggerTouchRateCorrectionIfNeeded: touches];
1067
1072
1068
- const CGFloat scale = [UIScreen mainScreen ].scale ;
1073
+ const CGFloat scale = [self screenIfViewLoaded ].scale ;
1069
1074
auto packet =
1070
1075
std::make_unique<flutter::PointerDataPacket>(touches.count + touches_to_remove_count);
1071
1076
@@ -1280,7 +1285,7 @@ - (void)updateViewportMetricsIfNeeded {
1280
1285
1281
1286
- (void )viewDidLayoutSubviews {
1282
1287
CGRect viewBounds = self.view .bounds ;
1283
- CGFloat scale = [UIScreen mainScreen ].scale ;
1288
+ CGFloat scale = [self screenIfViewLoaded ].scale ;
1284
1289
1285
1290
// Purposefully place this not visible.
1286
1291
_scrollView.get ().frame = CGRectMake (0.0 , 0.0 , viewBounds.size .width , 0.0 );
@@ -1327,12 +1332,12 @@ - (void)viewSafeAreaInsetsDidChange {
1327
1332
1328
1333
// Set _viewportMetrics physical size.
1329
1334
- (void )setViewportMetricsSize {
1330
- UIScreen* mainScreen = [self mainScreenIfViewLoaded ];
1331
- if (!mainScreen ) {
1335
+ UIScreen* screen = [self screenIfViewLoaded ];
1336
+ if (!screen ) {
1332
1337
return ;
1333
1338
}
1334
1339
1335
- CGFloat scale = mainScreen .scale ;
1340
+ CGFloat scale = screen .scale ;
1336
1341
_viewportMetrics.physical_width = self.view .bounds .size .width * scale;
1337
1342
_viewportMetrics.physical_height = self.view .bounds .size .height * scale;
1338
1343
}
@@ -1341,12 +1346,12 @@ - (void)setViewportMetricsSize {
1341
1346
//
1342
1347
// Viewport paddings represent the iOS safe area insets.
1343
1348
- (void )setViewportMetricsPaddings {
1344
- UIScreen* mainScreen = [self mainScreenIfViewLoaded ];
1345
- if (!mainScreen ) {
1349
+ UIScreen* screen = [self screenIfViewLoaded ];
1350
+ if (!screen ) {
1346
1351
return ;
1347
1352
}
1348
1353
1349
- CGFloat scale = mainScreen .scale ;
1354
+ CGFloat scale = screen .scale ;
1350
1355
_viewportMetrics.physical_padding_top = self.view .safeAreaInsets .top * scale;
1351
1356
_viewportMetrics.physical_padding_left = self.view .safeAreaInsets .left * scale;
1352
1357
_viewportMetrics.physical_padding_right = self.view .safeAreaInsets .right * scale;
@@ -1507,7 +1512,7 @@ - (FlutterKeyboardMode)calculateKeyboardAttachMode:(NSNotification*)notification
1507
1512
return FlutterKeyboardModeHidden;
1508
1513
}
1509
1514
1510
- CGRect screenRect = [self mainScreenIfViewLoaded ].bounds ;
1515
+ CGRect screenRect = [self screenIfViewLoaded ].bounds ;
1511
1516
CGRect adjustedKeyboardFrame = keyboardFrame;
1512
1517
adjustedKeyboardFrame.origin .y += [self calculateMultitaskingAdjustment: screenRect
1513
1518
keyboardFrame: keyboardFrame];
@@ -1547,7 +1552,7 @@ - (CGFloat)calculateMultitaskingAdjustment:(CGRect)screenRect keyboardFrame:(CGR
1547
1552
}
1548
1553
CGRect viewRectRelativeToScreen =
1549
1554
[self .viewIfLoaded convertRect: self .viewIfLoaded.frame
1550
- toCoordinateSpace: [self mainScreenIfViewLoaded ].coordinateSpace];
1555
+ toCoordinateSpace: [self screenIfViewLoaded ].coordinateSpace];
1551
1556
CGFloat viewBottom = CGRectGetMaxY (viewRectRelativeToScreen);
1552
1557
CGFloat offset = screenHeight - viewBottom;
1553
1558
if (offset > 0 ) {
@@ -1563,14 +1568,14 @@ - (CGFloat)calculateKeyboardInset:(CGRect)keyboardFrame keyboardMode:(NSInteger)
1563
1568
// Calculate how much of the keyboard intersects with the view.
1564
1569
CGRect viewRectRelativeToScreen =
1565
1570
[self .viewIfLoaded convertRect: self .viewIfLoaded.frame
1566
- toCoordinateSpace: [self mainScreenIfViewLoaded ].coordinateSpace];
1571
+ toCoordinateSpace: [self screenIfViewLoaded ].coordinateSpace];
1567
1572
CGRect intersection = CGRectIntersection (keyboardFrame, viewRectRelativeToScreen);
1568
1573
CGFloat portionOfKeyboardInView = CGRectGetHeight (intersection);
1569
1574
1570
1575
// The keyboard is treated as an inset since we want to effectively reduce the window size by
1571
1576
// the keyboard height. The Dart side will compute a value accounting for the keyboard-consuming
1572
1577
// bottom padding.
1573
- CGFloat scale = [self mainScreenIfViewLoaded ].scale ;
1578
+ CGFloat scale = [self screenIfViewLoaded ].scale ;
1574
1579
return portionOfKeyboardInView * scale;
1575
1580
}
1576
1581
return 0 ;
@@ -1869,9 +1874,8 @@ - (void)performOrientationUpdate:(UIInterfaceOrientationMask)new_preferences {
1869
1874
if (@available (iOS 13.0 , *)) {
1870
1875
UIWindowScene* windowScene = [self windowSceneIfViewLoaded ];
1871
1876
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." ;
1875
1879
return ;
1876
1880
}
1877
1881
currentInterfaceOrientation = 1 << windowScene.interfaceOrientation ;
@@ -2291,7 +2295,7 @@ - (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer
2291
2295
2292
2296
- (void )hoverEvent : (UIPanGestureRecognizer*)recognizer API_AVAILABLE(ios(13.4 )) {
2293
2297
CGPoint location = [recognizer locationInView: self .view];
2294
- CGFloat scale = [UIScreen mainScreen ].scale ;
2298
+ CGFloat scale = [self screenIfViewLoaded ].scale ;
2295
2299
CGPoint oldLocation = _mouseState.location ;
2296
2300
_mouseState.location = {location.x * scale, location.y * scale};
2297
2301
@@ -2348,7 +2352,7 @@ - (void)hoverEvent:(UIPanGestureRecognizer*)recognizer API_AVAILABLE(ios(13.4))
2348
2352
2349
2353
- (void )discreteScrollEvent : (UIPanGestureRecognizer*)recognizer API_AVAILABLE(ios(13.4 )) {
2350
2354
CGPoint translation = [recognizer translationInView: self .view];
2351
- const CGFloat scale = [UIScreen mainScreen ].scale ;
2355
+ const CGFloat scale = [self screenIfViewLoaded ].scale ;
2352
2356
2353
2357
translation.x *= scale;
2354
2358
translation.y *= scale;
@@ -2377,7 +2381,7 @@ - (void)discreteScrollEvent:(UIPanGestureRecognizer*)recognizer API_AVAILABLE(io
2377
2381
2378
2382
- (void )continuousScrollEvent : (UIPanGestureRecognizer*)recognizer API_AVAILABLE(ios(13.4 )) {
2379
2383
CGPoint translation = [recognizer translationInView: self .view];
2380
- const CGFloat scale = [UIScreen mainScreen ].scale ;
2384
+ const CGFloat scale = [self screenIfViewLoaded ].scale ;
2381
2385
2382
2386
flutter::PointerData pointer_data = [self generatePointerDataAtLastMouseLocation ];
2383
2387
pointer_data.device = reinterpret_cast <int64_t >(recognizer);
0 commit comments