From 5fe5e2a0b02267df5828dd9708692cf4ecaa4c30 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Sun, 10 Apr 2022 19:37:02 +0800 Subject: [PATCH] UPR: ctlIME // Reset KeyHandler when toggling funcModes. - Also fixing a typo inherited from the upstream. --- .../Modules/IMEModules/ctlInputMethod.swift | 24 +++++++++++++------ .../IMEModules/ctlInputMethod_Menu.swift | 8 +++++++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/Source/Modules/IMEModules/ctlInputMethod.swift b/Source/Modules/IMEModules/ctlInputMethod.swift index ef75f81d..d6983324 100644 --- a/Source/Modules/IMEModules/ctlInputMethod.swift +++ b/Source/Modules/IMEModules/ctlInputMethod.swift @@ -49,7 +49,7 @@ class ctlInputMethod: IMKInputController { // MARK: - - private var currentCandidateClient: Any? + private var currentClient: Any? private var keyHandler: KeyHandler = KeyHandler() private var state: InputState = InputState.Empty() @@ -76,6 +76,15 @@ class ctlInputMethod: IMKInputController { keyHandler.delegate = self } + // MARK: - KeyHandler Reset Command + + func resetKeyHandler() { + if let currentClient = currentClient { + keyHandler.clear() + self.handle(state: InputState.Empty(), client: currentClient) + } + } + // MARK: - IMKStateSetting protocol methods override func activateServer(_ client: Any!) { @@ -84,7 +93,7 @@ class ctlInputMethod: IMKInputController { // Override the keyboard layout to the basic one. setKeyLayout() // reset the state - currentCandidateClient = nil + currentClient = client keyHandler.clear() keyHandler.syncWithPreferences() @@ -94,6 +103,7 @@ class ctlInputMethod: IMKInputController { override func deactivateServer(_ client: Any!) { keyHandler.clear() + currentClient = nil self.handle(state: .Empty(), client: client) self.handle(state: .Deactivated(), client: client) } @@ -225,7 +235,7 @@ extension ctlInputMethod { } private func handle(state: InputState.Deactivated, previous: InputState, client: Any?) { - currentCandidateClient = nil + currentClient = nil ctlCandidateCurrent?.delegate = nil ctlCandidateCurrent?.visible = false @@ -441,7 +451,7 @@ extension ctlInputMethod { ctlCandidateCurrent?.delegate = self ctlCandidateCurrent?.reloadData() - currentCandidateClient = client + currentClient = client ctlCandidateCurrent?.visible = true @@ -558,7 +568,7 @@ extension ctlInputMethod: ctlCandidateDelegate { } func ctlCandidate(_ controller: ctlCandidate, didSelectCandidateAtIndex index: UInt) { - let client = currentCandidateClient + let client = currentClient if let state = state as? InputState.SymbolTable, let node = state.node.children?[Int(index)] @@ -566,7 +576,7 @@ extension ctlInputMethod: ctlCandidateDelegate { if let children = node.children, !children.isEmpty { self.handle( state: .SymbolTable(node: node, useVerticalMode: state.useVerticalMode), - client: currentCandidateClient) + client: currentClient) } else { self.handle(state: .Committing(poppedText: node.title), client: client) self.handle(state: .Empty(), client: client) @@ -603,7 +613,7 @@ extension ctlInputMethod: ctlCandidateDelegate { if let state = state as? InputState.AssociatedPhrases { let selectedValue = state.candidates[Int(index)] - handle(state: .Committing(poppedText: selectedValue), client: currentCandidateClient) + handle(state: .Committing(poppedText: selectedValue), client: currentClient) if mgrPrefs.associatedPhrasesEnabled, let associatePhrases = keyHandler.buildAssociatePhraseState( withKey: selectedValue, useVerticalMode: state.useVerticalMode) diff --git a/Source/Modules/IMEModules/ctlInputMethod_Menu.swift b/Source/Modules/IMEModules/ctlInputMethod_Menu.swift index a9152e56..a6a5aca2 100644 --- a/Source/Modules/IMEModules/ctlInputMethod_Menu.swift +++ b/Source/Modules/IMEModules/ctlInputMethod_Menu.swift @@ -163,6 +163,7 @@ extension ctlInputMethod { mgrPrefs.toggleSCPCTypingModeEnabled() ? NSLocalizedString("NotificationSwitchON", comment: "") : NSLocalizedString("NotificationSwitchOFF", comment: ""))) + resetKeyHandler() } @objc func toggleChineseConverter(_ sender: Any?) { @@ -172,6 +173,7 @@ extension ctlInputMethod { mgrPrefs.toggleChineseConversionEnabled() ? NSLocalizedString("NotificationSwitchON", comment: "") : NSLocalizedString("NotificationSwitchOFF", comment: ""))) + resetKeyHandler() } @objc func toggleShiftJISShinjitaiOutput(_ sender: Any?) { @@ -181,6 +183,7 @@ extension ctlInputMethod { mgrPrefs.toggleShiftJISShinjitaiOutputEnabled() ? NSLocalizedString("NotificationSwitchON", comment: "") : NSLocalizedString("NotificationSwitchOFF", comment: ""))) + resetKeyHandler() } @objc func toggleHalfWidthPunctuation(_ sender: Any?) { @@ -191,6 +194,7 @@ extension ctlInputMethod { mgrPrefs.toggleHalfWidthPunctuationEnabled() ? NSLocalizedString("NotificationSwitchON", comment: "") : NSLocalizedString("NotificationSwitchOFF", comment: ""))) + resetKeyHandler() } @objc func toggleCNS11643Enabled(_ sender: Any?) { @@ -200,6 +204,7 @@ extension ctlInputMethod { mgrPrefs.toggleCNS11643Enabled() ? NSLocalizedString("NotificationSwitchON", comment: "") : NSLocalizedString("NotificationSwitchOFF", comment: ""))) + resetKeyHandler() } @objc func toggleSymbolEnabled(_ sender: Any?) { @@ -209,6 +214,7 @@ extension ctlInputMethod { mgrPrefs.toggleSymbolInputEnabled() ? NSLocalizedString("NotificationSwitchON", comment: "") : NSLocalizedString("NotificationSwitchOFF", comment: ""))) + resetKeyHandler() } @objc func toggleAssociatedPhrasesEnabled(_ sender: Any?) { @@ -219,6 +225,7 @@ extension ctlInputMethod { mgrPrefs.toggleAssociatedPhrasesEnabled() ? NSLocalizedString("NotificationSwitchON", comment: "") : NSLocalizedString("NotificationSwitchOFF", comment: ""))) + resetKeyHandler() } @objc func togglePhraseReplacement(_ sender: Any?) { @@ -228,6 +235,7 @@ extension ctlInputMethod { mgrPrefs.togglePhraseReplacementEnabled() ? NSLocalizedString("NotificationSwitchON", comment: "") : NSLocalizedString("NotificationSwitchOFF", comment: ""))) + resetKeyHandler() } @objc func selfUninstall(_ sender: Any?) {