Skip to content

Commit

Permalink
Select by clicking
Browse files Browse the repository at this point in the history
  • Loading branch information
LEOYoon-Tsaw committed Feb 16, 2023
1 parent 839719a commit 6af298c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Squirrel.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@
CODE_SIGN_IDENTITY = "-";
COMBINE_HIDPI_IMAGES = YES;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 0.17.0;
CURRENT_PROJECT_VERSION = 0.17.1;
DEAD_CODE_STRIPPING = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand Down Expand Up @@ -674,7 +674,7 @@
CLANG_ENABLE_OBJC_ARC = YES;
CODE_SIGN_IDENTITY = "-";
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 0.17.0;
CURRENT_PROJECT_VERSION = 0.17.1;
DEAD_CODE_STRIPPING = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand Down
2 changes: 1 addition & 1 deletion SquirrelInputController.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
#import <InputMethodKit/InputMethodKit.h>

@interface SquirrelInputController : IMKInputController

- (BOOL)selectCandidate:(NSInteger)index;
@end
12 changes: 8 additions & 4 deletions SquirrelInputController.m
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,6 @@ - (BOOL)handleEvent:(NSEvent*)event client:(id)sender
[self rimeUpdate];
}
} break;
case NSEventTypeLeftMouseDown: {
[self commitComposition:_currentClient];
} break;
default:
break;
}
Expand Down Expand Up @@ -222,6 +219,12 @@ -(BOOL)processKey:(int)rime_keycode modifiers:(int)rime_modifiers
return handled;
}

- (BOOL)selectCandidate:(NSInteger)index {
BOOL success = rime_get_api()->select_candidate_on_current_page(_session, (int) index);
[self rimeUpdate];
return success;
}

-(void)onChordTimer:(NSTimer *)timer
{
// chord release triggered by timer
Expand Down Expand Up @@ -285,7 +288,7 @@ -(void)clearChord
-(NSUInteger)recognizedEvents:(id)sender
{
//NSLog(@"recognizedEvents:");
return NSEventMaskKeyDown | NSEventMaskFlagsChanged | NSEventMaskLeftMouseDown;
return NSEventMaskKeyDown | NSEventMaskFlagsChanged;
}

-(void)activateServer:(id)sender
Expand Down Expand Up @@ -449,6 +452,7 @@ -(void)showPanelWithPreedit:(NSString*)preedit
[_currentClient attributesForCharacterIndex:0 lineHeightRectangle:&inputPos];
SquirrelPanel* panel = NSApp.squirrelAppDelegate.panel;
panel.position = inputPos;
panel.inputController = self;
[panel showPreedit:preedit
selRange:selRange
caretPos:caretPos
Expand Down
5 changes: 4 additions & 1 deletion SquirrelPanel.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#import <Cocoa/Cocoa.h>
#import "SquirrelInputController.h"

@class SquirrelConfig;

@interface SquirrelPanel : NSWindow
@interface SquirrelPanel : NSPanel

// Linear candidate list, as opposed to stacked candidate list.
@property(nonatomic, readonly) BOOL linear;
Expand All @@ -15,6 +16,8 @@

// position of input caret on screen.
@property(nonatomic, assign) NSRect position;
// position of input caret on screen.
@property(nonatomic, assign) SquirrelInputController *inputController;

-(void)showPreedit:(NSString*)preedit
selRange:(NSRange)selRange
Expand Down
48 changes: 44 additions & 4 deletions SquirrelPanel.m
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,36 @@ - (void)drawRect:(NSRect)dirtyRect {
[_textView setTextContainerInset:NSMakeSize(textFieldOrigin.x, textFieldOrigin.y)];
}

- (BOOL)clickAtPoint:(NSPoint)_point index:(NSInteger *)_index {
if (CGPathContainsPoint(_shape.path, nil, _point, NO)) {
NSPoint point = NSMakePoint(_point.x - self.textView.textContainerInset.width,
_point.y - self.textView.textContainerInset.height);
NSTextLayoutFragment *fragment = [self.layoutManager textLayoutFragmentForPosition:point];
point = NSMakePoint(point.x - NSMinX(fragment.layoutFragmentFrame),
point.y - NSMinY(fragment.layoutFragmentFrame));
NSInteger index = [self.layoutManager offsetFromLocation: self.layoutManager.documentRange.location toLocation: fragment.rangeInElement.location];
for (NSUInteger i = 0; i < fragment.textLineFragments.count; i += 1) {
NSTextLineFragment *lineFragment = fragment.textLineFragments[i];
if (CGRectContainsPoint(lineFragment.typographicBounds, point)) {
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;
}
}
break;
}
}
return YES;
} else {
return NO;
}
}

@end

@implementation SquirrelPanel {
Expand Down Expand Up @@ -954,10 +984,7 @@ - (void)initializeUIStyleForDarkMode:(BOOL)isDark {
}

- (instancetype)init {
self = [super initWithContentRect:_position
styleMask:NSWindowStyleMaskBorderless
backing:NSBackingStoreBuffered
defer:YES];
self = [super initWithContentRect:_position styleMask:NSWindowStyleMaskNonactivatingPanel backing:NSBackingStoreBuffered defer:YES];
if (self) {
self.alphaValue = 1.0;
// _window.level = NSScreenSaverWindowLevel + 1;
Expand Down Expand Up @@ -986,6 +1013,19 @@ - (instancetype)init {
return self;
}

- (void)sendEvent:(NSEvent *)event {
if (event.type == NSEventTypeLeftMouseDown) {
NSPoint point = NSEvent.mouseLocation;
point = [self convertPointFromScreen:point];
point = [_view convertPoint:point fromView:nil];
NSInteger index = -1;
[_view clickAtPoint: point index:&index];
if (index >= 0) {
[self.inputController selectCandidate:index];
}
}
}

- (void)getCurrentScreen {
// get current screen
_screenRect = [NSScreen mainScreen].frame;
Expand Down

0 comments on commit 6af298c

Please sign in to comment.