From ed99d1bbfbb454f8a7fbe3aa82a9d3b1a53be6f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesu=CC=81s=20A=2E=20A=CC=81lvarez?= Date: Sat, 10 Feb 2024 14:23:42 +0100 Subject: [PATCH] visionOS: show layout menu from keyboard button --- Mini vMac/KBKeyboardView.h | 1 + Mini vMac/KBKeyboardView.m | 12 ++++++++---- Mini vMac/ViewController.h | 1 + Mini vMac/ViewController.m | 1 + Mini vMac/VisionSupport.swift | 16 ++++++++++++++++ 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/Mini vMac/KBKeyboardView.h b/Mini vMac/KBKeyboardView.h index 80215b0..ee93386 100644 --- a/Mini vMac/KBKeyboardView.h +++ b/Mini vMac/KBKeyboardView.h @@ -22,6 +22,7 @@ @property (weak, nonatomic, nullable) id delegate; @property (nonatomic, strong, nullable) KBKeyboardLayout *layout; +@property (nonatomic, strong, nullable) UIMenu *layoutMenu; @property (nonatomic, readonly, nonnull) NSArray* keys; @property (nonatomic, readonly, nonnull) NSArray* stickyKeys; diff --git a/Mini vMac/KBKeyboardView.m b/Mini vMac/KBKeyboardView.m index b124bd0..3624ef8 100644 --- a/Mini vMac/KBKeyboardView.m +++ b/Mini vMac/KBKeyboardView.m @@ -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) { diff --git a/Mini vMac/ViewController.h b/Mini vMac/ViewController.h index 38d7e95..a88dd4b 100644 --- a/Mini vMac/ViewController.h +++ b/Mini vMac/ViewController.h @@ -27,5 +27,6 @@ @property (nonatomic, readonly) UIViewController* keyboardViewController; - (void)initXr; + (void)adjustToScreenSize; +- (UIMenu*)keyboardLayoutMenu; @end #endif diff --git a/Mini vMac/ViewController.m b/Mini vMac/ViewController.m index 193d475..6bf6ae3 100644 --- a/Mini vMac/ViewController.m +++ b/Mini vMac/ViewController.m @@ -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; } diff --git a/Mini vMac/VisionSupport.swift b/Mini vMac/VisionSupport.swift index 377ac50..0d69379 100644 --- a/Mini vMac/VisionSupport.swift +++ b/Mini vMac/VisionSupport.swift @@ -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 }