InputHandler // Remove state parameter.

This commit is contained in:
ShikiSuen 2022-10-14 13:07:22 +08:00
parent 36a83f25a2
commit 581122720d
7 changed files with 63 additions and 127 deletions

View File

@ -40,7 +40,7 @@ public protocol InputHandlerDelegate {
func switchState(_ newState: IMEStateProtocol)
func candidateController() -> CtlCandidateProtocol
func candidateSelectionCalledByInputHandler(at index: Int)
func performUserPhraseOperation(with state: IMEStateProtocol, addToFilter: Bool)
func performUserPhraseOperation(addToFilter: Bool)
-> Bool
}

View File

@ -17,11 +17,9 @@ extension InputHandler {
///
/// - Parameters:
/// - input:
/// - state:
/// - errorCallback:
/// - Returns: IMK
func handleCandidate(
state: IMEStateProtocol,
input: InputSignalProtocol,
errorCallback: @escaping (String) -> Void
) -> Bool {
@ -31,6 +29,7 @@ extension InputHandler {
}
var ctlCandidate = delegate.candidateController()
let state = delegate.state
// MARK: (Cancel Candidate)
@ -287,9 +286,7 @@ extension InputHandler {
if candidateIndex != -114_514 {
delegate.candidateSelectionCalledByInputHandler(at: candidateIndex)
delegate.switchState(IMEState.ofAbortion())
return handleInput(
event: input, state: IMEState.ofEmpty(), errorCallback: errorCallback
)
return handleInput(event: input, errorCallback: errorCallback)
}
return true
}

View File

@ -126,7 +126,7 @@ extension InputHandler {
///
if prefs.useSCPCTypingMode {
let candidateState: IMEStateProtocol = generateStateOfCandidates(state: inputting)
let candidateState: IMEStateProtocol = generateStateOfCandidates()
switch candidateState.candidates.count {
case 2...: delegate.switchState(candidateState)
case 1:

View File

@ -25,8 +25,7 @@ extension InputHandler {
/// result bool IMK
/// handleCandidate()
private func doHandleInput(_ event: NSEvent) -> Bool {
guard let delegate = delegate else { return false }
return handleInput(event: event, state: delegate.state) { errorString in
handleInput(event: event) { errorString in
vCLog(errorString)
IMEApp.buzz()
}

View File

@ -20,12 +20,10 @@ extension InputHandler {
/// - Remark: inputHandler.handleEvent() IMKCandidates
/// - Parameters:
/// - input:
/// - state:
/// - errorCallback:
/// - Returns: IMK
func handleInput(
event input: InputSignalProtocol,
state: IMEStateProtocol,
errorCallback: @escaping (String) -> Void
) -> Bool {
// inputTest
@ -34,7 +32,7 @@ extension InputHandler {
guard !input.text.isEmpty, input.charCode.isPrintable, let delegate = delegate else { return false }
let inputText: String = input.text
var state = state //
var state = delegate.state //
// Megrez
if input.isInvalid {
@ -107,17 +105,13 @@ extension InputHandler {
// MARK: (Handle Candidates)
if [.ofCandidates, .ofSymbolTable].contains(state.type) {
return handleCandidate(
state: state, input: input, errorCallback: errorCallback
)
return handleCandidate(input: input, errorCallback: errorCallback)
}
// MARK: (Handle Associated Phrases)
if state.type == .ofAssociates {
if handleCandidate(
state: state, input: input, errorCallback: errorCallback
) {
if handleCandidate(input: input, errorCallback: errorCallback) {
return true
} else {
delegate.switchState(IMEState.ofEmpty())
@ -127,12 +121,7 @@ extension InputHandler {
// MARK: 便使() (Handle Marking)
if state.type == .ofMarking {
if handleMarkingState(
state, input: input,
errorCallback: errorCallback
) {
return true
}
if handleMarkingState(input: input, errorCallback: errorCallback) { return true }
state = state.convertedToInputting
delegate.switchState(state)
}
@ -172,13 +161,10 @@ extension InputHandler {
}
return true
} else if input.isShiftHold { // Tab Shift+Command+Space /
return handleInlineCandidateRotation(
state: state, reverseModifier: input.isCommandHold,
errorCallback: errorCallback
)
return handleInlineCandidateRotation(reverseOrder: input.isCommandHold, errorCallback: errorCallback)
}
}
let candidateState: IMEStateProtocol = generateStateOfCandidates(state: state)
let candidateState: IMEStateProtocol = generateStateOfCandidates()
if candidateState.candidates.isEmpty {
errorCallback("3572F238")
} else {
@ -191,56 +177,46 @@ extension InputHandler {
if let keyCodeType = KeyCode(rawValue: input.keyCode) {
switch keyCodeType {
case .kEscape: return handleEsc(state: state)
case .kEscape: return handleEsc()
case .kTab:
return handleInlineCandidateRotation(
state: state, reverseModifier: input.isShiftHold, errorCallback: errorCallback
)
return handleInlineCandidateRotation(reverseOrder: input.isShiftHold, errorCallback: errorCallback)
case .kUpArrow, .kDownArrow, .kLeftArrow, .kRightArrow:
if (input.isControlHold || input.isShiftHold) && (input.isOptionHold) {
if input.isLeft { // Ctrl+PgLf / Shift+PgLf
return handleHome(state: state, errorCallback: errorCallback)
return handleHome(errorCallback: errorCallback)
} else if input.isRight { // Ctrl+PgRt or Shift+PgRt
return handleEnd(state: state, errorCallback: errorCallback)
return handleEnd(errorCallback: errorCallback)
}
}
if input.isCursorBackward { // Forward
return handleBackward(
state: state, input: input, errorCallback: errorCallback
)
return handleBackward(input: input, errorCallback: errorCallback)
}
if input.isCursorForward { // Backward
return handleForward(
state: state, input: input, errorCallback: errorCallback
)
return handleForward(input: input, errorCallback: errorCallback)
}
if input.isCursorClockLeft || input.isCursorClockRight { // Clock keys
if input.isOptionHold, state.type == .ofInputting {
if input.isCursorClockRight {
return handleInlineCandidateRotation(
state: state, reverseModifier: false, errorCallback: errorCallback
)
return handleInlineCandidateRotation(reverseOrder: false, errorCallback: errorCallback)
}
if input.isCursorClockLeft {
return handleInlineCandidateRotation(
state: state, reverseModifier: true, errorCallback: errorCallback
)
return handleInlineCandidateRotation(reverseOrder: true, errorCallback: errorCallback)
}
}
return handleClockKey(state: state, errorCallback: errorCallback)
return handleClockKey(errorCallback: errorCallback)
}
case .kHome: return handleHome(state: state, errorCallback: errorCallback)
case .kEnd: return handleEnd(state: state, errorCallback: errorCallback)
case .kHome: return handleHome(errorCallback: errorCallback)
case .kEnd: return handleEnd(errorCallback: errorCallback)
case .kBackSpace:
return handleBackSpace(state: state, input: input, errorCallback: errorCallback)
return handleBackSpace(input: input, errorCallback: errorCallback)
case .kWindowsDelete:
return handleDelete(state: state, input: input, errorCallback: errorCallback)
return handleDelete(input: input, errorCallback: errorCallback)
case .kCarriageReturn, .kLineFeed:
return (input.isCommandHold && input.isControlHold)
? (input.isOptionHold
? handleCtrlOptionCommandEnter(state: state)
: handleCtrlCommandEnter(state: state))
: handleEnter(state: state)
? handleCtrlOptionCommandEnter()
: handleCtrlCommandEnter())
: handleEnter()
default: break
}
}
@ -258,7 +234,7 @@ extension InputHandler {
var inputting = generateStateOfInputting()
inputting.textToCommit = textToCommit
delegate.switchState(inputting)
let candidateState = generateStateOfCandidates(state: inputting)
let candidateState = generateStateOfCandidates()
if candidateState.candidates.isEmpty {
errorCallback("B5127D8A")
} else {
@ -273,7 +249,7 @@ extension InputHandler {
// commit buffer ESC
// Enter 使 commit buffer
// bool _ =
_ = handleEnter(state: state)
_ = handleEnter()
delegate.switchState(IMEState.ofSymbolTable(node: CandidateNode.root))
return true
}
@ -304,11 +280,7 @@ extension InputHandler {
let parser = currentKeyboardParser
let arrCustomPunctuations: [String] = [punctuationNamePrefix, parser, input.text]
let customPunctuation: String = arrCustomPunctuations.joined()
if handlePunctuation(
customPunctuation,
state: state,
errorCallback: errorCallback
) {
if handlePunctuation(customPunctuation, errorCallback: errorCallback) {
return true
}
@ -317,11 +289,7 @@ extension InputHandler {
let arrPunctuations: [String] = [punctuationNamePrefix, input.text]
let punctuation: String = arrPunctuations.joined()
if handlePunctuation(
punctuation,
state: state,
errorCallback: errorCallback
) {
if handlePunctuation(punctuation, errorCallback: errorCallback) {
return true
}
@ -354,11 +322,7 @@ extension InputHandler {
return true
default: // case 0
let letter = "_letter_\(inputText)"
if handlePunctuation(
letter,
state: state,
errorCallback: errorCallback
) {
if handlePunctuation(letter, errorCallback: errorCallback) {
return true
}
}

View File

@ -84,15 +84,12 @@ extension InputHandler {
///
/// - Parameters:
/// - currentState:
/// - Returns:
func generateStateOfCandidates(
state currentState: IMEStateProtocol
) -> IMEStateProtocol {
func generateStateOfCandidates() -> IMEStateProtocol {
IMEState.ofCandidates(
candidates: generateArrayOfCandidates(fixOrder: prefs.useFixecCandidateOrderOnSelection),
displayTextSegments: compositor.walkedNodes.values,
cursor: currentState.cursor
cursor: delegate?.state.cursor ?? generateStateOfInputting().cursor
)
}
@ -110,9 +107,7 @@ extension InputHandler {
/// - Parameters:
/// - key:
/// - Returns:
public func generateStateOfAssociates(
withPair pair: Megrez.Compositor.KeyValuePaired
) -> IMEStateProtocol {
public func generateStateOfAssociates(withPair pair: Megrez.Compositor.KeyValuePaired) -> IMEStateProtocol {
IMEState.ofAssociates(
candidates: generateArrayOfAssociates(withPair: pair))
}
@ -121,16 +116,15 @@ extension InputHandler {
///
/// - Parameters:
/// - state:
/// - input:
/// - errorCallback:
/// - Returns: SessionCtl IMK
func handleMarkingState(
_ state: IMEStateProtocol,
input: InputSignalProtocol,
errorCallback: @escaping (String) -> Void
) -> Bool {
guard let delegate = delegate else { return false }
let state = delegate.state
if input.isEsc {
delegate.switchState(generateStateOfInputting())
@ -154,7 +148,7 @@ extension InputHandler {
errorCallback("9AAFAC00")
return true
}
if !delegate.performUserPhraseOperation(with: state, addToFilter: false) {
if !delegate.performUserPhraseOperation(addToFilter: false) {
errorCallback("5B69CC8D")
return true
}
@ -168,7 +162,7 @@ extension InputHandler {
errorCallback("1F88B191")
return true
}
if !delegate.performUserPhraseOperation(with: state, addToFilter: true) {
if !delegate.performUserPhraseOperation(addToFilter: true) {
errorCallback("68D3C6C8")
return true
}
@ -231,10 +225,10 @@ extension InputHandler {
/// - Returns: SessionCtl IMK
func handlePunctuation(
_ customPunctuation: String,
state: IMEStateProtocol,
errorCallback: @escaping (String) -> Void
) -> Bool {
guard let delegate = delegate else { return false }
let state = delegate.state
if !currentLM.hasUnigramsFor(key: customPunctuation) {
return false
@ -258,7 +252,7 @@ extension InputHandler {
//
guard prefs.useSCPCTypingMode, composer.isEmpty else { return true }
let candidateState = generateStateOfCandidates(state: inputting)
let candidateState = generateStateOfCandidates()
switch candidateState.candidates.count {
case 2...: delegate.switchState(candidateState)
case 1:
@ -278,12 +272,10 @@ extension InputHandler {
/// Enter
/// - Parameters:
/// - state:
/// - Returns: SessionCtl IMK
func handleEnter(
state: IMEStateProtocol
) -> Bool {
func handleEnter() -> Bool {
guard let delegate = delegate else { return false }
let state = delegate.state
guard state.type == .ofInputting else { return false }
delegate.switchState(IMEState.ofCommitting(textToCommit: state.displayedText))
@ -295,12 +287,10 @@ extension InputHandler {
/// Command+Enter
/// - Parameters:
/// - state:
/// - Returns: SessionCtl IMK
func handleCtrlCommandEnter(
state: IMEStateProtocol
) -> Bool {
func handleCtrlCommandEnter() -> Bool {
guard let delegate = delegate else { return false }
let state = delegate.state
guard state.type == .ofInputting else { return false }
var displayedText = compositor.keys.joined(separator: "-")
@ -322,12 +312,10 @@ extension InputHandler {
/// Command+Option+Enter Ruby
/// - Parameters:
/// - state:
/// - Returns: SessionCtl IMK
func handleCtrlOptionCommandEnter(
state: IMEStateProtocol
) -> Bool {
func handleCtrlOptionCommandEnter() -> Bool {
guard let delegate = delegate else { return false }
let state = delegate.state
guard state.type == .ofInputting else { return false }
var composed = ""
@ -357,16 +345,15 @@ extension InputHandler {
/// BackSpace (macOS Delete)
/// - Parameters:
/// - state:
/// - input:
/// - errorCallback:
/// - Returns: SessionCtl IMK
func handleBackSpace(
state: IMEStateProtocol,
input: InputSignalProtocol,
errorCallback: @escaping (String) -> Void
) -> Bool {
guard let delegate = delegate else { return false }
let state = delegate.state
guard state.type == .ofInputting else { return false }
// macOS Shift+BackSpace
@ -418,16 +405,15 @@ extension InputHandler {
/// PC Delete (macOS Fn+BackSpace)
/// - Parameters:
/// - state:
/// - input:
/// - errorCallback:
/// - Returns: SessionCtl IMK
func handleDelete(
state: IMEStateProtocol,
input: InputSignalProtocol,
errorCallback: @escaping (String) -> Void
) -> Bool {
guard let delegate = delegate else { return false }
let state = delegate.state
guard state.type == .ofInputting else { return false }
if input.isShiftHold {
@ -462,14 +448,13 @@ extension InputHandler {
/// 90
/// - Parameters:
/// - state:
/// - errorCallback:
/// - Returns: SessionCtl IMK
func handleClockKey(
state: IMEStateProtocol,
errorCallback: @escaping (String) -> Void
) -> Bool {
guard let delegate = delegate else { return false }
let state = delegate.state
guard state.type == .ofInputting else { return false }
if !composer.isEmpty {
errorCallback("9B6F908D")
@ -482,14 +467,13 @@ extension InputHandler {
/// Home
/// - Parameters:
/// - state:
/// - errorCallback:
/// - Returns: SessionCtl IMK
func handleHome(
state: IMEStateProtocol,
errorCallback: @escaping (String) -> Void
) -> Bool {
guard let delegate = delegate else { return false }
let state = delegate.state
guard state.type == .ofInputting else { return false }
if !composer.isEmpty {
@ -513,14 +497,13 @@ extension InputHandler {
/// End
/// - Parameters:
/// - state:
/// - errorCallback:
/// - Returns: SessionCtl IMK
func handleEnd(
state: IMEStateProtocol,
errorCallback: @escaping (String) -> Void
) -> Bool {
guard let delegate = delegate else { return false }
let state = delegate.state
guard state.type == .ofInputting else { return false }
if !composer.isEmpty {
@ -544,12 +527,10 @@ extension InputHandler {
/// Esc
/// - Parameters:
/// - state:
/// - Returns: SessionCtl IMK
func handleEsc(
state: IMEStateProtocol
) -> Bool {
func handleEsc() -> Bool {
guard let delegate = delegate else { return false }
let state = delegate.state
guard state.type == .ofInputting else { return false }
if prefs.escToCleanInputBuffer {
@ -573,16 +554,15 @@ extension InputHandler {
///
/// - Parameters:
/// - state:
/// - input:
/// - errorCallback:
/// - Returns: SessionCtl IMK
func handleForward(
state: IMEStateProtocol,
input: InputSignalProtocol,
errorCallback: @escaping (String) -> Void
) -> Bool {
guard let delegate = delegate else { return false }
let state = delegate.state
guard state.type == .ofInputting else { return false }
if !composer.isEmpty {
@ -612,7 +592,7 @@ extension InputHandler {
}
} else if input.isOptionHold {
if input.isControlHold {
return handleEnd(state: state, errorCallback: errorCallback)
return handleEnd(errorCallback: errorCallback)
}
//
if !compositor.jumpCursorBySpan(to: .front) {
@ -641,16 +621,15 @@ extension InputHandler {
///
/// - Parameters:
/// - state:
/// - input:
/// - errorCallback:
/// - Returns: SessionCtl IMK
func handleBackward(
state: IMEStateProtocol,
input: InputSignalProtocol,
errorCallback: @escaping (String) -> Void
) -> Bool {
guard let delegate = delegate else { return false }
let state = delegate.state
guard state.type == .ofInputting else { return false }
if !composer.isEmpty {
@ -680,7 +659,7 @@ extension InputHandler {
}
} else if input.isOptionHold {
if input.isControlHold {
return handleHome(state: state, errorCallback: errorCallback)
return handleHome(errorCallback: errorCallback)
}
//
if !compositor.jumpCursorBySpan(to: .rear) {
@ -709,16 +688,15 @@ extension InputHandler {
///
/// - Parameters:
/// - state:
/// - reverseModifier:
/// - reverseOrder:
/// - errorCallback:
/// - Returns: SessionCtl IMK
func handleInlineCandidateRotation(
state: IMEStateProtocol,
reverseModifier: Bool,
reverseOrder: Bool,
errorCallback: @escaping (String) -> Void
) -> Bool {
guard let delegate = delegate else { return false }
let state = delegate.state
if composer.isEmpty, compositor.isEmpty || compositor.walkedNodes.isEmpty { return false }
guard state.type == .ofInputting else {
guard state.type == .ofEmpty else {
@ -772,12 +750,12 @@ extension InputHandler {
if candidates[0] == currentPaired {
///
///
currentIndex = reverseModifier ? candidates.count - 1 : 1
currentIndex = reverseOrder ? candidates.count - 1 : 1
}
} else {
for candidate in candidates {
if candidate == currentPaired {
if reverseModifier {
if reverseOrder {
if currentIndex == 0 {
currentIndex = candidates.count - 1
} else {

View File

@ -22,9 +22,7 @@ extension SessionCtl: InputHandlerDelegate {
candidatePairSelected(at: index)
}
public func performUserPhraseOperation(with state: IMEStateProtocol, addToFilter: Bool)
-> Bool
{
public func performUserPhraseOperation(addToFilter: Bool) -> Bool {
guard state.type == .ofMarking else { return false }
if !LMMgr.writeUserPhrase(
state.data.userPhraseDumped, inputMode: inputMode,