always show keyboard again when pressing keyboard button

This commit is contained in:
Jesús A. Álvarez 2024-02-17 14:41:22 +01:00
parent 772ce2c326
commit afef4e5b25
4 changed files with 28 additions and 12 deletions

View File

@ -32,6 +32,7 @@ extern NSString *DocumentsChangedNotification;
- (IBAction)showInsertDisk:(id)sender;
- (IBAction)showSettings:(id)sender;
- (IBAction)showGestureHelp:(id)sender;
- (UIScene*)sceneWithName:(NSString*)name;
@end

View File

@ -333,13 +333,12 @@ NSString *DocumentsChangedNotification = @"documentsChanged";
return [UISceneConfiguration configurationWithName:@"Keyboard" sessionRole:UIWindowSceneSessionRoleApplication];
}
}
if (![self hasDefaultScene]) {
if ([self sceneWithName:@"Default"] == nil) {
return [UISceneConfiguration configurationWithName:@"Default" sessionRole:UIWindowSceneSessionRoleApplication];
}
return nil;
}
- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
// if only keyboard is left, show default view again
if (![self hasDefaultScene]) {
@ -348,13 +347,13 @@ NSString *DocumentsChangedNotification = @"documentsChanged";
}
}
- (BOOL)hasDefaultScene {
- (UIScene*)sceneWithName:(NSString*)name {
for (UIScene *scene in [UIApplication sharedApplication].connectedScenes) {
if ([scene.session.configuration.name isEqualToString:@"Default"]) {
return YES;
if ([scene.session.configuration.name isEqualToString:name]) {
return scene;
}
}
return NO;
return nil;
}
@end

View File

@ -20,6 +20,7 @@ class KeyboardSceneDelegate: UIResponder, UIWindowSceneDelegate {
fatalError("No main view controller")
}
scene.activationConditions.canActivateForTargetContentIdentifierPredicate = NSPredicate(format: "self == 'net.namedfork.keyboard'", argumentArray: nil)
let defaultSize = mainViewController.keyboardViewController.preferredContentSize
let minSize = defaultSize.applying(.init(scaleX: 0.75, y: 0.75))
let maxSize = defaultSize.applying(.init(scaleX: 1.25, y: 1.25))
@ -33,6 +34,14 @@ class KeyboardSceneDelegate: UIResponder, UIWindowSceneDelegate {
))
window = UIWindow(windowScene: windowScene)
// destroy existing keyboard window
if let oldWindow = mainViewController.keyboardViewController?.view?.window {
oldWindow.rootViewController = nil
if let oldScene = oldWindow.windowScene?.session.scene {
UIApplication.shared.requestSceneSessionDestruction(oldScene.session, options: nil)
}
}
if let window {
window.rootViewController = mainViewController.keyboardViewController
window.makeKeyAndVisible()

View File

@ -320,17 +320,24 @@ static int8_t usb_to_adb_scancode[] = {
}
- (void)setKeyboardVisible:(BOOL)visible animated:(BOOL)animated {
if (self.keyboardVisible == visible) {
return;
}
#if defined(TARGET_OS_VISION) && TARGET_OS_VISION == 1
if (visible) {
UISceneSessionActivationRequest *request = [UISceneSessionActivationRequest requestWithRole:UIWindowSceneSessionRoleApplication];
request.userActivity = [[NSUserActivity alloc] initWithActivityType:@"net.namedfork.keyboard"];
[[UIApplication sharedApplication] activateSceneSessionForRequest:request errorHandler:nil];
} // only show, no hide
request.userActivity.targetContentIdentifier = @"net.namedfork.keyboard";
[[UIApplication sharedApplication] activateSceneSessionForRequest:request errorHandler:^(NSError * _Nonnull error) {
NSLog(@"Activation error: %@", error);
}];
} else {
UIScene *keyboardScene = [[AppDelegate sharedInstance] sceneWithName:@"Keyboard"];
if (keyboardScene != nil) {
[[UIApplication sharedApplication] requestSceneSessionDestruction:keyboardScene.session options:nil errorHandler:nil];
}
}
#else
if (self.keyboardVisible == visible) {
return;
}
if (visible) {
[[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:@"keyboardLayout" options:0 context:NULL];
[self loadKeyboardView];