From 0debf92cf4909cb15f4b8deee6bd1f2797974c42 Mon Sep 17 00:00:00 2001 From: Guangming Mao Date: Tue, 27 Jun 2023 02:44:13 -0700 Subject: [PATCH] =?UTF-8?q?fix=20#227=20=E4=BF=AE=E5=A4=8D=20iPad=20?= =?UTF-8?q?=E6=B5=AE=E5=8A=A8=E9=94=AE=E7=9B=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Input/ChineseInputSetProvider.swift | 104 ++++++++++++------ .../Input/HamsterInputSetProvider.swift | 3 +- .../Lab/Keybaord/KeyboardViewController.swift | 18 +++ .../iPadKeyboardLayoutProvider.swift | 12 +- 4 files changed, 92 insertions(+), 45 deletions(-) diff --git a/General/Lab/Keybaord/Input/ChineseInputSetProvider.swift b/General/Lab/Keybaord/Input/ChineseInputSetProvider.swift index 09424e66..46fd0443 100644 --- a/General/Lab/Keybaord/Input/ChineseInputSetProvider.swift +++ b/General/Lab/Keybaord/Input/ChineseInputSetProvider.swift @@ -13,32 +13,80 @@ import KeyboardKit */ public class ChineseInputSetProvider: InputSetProvider, LocalizedService { public var localeKey: String = "cn" + private let keyboardContext: KeyboardContext - public init() {} + public init(keyboardContext: KeyboardContext) { + self.keyboardContext = keyboardContext + } - public var alphabeticInputSet: AlphabeticInputSet = .chinese - public let numericInputSet: NumericInputSet = .chinese() - public let numericNineGridInputSet: NumericInputSet = .chineseNineGrid() - public let symbolicInputSet: SymbolicInputSet = .chinese() -} + // 主键盘 + private let padChineseAlphabetic = AlphabeticInputSet(rows: [ + .init(chars: "qwertyuiop"), + .init(chars: "asdfghjkl"), + .init(chars: "zxcvbnm,."), + ]) + + private let phoneChineseAlphabetic = AlphabeticInputSet(rows: [ + .init(chars: "qwertyuiop"), + .init(chars: "asdfghjkl"), + .init(chars: "zxcvbnm"), + ]) -public extension AlphabeticInputSet { - static let chinese = AlphabeticInputSet.qwerty -} + public var alphabeticInputSet: AlphabeticInputSet { + if self.keyboardContext.deviceType == .pad { + return padChineseAlphabetic + } else { + return phoneChineseAlphabetic + } + } + + // 数字键盘 + private let padChineseNumeric = NumericInputSet(rows: [ + .init(chars: "1234567890"), + .init(chars: "@#¥/()“”’"), + .init(chars: "%-~…、;:,。"), + ]) + + private let phoneChineseNumeric = NumericInputSet(rows: [ + .init(chars: "1234567890"), + .init(chars: "-/:;()¥@“”"), + .init(chars: "。,、?!.") + ]) + + public var numericInputSet: NumericInputSet { + if self.keyboardContext.deviceType == .pad { + return padChineseNumeric + } else { + return phoneChineseNumeric + } + } -public extension NumericInputSet { - static func chinese() -> NumericInputSet { - NumericInputSet(rows: [ - .init(chars: "1234567890"), - .init( - phone: "-/:;()¥@“”", - pad: "%-~…、;:,。"), - .init( - phone: "。,、?!.", - pad: "%-~…、;:,。") - ]) + // 九宫格数字键盘 + public let numericNineGridInputSet: NumericInputSet = .chineseNineGrid() + + // 符号键盘 + private let padChineseSymbolic = SymbolicInputSet(rows: [ + .init(chars: "^_|\\<>{},."), + .init(chars: "&$€*【】「」•"), + .init(chars: "。—+=·《》!?") + ]) + + private let phoneChineseSymbolic = SymbolicInputSet(rows: [ + .init(chars: "【】{}#%^*+="), + .init(chars: "_—\\|~《》$&·"), + .init(chars: "…,。?!‘") + ]) + + public var symbolicInputSet: SymbolicInputSet { + if self.keyboardContext.deviceType == .pad { + return padChineseSymbolic + } else { + return phoneChineseSymbolic + } } +} +public extension NumericInputSet { static func chineseNineGrid() -> NumericInputSet { .init(rows: [ .init(chars: "123"), @@ -48,19 +96,3 @@ public extension NumericInputSet { ]) } } - -public extension SymbolicInputSet { - static func chinese() -> SymbolicInputSet { - SymbolicInputSet(rows: [ - .init( - phone: "【】{}#%^*+=", - pad: "^_|\\<>{},."), - .init( - phone: "_—\\|~《》$&·", - pad: "&$€*【】「」•"), - .init( - phone: "…,。?!‘", - pad: "。—+=·《》!?") - ]) - } -} diff --git a/General/Lab/Keybaord/Input/HamsterInputSetProvider.swift b/General/Lab/Keybaord/Input/HamsterInputSetProvider.swift index b852ee3a..40ed1710 100644 --- a/General/Lab/Keybaord/Input/HamsterInputSetProvider.swift +++ b/General/Lab/Keybaord/Input/HamsterInputSetProvider.swift @@ -17,13 +17,14 @@ class HamsterInputSetProvider: InputSetProvider { self.keyboardContext = keyboardContext self.appSettings = appSettings self.rimeContext = rimeContext + self.chineseProvider = ChineseInputSetProvider(keyboardContext: self.keyboardContext) } public let keyboardContext: KeyboardContext var appSettings: HamsterAppSettings var rimeContext: RimeContext - let chineseProvider = ChineseInputSetProvider() + let chineseProvider: ChineseInputSetProvider let englishProvider = EnglishInputSetProvider() /** diff --git a/General/Lab/Keybaord/KeyboardViewController.swift b/General/Lab/Keybaord/KeyboardViewController.swift index 9daaf8d0..e09283d9 100644 --- a/General/Lab/Keybaord/KeyboardViewController.swift +++ b/General/Lab/Keybaord/KeyboardViewController.swift @@ -120,6 +120,24 @@ open class HamsterKeyboardViewController: KeyboardInputViewController { // NotificationCenter.default.removeObserver(self) } + override open func viewDidLayoutSubviews() { + self.log.debug("viewDidLayoutSubviews()") + + super.viewDidLayoutSubviews() + + // 过滤掉不合理的 view width + guard self.view.frame.width > 0 else { + return + } + + // 在 iPad 浮动键盘里强制显示 iPhone 布局 + let deviceTypeToUse = DeviceType.current == .pad && self.keyboardContext.isKeyboardFloating ? DeviceType.phone : DeviceType.current + if self.keyboardContext.deviceType != deviceTypeToUse { + self.keyboardContext.deviceType = deviceTypeToUse + viewWillSetupKeyboard() + } + } + override public func viewWillSetupKeyboard() { // super.viewWillSetupKeyboard() self.log.debug("viewWillSetupKeyboard() begin") diff --git a/General/Lab/Keybaord/Layout/Providers/iPadKeyboardLayoutProvider.swift b/General/Lab/Keybaord/Layout/Providers/iPadKeyboardLayoutProvider.swift index 1931fae7..b1d9ee69 100644 --- a/General/Lab/Keybaord/Layout/Providers/iPadKeyboardLayoutProvider.swift +++ b/General/Lab/Keybaord/Layout/Providers/iPadKeyboardLayoutProvider.swift @@ -49,16 +49,12 @@ class HamsteriPadKeyboardLayoutProvider: iPadKeyboardLayoutProvider { if context.needsInputModeSwitchKey { result.append(.nextKeyboard) } - + // 听写键 - if context.hasDictationKey { - if let action = context.keyboardDictationReplacement { - result.append(action) - } else { - result.append(.dictation) - } + if context.hasDictationKey, let action = context.keyboardDictationReplacement { + result.append(action) } - + // 底部根据配置, 添加自定义功能键 if appSettings.showSpaceLeftButton { result.append(.custom(named: appSettings.spaceLeftButtonValue))