[ios]make the screenIfViewLoaded and windowSceneIfLoaded helpers reusable (flutter/engine#44303)
The existing `screenIfViewLoaded` and `windowSceneIfLoaded` functions are private helpers of `FlutterViewController` class. This PR moves the logic to a category of the `UIViewController`, so it can be reused for any `UIViewController`s. *List which issues are fixed by this PR. You must list at least one issue.* https://github.com/flutter/engine/pull/43972#discussion_r1282214557 *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 = [
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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.";
|
||||
|
||||
@@ -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<flutter::PointerDataPacket>(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<UIScene*>* 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<int64_t>(recognizer);
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -65,7 +65,6 @@ typedef void (^FlutterKeyboardAnimationCallback)(fml::TimePoint);
|
||||
- (void)addInternalPlugins;
|
||||
- (void)deregisterNotifications;
|
||||
- (int32_t)accessibilityFlags;
|
||||
- (UIWindowScene*)windowSceneIfViewLoaded API_AVAILABLE(ios(13.0));
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@@ -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 <UIKit/UIKit.h>
|
||||
|
||||
@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_
|
||||
@@ -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
|
||||
@@ -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 <XCTest/XCTest.h>
|
||||
|
||||
#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<UIScene*>* 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<UIScene*>* 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
|
||||
Reference in New Issue
Block a user