InputHandler // Rebranding to InputSignal, plus refactoring.
This commit is contained in:
parent
dc3c1e2f1d
commit
384a4138f2
|
@ -117,7 +117,7 @@ enum CharCode: UInt /* 16 */ {
|
|||
// ... but only focuses on which physical key is pressed.
|
||||
}
|
||||
|
||||
class InputHandler: NSObject {
|
||||
struct InputSignal: CustomStringConvertible {
|
||||
private(set) var useVerticalMode: Bool
|
||||
private(set) var inputText: String?
|
||||
private(set) var inputTextIgnoringModifiers: String?
|
||||
|
@ -125,15 +125,15 @@ class InputHandler: NSObject {
|
|||
private(set) var keyCode: UInt16
|
||||
private var isFlagChanged: Bool
|
||||
private var flags: NSEvent.ModifierFlags
|
||||
private var cursorForwardKey: KeyCode
|
||||
private var cursorBackwardKey: KeyCode
|
||||
private var extraChooseCandidateKey: KeyCode
|
||||
private var extraChooseCandidateKeyReverse: KeyCode
|
||||
private var absorbedArrowKey: KeyCode
|
||||
private var verticalModeOnlyChooseCandidateKey: KeyCode
|
||||
private var cursorForwardKey: KeyCode = .kNone
|
||||
private var cursorBackwardKey: KeyCode = .kNone
|
||||
private var extraChooseCandidateKey: KeyCode = .kNone
|
||||
private var extraChooseCandidateKeyReverse: KeyCode = .kNone
|
||||
private var absorbedArrowKey: KeyCode = .kNone
|
||||
private var verticalModeOnlyChooseCandidateKey: KeyCode = .kNone
|
||||
private(set) var emacsKey: vChewingEmacsKey
|
||||
|
||||
init(
|
||||
public init(
|
||||
inputText: String?, keyCode: UInt16, charCode: UInt16, flags: NSEvent.ModifierFlags,
|
||||
isVerticalMode: Bool, inputTextIgnoringModifiers: String? = nil
|
||||
) {
|
||||
|
@ -150,17 +150,11 @@ class InputHandler: NSObject {
|
|||
emacsKey = EmacsKeyHelper.detect(
|
||||
charCode: AppleKeyboardConverter.cnvApple2ABC(charCode), flags: flags
|
||||
)
|
||||
// Define Arrow Keys
|
||||
cursorForwardKey = useVerticalMode ? .kDownArrow : .kRightArrow
|
||||
cursorBackwardKey = useVerticalMode ? .kUpArrow : .kLeftArrow
|
||||
extraChooseCandidateKey = useVerticalMode ? .kLeftArrow : .kDownArrow
|
||||
extraChooseCandidateKeyReverse = useVerticalMode ? .kRightArrow : .kUpArrow
|
||||
absorbedArrowKey = useVerticalMode ? .kRightArrow : .kUpArrow
|
||||
verticalModeOnlyChooseCandidateKey = useVerticalMode ? absorbedArrowKey : .kNone
|
||||
super.init()
|
||||
// Define Arrow Keys in the same way above.
|
||||
defineArrowKeys()
|
||||
}
|
||||
|
||||
init(event: NSEvent, isVerticalMode: Bool) {
|
||||
public init(event: NSEvent, isVerticalMode: Bool) {
|
||||
inputText = AppleKeyboardConverter.cnvStringApple2ABC(event.characters ?? "")
|
||||
inputTextIgnoringModifiers = AppleKeyboardConverter.cnvStringApple2ABC(
|
||||
event.charactersIgnoringModifiers ?? "")
|
||||
|
@ -181,22 +175,20 @@ class InputHandler: NSObject {
|
|||
charCode: AppleKeyboardConverter.cnvApple2ABC(charCode), flags: flags
|
||||
)
|
||||
// Define Arrow Keys in the same way above.
|
||||
defineArrowKeys()
|
||||
}
|
||||
|
||||
mutating func defineArrowKeys() {
|
||||
cursorForwardKey = useVerticalMode ? .kDownArrow : .kRightArrow
|
||||
cursorBackwardKey = useVerticalMode ? .kUpArrow : .kLeftArrow
|
||||
extraChooseCandidateKey = useVerticalMode ? .kLeftArrow : .kDownArrow
|
||||
extraChooseCandidateKeyReverse = useVerticalMode ? .kRightArrow : .kUpArrow
|
||||
absorbedArrowKey = useVerticalMode ? .kRightArrow : .kUpArrow
|
||||
verticalModeOnlyChooseCandidateKey = useVerticalMode ? absorbedArrowKey : .kNone
|
||||
super.init()
|
||||
}
|
||||
|
||||
override var description: String {
|
||||
charCode = AppleKeyboardConverter.cnvApple2ABC(charCode)
|
||||
inputText = AppleKeyboardConverter.cnvStringApple2ABC(inputText ?? "")
|
||||
inputTextIgnoringModifiers = AppleKeyboardConverter.cnvStringApple2ABC(
|
||||
inputTextIgnoringModifiers ?? "")
|
||||
return
|
||||
"<\(super.description) inputText:\(String(describing: inputText)), inputTextIgnoringModifiers:\(String(describing: inputTextIgnoringModifiers)) charCode:\(charCode), keyCode:\(keyCode), flags:\(flags), cursorForwardKey:\(cursorForwardKey), cursorBackwardKey:\(cursorBackwardKey), extraChooseCandidateKey:\(extraChooseCandidateKey), extraChooseCandidateKeyReverse:\(extraChooseCandidateKeyReverse), absorbedArrowKey:\(absorbedArrowKey), verticalModeOnlyChooseCandidateKey:\(verticalModeOnlyChooseCandidateKey), emacsKey:\(emacsKey), useVerticalMode:\(useVerticalMode)>"
|
||||
var description: String {
|
||||
"<inputText:\(String(describing: inputText)), inputTextIgnoringModifiers:\(String(describing: inputTextIgnoringModifiers)) charCode:\(charCode), keyCode:\(keyCode), flags:\(flags), cursorForwardKey:\(cursorForwardKey), cursorBackwardKey:\(cursorBackwardKey), extraChooseCandidateKey:\(extraChooseCandidateKey), extraChooseCandidateKeyReverse:\(extraChooseCandidateKeyReverse), absorbedArrowKey:\(absorbedArrowKey), verticalModeOnlyChooseCandidateKey:\(verticalModeOnlyChooseCandidateKey), emacsKey:\(emacsKey), useVerticalMode:\(useVerticalMode)>"
|
||||
}
|
||||
|
||||
// 除了 ANSI charCode 以外,其餘一律過濾掉,免得純 Swift 版 KeyHandler 被餵屎。
|
||||
|
@ -365,7 +357,7 @@ enum vChewingEmacsKey: UInt16 {
|
|||
case nextPage = 22 // V
|
||||
}
|
||||
|
||||
class EmacsKeyHelper: NSObject {
|
||||
enum EmacsKeyHelper {
|
||||
static func detect(charCode: UniChar, flags: NSEvent.ModifierFlags) -> vChewingEmacsKey {
|
||||
let charCode = AppleKeyboardConverter.cnvApple2ABC(charCode)
|
||||
if flags.contains(.control) {
|
|
@ -31,7 +31,7 @@ import Cocoa
|
|||
extension KeyHandler {
|
||||
func handleCandidate(
|
||||
state: InputState,
|
||||
input: InputHandler,
|
||||
input: InputSignal,
|
||||
stateCallback: @escaping (InputState) -> Void,
|
||||
errorCallback: @escaping () -> Void
|
||||
) -> Bool {
|
||||
|
|
|
@ -30,7 +30,7 @@ import Cocoa
|
|||
|
||||
extension KeyHandler {
|
||||
func handle(
|
||||
input: InputHandler,
|
||||
input: InputSignal,
|
||||
state: InputState,
|
||||
stateCallback: @escaping (InputState) -> Void,
|
||||
errorCallback: @escaping () -> Void
|
||||
|
|
|
@ -134,7 +134,7 @@ extension KeyHandler {
|
|||
|
||||
func handleMarkingState(
|
||||
_ state: InputState.Marking,
|
||||
input: InputHandler,
|
||||
input: InputSignal,
|
||||
stateCallback: @escaping (InputState) -> Void,
|
||||
errorCallback: @escaping () -> Void
|
||||
) -> Bool {
|
||||
|
@ -504,7 +504,7 @@ extension KeyHandler {
|
|||
|
||||
func handleForward(
|
||||
state: InputState,
|
||||
input: InputHandler,
|
||||
input: InputSignal,
|
||||
stateCallback: @escaping (InputState) -> Void,
|
||||
errorCallback: @escaping () -> Void
|
||||
) -> Bool {
|
||||
|
@ -553,7 +553,7 @@ extension KeyHandler {
|
|||
|
||||
func handleBackward(
|
||||
state: InputState,
|
||||
input: InputHandler,
|
||||
input: InputSignal,
|
||||
stateCallback: @escaping (InputState) -> Void,
|
||||
errorCallback: @escaping () -> Void
|
||||
) -> Bool {
|
||||
|
|
|
@ -51,7 +51,7 @@ class ctlInputMethod: IMKInputController {
|
|||
|
||||
// 想讓 KeyHandler 能夠被外界調查狀態與參數的話,就得對 KeyHandler 做常態處理。
|
||||
// 這樣 InputState 可以藉由這個 ctlInputMethod 了解到當前的輸入模式是簡體中文還是繁體中文。
|
||||
// 然而,要是直接對 keyHandler 做常態處理的話,反而會導致 InputHandler 無法協同處理。
|
||||
// 然而,要是直接對 keyHandler 做常態處理的話,反而會導致 InputSignal 無法協同處理。
|
||||
// 所以才需要「currentKeyHandler」這個假 KeyHandler。
|
||||
// 這個「currentKeyHandler」僅用來讓其他模組知道當前的輸入模式是什麼模式,除此之外別無屌用。
|
||||
static var currentKeyHandler: KeyHandler = .init()
|
||||
|
@ -182,7 +182,7 @@ class ctlInputMethod: IMKInputController {
|
|||
IME.areWeUsingOurOwnPhraseEditor = false
|
||||
}
|
||||
|
||||
let input = InputHandler(event: event, isVerticalMode: useVerticalMode)
|
||||
let input = InputSignal(event: event, isVerticalMode: useVerticalMode)
|
||||
|
||||
// 無法列印的訊號輸入,一概不作處理。
|
||||
// 這個過程不能放在 KeyHandler 內,否則不會起作用。
|
||||
|
|
|
@ -106,7 +106,7 @@
|
|||
6ACA41FD15FC1D9000935EF6 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6ACA41F015FC1D9000935EF6 /* MainMenu.xib */; };
|
||||
6ACA420215FC1E5200935EF6 /* vChewing.app in Resources */ = {isa = PBXBuildFile; fileRef = 6A0D4EA215FC0D2D00ABF4B3 /* vChewing.app */; };
|
||||
D427F76C278CA2B0004A2160 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D427F76B278CA1BA004A2160 /* AppDelegate.swift */; };
|
||||
D456576E279E4F7B00DF6BC9 /* InputHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = D456576D279E4F7B00DF6BC9 /* InputHandler.swift */; };
|
||||
D456576E279E4F7B00DF6BC9 /* InputSignal.swift in Sources */ = {isa = PBXBuildFile; fileRef = D456576D279E4F7B00DF6BC9 /* InputSignal.swift */; };
|
||||
D461B792279DAC010070E734 /* InputState.swift in Sources */ = {isa = PBXBuildFile; fileRef = D461B791279DAC010070E734 /* InputState.swift */; };
|
||||
D47B92C027972AD100458394 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = D47B92BF27972AC800458394 /* main.swift */; };
|
||||
D47F7DCE278BFB57002F9DD7 /* ctlPrefWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = D47F7DCD278BFB57002F9DD7 /* ctlPrefWindow.swift */; };
|
||||
|
@ -311,7 +311,7 @@
|
|||
6ACA41F315FC1D9000935EF6 /* Installer-Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "Installer-Prefix.pch"; path = "Installer/Installer-Prefix.pch"; sourceTree = SOURCE_ROOT; };
|
||||
D427A9BF25ED28CC005D43E0 /* vChewing-Bridging-Header.h */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = "vChewing-Bridging-Header.h"; sourceTree = "<group>"; tabWidth = 4; usesTabs = 0; };
|
||||
D427F76B278CA1BA004A2160 /* AppDelegate.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = AppDelegate.swift; sourceTree = "<group>"; tabWidth = 2; usesTabs = 0; };
|
||||
D456576D279E4F7B00DF6BC9 /* InputHandler.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = InputHandler.swift; sourceTree = "<group>"; tabWidth = 2; usesTabs = 0; };
|
||||
D456576D279E4F7B00DF6BC9 /* InputSignal.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = InputSignal.swift; sourceTree = "<group>"; tabWidth = 2; usesTabs = 0; };
|
||||
D461B791279DAC010070E734 /* InputState.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = InputState.swift; sourceTree = "<group>"; tabWidth = 2; usesTabs = 0; };
|
||||
D47B92BF27972AC800458394 /* main.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = main.swift; sourceTree = "<group>"; tabWidth = 2; usesTabs = 0; };
|
||||
D47F7DCD278BFB57002F9DD7 /* ctlPrefWindow.swift */ = {isa = PBXFileReference; explicitFileType = sourcecode.swift; fileEncoding = 4; indentWidth = 2; lineEnding = 0; path = ctlPrefWindow.swift; sourceTree = "<group>"; tabWidth = 2; usesTabs = 0; };
|
||||
|
@ -411,7 +411,7 @@
|
|||
5B11328827B94CFB00E58451 /* AppleKeyboardConverter.swift */,
|
||||
D4E569DA27A34CC100AC2CEF /* CTools.h */,
|
||||
D4E569DB27A34CC100AC2CEF /* CTools.m */,
|
||||
D456576D279E4F7B00DF6BC9 /* InputHandler.swift */,
|
||||
D456576D279E4F7B00DF6BC9 /* InputSignal.swift */,
|
||||
D461B791279DAC010070E734 /* InputState.swift */,
|
||||
5BD0113C2818543900609769 /* KeyHandler_Core.swift */,
|
||||
5B782EC3280C243C007276DE /* KeyHandler_HandleCandidate.swift */,
|
||||
|
@ -1051,7 +1051,7 @@
|
|||
D47F7DD0278C0897002F9DD7 /* ctlNonModalAlertWindow.swift in Sources */,
|
||||
5B38F5A2281E2E49007D5F5D /* 0_Megrez.swift in Sources */,
|
||||
5B949BD92816DC5400D87B5D /* LineReader.swift in Sources */,
|
||||
D456576E279E4F7B00DF6BC9 /* InputHandler.swift in Sources */,
|
||||
D456576E279E4F7B00DF6BC9 /* InputSignal.swift in Sources */,
|
||||
5BA9FD1027FEDB6B002DE248 /* suiPrefPaneKeyboard.swift in Sources */,
|
||||
5B3133BF280B229700A4A505 /* KeyHandler_States.swift in Sources */,
|
||||
5BA9FD4327FEF3C8002DE248 /* Preferences.swift in Sources */,
|
||||
|
|
Loading…
Reference in New Issue