From e17d06fd2a6a28e26522f3eae13939af11c3f73e Mon Sep 17 00:00:00 2001 From: zonble Date: Fri, 28 Jan 2022 01:58:25 +0800 Subject: [PATCH] Fixes the cursor index when choosing candidates. --- Source/InputMethodController.mm | 19 ++++++++----------- Source/InputState.swift | 6 +++--- Source/en.lproj/Localizable.strings | 2 +- Source/zh-Hant.lproj/Localizable.strings | 2 +- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/Source/InputMethodController.mm b/Source/InputMethodController.mm index 10e9fdd6..5967b1a6 100644 --- a/Source/InputMethodController.mm +++ b/Source/InputMethodController.mm @@ -1408,7 +1408,11 @@ static double FindHighestScore(const vector &nodes, double epsilon) [client setMarkedText:attrString selectionRange:NSMakeRange(cursorIndex, 0) replacementRange:NSMakeRange(NSNotFound, NSNotFound)]; gCurrentCandidateController.visible = NO; - [self _showTooltip:state.tooltip composingBuffer:state.composingBuffer cursorIndex:state.cursorIndex client:client]; + if (state.tooltip.length) { + [self _showTooltip:state.tooltip composingBuffer:state.composingBuffer cursorIndex:cursorIndex client:client]; + } else { + [self _hideTooltip]; + } } - (void)_handleInputStateChoosingCandidate:(InputStateChoosingCandidate *)state previous:(InputState *)previous client:(id)client @@ -1416,6 +1420,8 @@ static double FindHighestScore(const vector &nodes, double epsilon) NSUInteger cursorIndex = [state cursorIndex]; NSAttributedString *attrString = [state attributedString]; + NSLog(@"_handleInputStateChoosingCandidate cursorIndex %lu", (unsigned long)cursorIndex); + // the selection range is where the cursor is, with the length being 0 and replacement range NSNotFound, // i.e. the client app needs to take care of where to put ths composing buffer [client setMarkedText:attrString selectionRange:NSMakeRange(cursorIndex, 0) replacementRange:NSMakeRange(NSNotFound, NSNotFound)]; @@ -1470,19 +1476,12 @@ static double FindHighestScore(const vector &nodes, double epsilon) } gCurrentCandidateController.keyLabels = keyLabels; - gCurrentCandidateController.delegate = self; [gCurrentCandidateController reloadData]; - - // update the composing text, set the client - NSInteger cursor = 0; - - NSAttributedString *attrString = [state attributedString]; - [client setMarkedText:attrString selectionRange:NSMakeRange(cursor, 0) replacementRange:NSMakeRange(NSNotFound, NSNotFound)]; _currentCandidateClient = client; NSRect lineHeightRect = NSMakeRect(0.0, 0.0, 16.0, 16.0); - cursor = state.cursorIndex; + NSInteger cursor = state.cursorIndex; if (cursor == [state.composingBuffer length] && cursor != 0) { cursor--; } @@ -1495,8 +1494,6 @@ static double FindHighestScore(const vector &nodes, double epsilon) NSLog(@"lineHeightRectangle %@", exception); } -// NSLog(@"lineHeightRectangle %@", NSStringFromRect(lineHeightRect)); - if (useVerticalMode) { [gCurrentCandidateController setWindowTopLeftPoint:NSMakePoint(lineHeightRect.origin.x + lineHeightRect.size.width + 4.0, lineHeightRect.origin.y - 4.0) bottomOutOfScreenAdjustmentHeight:lineHeightRect.size.height + 4.0]; } else { diff --git a/Source/InputState.swift b/Source/InputState.swift index 312a44d4..d2f1a885 100644 --- a/Source/InputState.swift +++ b/Source/InputState.swift @@ -72,8 +72,8 @@ private let kMaxMarkRangeLength = 6 /// Represents that the user is marking a range in the composing buffer. class InputStateMarking: InputStateNotEmpty { - @objc private(set) var markerIndex: UInt = 0 - @objc private(set) var markedRange: NSRange = NSRange(location: 0, length: 0) + @objc private(set) var markerIndex: UInt + @objc private(set) var markedRange: NSRange @objc var tooltip: String { if Preferences.phraseReplacementEnabled { @@ -98,11 +98,11 @@ class InputStateMarking: InputStateNotEmpty { @objc var readings: [String] = [] @objc init(composingBuffer: String, cursorIndex: UInt, markerIndex: UInt) { - super.init(composingBuffer: composingBuffer, cursorIndex: cursorIndex) self.markerIndex = markerIndex let begin = min(cursorIndex, markerIndex) let end = max(cursorIndex, markerIndex) self.markedRange = NSMakeRange(Int(begin), Int(end - begin)) + super.init(composingBuffer: composingBuffer, cursorIndex: cursorIndex) } @objc var attributedString: NSAttributedString { diff --git a/Source/en.lproj/Localizable.strings b/Source/en.lproj/Localizable.strings index ba78261c..b93caab3 100644 --- a/Source/en.lproj/Localizable.strings +++ b/Source/en.lproj/Localizable.strings @@ -69,7 +69,7 @@ "You are now selecting \"%@\". Press enter to add a new phrase." = "You are now selecting \"%@\". Press enter to add a new phrase."; -"You are now selecting \"%@\". A phrase cannot be longer than 6 characters." = "You are now selecting \"%@\". A phrase cannot be longer than 6 characters."; +"You are now selecting \"%@\". A phrase cannot be longer than %d characters." = "You are now selecting \"%@\". A phrase cannot be longer than %d characters."; "Chinese conversion on" = "Chinese conversion on"; diff --git a/Source/zh-Hant.lproj/Localizable.strings b/Source/zh-Hant.lproj/Localizable.strings index b7c711a0..c7d50ed9 100644 --- a/Source/zh-Hant.lproj/Localizable.strings +++ b/Source/zh-Hant.lproj/Localizable.strings @@ -69,7 +69,7 @@ "You are now selecting \"%@\". Press enter to add a new phrase." = "您目前選擇了 \"%@\"。按下 Enter 就可以加入到使用者詞彙中。"; -"You are now selecting \"%@\". A phrase cannot be longer than 6 characters." = "您目前選擇了 \"%@\"。自訂詞彙不能超過六個字元。"; +"You are now selecting \"%@\". A phrase cannot be longer than %d characters." = "您目前選擇了 \"%@\"。自訂詞彙不能超過 %d 個字元。"; "Chinese conversion on" = "已經切換到簡體中文模式";