KeyHandler // Code simplification in Swift.

This commit is contained in:
ShikiSuen 2022-04-19 23:49:27 +08:00
parent 33fcb58130
commit 77952c8473
2 changed files with 37 additions and 93 deletions

View File

@ -80,8 +80,7 @@ import Cocoa
}
// Commit the entire input buffer.
let committingState = InputState.Committing(poppedText: inputText.lowercased())
stateCallback(committingState)
stateCallback(InputState.Committing(poppedText: inputText.lowercased()))
stateCallback(InputState.Empty())
return true
@ -95,8 +94,7 @@ import Cocoa
{
clear()
stateCallback(InputState.Empty())
let committing = InputState.Committing(poppedText: inputText.lowercased())
stateCallback(committing)
stateCallback(InputState.Committing(poppedText: inputText.lowercased()))
stateCallback(InputState.Empty())
return true
}
@ -113,10 +111,9 @@ import Cocoa
// MARK: Handle Associated Phrases.
if state is InputState.AssociatedPhrases {
let result = handleCandidate(
if handleCandidate(
state: state, input: input, stateCallback: stateCallback, errorCallback: errorCallback
)
if result {
) {
return true
} else {
stateCallback(InputState.Empty())
@ -150,8 +147,7 @@ import Cocoa
// update the composing buffer.
composeReading = checkWhetherToneMarkerConfirmsPhoneticReadingBuffer()
if !composeReading {
let inputting = buildInputtingState()
stateCallback(inputting)
stateCallback(buildInputtingState())
return true
}
}
@ -166,8 +162,7 @@ import Cocoa
if !ifLangModelHasUnigrams(forKey: reading) {
IME.prtDebugIntel("B49C0979")
errorCallback()
let inputting = buildInputtingState()
stateCallback(inputting)
stateCallback(buildInputtingState())
return true
}
@ -195,8 +190,7 @@ import Cocoa
if choosingCandidates.candidates.count == 1 {
clear()
let text: String = choosingCandidates.candidates.first ?? ""
let committing = InputState.Committing(poppedText: text)
stateCallback(committing)
stateCallback(InputState.Committing(poppedText: text))
if !mgrPrefs.associatedPhrasesEnabled {
stateCallback(InputState.Empty())
@ -233,14 +227,11 @@ import Cocoa
if getBuilderCursorIndex() >= getBuilderLength() {
let composingBuffer = currentState.composingBuffer
if (composingBuffer.count) != 0 {
let committing = InputState.Committing(poppedText: composingBuffer)
stateCallback(committing)
stateCallback(InputState.Committing(poppedText: composingBuffer))
}
clear()
let committing = InputState.Committing(poppedText: " ")
stateCallback(committing)
let empty = InputState.Empty()
stateCallback(empty)
stateCallback(InputState.Committing(poppedText: " "))
stateCallback(InputState.Empty())
} else if ifLangModelHasUnigrams(forKey: " ") {
insertReadingToBuilder(atCursor: " ")
let poppedText = _popOverflowComposingTextAndWalk()
@ -251,11 +242,7 @@ import Cocoa
return true
}
}
let choosingCandidates = buildCandidate(
state: currentState,
useVerticalMode: input.useVerticalMode
)
stateCallback(choosingCandidates)
stateCallback(buildCandidate(state: currentState, useVerticalMode: input.useVerticalMode))
return true
}
}
@ -348,11 +335,7 @@ import Cocoa
let inputting = buildInputtingState()
inputting.poppedText = poppedText
stateCallback(inputting)
let choosingCandidate = buildCandidate(
state: inputting,
useVerticalMode: input.useVerticalMode
)
stateCallback(choosingCandidate)
stateCallback(buildCandidate(state: inputting, useVerticalMode: input.useVerticalMode))
} else { // If there is still unfinished bpmf reading, ignore the punctuation
IME.prtDebugIntel("17446655")
errorCallback()
@ -364,10 +347,7 @@ import Cocoa
// Enter 使 commit buffer
// bool _ =
_ = handleEnter(state: state, stateCallback: stateCallback, errorCallback: errorCallback)
let root: SymbolNode! = SymbolNode.root
let symbolState =
InputState.SymbolTable(node: root, useVerticalMode: input.useVerticalMode)
stateCallback(symbolState)
stateCallback(InputState.SymbolTable(node: SymbolNode.root, useVerticalMode: input.useVerticalMode))
return true
}
}

View File

@ -69,15 +69,12 @@ import Cocoa
state currentState: InputState.NotEmpty,
useVerticalMode: Bool
) -> InputState.ChoosingCandidate {
let candidatesArray = getCandidatesArray()
let state = InputState.ChoosingCandidate(
InputState.ChoosingCandidate(
composingBuffer: currentState.composingBuffer,
cursorIndex: currentState.cursorIndex,
candidates: candidatesArray,
candidates: getCandidatesArray(),
useVerticalMode: useVerticalMode
)
return state
}
// MARK: -
@ -94,7 +91,7 @@ import Cocoa
useVerticalMode: Bool
) -> InputState.AssociatedPhrases! {
//  Xcode
return InputState.AssociatedPhrases(
InputState.AssociatedPhrases(
candidates: buildAssociatePhraseArray(withKey: key), useVerticalMode: useVerticalMode)
}
@ -107,8 +104,7 @@ import Cocoa
errorCallback: @escaping () -> Void
) -> Bool {
if input.isESC {
let inputting = buildInputtingState()
stateCallback(inputting)
stateCallback(buildInputtingState())
return true
}
@ -121,9 +117,7 @@ import Cocoa
return true
}
}
let inputting = buildInputtingState()
stateCallback(inputting)
stateCallback(buildInputtingState())
return true
}
@ -139,13 +133,7 @@ import Cocoa
readings: state.readings
)
marking.tooltipForInputting = state.tooltipForInputting
if marking.markedRange.length == 0 {
let inputting = marking.convertToInputting()
stateCallback(inputting)
} else {
stateCallback(marking)
}
stateCallback(marking.markedRange.length == 0 ? marking.convertToInputting() : marking)
} else {
IME.prtDebugIntel("1149908D")
errorCallback()
@ -168,12 +156,7 @@ import Cocoa
readings: state.readings
)
marking.tooltipForInputting = state.tooltipForInputting
if marking.markedRange.length == 0 {
let inputting = marking.convertToInputting()
stateCallback(inputting)
} else {
stateCallback(marking)
}
stateCallback(marking.markedRange.length == 0 ? marking.convertToInputting() : marking)
} else {
IME.prtDebugIntel("9B51408D")
errorCallback()
@ -212,11 +195,8 @@ import Cocoa
if candidateState.candidates.count == 1 {
clear()
if let strPoppedText: String = candidateState.candidates.first {
let committing =
InputState.Committing(poppedText: strPoppedText) as InputState.Committing
stateCallback(committing)
let empty = InputState.Empty()
stateCallback(empty)
stateCallback(InputState.Committing(poppedText: strPoppedText) as InputState.Committing)
stateCallback(InputState.Empty())
} else {
stateCallback(candidateState)
}
@ -236,7 +216,7 @@ import Cocoa
// MARK: - Enter
@discardableResult func handleEnter(
func handleEnter(
state: InputState,
stateCallback: @escaping (InputState) -> Void,
errorCallback _: @escaping () -> Void
@ -248,14 +228,10 @@ import Cocoa
clear()
if let current = state as? InputState.Inputting {
let composingBuffer = current.composingBuffer
let committing = InputState.Committing(poppedText: composingBuffer)
stateCallback(committing)
stateCallback(InputState.Committing(poppedText: current.composingBuffer))
}
let empty = InputState.Empty()
stateCallback(empty)
stateCallback(InputState.Empty())
return true
}
@ -278,10 +254,8 @@ import Cocoa
clear()
let committing = InputState.Committing(poppedText: composingBuffer)
stateCallback(committing)
let empty = InputState.Empty()
stateCallback(empty)
stateCallback(InputState.Committing(poppedText: composingBuffer))
stateCallback(InputState.Empty())
return true
}
@ -311,11 +285,9 @@ import Cocoa
}
if isPhoneticReadingBufferEmpty(), getBuilderLength() == 0 {
let empty = InputState.EmptyIgnoringPreviousState()
stateCallback(empty)
stateCallback(InputState.EmptyIgnoringPreviousState())
} else {
let inputting = buildInputtingState()
stateCallback(inputting)
stateCallback(buildInputtingState())
}
return true
}
@ -338,8 +310,7 @@ import Cocoa
let inputting = buildInputtingState()
// count > 0!isEmpty滿
if !inputting.composingBuffer.isEmpty {
let empty = InputState.EmptyIgnoringPreviousState()
stateCallback(empty)
stateCallback(InputState.EmptyIgnoringPreviousState())
} else {
stateCallback(inputting)
}
@ -395,8 +366,7 @@ import Cocoa
if getBuilderCursorIndex() != 0 {
setBuilderCursorIndex(0)
let inputting = buildInputtingState()
stateCallback(inputting)
stateCallback(buildInputtingState())
} else {
IME.prtDebugIntel("66D97F90")
errorCallback()
@ -426,8 +396,7 @@ import Cocoa
if getBuilderCursorIndex() != getBuilderLength() {
setBuilderCursorIndex(getBuilderLength())
let inputting = buildInputtingState()
stateCallback(inputting)
stateCallback(buildInputtingState())
} else {
IME.prtDebugIntel("9B69908E")
errorCallback()
@ -454,18 +423,15 @@ import Cocoa
// 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.
clear()
let empty = InputState.EmptyIgnoringPreviousState()
stateCallback(empty)
stateCallback(InputState.EmptyIgnoringPreviousState())
} else {
// If reading is not empty, we cancel the reading.
if !isPhoneticReadingBufferEmpty() {
clearPhoneticReadingBuffer()
if getBuilderLength() == 0 {
let empty = InputState.Empty()
stateCallback(empty)
stateCallback(InputState.Empty())
} else {
let inputting = buildInputtingState()
stateCallback(inputting)
stateCallback(buildInputtingState())
}
}
}
@ -511,8 +477,7 @@ import Cocoa
} else {
if getBuilderCursorIndex() < getBuilderLength() {
setBuilderCursorIndex(getBuilderCursorIndex() + 1)
let inputting = buildInputtingState()
stateCallback(inputting)
stateCallback(buildInputtingState())
} else {
IME.prtDebugIntel("A96AAD58")
errorCallback()
@ -563,8 +528,7 @@ import Cocoa
} else {
if getBuilderCursorIndex() > 0 {
setBuilderCursorIndex(getBuilderCursorIndex() - 1)
let inputting = buildInputtingState()
stateCallback(inputting)
stateCallback(buildInputtingState())
} else {
IME.prtDebugIntel("7045E6F3")
errorCallback()