From c7d47c3b3a2bc7ce45fd7a177e1a55d5f27e9b38 Mon Sep 17 00:00:00 2001 From: LEO Yoon-Tsaw Date: Sun, 28 Apr 2024 11:09:47 -0400 Subject: [PATCH 1/2] Enable moving caret position by mouse click --- SquirrelConfig.m | 16 ++++++------- SquirrelInputController.h | 1 + SquirrelInputController.m | 18 +++++++++++++++ SquirrelPanel.m | 48 ++++++++++++++++++++++++++++----------- 4 files changed, 62 insertions(+), 21 deletions(-) diff --git a/SquirrelConfig.m b/SquirrelConfig.m index 7d26d75c1..ecd0ffe37 100644 --- a/SquirrelConfig.m +++ b/SquirrelConfig.m @@ -189,15 +189,15 @@ - (NSColor*)colorFromString:(NSString*)string { sscanf(string.UTF8String, "0x%02x%02x%02x", &b, &g, &r); } if ([self.colorSpace isEqualToString:@"display_p3"]) { - return [NSColor colorWithDisplayP3Red:r / 255.0 - green:g / 255.0 - blue:b / 255.0 - alpha:a / 255.0]; + return [NSColor colorWithDisplayP3Red:(CGFloat)r / 255. + green:(CGFloat)g / 255. + blue:(CGFloat)b / 255. + alpha:(CGFloat)a / 255.]; } else { // sRGB by default - return [NSColor colorWithSRGBRed:r / 255.0 - green:g / 255.0 - blue:b / 255.0 - alpha:a / 255.0]; + return [NSColor colorWithSRGBRed:(CGFloat)r / 255. + green:(CGFloat)g / 255. + blue:(CGFloat)b / 255. + alpha:(CGFloat)a / 255.]; } } diff --git a/SquirrelInputController.h b/SquirrelInputController.h index 5e718b325..cea5029de 100644 --- a/SquirrelInputController.h +++ b/SquirrelInputController.h @@ -3,5 +3,6 @@ @interface SquirrelInputController : IMKInputController - (BOOL)selectCandidate:(NSInteger)index; +- (BOOL)moveCaret:(BOOL)forward; - (BOOL)pageUp:(BOOL)up; @end diff --git a/SquirrelInputController.m b/SquirrelInputController.m index 69e9de150..9f52c66f4 100644 --- a/SquirrelInputController.m +++ b/SquirrelInputController.m @@ -251,6 +251,24 @@ - (BOOL)pageUp:(BOOL)up { return handled; } +- (BOOL)moveCaret:(BOOL)forward { + size_t current_caret_pos = rime_get_api()->get_caret_pos(_session); + const char* input = rime_get_api()->get_input(_session); + if (forward) { + if (current_caret_pos <= 0) { + return NO; + } + rime_get_api()->set_caret_pos(_session, current_caret_pos - 1); + } else { + if (current_caret_pos >= strlen(input)) { + return NO; + } + rime_get_api()->set_caret_pos(_session, current_caret_pos + 1); + } + [self rimeUpdate]; + return YES; +} + - (void)onChordTimer:(NSTimer*)timer { // chord release triggered by timer int processed_keys = 0; diff --git a/SquirrelPanel.m b/SquirrelPanel.m index 37f056797..1fd79a2a2 100644 --- a/SquirrelPanel.m +++ b/SquirrelPanel.m @@ -829,6 +829,10 @@ - (void)drawRect:(NSRect)dirtyRect { CGPathRef preeditPath = CGPathCreateMutable(); SquirrelTheme* theme = self.currentTheme; + NSPoint textFieldOrigin = dirtyRect.origin; + textFieldOrigin.y += theme.edgeInset.height; + textFieldOrigin.x += theme.edgeInset.width; + // Draw preedit Rect NSRect backgroundRect = dirtyRect; NSRect containingRect = dirtyRect; @@ -1026,9 +1030,13 @@ - (void)drawRect:(NSRect)dirtyRect { } [panelLayer addSublayer:layer]; } + [_textView + setTextContainerInset:NSMakeSize(textFieldOrigin.x, textFieldOrigin.y)]; } -- (BOOL)clickAtPoint:(NSPoint)_point index:(NSInteger*)_index { +- (BOOL)clickAtPoint:(NSPoint)_point + index:(NSInteger*)_index + preeditIndex:(NSInteger*)_preeditIndex { if (CGPathContainsPoint(_shape.path, nil, _point, NO)) { NSPoint point = NSMakePoint(_point.x - self.textView.textContainerInset.width, @@ -1047,11 +1055,20 @@ - (BOOL)clickAtPoint:(NSPoint)_point index:(NSInteger*)_index { point = NSMakePoint(point.x - NSMinX(lineFragment.typographicBounds), point.y - NSMinY(lineFragment.typographicBounds)); index += [lineFragment characterIndexForPoint:point]; - for (NSUInteger i = 0; i < _candidateRanges.count; i += 1) { - NSRange range = [_candidateRanges[i] rangeValue]; - if (index >= range.location && index < NSMaxRange(range)) { - *_index = i; - break; + if (index >= _preeditRange.location && + index < NSMaxRange(_preeditRange)) { + if (_preeditIndex) { + *_preeditIndex = index; + } + } else { + for (NSUInteger i = 0; i < _candidateRanges.count; i += 1) { + NSRange range = [_candidateRanges[i] rangeValue]; + if (index >= range.location && index < NSMaxRange(range)) { + if (_index) { + *_index = i; + } + break; + } } } break; @@ -1227,7 +1244,7 @@ - (void)sendEvent:(NSEvent*)event { case NSEventTypeLeftMouseDown: { NSPoint point = [self mousePosition]; NSInteger index = -1; - if ([_view clickAtPoint:point index:&index]) { + if ([_view clickAtPoint:point index:&index preeditIndex:nil]) { if (index >= 0 && index < _candidates.count) { _index = index; } @@ -1236,7 +1253,15 @@ - (void)sendEvent:(NSEvent*)event { case NSEventTypeLeftMouseUp: { NSPoint point = [self mousePosition]; NSInteger index = -1; - if ([_view clickAtPoint:point index:&index]) { + NSInteger preeditIndex = -1; + if ([_view clickAtPoint:point index:&index preeditIndex:&preeditIndex]) { + if (preeditIndex >= 0 && preeditIndex < _preedit.length) { + if (preeditIndex < _caretPos) { + [_inputController moveCaret:YES]; + } else if (preeditIndex > _caretPos) { + [_inputController moveCaret:NO]; + } + } if (index >= 0 && index < _candidates.count && index == _index) { [_inputController selectCandidate:index]; } @@ -1261,7 +1286,7 @@ - (void)sendEvent:(NSEvent*)event { case NSEventTypeMouseMoved: { NSPoint point = [self mousePosition]; NSInteger index = -1; - if ([_view clickAtPoint:point index:&index]) { + if ([_view clickAtPoint:point index:&index preeditIndex:nil]) { if (index >= 0 && index < _candidates.count && _cursorIndex != index) { [self showPreedit:_preedit selRange:_selRange @@ -1459,12 +1484,9 @@ - (void)show { [self.contentView setBoundsOrigin:NSMakePoint(0, 0)]; [_view.textView setBoundsOrigin:NSMakePoint(0, 0)]; } + BOOL translucency = theme.translucency; [_view setFrame:self.contentView.bounds]; [_view.textView setFrame:self.contentView.bounds]; - [_view.textView setTextContainerInset:NSMakeSize(theme.edgeInset.width, - theme.edgeInset.height)]; - - BOOL translucency = theme.translucency; if (translucency) { [_back setFrame:self.contentView.bounds]; _back.appearance = NSApp.effectiveAppearance; From 7975ef3e758c5a4677f1da977da94275ed6777ea Mon Sep 17 00:00:00 2001 From: LEO Yoon-Tsaw Date: Sun, 28 Apr 2024 11:10:10 -0400 Subject: [PATCH 2/2] Update librime, plum and Sparkle to latest --- Sparkle | 2 +- librime | 2 +- plum | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Sparkle b/Sparkle index 47d3d90ae..5264c01d3 160000 --- a/Sparkle +++ b/Sparkle @@ -1 +1 @@ -Subproject commit 47d3d90aee3c52b6f61d04ceae426e607df62347 +Subproject commit 5264c01d37e07f73c579f3f01a90a5605453c577 diff --git a/librime b/librime index 76a0a16c5..a1c814443 160000 --- a/librime +++ b/librime @@ -1 +1 @@ -Subproject commit 76a0a16c5ca0c7efc80fa918c8e0c88754699fd7 +Subproject commit a1c814443264b49a3b8633ebf89071c6ca667a94 diff --git a/plum b/plum index 6f502ff6f..4c28f11f4 160000 --- a/plum +++ b/plum @@ -1 +1 @@ -Subproject commit 6f502ff6fa87789847fa18200415318e705bffa4 +Subproject commit 4c28f11f451facef809b380502874a48ba964ddb