visionOS: show layout menu from keyboard button

This commit is contained in:
Jesús A. Álvarez 2024-02-10 14:23:42 +01:00
parent 53cb92f4ef
commit ed99d1bbfb
5 changed files with 27 additions and 4 deletions

View File

@ -22,6 +22,7 @@
@property (weak, nonatomic, nullable) id<KBKeyboardViewDelegate> delegate;
@property (nonatomic, strong, nullable) KBKeyboardLayout *layout;
@property (nonatomic, strong, nullable) UIMenu *layoutMenu;
@property (nonatomic, readonly, nonnull) NSArray<KBKey*>* keys;
@property (nonatomic, readonly, nonnull) NSArray<KBKey*>* stickyKeys;

View File

@ -126,11 +126,15 @@
keyFrame.origin.x += safeAreaInsets.left;
}
if (scancode == VKC_HIDE) {
#if defined(TARGET_OS_VISION) && TARGET_OS_VISION == 1
// close window to hide
return;
#else
key = [[KBHideKey alloc] initWithFrame:keyFrame];
#if defined(TARGET_OS_VISION) && TARGET_OS_VISION == 1
if (self.layoutMenu != nil) {
key.showsMenuAsPrimaryAction = YES;
key.menu = self.layoutMenu;
} else {
return;
}
#else
[key addTarget:self action:@selector(hideKeyboard:) forControlEvents:UIControlEventTouchUpInside];
#endif
} else if (scancode == VKC_SHIFT_CAPS) {

View File

@ -27,5 +27,6 @@
@property (nonatomic, readonly) UIViewController* keyboardViewController;
- (void)initXr;
+ (void)adjustToScreenSize;
- (UIMenu*)keyboardLayoutMenu;
@end
#endif

View File

@ -67,6 +67,7 @@ API_AVAILABLE(ios(13.4))
}
}
keyboardView = [[KBKeyboardView alloc] initWithFrame:CGRectMake(0, 0, keyboardSize.width, keyboardSize.height)];
keyboardView.layoutMenu = [self keyboardLayoutMenu];
keyboardView.layout = layout;
keyboardView.delegate = self;
}

View File

@ -62,5 +62,21 @@ extension ViewController {
resizingRestrictions: .uniform
))
}
@objc
func keyboardLayoutMenu() -> UIMenu {
let layouts = AppDelegate.shared.keyboardLayoutPaths ?? []
let items: [UIMenuElement] = layouts.map({ path in
UIDeferredMenuElement.uncached { completion in
let layoutId = (path as NSString).lastPathComponent
let displayName = (layoutId as NSString).deletingPathExtension
let selected = UserDefaults.standard.string(forKey: "keyboardLayout") == layoutId
completion([UIAction(title: displayName, state: selected ? .on : .off) { _ in
UserDefaults.standard.setValue(layoutId, forKey: "keyboardLayout")
}])
}
})
return UIMenu(title: "Layout", options: [], children: items)
}
#endif
}