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:
// (c) 2011 and onwards The OpenVanilla Project (MIT License).
/*
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
the Software without restriction, including without limitation the rights to
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,
subject to the following conditions:
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
the Software without restriction, including without limitation the rights to
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,
subject to the following conditions:
1. The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
1. The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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
requirements above.
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
requirements above.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
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
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.
*/
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
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
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.
*/
import Cocoa
@ -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
}
}