From ce611799dcdac823e444af780a47d7fb735a62dd Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Sun, 7 May 2023 10:33:54 +0800 Subject: [PATCH] IMEState // Track the index of unfinished readings / strokes. --- .../Shared/Protocols/IMEStateProtocol.swift | 1 + Source/Modules/IMEState.swift | 5 ++++- Source/Modules/IMEStateData.swift | 1 + Source/Modules/InputHandler_HandleStates.swift | 14 ++++++++++++-- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Packages/vChewing_Shared/Sources/Shared/Protocols/IMEStateProtocol.swift b/Packages/vChewing_Shared/Sources/Shared/Protocols/IMEStateProtocol.swift index 4a7d411f..75d2ba28 100644 --- a/Packages/vChewing_Shared/Sources/Shared/Protocols/IMEStateProtocol.swift +++ b/Packages/vChewing_Shared/Sources/Shared/Protocols/IMEStateProtocol.swift @@ -44,6 +44,7 @@ public protocol IMEStateDataProtocol { var textToCommit: String { get set } var markedReadings: [String] { get set } var displayTextSegments: [String] { get set } + var highlightAtSegment: Int? { get set } var isFilterable: Bool { get } var isMarkedLengthValid: Bool { get } var candidates: [(keyArray: [String], value: String)] { get set } diff --git a/Source/Modules/IMEState.swift b/Source/Modules/IMEState.swift index 88d905d3..17910e41 100644 --- a/Source/Modules/IMEState.swift +++ b/Source/Modules/IMEState.swift @@ -111,9 +111,12 @@ public extension IMEState { return result } - static func ofInputting(displayTextSegments: [String], cursor: Int) -> IMEState { + static func ofInputting(displayTextSegments: [String], cursor: Int, highlightAt highlightAtSegment: Int? = nil) -> IMEState { var result = IMEState(displayTextSegments: displayTextSegments, cursor: cursor) result.type = .ofInputting + if let readingAtSegment = highlightAtSegment { + result.data.highlightAtSegment = readingAtSegment + } return result } diff --git a/Source/Modules/IMEStateData.swift b/Source/Modules/IMEStateData.swift index 875e8c2f..3948ea18 100644 --- a/Source/Modules/IMEStateData.swift +++ b/Source/Modules/IMEStateData.swift @@ -91,6 +91,7 @@ public struct IMEStateData: IMEStateDataProtocol { } } + public var highlightAtSegment: Int? public var reading: String = "" public var markedReadings = [String]() public var candidates = [(keyArray: [String], value: String)]() diff --git a/Source/Modules/InputHandler_HandleStates.swift b/Source/Modules/InputHandler_HandleStates.swift index 476c56ad..d7830f61 100644 --- a/Source/Modules/InputHandler_HandleStates.swift +++ b/Source/Modules/InputHandler_HandleStates.swift @@ -20,6 +20,7 @@ extension InputHandler { /// 生成「正在輸入」狀態。相關的內容會被拿給狀態機械用來處理在電腦螢幕上顯示的內容。 public func generateStateOfInputting(sansReading: Bool = false) -> IMEStateProtocol { if isConsideredEmptyForNow { return IMEState.ofAbortion() } + var segHighlightedAt: Int? let cpInput = isCodePointInputMode && !sansReading /// 「更新內文組字區 (Update the composing buffer)」是指要求客體軟體將組字緩衝區的內容 /// 換成由此處重新生成的原始資料在 IMEStateData 當中生成的 NSAttributeString。 @@ -39,6 +40,8 @@ extension InputHandler { if charCounter == cursor { newDisplayTextSegments.append(temporaryNode) temporaryNode = "" + // 處理在組字區中間或者最後方插入游標的情形。 + segHighlightedAt = newDisplayTextSegments.count newDisplayTextSegments.append(reading) } temporaryNode += String(char) @@ -47,7 +50,11 @@ extension InputHandler { newDisplayTextSegments.append(temporaryNode) temporaryNode = "" } - if newDisplayTextSegments == displayTextSegments { newDisplayTextSegments.append(reading) } + if newDisplayTextSegments == displayTextSegments { + // 處理在組字區最前方插入游標的情形。 + segHighlightedAt = newDisplayTextSegments.count + newDisplayTextSegments.append(reading) + } displayTextSegments = newDisplayTextSegments cursor += reading.count } @@ -55,7 +62,10 @@ extension InputHandler { displayTextSegments[i] = displayTextSegments[i].trimmingCharacters(in: .newlines) } /// 這裡生成準備要拿來回呼的「正在輸入」狀態。 - return IMEState.ofInputting(displayTextSegments: displayTextSegments, cursor: cursor) + return IMEState.ofInputting( + displayTextSegments: displayTextSegments, + cursor: cursor, highlightAt: segHighlightedAt + ) } /// 生成「正在輸入」狀態。