KeyHandler // Code simplification in Swift.
This commit is contained in:
parent
fcb891affa
commit
cf8ccf395d
|
@ -80,8 +80,7 @@ import Cocoa
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commit the entire input buffer.
|
// Commit the entire input buffer.
|
||||||
let committingState = InputState.Committing(poppedText: inputText.lowercased())
|
stateCallback(InputState.Committing(poppedText: inputText.lowercased()))
|
||||||
stateCallback(committingState)
|
|
||||||
stateCallback(InputState.Empty())
|
stateCallback(InputState.Empty())
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
@ -95,8 +94,7 @@ import Cocoa
|
||||||
{
|
{
|
||||||
clear()
|
clear()
|
||||||
stateCallback(InputState.Empty())
|
stateCallback(InputState.Empty())
|
||||||
let committing = InputState.Committing(poppedText: inputText.lowercased())
|
stateCallback(InputState.Committing(poppedText: inputText.lowercased()))
|
||||||
stateCallback(committing)
|
|
||||||
stateCallback(InputState.Empty())
|
stateCallback(InputState.Empty())
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -113,10 +111,9 @@ import Cocoa
|
||||||
// MARK: Handle Associated Phrases.
|
// MARK: Handle Associated Phrases.
|
||||||
|
|
||||||
if state is InputState.AssociatedPhrases {
|
if state is InputState.AssociatedPhrases {
|
||||||
let result = handleCandidate(
|
if handleCandidate(
|
||||||
state: state, input: input, stateCallback: stateCallback, errorCallback: errorCallback
|
state: state, input: input, stateCallback: stateCallback, errorCallback: errorCallback
|
||||||
)
|
) {
|
||||||
if result {
|
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
stateCallback(InputState.Empty())
|
stateCallback(InputState.Empty())
|
||||||
|
@ -150,8 +147,7 @@ import Cocoa
|
||||||
// update the composing buffer.
|
// update the composing buffer.
|
||||||
composeReading = checkWhetherToneMarkerConfirmsPhoneticReadingBuffer()
|
composeReading = checkWhetherToneMarkerConfirmsPhoneticReadingBuffer()
|
||||||
if !composeReading {
|
if !composeReading {
|
||||||
let inputting = buildInputtingState()
|
stateCallback(buildInputtingState())
|
||||||
stateCallback(inputting)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -166,8 +162,7 @@ import Cocoa
|
||||||
if !ifLangModelHasUnigrams(forKey: reading) {
|
if !ifLangModelHasUnigrams(forKey: reading) {
|
||||||
IME.prtDebugIntel("B49C0979")
|
IME.prtDebugIntel("B49C0979")
|
||||||
errorCallback()
|
errorCallback()
|
||||||
let inputting = buildInputtingState()
|
stateCallback(buildInputtingState())
|
||||||
stateCallback(inputting)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -195,8 +190,7 @@ import Cocoa
|
||||||
if choosingCandidates.candidates.count == 1 {
|
if choosingCandidates.candidates.count == 1 {
|
||||||
clear()
|
clear()
|
||||||
let text: String = choosingCandidates.candidates.first ?? ""
|
let text: String = choosingCandidates.candidates.first ?? ""
|
||||||
let committing = InputState.Committing(poppedText: text)
|
stateCallback(InputState.Committing(poppedText: text))
|
||||||
stateCallback(committing)
|
|
||||||
|
|
||||||
if !mgrPrefs.associatedPhrasesEnabled {
|
if !mgrPrefs.associatedPhrasesEnabled {
|
||||||
stateCallback(InputState.Empty())
|
stateCallback(InputState.Empty())
|
||||||
|
@ -233,14 +227,11 @@ import Cocoa
|
||||||
if getBuilderCursorIndex() >= getBuilderLength() {
|
if getBuilderCursorIndex() >= getBuilderLength() {
|
||||||
let composingBuffer = currentState.composingBuffer
|
let composingBuffer = currentState.composingBuffer
|
||||||
if (composingBuffer.count) != 0 {
|
if (composingBuffer.count) != 0 {
|
||||||
let committing = InputState.Committing(poppedText: composingBuffer)
|
stateCallback(InputState.Committing(poppedText: composingBuffer))
|
||||||
stateCallback(committing)
|
|
||||||
}
|
}
|
||||||
clear()
|
clear()
|
||||||
let committing = InputState.Committing(poppedText: " ")
|
stateCallback(InputState.Committing(poppedText: " "))
|
||||||
stateCallback(committing)
|
stateCallback(InputState.Empty())
|
||||||
let empty = InputState.Empty()
|
|
||||||
stateCallback(empty)
|
|
||||||
} else if ifLangModelHasUnigrams(forKey: " ") {
|
} else if ifLangModelHasUnigrams(forKey: " ") {
|
||||||
insertReadingToBuilder(atCursor: " ")
|
insertReadingToBuilder(atCursor: " ")
|
||||||
let poppedText = _popOverflowComposingTextAndWalk()
|
let poppedText = _popOverflowComposingTextAndWalk()
|
||||||
|
@ -251,11 +242,7 @@ import Cocoa
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let choosingCandidates = buildCandidate(
|
stateCallback(buildCandidate(state: currentState, useVerticalMode: input.useVerticalMode))
|
||||||
state: currentState,
|
|
||||||
useVerticalMode: input.useVerticalMode
|
|
||||||
)
|
|
||||||
stateCallback(choosingCandidates)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -348,11 +335,7 @@ import Cocoa
|
||||||
let inputting = buildInputtingState()
|
let inputting = buildInputtingState()
|
||||||
inputting.poppedText = poppedText
|
inputting.poppedText = poppedText
|
||||||
stateCallback(inputting)
|
stateCallback(inputting)
|
||||||
let choosingCandidate = buildCandidate(
|
stateCallback(buildCandidate(state: inputting, useVerticalMode: input.useVerticalMode))
|
||||||
state: inputting,
|
|
||||||
useVerticalMode: input.useVerticalMode
|
|
||||||
)
|
|
||||||
stateCallback(choosingCandidate)
|
|
||||||
} else { // If there is still unfinished bpmf reading, ignore the punctuation
|
} else { // If there is still unfinished bpmf reading, ignore the punctuation
|
||||||
IME.prtDebugIntel("17446655")
|
IME.prtDebugIntel("17446655")
|
||||||
errorCallback()
|
errorCallback()
|
||||||
|
@ -364,10 +347,7 @@ import Cocoa
|
||||||
// 於是這裡用「模擬一次 Enter 鍵的操作」使其代為執行這個 commit buffer 的動作。
|
// 於是這裡用「模擬一次 Enter 鍵的操作」使其代為執行這個 commit buffer 的動作。
|
||||||
// 這裡不需要該函數所傳回的 bool 結果,所以用「_ =」解消掉。
|
// 這裡不需要該函數所傳回的 bool 結果,所以用「_ =」解消掉。
|
||||||
_ = handleEnter(state: state, stateCallback: stateCallback, errorCallback: errorCallback)
|
_ = handleEnter(state: state, stateCallback: stateCallback, errorCallback: errorCallback)
|
||||||
let root: SymbolNode! = SymbolNode.root
|
stateCallback(InputState.SymbolTable(node: SymbolNode.root, useVerticalMode: input.useVerticalMode))
|
||||||
let symbolState =
|
|
||||||
InputState.SymbolTable(node: root, useVerticalMode: input.useVerticalMode)
|
|
||||||
stateCallback(symbolState)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,15 +69,12 @@ import Cocoa
|
||||||
state currentState: InputState.NotEmpty,
|
state currentState: InputState.NotEmpty,
|
||||||
useVerticalMode: Bool
|
useVerticalMode: Bool
|
||||||
) -> InputState.ChoosingCandidate {
|
) -> InputState.ChoosingCandidate {
|
||||||
let candidatesArray = getCandidatesArray()
|
InputState.ChoosingCandidate(
|
||||||
|
|
||||||
let state = InputState.ChoosingCandidate(
|
|
||||||
composingBuffer: currentState.composingBuffer,
|
composingBuffer: currentState.composingBuffer,
|
||||||
cursorIndex: currentState.cursorIndex,
|
cursorIndex: currentState.cursorIndex,
|
||||||
candidates: candidatesArray,
|
candidates: getCandidatesArray(),
|
||||||
useVerticalMode: useVerticalMode
|
useVerticalMode: useVerticalMode
|
||||||
)
|
)
|
||||||
return state
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - 用以接收聯想詞陣列且生成狀態
|
// MARK: - 用以接收聯想詞陣列且生成狀態
|
||||||
|
@ -94,7 +91,7 @@ import Cocoa
|
||||||
useVerticalMode: Bool
|
useVerticalMode: Bool
|
||||||
) -> InputState.AssociatedPhrases! {
|
) -> InputState.AssociatedPhrases! {
|
||||||
// 上一行必須要用驚嘆號,否則 Xcode 會誤導你砍掉某些實際上必需的語句。
|
// 上一行必須要用驚嘆號,否則 Xcode 會誤導你砍掉某些實際上必需的語句。
|
||||||
return InputState.AssociatedPhrases(
|
InputState.AssociatedPhrases(
|
||||||
candidates: buildAssociatePhraseArray(withKey: key), useVerticalMode: useVerticalMode)
|
candidates: buildAssociatePhraseArray(withKey: key), useVerticalMode: useVerticalMode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,8 +104,7 @@ import Cocoa
|
||||||
errorCallback: @escaping () -> Void
|
errorCallback: @escaping () -> Void
|
||||||
) -> Bool {
|
) -> Bool {
|
||||||
if input.isESC {
|
if input.isESC {
|
||||||
let inputting = buildInputtingState()
|
stateCallback(buildInputtingState())
|
||||||
stateCallback(inputting)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,9 +117,7 @@ import Cocoa
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
stateCallback(buildInputtingState())
|
||||||
let inputting = buildInputtingState()
|
|
||||||
stateCallback(inputting)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,13 +133,7 @@ import Cocoa
|
||||||
readings: state.readings
|
readings: state.readings
|
||||||
)
|
)
|
||||||
marking.tooltipForInputting = state.tooltipForInputting
|
marking.tooltipForInputting = state.tooltipForInputting
|
||||||
|
stateCallback(marking.markedRange.length == 0 ? marking.convertToInputting() : marking)
|
||||||
if marking.markedRange.length == 0 {
|
|
||||||
let inputting = marking.convertToInputting()
|
|
||||||
stateCallback(inputting)
|
|
||||||
} else {
|
|
||||||
stateCallback(marking)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
IME.prtDebugIntel("1149908D")
|
IME.prtDebugIntel("1149908D")
|
||||||
errorCallback()
|
errorCallback()
|
||||||
|
@ -168,12 +156,7 @@ import Cocoa
|
||||||
readings: state.readings
|
readings: state.readings
|
||||||
)
|
)
|
||||||
marking.tooltipForInputting = state.tooltipForInputting
|
marking.tooltipForInputting = state.tooltipForInputting
|
||||||
if marking.markedRange.length == 0 {
|
stateCallback(marking.markedRange.length == 0 ? marking.convertToInputting() : marking)
|
||||||
let inputting = marking.convertToInputting()
|
|
||||||
stateCallback(inputting)
|
|
||||||
} else {
|
|
||||||
stateCallback(marking)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
IME.prtDebugIntel("9B51408D")
|
IME.prtDebugIntel("9B51408D")
|
||||||
errorCallback()
|
errorCallback()
|
||||||
|
@ -212,11 +195,8 @@ import Cocoa
|
||||||
if candidateState.candidates.count == 1 {
|
if candidateState.candidates.count == 1 {
|
||||||
clear()
|
clear()
|
||||||
if let strPoppedText: String = candidateState.candidates.first {
|
if let strPoppedText: String = candidateState.candidates.first {
|
||||||
let committing =
|
stateCallback(InputState.Committing(poppedText: strPoppedText) as InputState.Committing)
|
||||||
InputState.Committing(poppedText: strPoppedText) as InputState.Committing
|
stateCallback(InputState.Empty())
|
||||||
stateCallback(committing)
|
|
||||||
let empty = InputState.Empty()
|
|
||||||
stateCallback(empty)
|
|
||||||
} else {
|
} else {
|
||||||
stateCallback(candidateState)
|
stateCallback(candidateState)
|
||||||
}
|
}
|
||||||
|
@ -236,7 +216,7 @@ import Cocoa
|
||||||
|
|
||||||
// MARK: - Enter 鍵處理
|
// MARK: - Enter 鍵處理
|
||||||
|
|
||||||
@discardableResult func handleEnter(
|
func handleEnter(
|
||||||
state: InputState,
|
state: InputState,
|
||||||
stateCallback: @escaping (InputState) -> Void,
|
stateCallback: @escaping (InputState) -> Void,
|
||||||
errorCallback _: @escaping () -> Void
|
errorCallback _: @escaping () -> Void
|
||||||
|
@ -248,14 +228,10 @@ import Cocoa
|
||||||
clear()
|
clear()
|
||||||
|
|
||||||
if let current = state as? InputState.Inputting {
|
if let current = state as? InputState.Inputting {
|
||||||
let composingBuffer = current.composingBuffer
|
stateCallback(InputState.Committing(poppedText: current.composingBuffer))
|
||||||
|
|
||||||
let committing = InputState.Committing(poppedText: composingBuffer)
|
|
||||||
stateCallback(committing)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let empty = InputState.Empty()
|
stateCallback(InputState.Empty())
|
||||||
stateCallback(empty)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -278,10 +254,8 @@ import Cocoa
|
||||||
|
|
||||||
clear()
|
clear()
|
||||||
|
|
||||||
let committing = InputState.Committing(poppedText: composingBuffer)
|
stateCallback(InputState.Committing(poppedText: composingBuffer))
|
||||||
stateCallback(committing)
|
stateCallback(InputState.Empty())
|
||||||
let empty = InputState.Empty()
|
|
||||||
stateCallback(empty)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -311,11 +285,9 @@ import Cocoa
|
||||||
}
|
}
|
||||||
|
|
||||||
if isPhoneticReadingBufferEmpty(), getBuilderLength() == 0 {
|
if isPhoneticReadingBufferEmpty(), getBuilderLength() == 0 {
|
||||||
let empty = InputState.EmptyIgnoringPreviousState()
|
stateCallback(InputState.EmptyIgnoringPreviousState())
|
||||||
stateCallback(empty)
|
|
||||||
} else {
|
} else {
|
||||||
let inputting = buildInputtingState()
|
stateCallback(buildInputtingState())
|
||||||
stateCallback(inputting)
|
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -338,8 +310,7 @@ import Cocoa
|
||||||
let inputting = buildInputtingState()
|
let inputting = buildInputtingState()
|
||||||
// 這裡不用「count > 0」,因為該整數變數只要「!isEmpty」那就必定滿足這個條件。
|
// 這裡不用「count > 0」,因為該整數變數只要「!isEmpty」那就必定滿足這個條件。
|
||||||
if !inputting.composingBuffer.isEmpty {
|
if !inputting.composingBuffer.isEmpty {
|
||||||
let empty = InputState.EmptyIgnoringPreviousState()
|
stateCallback(InputState.EmptyIgnoringPreviousState())
|
||||||
stateCallback(empty)
|
|
||||||
} else {
|
} else {
|
||||||
stateCallback(inputting)
|
stateCallback(inputting)
|
||||||
}
|
}
|
||||||
|
@ -395,8 +366,7 @@ import Cocoa
|
||||||
|
|
||||||
if getBuilderCursorIndex() != 0 {
|
if getBuilderCursorIndex() != 0 {
|
||||||
setBuilderCursorIndex(0)
|
setBuilderCursorIndex(0)
|
||||||
let inputting = buildInputtingState()
|
stateCallback(buildInputtingState())
|
||||||
stateCallback(inputting)
|
|
||||||
} else {
|
} else {
|
||||||
IME.prtDebugIntel("66D97F90")
|
IME.prtDebugIntel("66D97F90")
|
||||||
errorCallback()
|
errorCallback()
|
||||||
|
@ -426,8 +396,7 @@ import Cocoa
|
||||||
|
|
||||||
if getBuilderCursorIndex() != getBuilderLength() {
|
if getBuilderCursorIndex() != getBuilderLength() {
|
||||||
setBuilderCursorIndex(getBuilderLength())
|
setBuilderCursorIndex(getBuilderLength())
|
||||||
let inputting = buildInputtingState()
|
stateCallback(buildInputtingState())
|
||||||
stateCallback(inputting)
|
|
||||||
} else {
|
} else {
|
||||||
IME.prtDebugIntel("9B69908E")
|
IME.prtDebugIntel("9B69908E")
|
||||||
errorCallback()
|
errorCallback()
|
||||||
|
@ -454,18 +423,15 @@ import Cocoa
|
||||||
// is by default in macOS 10.0-10.5 built-in Panasonic Hanin and later macOS Zhuyin.
|
// is by default in macOS 10.0-10.5 built-in Panasonic Hanin and later macOS Zhuyin.
|
||||||
// Some Windows users hate this design, hence the option here to disable it.
|
// Some Windows users hate this design, hence the option here to disable it.
|
||||||
clear()
|
clear()
|
||||||
let empty = InputState.EmptyIgnoringPreviousState()
|
stateCallback(InputState.EmptyIgnoringPreviousState())
|
||||||
stateCallback(empty)
|
|
||||||
} else {
|
} else {
|
||||||
// If reading is not empty, we cancel the reading.
|
// If reading is not empty, we cancel the reading.
|
||||||
if !isPhoneticReadingBufferEmpty() {
|
if !isPhoneticReadingBufferEmpty() {
|
||||||
clearPhoneticReadingBuffer()
|
clearPhoneticReadingBuffer()
|
||||||
if getBuilderLength() == 0 {
|
if getBuilderLength() == 0 {
|
||||||
let empty = InputState.Empty()
|
stateCallback(InputState.Empty())
|
||||||
stateCallback(empty)
|
|
||||||
} else {
|
} else {
|
||||||
let inputting = buildInputtingState()
|
stateCallback(buildInputtingState())
|
||||||
stateCallback(inputting)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -511,8 +477,7 @@ import Cocoa
|
||||||
} else {
|
} else {
|
||||||
if getBuilderCursorIndex() < getBuilderLength() {
|
if getBuilderCursorIndex() < getBuilderLength() {
|
||||||
setBuilderCursorIndex(getBuilderCursorIndex() + 1)
|
setBuilderCursorIndex(getBuilderCursorIndex() + 1)
|
||||||
let inputting = buildInputtingState()
|
stateCallback(buildInputtingState())
|
||||||
stateCallback(inputting)
|
|
||||||
} else {
|
} else {
|
||||||
IME.prtDebugIntel("A96AAD58")
|
IME.prtDebugIntel("A96AAD58")
|
||||||
errorCallback()
|
errorCallback()
|
||||||
|
@ -563,8 +528,7 @@ import Cocoa
|
||||||
} else {
|
} else {
|
||||||
if getBuilderCursorIndex() > 0 {
|
if getBuilderCursorIndex() > 0 {
|
||||||
setBuilderCursorIndex(getBuilderCursorIndex() - 1)
|
setBuilderCursorIndex(getBuilderCursorIndex() - 1)
|
||||||
let inputting = buildInputtingState()
|
stateCallback(buildInputtingState())
|
||||||
stateCallback(inputting)
|
|
||||||
} else {
|
} else {
|
||||||
IME.prtDebugIntel("7045E6F3")
|
IME.prtDebugIntel("7045E6F3")
|
||||||
errorCallback()
|
errorCallback()
|
||||||
|
|
Loading…
Reference in New Issue