From 8e0b9fac2281cc15a3783e0256c8a4202d2ca112 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Tue, 19 Apr 2022 14:49:11 +0800 Subject: [PATCH] KeyHandler // Swiftifying "buildAssociatePhraseStateWithKey". --- Source/Modules/ControllerModules/KeyHandler.h | 2 -- .../Modules/ControllerModules/KeyHandler.mm | 9 -------- .../KeyHandler_HandleInput.swift | 6 ++--- .../ControllerModules/KeyHandler_States.swift | 22 ++++++++++++++++--- .../Modules/IMEModules/ctlInputMethod.swift | 6 ++--- 5 files changed, 24 insertions(+), 21 deletions(-) diff --git a/Source/Modules/ControllerModules/KeyHandler.h b/Source/Modules/ControllerModules/KeyHandler.h index 231f653b..1c40ce61 100644 --- a/Source/Modules/ControllerModules/KeyHandler.h +++ b/Source/Modules/ControllerModules/KeyHandler.h @@ -59,8 +59,6 @@ struct BufferStatePackage - (void)fixNodeWithValue:(NSString *)value NS_SWIFT_NAME(fixNode(value:)); - (void)clear; -- (nullable InputState *)buildAssociatePhraseStateWithKey:(NSString *)key useVerticalMode:(BOOL)useVerticalMode; - @property(strong, nonatomic) InputMode inputMode; @property(weak, nonatomic) id delegate; diff --git a/Source/Modules/ControllerModules/KeyHandler.mm b/Source/Modules/ControllerModules/KeyHandler.mm index 3f8be28b..4843438c 100644 --- a/Source/Modules/ControllerModules/KeyHandler.mm +++ b/Source/Modules/ControllerModules/KeyHandler.mm @@ -421,15 +421,6 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot"; return array; } -- (nullable InputState *)buildAssociatePhraseStateWithKey:(NSString *)key useVerticalMode:(BOOL)useVerticalMode -{ - NSArray *array = [self buildAssociatePhraseArrayWithKey:key]; - if (array == nil || [array count] == 0) - return nil; - else - return [[InputStateAssociatedPhrases alloc] initWithCandidates:array useVerticalMode:useVerticalMode]; -} - #pragma mark - 必須用 ObjCpp 處理的部分: Mandarin - (BOOL)chkKeyValidity:(UniChar)charCode diff --git a/Source/Modules/ControllerModules/KeyHandler_HandleInput.swift b/Source/Modules/ControllerModules/KeyHandler_HandleInput.swift index db31bfdd..be9d33e7 100644 --- a/Source/Modules/ControllerModules/KeyHandler_HandleInput.swift +++ b/Source/Modules/ControllerModules/KeyHandler_HandleInput.swift @@ -201,12 +201,12 @@ import Cocoa if !mgrPrefs.associatedPhrasesEnabled { stateCallback(InputState.Empty()) } else { - let associatedPhrases = + if let associatedPhrases = buildAssociatePhraseState( withKey: text, useVerticalMode: input.useVerticalMode - ) as? InputState.AssociatedPhrases - if let associatedPhrases = associatedPhrases { + ), !associatedPhrases.candidates.isEmpty + { stateCallback(associatedPhrases) } else { stateCallback(InputState.Empty()) diff --git a/Source/Modules/ControllerModules/KeyHandler_States.swift b/Source/Modules/ControllerModules/KeyHandler_States.swift index db3abdcd..430f3dfa 100644 --- a/Source/Modules/ControllerModules/KeyHandler_States.swift +++ b/Source/Modules/ControllerModules/KeyHandler_States.swift @@ -32,7 +32,7 @@ import Cocoa // MARK: - 構築狀態(State Building) func buildInputtingState() -> InputState.Inputting { - // 觸發資料封裝更新,否則下文拿到的數據會是錯的。 + // 觸發資料封裝更新,否則下文拿到的資料會是過期的。 packageBufferStateMaterials() // 獲取封裝好的資料 let composedText = getComposedText() @@ -63,7 +63,7 @@ import Cocoa return newState } - // MARK: - 用以生成候選詞數組及狀態 + // MARK: - 用以生成候選詞陣列及狀態 func _buildCandidateState( _ currentState: InputState.NotEmpty, @@ -80,7 +80,23 @@ import Cocoa return state } - // MARK: - 用以接收聯想詞數組且生成狀態 + // MARK: - 用以接收聯想詞陣列且生成狀態 + + // 這次重寫時,針對「buildAssociatePhraseStateWithKey」這個(用以生成帶有 + // 聯想詞候選清單的結果的狀態回呼的)函數進行了小幅度的重構處理,使其始終 + // 可以從 ObjC 部分的「buildAssociatePhraseArray」函數獲取到一個內容類型 + // 為「String」的標準 Swift 陣列。這樣一來,該聯想詞狀態回呼函數將始終能 + // 夠傳回正確的結果形態、永遠也無法傳回 nil。於是,所有在用到該函數時以 + // 回傳結果類型判斷作為合法性判斷依據的函數,全都將依據改為檢查傳回的陣列 + // 是否為空:如果陣列為空的話,直接回呼一個空狀態。 + func buildAssociatePhraseState( + withKey key: String!, + useVerticalMode: Bool + ) -> InputState.AssociatedPhrases! { + // 上一行必須要用驚嘆號,否則 Xcode 會誤導你砍掉某些實際上必需的語句。 + return InputState.AssociatedPhrases( + candidates: buildAssociatePhraseArray(withKey: key), useVerticalMode: useVerticalMode) + } // MARK: - 用以處理就地新增自訂語彙時的行為 diff --git a/Source/Modules/IMEModules/ctlInputMethod.swift b/Source/Modules/IMEModules/ctlInputMethod.swift index bdc3d9c6..8bf699bf 100644 --- a/Source/Modules/IMEModules/ctlInputMethod.swift +++ b/Source/Modules/IMEModules/ctlInputMethod.swift @@ -626,8 +626,7 @@ extension ctlInputMethod: ctlCandidateDelegate { if mgrPrefs.associatedPhrasesEnabled, let associatePhrases = keyHandler.buildAssociatePhraseState( withKey: composingBuffer, useVerticalMode: state.useVerticalMode - ) - as? InputState.AssociatedPhrases + ), !associatePhrases.candidates.isEmpty { handle(state: associatePhrases, client: client) } else { @@ -645,8 +644,7 @@ extension ctlInputMethod: ctlCandidateDelegate { if mgrPrefs.associatedPhrasesEnabled, let associatePhrases = keyHandler.buildAssociatePhraseState( withKey: selectedValue, useVerticalMode: state.useVerticalMode - ) - as? InputState.AssociatedPhrases + ), !associatePhrases.candidates.isEmpty { handle(state: associatePhrases, client: client) } else {