From 1305ce2a26346efaa594ca3c174b8e3e93a718d4 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Fri, 23 Dec 2022 12:49:43 +0800 Subject: [PATCH] Repo // Move certain vertical-cases from IMEState to SessionCtl. - InputHandler // Fix a case which should use delegate.isVerticalTyping. --- .../Sources/Shared/Protocols/IMEStateProtocol.swift | 4 ---- Source/Modules/IMEState.swift | 9 --------- Source/Modules/IMEStateData.swift | 3 +-- Source/Modules/InputHandler_Core.swift | 1 + Source/Modules/InputHandler_HandleInput.swift | 4 ++-- Source/Modules/InputHandler_HandleStates.swift | 3 ++- Source/Modules/SessionCtl_Core.swift | 3 +++ Source/Modules/SessionCtl_HandleDisplay.swift | 2 +- Source/Modules/SessionCtl_HandleEvent.swift | 6 +----- 9 files changed, 11 insertions(+), 24 deletions(-) diff --git a/Packages/vChewing_Shared/Sources/Shared/Protocols/IMEStateProtocol.swift b/Packages/vChewing_Shared/Sources/Shared/Protocols/IMEStateProtocol.swift index f2f9a3f8..ad02d9d0 100644 --- a/Packages/vChewing_Shared/Sources/Shared/Protocols/IMEStateProtocol.swift +++ b/Packages/vChewing_Shared/Sources/Shared/Protocols/IMEStateProtocol.swift @@ -12,9 +12,6 @@ import Cocoa public protocol IMEStateProtocol { var type: StateType { get } var data: IMEStateDataProtocol { get set } - var isASCIIMode: Bool { get set } - var isVerticalTyping: Bool { get set } - var isVerticalCandidateWindow: Bool { get set } var candidates: [([String], String)] { get set } var hasComposition: Bool { get } var isCandidateContainer: Bool { get } @@ -47,7 +44,6 @@ public protocol IMEStateDataProtocol { var markedReadings: [String] { get set } var displayTextSegments: [String] { get set } var isFilterable: Bool { get } - var isVerticalTyping: Bool { get set } var isMarkedLengthValid: Bool { get } var candidates: [([String], String)] { get set } var displayedText: String { get set } diff --git a/Source/Modules/IMEState.swift b/Source/Modules/IMEState.swift index eb58664c..062e3888 100644 --- a/Source/Modules/IMEState.swift +++ b/Source/Modules/IMEState.swift @@ -47,12 +47,9 @@ public struct IMEState: IMEStateProtocol { public var type: StateType = .ofEmpty public var data: IMEStateDataProtocol = IMEStateData() as IMEStateDataProtocol public var node: CandidateNode = .init(name: "") - public var isASCIIMode = false - public var isVerticalCandidateWindow = false init(_ data: IMEStateDataProtocol = IMEStateData() as IMEStateDataProtocol, type: StateType = .ofEmpty) { self.data = data self.type = type - isVerticalTyping = SessionCtl.isVerticalTyping } /// 內部專用初期化函式,僅用於生成「有輸入內容」的狀態。 @@ -174,7 +171,6 @@ extension IMEState { if type == .ofInputting { return self } var result = Self.ofInputting(displayTextSegments: data.displayTextSegments, cursor: data.cursor) result.tooltip = data.tooltipBackupForInputting - result.isVerticalTyping = isVerticalTyping return result } @@ -209,11 +205,6 @@ extension IMEState { } } - public var isVerticalTyping: Bool { - get { data.isVerticalTyping } - set { data.isVerticalTyping = newValue } - } - public var isCandidateContainer: Bool { switch type { case .ofCandidates, .ofAssociates, .ofSymbolTable: return true diff --git a/Source/Modules/IMEStateData.swift b/Source/Modules/IMEStateData.swift index e7031d66..1ed8502f 100644 --- a/Source/Modules/IMEStateData.swift +++ b/Source/Modules/IMEStateData.swift @@ -75,7 +75,6 @@ public struct IMEStateData: IMEStateDataProtocol { // MARK: Other data for non-empty states. - public var isVerticalTyping = false public var markedTargetExists: Bool { let pair = userPhraseKVPair return LMMgr.checkIfUserPhraseExist( @@ -190,7 +189,7 @@ extension IMEStateData { var subNeta = subNeta if !PrefMgr.shared.cassetteEnabled { if PrefMgr.shared.showHanyuPinyinInCompositionBuffer, - PrefMgr.shared.alwaysShowTooltipTextsHorizontally || !isVerticalTyping + PrefMgr.shared.alwaysShowTooltipTextsHorizontally || !SessionCtl.isVerticalTyping { // 恢復陰平標記->注音轉拼音->轉教科書式標調 subNeta = Tekkon.restoreToneOneInPhona(target: subNeta) diff --git a/Source/Modules/InputHandler_Core.swift b/Source/Modules/InputHandler_Core.swift index f096f17a..81e2ea34 100644 --- a/Source/Modules/InputHandler_Core.swift +++ b/Source/Modules/InputHandler_Core.swift @@ -56,6 +56,7 @@ extension InputHandlerProtocol { /// InputHandler 委任協定 public protocol InputHandlerDelegate { + var isASCIIMode: Bool { get } var isVerticalTyping: Bool { get } var selectionKeys: String { get } var state: IMEStateProtocol { get set } diff --git a/Source/Modules/InputHandler_HandleInput.swift b/Source/Modules/InputHandler_HandleInput.swift index 37cc7aee..08ac4200 100644 --- a/Source/Modules/InputHandler_HandleInput.swift +++ b/Source/Modules/InputHandler_HandleInput.swift @@ -51,12 +51,12 @@ extension InputHandler { /// 若 Caps Lock 被啟用的話,則暫停對注音輸入的處理。 /// 這裡的處理仍舊有用,不然 Caps Lock 英文模式無法直接鍵入小寫字母。 - if input.isCapsLockOn || state.isASCIIMode { + if input.isCapsLockOn || delegate.isASCIIMode { // 低於 macOS 12 的系統無法偵測 CapsLock 的啟用狀態,所以這裡一律強制重置狀態為 .ofEmpty()。 delegate.switchState(IMEState.ofEmpty()) // 字母鍵摁 Shift 的話,無須額外處理,因為直接就會敲出大寫字母。 - if (input.isUpperCaseASCIILetterKey && state.isASCIIMode) + if (input.isUpperCaseASCIILetterKey && delegate.isASCIIMode) || (input.isCapsLockOn && input.isShiftHold) { return false diff --git a/Source/Modules/InputHandler_HandleStates.swift b/Source/Modules/InputHandler_HandleStates.swift index e87376d3..a2417027 100644 --- a/Source/Modules/InputHandler_HandleStates.swift +++ b/Source/Modules/InputHandler_HandleStates.swift @@ -735,11 +735,12 @@ extension InputHandler { candidate: candidates[currentIndex], respectCursorPushing: false, preConsolidate: false, skipObservation: true ) + delegate.updateVerticalTypingStatus() var newState = generateStateOfInputting() newState.tooltip = (currentIndex + 1).description + " / " + candidates.count.description + " " vCLog(newState.tooltip) if #available(macOS 10.13, *) { - if delegate.state.isVerticalTyping, Bundle.main.preferredLocalizations[0] != "en" { + if delegate.isVerticalTyping, Bundle.main.preferredLocalizations[0] != "en" { let locID = Bundle.main.preferredLocalizations[0] newState.tooltip = (currentIndex + 1).i18n(loc: locID) + "・" + candidates.count.i18n(loc: locID) + "" } diff --git a/Source/Modules/SessionCtl_Core.swift b/Source/Modules/SessionCtl_Core.swift index 2b012dbb..a66f935a 100644 --- a/Source/Modules/SessionCtl_Core.swift +++ b/Source/Modules/SessionCtl_Core.swift @@ -114,6 +114,9 @@ public class SessionCtl: IMKInputController { isVerticalTyping = result } + /// 當前選字窗是否為縱向。(縱排輸入時,只會啟用縱排選字窗。) + public var isVerticalCandidateWindow = false + /// InputMode 需要在每次出現內容變更的時候都連帶重設組字器與各項語言模組, /// 順帶更新 IME 模組及 UserPrefs 當中對於當前語言模式的記載。 public var inputMode: Shared.InputMode = IMEApp.currentInputMode { diff --git a/Source/Modules/SessionCtl_HandleDisplay.swift b/Source/Modules/SessionCtl_HandleDisplay.swift index 295c1912..cb769798 100644 --- a/Source/Modules/SessionCtl_HandleDisplay.swift +++ b/Source/Modules/SessionCtl_HandleDisplay.swift @@ -75,7 +75,7 @@ extension SessionCtl { public func showCandidates() { guard let client = client() else { return } updateVerticalTypingStatus() - state.isVerticalCandidateWindow = (isVerticalTyping || !PrefMgr.shared.useHorizontalCandidateList) + isVerticalCandidateWindow = (isVerticalTyping || !PrefMgr.shared.useHorizontalCandidateList) /// 無論是田所選字窗還是 IMK 選字窗,在這裡都有必要重新初期化。 let candidateLayout: NSUserInterfaceLayoutOrientation = diff --git a/Source/Modules/SessionCtl_HandleEvent.swift b/Source/Modules/SessionCtl_HandleEvent.swift index 2684b453..0d0712e3 100644 --- a/Source/Modules/SessionCtl_HandleEvent.swift +++ b/Source/Modules/SessionCtl_HandleEvent.swift @@ -32,10 +32,6 @@ extension SessionCtl { return handle(event, client: client) } - // 更新此時的靜態狀態標記。 - state.isASCIIMode = isASCIIMode - state.isVerticalTyping = isVerticalTyping - // 就這傳入的 NSEvent 都還有可能是 nil,Apple InputMethodKit 團隊到底在搞三小。 // 只針對特定類型的 client() 進行處理。 guard let event = event, sender is IMKTextInput else { @@ -125,7 +121,7 @@ extension SessionCtl { // 使 NSEvent 自翻譯,這樣可以讓 Emacs NSEvent 變成標準 NSEvent。 if eventToDeal.isEmacsKey { - let verticalProcessing = (state.isCandidateContainer) ? state.isVerticalCandidateWindow : state.isVerticalTyping + let verticalProcessing = (state.isCandidateContainer) ? isVerticalCandidateWindow : isVerticalTyping eventToDeal = eventToDeal.convertFromEmacsKeyEvent(isVerticalContext: verticalProcessing) }