From dd70417aab20b8b82db6d2d62fb01389cc9a6dfe Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Tue, 31 May 2022 12:43:52 +0800 Subject: [PATCH] KeyHandler // Implementing MS-New-Phonetic style rear-cursor mode. --- .../ControllerModules/KeyHandler_Core.swift | 9 +++-- .../ControllerModules/KeyHandler_Misc.swift | 34 ++++++++++++------- .../ControllerModules/KeyHandler_States.swift | 5 ++- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/Source/Modules/ControllerModules/KeyHandler_Core.swift b/Source/Modules/ControllerModules/KeyHandler_Core.swift index 4b4dfeb1..62332f68 100644 --- a/Source/Modules/ControllerModules/KeyHandler_Core.swift +++ b/Source/Modules/ControllerModules/KeyHandler_Core.swift @@ -161,7 +161,7 @@ class KeyHandler { } func fixNode(value: String, respectCursorPushing: Bool = true) { - let cursorIndex: Int = actualCandidateCursorIndex + let cursorIndex = min(actualCandidateCursorIndex + (mgrPrefs.setRearCursorMode ? 1 : 0), builderLength) let selectedNode: Megrez.NodeAnchor = _builder.grid.fixNodeSelectedCandidate( location: cursorIndex, value: value ) @@ -244,7 +244,7 @@ class KeyHandler { IME.prtDebugIntel( "UOM: Suggestion retrieved, overriding the node score of the selected candidate.") _builder.grid.overrideNodeScoreForSelectedCandidate( - location: actualCandidateCursorIndex, + location: min(actualCandidateCursorIndex + (mgrPrefs.setRearCursorMode ? 1 : 0), builderLength), value: overrideValue, overridingScore: findHighestScore(nodes: rawNodes, epsilon: kEpsilon) ) @@ -309,10 +309,9 @@ class KeyHandler { var rawNodes: [Megrez.NodeAnchor] { /// 警告:不要對游標前置風格使用 nodesCrossing,否則會導致游標行為與 macOS 內建注音輸入法不一致。 - /// 微軟新注音輸入法的游標後置風格也是不允許 nodeCrossing 的,但目前 Megrez 暫時缺乏對該特性的支援。 - /// 所以暫時只能將威注音的游標後置風格描述成「跟 Windows 版雅虎奇摩注音一致」。 + /// 微軟新注音輸入法的游標後置風格也是不允許 nodeCrossing 的。 mgrPrefs.setRearCursorMode - ? _builder.grid.nodesCrossingOrEndingAt(location: actualCandidateCursorIndex) + ? _builder.grid.nodesBeginningAt(location: actualCandidateCursorIndex) : _builder.grid.nodesEndingAt(location: actualCandidateCursorIndex) } diff --git a/Source/Modules/ControllerModules/KeyHandler_Misc.swift b/Source/Modules/ControllerModules/KeyHandler_Misc.swift index e0b7594d..70996a4a 100644 --- a/Source/Modules/ControllerModules/KeyHandler_Misc.swift +++ b/Source/Modules/ControllerModules/KeyHandler_Misc.swift @@ -35,19 +35,27 @@ extension KeyHandler { var actualCandidateCursorIndex: Int { var cursorIndex = builderCursorIndex - // Windows Yahoo Kimo IME style, phrase is *at the rear of* the cursor. - // (i.e. the cursor is always *before* the phrase.) - // This is different from MS Phonetics IME style ... - // ... since Windows Yahoo Kimo allows "node crossing". - if (mgrPrefs.setRearCursorMode - && (cursorIndex < builderLength)) - || cursorIndex == 0 - { - if cursorIndex == 0, !mgrPrefs.setRearCursorMode { - cursorIndex += keyLengthAtIndexZero - } else { - cursorIndex += 1 - } + switch mgrPrefs.setRearCursorMode { + case false: + do { + // macOS built-in Zhuyin style. + // (i.e. the cursor is always in front of the phrase.) + // No crossing. + switch cursorIndex { + case 0: cursorIndex = 1 + default: break + } + } + case true: + do { + // Microsoft new phonetics style. + // (i.e. the cursor is always at the rear of the phrase.) + // No crossing. + switch cursorIndex { + case builderLength: cursorIndex -= 1 + default: break + } + } } return cursorIndex } diff --git a/Source/Modules/ControllerModules/KeyHandler_States.swift b/Source/Modules/ControllerModules/KeyHandler_States.swift index e0f7de2a..752faad0 100644 --- a/Source/Modules/ControllerModules/KeyHandler_States.swift +++ b/Source/Modules/ControllerModules/KeyHandler_States.swift @@ -625,9 +625,12 @@ extension KeyHandler { var length = 0 var currentAnchor = Megrez.NodeAnchor() + let cursorIndex = min( + actualCandidateCursorIndex + (mgrPrefs.useRearCursorMode ? 1 : 0), builderLength + ) for anchor in _walkedNodes { length += anchor.spanningLength - if length >= actualCandidateCursorIndex { + if length >= cursorIndex { currentAnchor = anchor break }