KeyHandler // Improve behaviors of Delete & BackSpace.

- Allow using Delete to clear composer.
- Allow using Shift+BkSp/Delete to clear compositor.
This commit is contained in:
ShikiSuen 2022-08-06 15:59:13 +08:00
parent c7f218ae93
commit 71e8e3645a
2 changed files with 25 additions and 9 deletions

View File

@ -259,13 +259,13 @@ extension KeyHandler {
// MARK: Backspace // MARK: Backspace
if input.isBackSpace { if input.isBackSpace {
return handleBackSpace(state: state, stateCallback: stateCallback, errorCallback: errorCallback) return handleBackSpace(state: state, input: input, stateCallback: stateCallback, errorCallback: errorCallback)
} }
// MARK: Delete // MARK: Delete
if input.isDelete || input.emacsKey == EmacsKey.delete { if input.isDelete || input.emacsKey == EmacsKey.delete {
return handleDelete(state: state, stateCallback: stateCallback, errorCallback: errorCallback) return handleDelete(state: state, input: input, stateCallback: stateCallback, errorCallback: errorCallback)
} }
// MARK: Enter // MARK: Enter

View File

@ -381,16 +381,25 @@ extension KeyHandler {
/// Backspace (macOS Delete) /// Backspace (macOS Delete)
/// - Parameters: /// - Parameters:
/// - state: /// - state:
/// - input:
/// - stateCallback: /// - stateCallback:
/// - errorCallback: /// - errorCallback:
/// - Returns: ctlInputMethod IMK /// - Returns: ctlInputMethod IMK
func handleBackSpace( func handleBackSpace(
state: InputStateProtocol, state: InputStateProtocol,
input: InputSignal,
stateCallback: @escaping (InputStateProtocol) -> Void, stateCallback: @escaping (InputStateProtocol) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void
) -> Bool { ) -> Bool {
guard state is InputState.Inputting else { return false } guard state is InputState.Inputting else { return false }
if input.isShiftHold {
clear()
stateCallback(InputState.EmptyIgnoringPreviousState())
stateCallback(InputState.Empty())
return true
}
if composer.hasToneMarker(withNothingElse: true) { if composer.hasToneMarker(withNothingElse: true) {
composer.clear() composer.clear()
} else if composer.isEmpty { } else if composer.isEmpty {
@ -421,32 +430,39 @@ extension KeyHandler {
/// PC Delete (macOS Fn+BackSpace) /// PC Delete (macOS Fn+BackSpace)
/// - Parameters: /// - Parameters:
/// - state: /// - state:
/// - input:
/// - stateCallback: /// - stateCallback:
/// - errorCallback: /// - errorCallback:
/// - Returns: ctlInputMethod IMK /// - Returns: ctlInputMethod IMK
func handleDelete( func handleDelete(
state: InputStateProtocol, state: InputStateProtocol,
input: InputSignal,
stateCallback: @escaping (InputStateProtocol) -> Void, stateCallback: @escaping (InputStateProtocol) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void
) -> Bool { ) -> Bool {
guard state is InputState.Inputting else { return false } guard state is InputState.Inputting else { return false }
guard composer.isEmpty else { if input.isShiftHold {
IME.prtDebugIntel("9C69908D") clear()
errorCallback() stateCallback(InputState.EmptyIgnoringPreviousState())
stateCallback(state) stateCallback(InputState.Empty())
return true return true
} }
guard compositor.cursor != compositor.length else { if compositor.cursor == compositor.length, composer.isEmpty {
IME.prtDebugIntel("9B69938D") IME.prtDebugIntel("9B69938D")
errorCallback() errorCallback()
stateCallback(state) stateCallback(state)
return true return true
} }
compositor.dropReading(direction: .front) if composer.isEmpty {
walk() compositor.dropReading(direction: .front)
walk()
} else {
composer.clear()
}
let inputting = buildInputtingState let inputting = buildInputtingState
// count > 0!isEmpty滿 // count > 0!isEmpty滿
switch inputting.composingBuffer.isEmpty { switch inputting.composingBuffer.isEmpty {