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

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

View File

@ -2,27 +2,27 @@
// Refactored from the ObjCpp-version of this class by: // Refactored from the ObjCpp-version of this class by:
// (c) 2011 and onwards The OpenVanilla Project (MIT License). // (c) 2011 and onwards The OpenVanilla Project (MIT License).
/* /*
Permission is hereby granted, free of charge, to any person obtaining a copy of Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so, the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions: subject to the following conditions:
1. The above copyright notice and this permission notice shall be included in 1. The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software. all copies or substantial portions of the Software.
2. No trademark license is granted to use the trade names, trademarks, service 2. No trademark license is granted to use the trade names, trademarks, service
marks, or product names of Contributor, except as required to fulfill notice marks, or product names of Contributor, except as required to fulfill notice
requirements above. requirements above.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
import Cocoa import Cocoa
@ -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
} }
} }