KeyHandler // Refactor if-let to guard-let for state conditioning.
This commit is contained in:
parent
6d6e50ab8f
commit
6e6cce7f4f
|
@ -256,16 +256,10 @@ extension KeyHandler {
|
||||||
stateCallback: @escaping (InputState) -> Void,
|
stateCallback: @escaping (InputState) -> Void,
|
||||||
errorCallback _: @escaping () -> Void
|
errorCallback _: @escaping () -> Void
|
||||||
) -> Bool {
|
) -> Bool {
|
||||||
if !(state is InputState.Inputting) {
|
guard let currentState = state as? InputState.Inputting else { return false }
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
clear()
|
clear()
|
||||||
|
stateCallback(InputState.Committing(poppedText: currentState.composingBuffer))
|
||||||
if let current = state as? InputState.Inputting {
|
|
||||||
stateCallback(InputState.Committing(poppedText: current.composingBuffer))
|
|
||||||
}
|
|
||||||
|
|
||||||
stateCallback(InputState.Empty())
|
stateCallback(InputState.Empty())
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -277,9 +271,7 @@ extension KeyHandler {
|
||||||
stateCallback: @escaping (InputState) -> Void,
|
stateCallback: @escaping (InputState) -> Void,
|
||||||
errorCallback _: @escaping () -> Void
|
errorCallback _: @escaping () -> Void
|
||||||
) -> Bool {
|
) -> Bool {
|
||||||
if !(state is InputState.Inputting) {
|
guard state is InputState.Inputting else { return false }
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
var composingBuffer = currentReadings().joined(separator: "-")
|
var composingBuffer = currentReadings().joined(separator: "-")
|
||||||
if mgrPrefs.inlineDumpPinyinInLieuOfZhuyin {
|
if mgrPrefs.inlineDumpPinyinInLieuOfZhuyin {
|
||||||
|
@ -305,9 +297,7 @@ extension KeyHandler {
|
||||||
stateCallback: @escaping (InputState) -> Void,
|
stateCallback: @escaping (InputState) -> Void,
|
||||||
errorCallback _: @escaping () -> Void
|
errorCallback _: @escaping () -> Void
|
||||||
) -> Bool {
|
) -> Bool {
|
||||||
if !(state is InputState.Inputting) {
|
guard state is InputState.Inputting else { return false }
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
var composed = ""
|
var composed = ""
|
||||||
|
|
||||||
|
@ -346,9 +336,7 @@ extension KeyHandler {
|
||||||
stateCallback: @escaping (InputState) -> Void,
|
stateCallback: @escaping (InputState) -> Void,
|
||||||
errorCallback: @escaping () -> Void
|
errorCallback: @escaping () -> Void
|
||||||
) -> Bool {
|
) -> Bool {
|
||||||
if !(state is InputState.Inputting) {
|
guard state is InputState.Inputting else { return false }
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if _composer.hasToneMarker(withNothingElse: true) {
|
if _composer.hasToneMarker(withNothingElse: true) {
|
||||||
_composer.clear()
|
_composer.clear()
|
||||||
|
@ -381,9 +369,7 @@ extension KeyHandler {
|
||||||
stateCallback: @escaping (InputState) -> Void,
|
stateCallback: @escaping (InputState) -> Void,
|
||||||
errorCallback: @escaping () -> Void
|
errorCallback: @escaping () -> Void
|
||||||
) -> Bool {
|
) -> Bool {
|
||||||
if !(state is InputState.Inputting) {
|
guard state is InputState.Inputting else { return false }
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if _composer.isEmpty {
|
if _composer.isEmpty {
|
||||||
if getBuilderCursorIndex() != getBuilderLength() {
|
if getBuilderCursorIndex() != getBuilderLength() {
|
||||||
|
@ -417,9 +403,7 @@ extension KeyHandler {
|
||||||
stateCallback: @escaping (InputState) -> Void,
|
stateCallback: @escaping (InputState) -> Void,
|
||||||
errorCallback: @escaping () -> Void
|
errorCallback: @escaping () -> Void
|
||||||
) -> Bool {
|
) -> Bool {
|
||||||
if !(state is InputState.Inputting) {
|
guard state is InputState.Inputting else { return false }
|
||||||
return false
|
|
||||||
}
|
|
||||||
if !_composer.isEmpty {
|
if !_composer.isEmpty {
|
||||||
IME.prtDebugIntel("9B6F908D")
|
IME.prtDebugIntel("9B6F908D")
|
||||||
errorCallback()
|
errorCallback()
|
||||||
|
@ -435,9 +419,7 @@ extension KeyHandler {
|
||||||
stateCallback: @escaping (InputState) -> Void,
|
stateCallback: @escaping (InputState) -> Void,
|
||||||
errorCallback: @escaping () -> Void
|
errorCallback: @escaping () -> Void
|
||||||
) -> Bool {
|
) -> Bool {
|
||||||
if !(state is InputState.Inputting) {
|
guard state is InputState.Inputting else { return false }
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if !_composer.isEmpty {
|
if !_composer.isEmpty {
|
||||||
IME.prtDebugIntel("ABC44080")
|
IME.prtDebugIntel("ABC44080")
|
||||||
|
@ -465,9 +447,7 @@ extension KeyHandler {
|
||||||
stateCallback: @escaping (InputState) -> Void,
|
stateCallback: @escaping (InputState) -> Void,
|
||||||
errorCallback: @escaping () -> Void
|
errorCallback: @escaping () -> Void
|
||||||
) -> Bool {
|
) -> Bool {
|
||||||
if !(state is InputState.Inputting) {
|
guard state is InputState.Inputting else { return false }
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if !_composer.isEmpty {
|
if !_composer.isEmpty {
|
||||||
IME.prtDebugIntel("9B69908D")
|
IME.prtDebugIntel("9B69908D")
|
||||||
|
@ -495,7 +475,7 @@ extension KeyHandler {
|
||||||
stateCallback: @escaping (InputState) -> Void,
|
stateCallback: @escaping (InputState) -> Void,
|
||||||
errorCallback _: @escaping () -> Void
|
errorCallback _: @escaping () -> Void
|
||||||
) -> Bool {
|
) -> Bool {
|
||||||
if !(state is InputState.Inputting) { return false }
|
guard state is InputState.Inputting else { return false }
|
||||||
|
|
||||||
let escToClearInputBufferEnabled: Bool = mgrPrefs.escToCleanInputBuffer
|
let escToClearInputBufferEnabled: Bool = mgrPrefs.escToCleanInputBuffer
|
||||||
|
|
||||||
|
@ -528,7 +508,7 @@ extension KeyHandler {
|
||||||
stateCallback: @escaping (InputState) -> Void,
|
stateCallback: @escaping (InputState) -> Void,
|
||||||
errorCallback: @escaping () -> Void
|
errorCallback: @escaping () -> Void
|
||||||
) -> Bool {
|
) -> Bool {
|
||||||
if !(state is InputState.Inputting) { return false }
|
guard let currentState = state as? InputState.Inputting else { return false }
|
||||||
|
|
||||||
if !_composer.isEmpty {
|
if !_composer.isEmpty {
|
||||||
IME.prtDebugIntel("B3BA5257")
|
IME.prtDebugIntel("B3BA5257")
|
||||||
|
@ -537,34 +517,32 @@ extension KeyHandler {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
let nextPosition = (currentState.composingBuffer as NSString).nextUtf16Position(
|
||||||
let nextPosition = (currentState.composingBuffer as NSString).nextUtf16Position(
|
for: Int(currentState.cursorIndex))
|
||||||
for: Int(currentState.cursorIndex))
|
let marking: InputState.Marking! = InputState.Marking(
|
||||||
let marking: InputState.Marking! = InputState.Marking(
|
composingBuffer: currentState.composingBuffer,
|
||||||
composingBuffer: currentState.composingBuffer,
|
cursorIndex: currentState.cursorIndex,
|
||||||
cursorIndex: currentState.cursorIndex,
|
markerIndex: UInt(nextPosition),
|
||||||
markerIndex: UInt(nextPosition),
|
readings: currentReadings()
|
||||||
readings: currentReadings()
|
)
|
||||||
)
|
marking.tooltipForInputting = currentState.tooltip
|
||||||
marking.tooltipForInputting = currentState.tooltip
|
stateCallback(marking)
|
||||||
stateCallback(marking)
|
|
||||||
} else {
|
|
||||||
IME.prtDebugIntel("BB7F6DB9")
|
|
||||||
errorCallback()
|
|
||||||
stateCallback(state)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if getBuilderCursorIndex() < getBuilderLength() {
|
IME.prtDebugIntel("BB7F6DB9")
|
||||||
setBuilderCursorIndex(value: getBuilderCursorIndex() + 1)
|
errorCallback()
|
||||||
stateCallback(buildInputtingState())
|
stateCallback(state)
|
||||||
} else {
|
}
|
||||||
IME.prtDebugIntel("A96AAD58")
|
} else {
|
||||||
errorCallback()
|
if getBuilderCursorIndex() < getBuilderLength() {
|
||||||
stateCallback(state)
|
setBuilderCursorIndex(value: getBuilderCursorIndex() + 1)
|
||||||
}
|
stateCallback(buildInputtingState())
|
||||||
|
} else {
|
||||||
|
IME.prtDebugIntel("A96AAD58")
|
||||||
|
errorCallback()
|
||||||
|
stateCallback(state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -579,7 +557,7 @@ extension KeyHandler {
|
||||||
stateCallback: @escaping (InputState) -> Void,
|
stateCallback: @escaping (InputState) -> Void,
|
||||||
errorCallback: @escaping () -> Void
|
errorCallback: @escaping () -> Void
|
||||||
) -> Bool {
|
) -> Bool {
|
||||||
if !(state is InputState.Inputting) { return false }
|
guard let currentState = state as? InputState.Inputting else { return false }
|
||||||
|
|
||||||
if !_composer.isEmpty {
|
if !_composer.isEmpty {
|
||||||
IME.prtDebugIntel("6ED95318")
|
IME.prtDebugIntel("6ED95318")
|
||||||
|
@ -588,34 +566,32 @@ extension KeyHandler {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
let previousPosition = (currentState.composingBuffer as NSString).previousUtf16Position(
|
||||||
let previousPosition = (currentState.composingBuffer as NSString).previousUtf16Position(
|
for: Int(currentState.cursorIndex))
|
||||||
for: Int(currentState.cursorIndex))
|
let marking: InputState.Marking! = InputState.Marking(
|
||||||
let marking: InputState.Marking! = InputState.Marking(
|
composingBuffer: currentState.composingBuffer,
|
||||||
composingBuffer: currentState.composingBuffer,
|
cursorIndex: currentState.cursorIndex,
|
||||||
cursorIndex: currentState.cursorIndex,
|
markerIndex: UInt(previousPosition),
|
||||||
markerIndex: UInt(previousPosition),
|
readings: currentReadings()
|
||||||
readings: currentReadings()
|
)
|
||||||
)
|
marking.tooltipForInputting = currentState.tooltip
|
||||||
marking.tooltipForInputting = currentState.tooltip
|
stateCallback(marking)
|
||||||
stateCallback(marking)
|
|
||||||
} else {
|
|
||||||
IME.prtDebugIntel("D326DEA3")
|
|
||||||
errorCallback()
|
|
||||||
stateCallback(state)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if getBuilderCursorIndex() > 0 {
|
IME.prtDebugIntel("D326DEA3")
|
||||||
setBuilderCursorIndex(value: getBuilderCursorIndex() - 1)
|
errorCallback()
|
||||||
stateCallback(buildInputtingState())
|
stateCallback(state)
|
||||||
} else {
|
}
|
||||||
IME.prtDebugIntel("7045E6F3")
|
} else {
|
||||||
errorCallback()
|
if getBuilderCursorIndex() > 0 {
|
||||||
stateCallback(state)
|
setBuilderCursorIndex(value: getBuilderCursorIndex() - 1)
|
||||||
}
|
stateCallback(buildInputtingState())
|
||||||
|
} else {
|
||||||
|
IME.prtDebugIntel("7045E6F3")
|
||||||
|
errorCallback()
|
||||||
|
stateCallback(state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue