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

View File

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

View File

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