Skip to content

Commit

Permalink
fix #227 修复 iPad 浮动键盘
Browse files Browse the repository at this point in the history
  • Loading branch information
maogm12 authored and imfuxiao committed Jun 27, 2023
1 parent 47952cd commit 0debf92
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 45 deletions.
104 changes: 68 additions & 36 deletions General/Lab/Keybaord/Input/ChineseInputSetProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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: "。—+=·《》!?")
])
}
}
3 changes: 2 additions & 1 deletion General/Lab/Keybaord/Input/HamsterInputSetProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

/**
Expand Down
18 changes: 18 additions & 0 deletions General/Lab/Keybaord/KeyboardViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 0debf92

Please sign in to comment.