KeyHandler // Reducing the usage of "as!", etc.

This commit is contained in:
ShikiSuen 2022-04-19 13:08:08 +08:00
parent 0ba02d3f11
commit c1ed41cf16
3 changed files with 366 additions and 358 deletions

View File

@ -37,8 +37,7 @@ import Cocoa
) -> Bool { ) -> Bool {
let inputText = input.inputText let inputText = input.inputText
let charCode: UniChar = input.charCode let charCode: UniChar = input.charCode
let ctlCandidateCurrent = delegate!.ctlCandidate(for: self) as! ctlCandidate if let ctlCandidateCurrent = delegate!.ctlCandidate(for: self) as? ctlCandidate {
// MARK: Cancel Candidate // MARK: Cancel Candidate
let cancelCandidateKey = let cancelCandidateKey =
@ -247,16 +246,16 @@ import Cocoa
return true return true
} }
// MARK: End Key
var candidates: [String]! var candidates: [String]!
if state is InputState.ChoosingCandidate { if let state = state as? InputState.ChoosingCandidate {
candidates = (state as! InputState.ChoosingCandidate).candidates candidates = state.candidates
} else if state is InputState.AssociatedPhrases { } else if let state = state as? InputState.AssociatedPhrases {
candidates = (state as! InputState.AssociatedPhrases).candidates candidates = state.candidates
} }
// MARK: End Key
if candidates.isEmpty { if candidates.isEmpty {
return false return false
} else { // count > 0!isEmpty滿 } else { // count > 0!isEmpty滿
@ -267,7 +266,6 @@ import Cocoa
} else { } else {
ctlCandidateCurrent.selectedCandidateIndex = UInt(candidates.count - 1) ctlCandidateCurrent.selectedCandidateIndex = UInt(candidates.count - 1)
} }
return true
} }
} }
@ -279,7 +277,11 @@ import Cocoa
var index: Int = NSNotFound var index: Int = NSNotFound
var match: String! var match: String!
if state is InputState.AssociatedPhrases { match = input.inputTextIgnoringModifiers } else { match = inputText } if state is InputState.AssociatedPhrases {
match = input.inputTextIgnoringModifiers
} else {
match = inputText
}
var j = 0 var j = 0
while j < ctlCandidateCurrent.keyLabels.count { while j < ctlCandidateCurrent.keyLabels.count {
@ -294,7 +296,9 @@ import Cocoa
if index != NSNotFound { if index != NSNotFound {
let candidateIndex: UInt = ctlCandidateCurrent.candidateIndexAtKeyLabelIndex(UInt(index)) let candidateIndex: UInt = ctlCandidateCurrent.candidateIndexAtKeyLabelIndex(UInt(index))
if candidateIndex != UInt.max { if candidateIndex != UInt.max {
delegate!.keyHandler(self, didSelectCandidateAt: Int(candidateIndex), ctlCandidate: ctlCandidateCurrent) delegate!.keyHandler(
self, didSelectCandidateAt: Int(candidateIndex), ctlCandidate: ctlCandidateCurrent
)
return true return true
} }
} }
@ -353,6 +357,7 @@ import Cocoa
return true return true
} }
} }
} // END: "if let ctlCandidateCurrent"
IME.prtDebugIntel("172A0F81") IME.prtDebugIntel("172A0F81")
errorCallback() errorCallback()

View File

@ -125,16 +125,13 @@ import Cocoa
// MARK: Handle Marking. // MARK: Handle Marking.
if state is InputState.Marking { if let marking = state as? InputState.Marking {
let marking = state as! InputState.Marking
if _handleMarkingState( if _handleMarkingState(
state as! InputState.Marking, input: input, stateCallback: stateCallback, marking, input: input, stateCallback: stateCallback,
errorCallback: errorCallback errorCallback: errorCallback
) { ) {
return true return true
} }
state = marking.convertToInputting() state = marking.convertToInputting()
stateCallback(state) stateCallback(state)
} }
@ -224,16 +221,17 @@ import Cocoa
// MARK: Calling candidate window using Space or Down or PageUp / PageDn. // MARK: Calling candidate window using Space or Down or PageUp / PageDn.
if isPhoneticReadingBufferEmpty() && (state is InputState.NotEmpty) if let currentState = state as? InputState.NotEmpty {
&& (input.isExtraChooseCandidateKey || input.isExtraChooseCandidateKeyReverse || input.isSpace if isPhoneticReadingBufferEmpty(),
input.isExtraChooseCandidateKey || input.isExtraChooseCandidateKeyReverse || input.isSpace
|| input.isPageDown || input.isPageUp || input.isTab || input.isPageDown || input.isPageUp || input.isTab
|| (input.useVerticalMode && (input.isVerticalModeOnlyChooseCandidateKey))) || (input.useVerticalMode && (input.isVerticalModeOnlyChooseCandidateKey))
{ {
if input.isSpace { if input.isSpace {
// If the Space key is NOT set to be a selection key // If the Space key is NOT set to be a selection key
if input.isShiftHold || !mgrPrefs.chooseCandidateUsingSpace { if input.isShiftHold || !mgrPrefs.chooseCandidateUsingSpace {
if getBuilderCursorIndex() >= getBuilderLength() { if getBuilderCursorIndex() >= getBuilderLength() {
let composingBuffer = (state as! InputState.NotEmpty).composingBuffer let composingBuffer = currentState.composingBuffer
if (composingBuffer.count) != 0 { if (composingBuffer.count) != 0 {
let committing = InputState.Committing(poppedText: composingBuffer) let committing = InputState.Committing(poppedText: composingBuffer)
stateCallback(committing) stateCallback(committing)
@ -254,12 +252,13 @@ import Cocoa
} }
} }
let choosingCandidates = _buildCandidateState( let choosingCandidates = _buildCandidateState(
state as! InputState.NotEmpty, currentState,
useVerticalMode: input.useVerticalMode useVerticalMode: input.useVerticalMode
) )
stateCallback(choosingCandidates) stateCallback(choosingCandidates)
return true return true
} }
}
// MARK: - // MARK: -

View File

@ -228,11 +228,13 @@ import Cocoa
clear() clear()
let current = state as! InputState.Inputting if let current = state as? InputState.Inputting {
let composingBuffer = current.composingBuffer let composingBuffer = current.composingBuffer
let committing = InputState.Committing(poppedText: composingBuffer) let committing = InputState.Committing(poppedText: composingBuffer)
stateCallback(committing) stateCallback(committing)
}
let empty = InputState.Empty() let empty = InputState.Empty()
stateCallback(empty) stateCallback(empty)
return true return true
@ -468,8 +470,7 @@ import Cocoa
return true return true
} }
let currentState = state as! InputState.Inputting if let currentState = state as? InputState.Inputting {
if input.isShiftHold { if input.isShiftHold {
// Shift + Right // Shift + Right
if currentState.cursorIndex < (currentState.composingBuffer as NSString).length { if currentState.cursorIndex < (currentState.composingBuffer as NSString).length {
@ -499,6 +500,8 @@ import Cocoa
stateCallback(state) stateCallback(state)
} }
} }
}
return true return true
} }
@ -519,8 +522,7 @@ import Cocoa
return true return true
} }
let currentState = state as! InputState.Inputting if let currentState = state as? InputState.Inputting {
if input.isShiftHold { if input.isShiftHold {
// Shift + left // Shift + left
if currentState.cursorIndex > 0 { if currentState.cursorIndex > 0 {
@ -550,6 +552,8 @@ import Cocoa
stateCallback(state) stateCallback(state)
} }
} }
}
return true return true
} }
} }