From 0b2a04688579c81b998fd9b4f38a9d0e17a2c492 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Fri, 25 Nov 2022 11:58:03 +0800 Subject: [PATCH] SessionCtl // ctlCandidateCurrent -> candidateUI. --- Source/Modules/SessionCtl_Core.swift | 7 +++-- Source/Modules/SessionCtl_Delegates.swift | 2 +- Source/Modules/SessionCtl_HandleDisplay.swift | 28 +++++++++---------- Source/Modules/SessionCtl_HandleStates.swift | 9 +++--- .../SessionCtl_IMKCandidatesData.swift | 4 +-- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/Source/Modules/SessionCtl_Core.swift b/Source/Modules/SessionCtl_Core.swift index a49080b3..2709963a 100644 --- a/Source/Modules/SessionCtl_Core.swift +++ b/Source/Modules/SessionCtl_Core.swift @@ -29,7 +29,7 @@ public class SessionCtl: IMKInputController { public static var areWeNerfing = false /// 目前在用的的選字窗副本。 - public var ctlCandidateCurrent: CtlCandidateProtocol = { + public var candidateUI: CtlCandidateProtocol = { let direction: NSUserInterfaceLayoutOrientation = PrefMgr.shared.useHorizontalCandidateList ? .horizontal : .vertical if #available(macOS 10.15, *) { @@ -135,7 +135,7 @@ public class SessionCtl: IMKInputController { // 雖然所有在這個函式內影響到的變數都改為動態變數了(不會出現跨副本波及的情況), // 但 IMKCandidates 是有內部共用副本的、會被波及。所以在這裡糾偏一下。 if PrefMgr.shared.useIMKCandidateWindow { - guard let imkC = ctlCandidateCurrent as? CtlCandidateIMK else { return } + guard let imkC = candidateUI as? CtlCandidateIMK else { return } if state.isCandidateContainer, !imkC.visible { handle(state: state, replace: false) } @@ -209,7 +209,8 @@ extension SessionCtl { // 因為偶爾會收到與 activateServer 有關的以「強制拆 nil」為理由的報錯, // 所以這裡添加這句、來試圖應對這種情況。 - if inputHandler.delegate == nil { inputHandler.delegate = self } + inputHandler.delegate = self + candidateUI.delegate = self // 這裡不需要 setValue(),因為 IMK 會在自動呼叫 activateServer() 之後自動執行 setValue()。 inputHandler.clear() // 這句不要砍,因為後面 handle State.Empty() 不一定執行。 inputHandler.ensureKeyboardParser() diff --git a/Source/Modules/SessionCtl_Delegates.swift b/Source/Modules/SessionCtl_Delegates.swift index 9f5ba240..a538c0c8 100644 --- a/Source/Modules/SessionCtl_Delegates.swift +++ b/Source/Modules/SessionCtl_Delegates.swift @@ -16,7 +16,7 @@ extension SessionCtl: InputHandlerDelegate { return client.bundleIdentifier() ?? "" } - public func candidateController() -> CtlCandidateProtocol { ctlCandidateCurrent } + public func candidateController() -> CtlCandidateProtocol { candidateUI } public func candidateSelectionCalledByInputHandler(at index: Int) { candidatePairSelected(at: index) diff --git a/Source/Modules/SessionCtl_HandleDisplay.swift b/Source/Modules/SessionCtl_HandleDisplay.swift index 965628c8..1644e750 100644 --- a/Source/Modules/SessionCtl_HandleDisplay.swift +++ b/Source/Modules/SessionCtl_HandleDisplay.swift @@ -82,35 +82,35 @@ extension SessionCtl { : .horizontal) /// 先取消既有的選字窗的內容顯示。否則可能會重複生成選字窗的 NSWindow()。 - ctlCandidateCurrent.visible = false + candidateUI.visible = false /// 然後再重新初期化。 if #available(macOS 10.15, *) { - ctlCandidateCurrent = + candidateUI = PrefMgr.shared.useIMKCandidateWindow ? CtlCandidateIMK(candidateLayout) : CtlCandidateTDK(candidateLayout) - if let candidateTDK = ctlCandidateCurrent as? CtlCandidateTDK { + if let candidateTDK = candidateUI as? CtlCandidateTDK { candidateTDK.maxLinesPerPage = isVerticalTyping ? 1 : 3 } } else { - ctlCandidateCurrent = CtlCandidateIMK(candidateLayout) + candidateUI = CtlCandidateIMK(candidateLayout) } - ctlCandidateCurrent.candidateFont = Self.candidateFont( + candidateUI.candidateFont = Self.candidateFont( name: PrefMgr.shared.candidateTextFontName, size: PrefMgr.shared.candidateListTextSize ) if PrefMgr.shared.cassetteEnabled { - ctlCandidateCurrent.tooltip = + candidateUI.tooltip = isVerticalTyping ? "📼" : "📼 " + NSLocalizedString("CIN Cassette Mode", comment: "") } if state.type == .ofAssociates { - ctlCandidateCurrent.tooltip = + candidateUI.tooltip = isVerticalTyping ? "⇧" : NSLocalizedString("Hold ⇧ to choose associates.", comment: "") } - ctlCandidateCurrent.useLangIdentifier = PrefMgr.shared.handleDefaultCandidateFontsByLangIdentifier - ctlCandidateCurrent.locale = { + candidateUI.useLangIdentifier = PrefMgr.shared.handleDefaultCandidateFontsByLangIdentifier + candidateUI.locale = { switch inputMode { case .imeModeCHS: return "zh-Hans" case .imeModeCHT: @@ -124,18 +124,18 @@ extension SessionCtl { if #available(macOS 10.14, *) { // Spotlight 視窗會擋住 IMK 選字窗,所以需要特殊處理。 - if let ctlCandidateCurrent = ctlCandidateCurrent as? CtlCandidateIMK { + if let ctlCandidateCurrent = candidateUI as? CtlCandidateIMK { while ctlCandidateCurrent.windowLevel() <= client.windowLevel() { ctlCandidateCurrent.setWindowLevel(UInt64(max(0, client.windowLevel() + 1000))) } } } - ctlCandidateCurrent.delegate = self // 會自動觸發田所選字窗的資料重載。 - ctlCandidateCurrent.visible = true + candidateUI.delegate = self // 會自動觸發田所選字窗的資料重載。 + candidateUI.visible = true if isVerticalTyping { - ctlCandidateCurrent.set( + candidateUI.set( windowTopLeftPoint: NSPoint( x: lineHeightRect().origin.x + lineHeightRect().size.width + 4.0, y: lineHeightRect().origin.y - 4.0 ), @@ -143,7 +143,7 @@ extension SessionCtl { useGCD: true ) } else { - ctlCandidateCurrent.set( + candidateUI.set( windowTopLeftPoint: NSPoint(x: lineHeightRect().origin.x, y: lineHeightRect().origin.y - 4.0), bottomOutOfScreenAdjustmentHeight: lineHeightRect().size.height + 4.0, useGCD: true diff --git a/Source/Modules/SessionCtl_HandleStates.swift b/Source/Modules/SessionCtl_HandleStates.swift index cfa81421..3cb2ce8e 100644 --- a/Source/Modules/SessionCtl_HandleStates.swift +++ b/Source/Modules/SessionCtl_HandleStates.swift @@ -45,7 +45,7 @@ extension SessionCtl { // 這裡移除一些處理,轉而交給 commitComposition() 代為執行。 // 這裡不需要 clearInlineDisplay() ,否則會觸發無限迴圈。 // 對於 IMK 選字窗的顯示狀態糾正的行為交給 inputMode.didSet() 來處理。 - ctlCandidateCurrent.visible = false + candidateUI.visible = false popupCompositionBuffer.hide() tooltipInstance.hide() case .ofEmpty, .ofAbortion, .ofCommitting: @@ -58,22 +58,21 @@ extension SessionCtl { if replace { state = IMEState.ofEmpty() } default: break innerCircle } - ctlCandidateCurrent.visible = false + candidateUI.visible = false // 全專案用以判斷「.Abortion」的地方僅此一處。 if previous.hasComposition, ![.ofAbortion, .ofCommitting].contains(newState.type) { commit(text: previous.displayedText) } showTooltip(newState.tooltip, duration: 1) // 會在工具提示為空的時候自動消除顯示。 clearInlineDisplay() - // 最後一道保險 inputHandler.clear() case .ofInputting: - ctlCandidateCurrent.visible = false + candidateUI.visible = false commit(text: newState.textToCommit) setInlineDisplayWithCursor() showTooltip(newState.tooltip, duration: 1) // 會在工具提示為空的時候自動消除顯示。 case .ofMarking: - ctlCandidateCurrent.visible = false + candidateUI.visible = false setInlineDisplayWithCursor() showTooltip(newState.tooltip) case .ofCandidates, .ofAssociates, .ofSymbolTable: diff --git a/Source/Modules/SessionCtl_IMKCandidatesData.swift b/Source/Modules/SessionCtl_IMKCandidatesData.swift index 3a101b7d..94a45033 100644 --- a/Source/Modules/SessionCtl_IMKCandidatesData.swift +++ b/Source/Modules/SessionCtl_IMKCandidatesData.swift @@ -62,8 +62,8 @@ extension SessionCtl { let annotation = reverseLookup(for: currentCandidate).joined(separator: "\n") guard !annotation.isEmpty else { return } vCLog("Current Annotation: \(annotation)") - DispatchQueue.main.async { - guard let imkCandidates = self.ctlCandidateCurrent as? CtlCandidateIMK else { return } + DispatchQueue.main.async { [self] in + guard let imkCandidates = candidateUI as? CtlCandidateIMK else { return } imkCandidates.showAnnotation(.init(string: annotation)) } }