InputHandler // Handle Alt+Shift+Enter, etc.
This commit is contained in:
parent
35d3c344ec
commit
d1a7f3dcc3
|
@ -89,7 +89,7 @@ extension InputHandler {
|
||||||
if input.isControlHold, input.isCommandHold, input.isEnter,
|
if input.isControlHold, input.isCommandHold, input.isEnter,
|
||||||
!input.isOptionHold, !input.isShiftHold, compositor.isEmpty
|
!input.isOptionHold, !input.isShiftHold, compositor.isEmpty
|
||||||
{
|
{
|
||||||
return handleCtrlCommandEnter()
|
return handleEnter(input: input, readingOnly: true)
|
||||||
}
|
}
|
||||||
// 向語言模型詢問是否有對應的記錄。
|
// 向語言模型詢問是否有對應的記錄。
|
||||||
if !currentLM.hasUnigramsFor(keyArray: [readingKey]) {
|
if !currentLM.hasUnigramsFor(keyArray: [readingKey]) {
|
||||||
|
@ -238,7 +238,7 @@ extension InputHandler {
|
||||||
if input.isControlHold, input.isCommandHold, input.isEnter,
|
if input.isControlHold, input.isCommandHold, input.isEnter,
|
||||||
!input.isOptionHold, !input.isShiftHold, composer.isEmpty
|
!input.isOptionHold, !input.isShiftHold, composer.isEmpty
|
||||||
{
|
{
|
||||||
return handleCtrlCommandEnter()
|
return handleEnter(input: input, readingOnly: true)
|
||||||
}
|
}
|
||||||
// 向語言模型詢問是否有對應的記錄。
|
// 向語言模型詢問是否有對應的記錄。
|
||||||
if !currentLM.hasUnigramsFor(keyArray: [calligrapher]) {
|
if !currentLM.hasUnigramsFor(keyArray: [calligrapher]) {
|
||||||
|
|
|
@ -140,12 +140,7 @@ extension InputHandler {
|
||||||
case .kEnd: return handleEnd()
|
case .kEnd: return handleEnd()
|
||||||
case .kBackSpace: return handleBackSpace(input: input)
|
case .kBackSpace: return handleBackSpace(input: input)
|
||||||
case .kWindowsDelete: return handleDelete(input: input)
|
case .kWindowsDelete: return handleDelete(input: input)
|
||||||
case .kCarriageReturn, .kLineFeed: // Enter
|
case .kCarriageReturn, .kLineFeed: return handleEnter(input: input)
|
||||||
return (input.isCommandHold && input.isControlHold)
|
|
||||||
? (input.isOptionHold
|
|
||||||
? handleCtrlOptionCommandEnter(isShiftPressed: input.isShiftHold)
|
|
||||||
: handleCtrlCommandEnter(isShiftPressed: input.isShiftHold))
|
|
||||||
: handleEnter()
|
|
||||||
case .kSpace: // 倘若沒有在偏好設定內將 Space 空格鍵設為選字窗呼叫用鍵的話………
|
case .kSpace: // 倘若沒有在偏好設定內將 Space 空格鍵設為選字窗呼叫用鍵的話………
|
||||||
// 空格字符輸入行為處理。
|
// 空格字符輸入行為處理。
|
||||||
switch state.type {
|
switch state.type {
|
||||||
|
|
|
@ -279,16 +279,30 @@ extension InputHandler {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Enter 鍵的處理
|
// MARK: - Enter 鍵的處理,包括對其他修飾鍵的應對。
|
||||||
|
|
||||||
/// Enter 鍵的處理。
|
/// Enter 鍵的處理。
|
||||||
|
/// - Parameter input: 輸入按鍵訊號。
|
||||||
/// - Returns: 將按鍵行為「是否有處理掉」藉由 SessionCtl 回報給 IMK。
|
/// - Returns: 將按鍵行為「是否有處理掉」藉由 SessionCtl 回報給 IMK。
|
||||||
@discardableResult func handleEnter() -> Bool {
|
@discardableResult func handleEnter(input: InputSignalProtocol, readingOnly: Bool = false) -> Bool {
|
||||||
guard let delegate = delegate else { return false }
|
guard let delegate = delegate else { return false }
|
||||||
let state = delegate.state
|
let state = delegate.state
|
||||||
guard state.type == .ofInputting else { return false }
|
guard state.type == .ofInputting else { return false }
|
||||||
|
|
||||||
delegate.switchState(IMEState.ofCommitting(textToCommit: state.displayedText))
|
var displayedText = state.displayedText
|
||||||
|
|
||||||
|
if input.modifierFlags == [.option, .shift] {
|
||||||
|
displayedText = displayedText.charComponents.joined(separator: " ")
|
||||||
|
} else if readingOnly {
|
||||||
|
displayedText = commissionByCtrlCommandEnter()
|
||||||
|
} else if input.isCommandHold, input.isControlHold {
|
||||||
|
displayedText =
|
||||||
|
input.isOptionHold
|
||||||
|
? commissionByCtrlOptionCommandEnter(isShiftPressed: input.isShiftHold)
|
||||||
|
: commissionByCtrlCommandEnter(isShiftPressed: input.isShiftHold)
|
||||||
|
}
|
||||||
|
|
||||||
|
delegate.switchState(IMEState.ofCommitting(textToCommit: displayedText))
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -297,11 +311,7 @@ extension InputHandler {
|
||||||
/// Command+Enter 鍵的處理(注音文)。
|
/// Command+Enter 鍵的處理(注音文)。
|
||||||
/// - Parameter isShiftPressed: 有沒有同時摁著 Shift 鍵。
|
/// - Parameter isShiftPressed: 有沒有同時摁著 Shift 鍵。
|
||||||
/// - Returns: 將按鍵行為「是否有處理掉」藉由 SessionCtl 回報給 IMK。
|
/// - Returns: 將按鍵行為「是否有處理掉」藉由 SessionCtl 回報給 IMK。
|
||||||
func handleCtrlCommandEnter(isShiftPressed: Bool = false) -> Bool {
|
private func commissionByCtrlCommandEnter(isShiftPressed: Bool = false) -> String {
|
||||||
guard let delegate = delegate else { return false }
|
|
||||||
let state = delegate.state
|
|
||||||
guard state.type == .ofInputting else { return false }
|
|
||||||
|
|
||||||
var displayedText = compositor.keys.joined(separator: "\t")
|
var displayedText = compositor.keys.joined(separator: "\t")
|
||||||
if compositor.isEmpty {
|
if compositor.isEmpty {
|
||||||
displayedText = readingForDisplay
|
displayedText = readingForDisplay
|
||||||
|
@ -325,9 +335,7 @@ extension InputHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
displayedText = displayedText.replacingOccurrences(of: "\t", with: isShiftPressed ? "-" : " ")
|
displayedText = displayedText.replacingOccurrences(of: "\t", with: isShiftPressed ? "-" : " ")
|
||||||
|
return displayedText
|
||||||
delegate.switchState(IMEState.ofCommitting(textToCommit: displayedText))
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Command+Option+Enter 鍵的處理(網頁 Ruby 注音文標記)
|
// MARK: - Command+Option+Enter 鍵的處理(網頁 Ruby 注音文標記)
|
||||||
|
@ -335,11 +343,7 @@ extension InputHandler {
|
||||||
/// Command+Option+Enter 鍵的處理(網頁 Ruby 注音文標記)。
|
/// Command+Option+Enter 鍵的處理(網頁 Ruby 注音文標記)。
|
||||||
/// - Parameter isShiftPressed: 有沒有同時摁著 Shift 鍵。摁了的話則只遞交讀音字串。
|
/// - Parameter isShiftPressed: 有沒有同時摁著 Shift 鍵。摁了的話則只遞交讀音字串。
|
||||||
/// - Returns: 將按鍵行為「是否有處理掉」藉由 SessionCtl 回報給 IMK。
|
/// - Returns: 將按鍵行為「是否有處理掉」藉由 SessionCtl 回報給 IMK。
|
||||||
func handleCtrlOptionCommandEnter(isShiftPressed: Bool = false) -> Bool {
|
private func commissionByCtrlOptionCommandEnter(isShiftPressed: Bool = false) -> String {
|
||||||
guard let delegate = delegate else { return false }
|
|
||||||
let state = delegate.state
|
|
||||||
guard state.type == .ofInputting else { return false }
|
|
||||||
|
|
||||||
var composed = ""
|
var composed = ""
|
||||||
|
|
||||||
compositor.walkedNodes.smashedPairs.forEach { key, value in
|
compositor.walkedNodes.smashedPairs.forEach { key, value in
|
||||||
|
@ -368,8 +372,7 @@ extension InputHandler {
|
||||||
composed += key.contains("_") ? value : "<ruby>\(value)<rp>(</rp><rt>\(key)</rt><rp>)</rp></ruby>"
|
composed += key.contains("_") ? value : "<ruby>\(value)<rp>(</rp><rt>\(key)</rt><rp>)</rp></ruby>"
|
||||||
}
|
}
|
||||||
|
|
||||||
delegate.switchState(IMEState.ofCommitting(textToCommit: composed))
|
return composed
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - 處理 BackSpace (macOS Delete) 按鍵行為
|
// MARK: - 處理 BackSpace (macOS Delete) 按鍵行為
|
||||||
|
|
Loading…
Reference in New Issue