Fixes the cursor index when choosing candidates.

This commit is contained in:
zonble 2022-01-28 01:58:25 +08:00
parent 9351d877ba
commit e17d06fd2a
4 changed files with 13 additions and 16 deletions

View File

@ -1408,7 +1408,11 @@ static double FindHighestScore(const vector<NodeAnchor> &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<NodeAnchor> &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<NodeAnchor> &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<NodeAnchor> &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 {

View File

@ -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 {

View File

@ -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";

View File

@ -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" = "已經切換到簡體中文模式";