Skip to content

Commit

Permalink
Fix: replace all currentWindow() with static variable
Browse files Browse the repository at this point in the history
  • Loading branch information
khanhduytran0 committed Aug 16, 2024
1 parent 15d2e1b commit bc36365
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 31 deletions.
3 changes: 3 additions & 0 deletions Natives/SceneDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#import "ios_uikit_bridge.h"
#import "utils.h"

extern UIWindow *mainWindow;

@interface SceneDelegate ()

@end
Expand All @@ -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];
}
Expand Down
7 changes: 4 additions & 3 deletions Natives/SceneExternalDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#import "SceneExternalDelegate.h"
#import "SurfaceViewController.h"

extern UIWindow* currentWindow();
extern UIWindow *externalWindow;

@interface SceneExternalDelegate ()

Expand All @@ -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)];
}
}

Expand All @@ -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)];
}
}

Expand Down
4 changes: 2 additions & 2 deletions Natives/SurfaceViewController+ExternalDisplay.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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];
Expand Down
1 change: 1 addition & 0 deletions Natives/SurfaceViewController.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import <UIKit/UIKit.h>
#import "UIKit+hook.h"

#import "customcontrols/ControlLayout.h"
#import "GameSurfaceView.h"
Expand Down
2 changes: 1 addition & 1 deletion Natives/SurfaceViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions Natives/UIKit+hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 11 additions & 16 deletions Natives/UIKit+hook.m
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
2 changes: 1 addition & 1 deletion Natives/customcontrols/ControlJoystick.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Natives/input/ControllerInput.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Natives/input/GyroInput.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Natives/input_bridge_v3.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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];
});
}
Expand Down
4 changes: 2 additions & 2 deletions Natives/ios_uikit_bridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]) {
Expand Down
2 changes: 0 additions & 2 deletions Natives/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit bc36365

Please sign in to comment.