InputHandler // Simplify handleCandidate().

This commit is contained in:
ShikiSuen 2022-10-14 13:52:51 +08:00
parent 96fe170a8d
commit b6b647d822
1 changed files with 63 additions and 159 deletions

View File

@ -9,6 +9,7 @@
/// 調 /// 調
import CandidateWindow import CandidateWindow
import CocoaExtension
import Shared import Shared
// MARK: - § 調 (Handle Candidate State). // MARK: - § 調 (Handle Candidate State).
@ -22,6 +23,7 @@ extension InputHandler {
guard let delegate = delegate else { return false } guard let delegate = delegate else { return false }
var ctlCandidate = delegate.candidateController() var ctlCandidate = delegate.candidateController()
let state = delegate.state let state = delegate.state
guard !state.candidates.isEmpty else { return false }
// MARK: (Cancel Candidate) // MARK: (Cancel Candidate)
@ -48,20 +50,18 @@ extension InputHandler {
return true return true
} }
// MARK: Enter // MARK:
if input.isEnter { if let keyCodeType = KeyCode(rawValue: input.keyCode) {
switch keyCodeType {
case .kLineFeed, .kCarriageReturn:
if state.type == .ofAssociates, !prefs.alsoConfirmAssociatedCandidatesByEnter { if state.type == .ofAssociates, !prefs.alsoConfirmAssociatedCandidatesByEnter {
delegate.switchState(IMEState.ofAbortion()) delegate.switchState(IMEState.ofAbortion())
return true return true
} }
delegate.candidateSelectionCalledByInputHandler(at: ctlCandidate.selectedCandidateIndex) delegate.candidateSelectionCalledByInputHandler(at: ctlCandidate.selectedCandidateIndex)
return true return true
} case .kTab:
// MARK: Tab
if input.isTab {
let updated: Bool = let updated: Bool =
prefs.specifyShiftTabKeyBehavior prefs.specifyShiftTabKeyBehavior
? (input.isShiftHold ? (input.isShiftHold
@ -70,15 +70,9 @@ extension InputHandler {
: (input.isShiftHold : (input.isShiftHold
? ctlCandidate.highlightPreviousCandidate() ? ctlCandidate.highlightPreviousCandidate()
: ctlCandidate.highlightNextCandidate()) : ctlCandidate.highlightNextCandidate())
if !updated { _ = updated ? {}() : delegate.callError("9B691919")
delegate.callError("9B691919")
}
return true return true
} case .kSpace:
// MARK: Space
if input.isSpace {
let updated: Bool = let updated: Bool =
prefs.specifyShiftSpaceKeyBehavior prefs.specifyShiftSpaceKeyBehavior
? (input.isShiftHold ? (input.isShiftHold
@ -87,129 +81,39 @@ extension InputHandler {
: (input.isShiftHold : (input.isShiftHold
? ctlCandidate.showNextLine() ? ctlCandidate.showNextLine()
: ctlCandidate.highlightNextCandidate()) : ctlCandidate.highlightNextCandidate())
if !updated { _ = updated ? {}() : delegate.callError("A11C781F")
delegate.callError("A11C781F") return true
case .kPageDown:
_ = ctlCandidate.showNextPage() ? {}() : delegate.callError("9B691919")
return true
case .kPageUp:
_ = ctlCandidate.showPreviousPage() ? {}() : delegate.callError("9569955D")
return true
case .kUpArrow, .kDownArrow, .kLeftArrow, .kRightArrow:
handleArrowKey: switch (keyCodeType, ctlCandidate.currentLayout) {
case (.kLeftArrow, .horizontal), (.kUpArrow, .vertical): // Previous Candidate
_ = ctlCandidate.highlightPreviousCandidate() ? {}() : delegate.callError("5548FD14")
case (.kRightArrow, .horizontal), (.kDownArrow, .vertical): // Next Candidate
_ = ctlCandidate.highlightNextCandidate() ? {}() : delegate.callError("3CEFB82E")
case (.kUpArrow, .horizontal), (.kLeftArrow, .vertical): // Previous Line
_ = ctlCandidate.showPreviousLine() ? {}() : delegate.callError("827BBD79")
case (.kDownArrow, .horizontal), (.kRightArrow, .vertical): // Next Line
_ = ctlCandidate.showNextLine() ? {}() : delegate.callError("7A0C7FBD")
default: break handleArrowKey
} }
return true return true
} case .kHome:
_ =
// MARK: PgDn (ctlCandidate.selectedCandidateIndex == 0)
? delegate.callError("9B6EDE8D") : (ctlCandidate.selectedCandidateIndex = 0)
if input.isPageDown {
let updated: Bool = ctlCandidate.showNextPage()
if !updated {
delegate.callError("9B691919")
}
return true return true
} case .kEnd:
let maxIndex = state.candidates.count - 1
// MARK: PgUp _ =
(ctlCandidate.selectedCandidateIndex == maxIndex)
if input.isPageUp { ? delegate.callError("9B69AAAD") : (ctlCandidate.selectedCandidateIndex = maxIndex)
let updated: Bool = ctlCandidate.showPreviousPage()
if !updated {
delegate.callError("9569955D")
}
return true
}
// MARK: Left Arrow
if input.isLeft {
switch ctlCandidate.currentLayout {
case .horizontal:
if !ctlCandidate.highlightPreviousCandidate() {
delegate.callError("1145148D")
}
case .vertical:
if !ctlCandidate.showPreviousLine() {
delegate.callError("1919810D")
}
@unknown default:
break
}
return true
}
// MARK: Right Arrow
if input.isRight {
switch ctlCandidate.currentLayout {
case .horizontal:
if !ctlCandidate.highlightNextCandidate() {
delegate.callError("9B65138D")
}
case .vertical:
if !ctlCandidate.showNextLine() {
delegate.callError("9244908D")
}
@unknown default:
break
}
return true
}
// MARK: Up Arrow
if input.isUp {
switch ctlCandidate.currentLayout {
case .horizontal:
if !ctlCandidate.showPreviousLine() {
delegate.callError("9B614524")
}
case .vertical:
if !ctlCandidate.highlightPreviousCandidate() {
delegate.callError("ASD9908D")
}
@unknown default:
break
}
return true
}
// MARK: Down Arrow
if input.isDown {
switch ctlCandidate.currentLayout {
case .horizontal:
if !ctlCandidate.showNextLine() {
delegate.callError("92B990DD")
break
}
case .vertical:
if !ctlCandidate.highlightNextCandidate() {
delegate.callError("6B99908D")
}
@unknown default:
break
}
return true
}
// MARK: Home Key
if input.isHome {
if ctlCandidate.selectedCandidateIndex == 0 {
delegate.callError("9B6EDE8D")
} else {
ctlCandidate.selectedCandidateIndex = 0
}
return true
}
// MARK: End Key
if state.candidates.isEmpty {
return false
} else { // count > 0!isEmpty滿
if input.isEnd {
if ctlCandidate.selectedCandidateIndex == state.candidates.count - 1 {
delegate.callError("9B69AAAD")
} else {
ctlCandidate.selectedCandidateIndex = state.candidates.count - 1
}
return true return true
default: break
} }
} }