From bc36365994ca05fad581b5d44da69edfd84373d7 Mon Sep 17 00:00:00 2001 From: khanhduytran0 Date: Fri, 16 Aug 2024 09:51:34 +0700 Subject: [PATCH] Fix: replace all currentWindow() with static variable --- Natives/SceneDelegate.m | 3 +++ Natives/SceneExternalDelegate.m | 7 ++--- .../SurfaceViewController+ExternalDisplay.m | 4 +-- Natives/SurfaceViewController.h | 1 + Natives/SurfaceViewController.m | 2 +- Natives/UIKit+hook.h | 5 ++++ Natives/UIKit+hook.m | 27 ++++++++----------- Natives/customcontrols/ControlJoystick.m | 2 +- Natives/input/ControllerInput.m | 2 +- Natives/input/GyroInput.m | 2 +- Natives/input_bridge_v3.m | 4 +-- Natives/ios_uikit_bridge.m | 4 +-- Natives/utils.h | 2 -- 13 files changed, 34 insertions(+), 31 deletions(-) diff --git a/Natives/SceneDelegate.m b/Natives/SceneDelegate.m index ff67d77582..05cb3481b0 100644 --- a/Natives/SceneDelegate.m +++ b/Natives/SceneDelegate.m @@ -2,6 +2,8 @@ #import "ios_uikit_bridge.h" #import "utils.h" +extern UIWindow *mainWindow; + @interface SceneDelegate () @end @@ -13,6 +15,7 @@ - (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session op UIWindowScene *windowScene = (UIWindowScene *)scene; self.window = [[UIWindow alloc] initWithWindowScene:windowScene]; self.window.frame = windowScene.coordinateSpace.bounds; + mainWindow = self.window; launchInitialViewController(self.window); [self.window makeKeyAndVisible]; } diff --git a/Natives/SceneExternalDelegate.m b/Natives/SceneExternalDelegate.m index 540f072fa7..f9914ddb8b 100644 --- a/Natives/SceneExternalDelegate.m +++ b/Natives/SceneExternalDelegate.m @@ -2,7 +2,7 @@ #import "SceneExternalDelegate.h" #import "SurfaceViewController.h" -extern UIWindow* currentWindow(); +extern UIWindow *externalWindow; @interface SceneExternalDelegate () @@ -15,8 +15,9 @@ - (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session op UIWindowScene *windowScene = (UIWindowScene *)scene; self.window = [[UIWindow alloc] initWithWindowScene:windowScene]; self.window.frame = windowScene.coordinateSpace.bounds; + externalWindow = self.window; if (SurfaceViewController.isRunning && getPrefBool(@"video.fullscreen_airplay")) { - [currentWindow().rootViewController performSelector:@selector(switchToExternalDisplay)]; + [UIWindow.mainWindow.rootViewController performSelector:@selector(switchToExternalDisplay)]; } } @@ -27,7 +28,7 @@ - (void)sceneDidDisconnect:(UIScene *)scene { // Release any resources associated with this scene that can be re-created the next time the scene connects. // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead). if (SurfaceViewController.isRunning && getPrefBool(@"video.fullscreen_airplay")) { - [currentWindow().rootViewController performSelector:@selector(switchToInternalDisplay)]; + [UIWindow.mainWindow.rootViewController performSelector:@selector(switchToInternalDisplay)]; } } diff --git a/Natives/SurfaceViewController+ExternalDisplay.m b/Natives/SurfaceViewController+ExternalDisplay.m index bd4483e90b..d71c9cac0c 100644 --- a/Natives/SurfaceViewController+ExternalDisplay.m +++ b/Natives/SurfaceViewController+ExternalDisplay.m @@ -16,7 +16,7 @@ - (void)switchToExternalDisplay { noteLabel.text = localize(@"game.note.airplay", nil); [self.touchView addSubview:noteLabel]; - UIWindow *secondWindow = currentWindowInScene(1); + UIWindow *secondWindow = UIWindow.externalWindow; secondWindow.rootViewController = [[UIViewController alloc] init]; [secondWindow.rootViewController.view addSubview:self.surfaceView]; [secondWindow.rootViewController.view addSubview:self.mousePointerView]; @@ -29,7 +29,7 @@ - (void)switchToInternalDisplay { [self.surfaceView removeFromSuperview]; [self.mousePointerView removeFromSuperview]; - UIWindow *secondWindow = currentWindowInScene(1); + UIWindow *secondWindow = UIWindow.externalWindow; secondWindow.hidden = YES; [self.touchView.subviews[0] removeFromSuperview]; diff --git a/Natives/SurfaceViewController.h b/Natives/SurfaceViewController.h index 0e25eabfce..8ca4eee41c 100644 --- a/Natives/SurfaceViewController.h +++ b/Natives/SurfaceViewController.h @@ -1,4 +1,5 @@ #import +#import "UIKit+hook.h" #import "customcontrols/ControlLayout.h" #import "GameSurfaceView.h" diff --git a/Natives/SurfaceViewController.m b/Natives/SurfaceViewController.m index 41629919b3..9afc3b833c 100644 --- a/Natives/SurfaceViewController.m +++ b/Natives/SurfaceViewController.m @@ -1064,7 +1064,7 @@ - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event } + (BOOL)isRunning { - return [currentWindow().rootViewController isKindOfClass:SurfaceViewController.class]; + return [UIWindow.mainWindow.rootViewController isKindOfClass:SurfaceViewController.class]; } + (GameSurfaceView *)surface { diff --git a/Natives/UIKit+hook.h b/Natives/UIKit+hook.h index d36bf8b316..c4fb0ca044 100644 --- a/Natives/UIKit+hook.h +++ b/Natives/UIKit+hook.h @@ -32,6 +32,11 @@ @property(assign, nonatomic) NSInteger nonEditingLinebreakMode; @end +@interface UIWindow(global) ++ (UIWindow *)mainWindow; ++ (UIWindow *)externalWindow; +@end + /* @interface WFTextTokenTextView : UITextField @property(nonatomic) NSString* placeholder diff --git a/Natives/UIKit+hook.m b/Natives/UIKit+hook.m index da9ea44410..0b380c4e7e 100644 --- a/Natives/UIKit+hook.m +++ b/Natives/UIKit+hook.m @@ -4,6 +4,8 @@ #import "UIKit+hook.h" #import "utils.h" +__weak UIWindow *mainWindow, *externalWindow; + void swizzle(Class class, SEL originalAction, SEL swizzledAction) { method_exchangeImplementations(class_getInstanceMethod(class, originalAction), class_getInstanceMethod(class, swizzledAction)); } @@ -119,6 +121,14 @@ - (UIUserInterfaceSizeClass)verticalSizeClass { @implementation UIWindow(hook) ++ (UIWindow *)mainWindow { + return mainWindow; +} + ++ (UIWindow *)externalWindow { + return externalWindow; +} + - (UIViewController *)visibleViewController { UIViewController *current = self.rootViewController; while (current.presentedViewController) { @@ -144,21 +154,6 @@ - (BOOL)forceFullHeightInLandscape { } @end -UIWindow* currentWindowInScene(BOOL external) { - id delegate = UIApplication.sharedApplication.delegate; - for (UIScene *scene in UIApplication.sharedApplication.connectedScenes.allObjects) { - if (external != (scene.session.role == UIWindowSceneSessionRoleApplication)) { - delegate = scene.delegate; - break; - } - } - return [delegate window]; -} - -UIWindow* currentWindow() { - return currentWindowInScene(0); -} - UIViewController* currentVC() { - return currentWindow().visibleViewController; + return UIWindow.mainWindow.visibleViewController; } diff --git a/Natives/customcontrols/ControlJoystick.m b/Natives/customcontrols/ControlJoystick.m index 40fa1617e2..4f0533a853 100644 --- a/Natives/customcontrols/ControlJoystick.m +++ b/Natives/customcontrols/ControlJoystick.m @@ -119,7 +119,7 @@ - (void)callbackMoveX:(CGFloat)xValue Y:(CGFloat)yValue { debugLabel.lineBreakMode = NSLineBreakByWordWrapping; debugLabel.numberOfLines = 0; debugLabel.userInteractionEnabled = NO; - [currentWindow() addSubview:debugLabel]; + [UIWindow.mainWindow addSubview:debugLabel]; } #endif diff --git a/Natives/input/ControllerInput.m b/Natives/input/ControllerInput.m index eb18bf39e9..b24af9961d 100644 --- a/Natives/input/ControllerInput.m +++ b/Natives/input/ControllerInput.m @@ -211,7 +211,7 @@ + (void)tick { deltaX *= deltaTimeScale; deltaY *= deltaTimeScale; - SurfaceViewController *vc = (id)(currentWindow().rootViewController); + SurfaceViewController *vc = (id)UIWindow.mainWindow.rootViewController; [vc sendTouchPoint:CGPointMake(deltaX, deltaY) withEvent:ACTION_MOVE_MOTION]; } lastFrameTime = frameTime; diff --git a/Natives/input/GyroInput.m b/Natives/input/GyroInput.m index abf37cd0bd..dd2a698207 100644 --- a/Natives/input/GyroInput.m +++ b/Natives/input/GyroInput.m @@ -62,7 +62,7 @@ + (void)tick { lastYValue = cmInstance.deviceMotion.rotationRate.y / (M_PI*90) * windowHeight * factor; } - SurfaceViewController *vc = (id)(currentWindow().rootViewController); + SurfaceViewController *vc = (id)UIWindow.mainWindow.rootViewController; [vc sendTouchPoint:CGPointMake(lastXValue, lastYValue) withEvent:ACTION_MOVE_MOTION]; lastFrameTime = frameTime; diff --git a/Natives/input_bridge_v3.m b/Natives/input_bridge_v3.m index caf454fa90..0efcd05da3 100644 --- a/Natives/input_bridge_v3.m +++ b/Natives/input_bridge_v3.m @@ -55,7 +55,7 @@ void openURLGlobal(NSString *path) { dispatch_async(dispatch_get_main_queue(), ^{ if ([path hasPrefix:@"http"]) { - openLink(currentWindow().rootViewController, [NSURL URLWithString:path]); + openLink(UIWindow.mainWindow.rootViewController, [NSURL URLWithString:path]); dispatch_group_leave(group); return; } @@ -395,7 +395,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_glfw_CallbackBridge_nativeSetGrabbing(JNIE isGrabbing = grabbing; dispatch_async(dispatch_get_main_queue(), ^{ - SurfaceViewController *vc = ((SurfaceViewController *)currentWindow().rootViewController); + SurfaceViewController *vc = ((SurfaceViewController *)UIWindow.mainWindow.rootViewController); [vc updateGrabState]; }); } diff --git a/Natives/ios_uikit_bridge.m b/Natives/ios_uikit_bridge.m index ae1c2a5951..61d2d91d7d 100644 --- a/Natives/ios_uikit_bridge.m +++ b/Natives/ios_uikit_bridge.m @@ -21,7 +21,7 @@ void internal_showDialog(NSString* title, NSString* message) { UIAlertAction* okAction = [UIAlertAction actionWithTitle:localize(@"OK", nil) style:UIAlertActionStyleDefault handler:nil]; [alert addAction:okAction]; - UIWindow *alertWindow = [[UIWindow alloc] initWithWindowScene:currentWindow().windowScene]; + UIWindow *alertWindow = [[UIWindow alloc] initWithWindowScene:UIWindow.mainWindow.windowScene]; alertWindow.frame = UIScreen.mainScreen.bounds; alertWindow.rootViewController = [UIViewController new]; alertWindow.windowLevel = 1000; @@ -128,7 +128,7 @@ void UIKit_returnToSplitView() { // Researching memory-safe ways to return from SurfaceViewController to the split view // so that the app doesn't close when quitting the game (similar behaviour to Android) dispatch_async(dispatch_get_main_queue(), ^{ - UIWindow *window = currentWindow(); + UIWindow *window = UIWindow.mainWindow; // Return from JavaGUIViewController if ([window.rootViewController isKindOfClass:LauncherSplitViewController.class]) { diff --git a/Natives/utils.h b/Natives/utils.h index 59c582f1b7..04eccc74cc 100644 --- a/Natives/utils.h +++ b/Natives/utils.h @@ -65,8 +65,6 @@ void init_setupMultiDir(); BOOL PLPatchMachOPlatformForFile(const char *path); -UIWindow* currentWindow(); -UIWindow* currentWindowInScene(BOOL external); UIViewController* currentVC(); void openLink(UIViewController* sender, NSURL* link);