Skip to content

Commit

Permalink
compatible with predictor & cleanup codes
Browse files Browse the repository at this point in the history
  • Loading branch information
groverlynn committed Nov 18, 2023
1 parent 35fd9a4 commit 532a2d0
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 253 deletions.
12 changes: 6 additions & 6 deletions SquirrelApplicationDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ - (void)loadSettings {

_enableNotifications =
![[_config getString:@"show_notifications_when"] isEqualToString:@"never"];
[self.panel loadConfig:_config forDarkMode:NO];
[self.panel loadConfig:_config forAppearance:defaultAppear];
if (@available(macOS 10.14, *)) {
[self.panel loadConfig:_config forDarkMode:YES];
[self.panel loadConfig:_config forAppearance:darkAppear];
}
}

Expand All @@ -160,16 +160,16 @@ - (void)loadSchemaSpecificSettings:(NSString *)schemaId {
SquirrelConfig *schema = [[SquirrelConfig alloc] init];
if ([schema openWithSchemaId:schemaId baseConfig:self.config] &&
[schema hasSection:@"style"]) {
[self.panel loadConfig:schema forDarkMode:NO];
[self.panel loadConfig:schema forAppearance:defaultAppear];
} else {
[self.panel loadConfig:self.config forDarkMode:NO];
[self.panel loadConfig:self.config forAppearance:defaultAppear];
}
if (@available(macOS 10.14, *)) {
if ([schema openWithSchemaId:schemaId baseConfig:self.config] &&
[schema hasSection:@"style"]) {
[self.panel loadConfig:schema forDarkMode:YES];
[self.panel loadConfig:schema forAppearance:darkAppear];
} else {
[self.panel loadConfig:self.config forDarkMode:YES];
[self.panel loadConfig:self.config forAppearance:darkAppear];
}
}
[schema close];
Expand Down
22 changes: 17 additions & 5 deletions SquirrelInputController.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@

@interface SquirrelInputController : IMKInputController

- (BOOL)perform:(NSUInteger)action onIndex:(NSUInteger)index;
typedef enum {
kSELECT = 1, // accepts indices in both digits and selection keys
kDELETE = 2, // only accepts indices in digits, e.g. (int) 1
kCHOOSE = 3 // only accepts indices in selection keys, e.g. (char) '1' / 'A'
} rimeAction;

@end
typedef enum {
// 0 ... 9 are ordinal digits, used as (int) index
// 0x21 ... 0x7e are ASCII chars (as selection keys)
// other rime keycodes (as function keys), for paging etc.
kPageUp = 0xff55, // XK_Page_Up
kPageDown = 0xff56, // XK_Page_Down
kVoidSymbol = 0xffffff // XK_VoidSymbol
} rimeIndex;

- (void)perform:(rimeAction)action
onIndex:(rimeIndex)index;

#define kSELECT 0x1
#define kDELETE 0x2
#define kCHOOSE 0x3
@end
136 changes: 72 additions & 64 deletions SquirrelInputController.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ @implementation SquirrelInputController {
NSString *_schemaId;
BOOL _inlinePreedit;
BOOL _inlineCandidate;
BOOL _showingSwitcherMenu;
// for chord-typing
int _chordKeyCodes[N_KEY_ROLL_OVER];
int _chordModifiers[N_KEY_ROLL_OVER];
Expand All @@ -44,7 +45,8 @@ @implementation SquirrelInputController {
@abstract Receive incoming event
@discussion This method receives key events from the client application.
*/
- (BOOL)handleEvent:(NSEvent *)event client:(id)sender
- (BOOL)handleEvent:(NSEvent *)event
client:(id)sender
{
// Return YES to indicate the the key input was received and dealt with.
// Key processing will not continue in that case. In other words the
Expand Down Expand Up @@ -82,7 +84,7 @@ - (BOOL)handleEvent:(NSEvent *)event client:(id)sender
int rime_modifiers = osx_modifiers_to_rime_modifiers(modifiers);
int rime_keycode = 0;
// For flags-changed event, keyCode is available since macOS 10.15 (#715)
Bool keyCodeAvailable = NO;
BOOL keyCodeAvailable = NO;
if (@available(macOS 10.15, *)) {
keyCodeAvailable = YES;
rime_keycode = osx_keycode_to_rime_keycode(event.keyCode, 0, 0, 0);
Expand Down Expand Up @@ -165,7 +167,8 @@ - (BOOL)handleEvent:(NSEvent *)event client:(id)sender
return handled;
}

- (BOOL)processKey:(int)rime_keycode modifiers:(int)rime_modifiers
- (BOOL)processKey:(int)rime_keycode
modifiers:(int)rime_modifiers
{
// with linear candidate list, arrow keys may behave differently.
Bool is_linear = NSApp.squirrelAppDelegate.panel.linear;
Expand Down Expand Up @@ -215,25 +218,21 @@ - (BOOL)processKey:(int)rime_keycode modifiers:(int)rime_modifiers
return handled;
}

- (BOOL)perform:(NSUInteger)action onIndex:(NSUInteger)index {
Bool handled = False;
if (index == NSPageUpFunctionKey && action == kSELECT) {
handled = rime_get_api()->process_key(_session, XK_Page_Up, 0);
} else if (index == NSPageDownFunctionKey && action == kSELECT) {
handled = rime_get_api()->process_key(_session, XK_Page_Down, 0);
} else if (index >= 0 && index < 10) {
if (action == kSELECT) {
handled = rime_get_api()->select_candidate_on_current_page(_session, (int)index);
} else if (action == kCHOOSE) {
handled = rime_get_api()->choose_candidate_on_current_page(_session, (int)index);
} else if (action == kDELETE) {
handled = rime_get_api()->delete_candidate_on_current_page(_session, (int)index);
}
- (void)perform:(rimeAction)action
onIndex:(rimeIndex)index {
bool handled = false;
if (action == kSELECT && ((index >= '!' && index <= '~') ||
index == kPageUp || index == kPageDown)) {
handled = RimeProcessKey(_session, (int)index, 0);
} else if (action == kCHOOSE && index >= '!' && index <= '~') {
handled = RimeProcessKey(_session, (int)index, kAltMask);
} else if (action == kDELETE && index >= 0 && index < 10) {
// kDELETE takes ordinal digits (instead of characters) as indexes
handled= RimeDeleteCandidateOnCurrentPage(_session, (size_t)index);
}
if (handled && action != kCHOOSE) {
if (handled) {
[self rimeUpdate];
}
return (BOOL)handled;
}

- (void)onChordTimer:(NSTimer *)timer
Expand All @@ -255,7 +254,8 @@ - (void)onChordTimer:(NSTimer *)timer
}
}

- (void)updateChord:(int)keycode modifiers:(int)modifiers
- (void)updateChord:(int)keycode
modifiers:(int)modifiers
{
//NSLog(@"update chord: {%s} << %x", _chord, keycode);
for (int i = 0; i < _chordKeyCount; ++i) {
Expand Down Expand Up @@ -317,18 +317,17 @@ - (void)activateServer:(id)sender
if (keyboardLayout) {
[sender overrideKeyboardWithKeyboardNamed:keyboardLayout];
}
_preeditString = @"";
}

- (instancetype)initWithServer:(IMKServer *)server delegate:(id)delegate client:(id)inputClient
- (instancetype)initWithServer:(IMKServer *)server
delegate:(id)delegate
client:(id)inputClient
{
//NSLog(@"initWithServer:delegate:client:");
if (self = [super initWithServer:server delegate:delegate client:inputClient]) {
_currentClient = inputClient;
[self createSession];
_composedString = @"";
_originalString = @"";
_preeditString = [[NSMutableAttributedString alloc] initWithString:@""];
_preeditString = @"";
}
return self;
}
Expand Down Expand Up @@ -360,7 +359,7 @@ - (void)commitComposition:(id)sender
if (raw_input) {
[self commitString:@(raw_input)];
rime_get_api()->clear_composition(_session);
}
};
}
}

Expand Down Expand Up @@ -398,7 +397,7 @@ - (NSMenu *)menu
return NSApp.squirrelAppDelegate.menu;
}

- (NSArray *)candidates:(id)sender
- (NSArray<NSString *> *)candidates:(id)sender
{
return _candidates;
}
Expand Down Expand Up @@ -469,27 +468,32 @@ - (void)showPanelWithPreedit:(NSString *)preedit
SquirrelPanel *panel = NSApp.squirrelAppDelegate.panel;
if (@available(macOS 14.0, *)) { // avoid overlapping with cursor effects view
if (_lastModifier & NSEventModifierFlagCapsLock) {
if (NSHeight(inputPos) > NSWidth(inputPos)) {
inputPos.size.height += 26;
inputPos.origin.y -= 26;
} else {
inputPos.size.width += 33;
inputPos.origin.x -= 33;
}
NSRect screenRect = [[NSScreen mainScreen] visibleFrame];
NSRect capslockAccessory = NSWidth(inputPos) > NSHeight(inputPos) ?
NSMakeRect(NSMinX(inputPos) - 30, NSMinY(inputPos) - 13, 27, 27) :
NSMakeRect(NSMinX(inputPos) - 13, NSMinY(inputPos) - 33, 27, 30);
if (NSMinX(capslockAccessory) < NSMinX(screenRect))
capslockAccessory.origin.x = NSMinX(screenRect);
if (NSMaxX(capslockAccessory) > NSMaxX(screenRect))
capslockAccessory.origin.x = NSMaxX(screenRect) - NSWidth(capslockAccessory);
if (NSMinY(capslockAccessory) < NSMinY(screenRect))
capslockAccessory.origin.y = NSWidth(inputPos) > NSHeight(inputPos) ? NSMinY(screenRect) : NSMaxY(inputPos) + 3;
if (NSMaxY(capslockAccessory) > NSMaxY(screenRect))
capslockAccessory.origin.y = NSMaxY(screenRect) - NSHeight(capslockAccessory);
inputPos = NSUnionRect(inputPos, capslockAccessory);
}
}
panel.position = inputPos;
panel.inputController = self;
panel.level = self.client.windowLevel + 1;
[panel showPreedit:preedit
selRange:selRange
caretPos:caretPos
candidates:candidates
comments:comments
highlighted:index
pageNum:pageNum
lastPage:lastPage
turnPage:NSNotFound
update:YES];
lastPage:lastPage];
}

@end // SquirrelController
Expand Down Expand Up @@ -586,23 +590,24 @@ - (void)rimeUpdate
//NSLog(@"rimeUpdate");
[self rimeConsumeCommittedText];

BOOL switcherMenu = rime_get_api()->get_option(_session, "dumb");
RIME_STRUCT(RimeStatus, status);
if (rime_get_api()->get_status(_session, &status)) {
// enable schema specific ui style
if (!switcherMenu && (!_schemaId || strcmp(_schemaId.UTF8String, status.schema_id))) {
if ((!_schemaId || strcmp(_schemaId.UTF8String, status.schema_id))) {
_schemaId = @(status.schema_id);
[self updateStyleOptions];
[NSApp.squirrelAppDelegate loadSchemaSpecificLabels:_schemaId];
[NSApp.squirrelAppDelegate loadSchemaSpecificSettings:_schemaId];
// inline preedit
_inlinePreedit = (NSApp.squirrelAppDelegate.panel.inlinePreedit &&
!rime_get_api()->get_option(_session, "no_inline")) ||
rime_get_api()->get_option(_session, "inline");
_inlineCandidate = (NSApp.squirrelAppDelegate.panel.inlineCandidate &&
!rime_get_api()->get_option(_session, "no_inline"));
// if not inline, embed soft cursor in preedit string
rime_get_api()->set_option(_session, "soft_cursor", !_inlinePreedit);
if (!(_showingSwitcherMenu = RimeGetOption(_session, "dumb"))) {
[self updateStyleOptions];
[NSApp.squirrelAppDelegate loadSchemaSpecificLabels:_schemaId];
[NSApp.squirrelAppDelegate loadSchemaSpecificSettings:_schemaId];
// inline preedit
_inlinePreedit = (NSApp.squirrelAppDelegate.panel.inlinePreedit &&
!rime_get_api()->get_option(_session, "no_inline")) ||
rime_get_api()->get_option(_session, "inline");
_inlineCandidate = (NSApp.squirrelAppDelegate.panel.inlineCandidate &&
!rime_get_api()->get_option(_session, "no_inline"));
// if not inline, embed soft cursor in preedit string
rime_get_api()->set_option(_session, "soft_cursor", !_inlinePreedit);
}
}
rime_get_api()->free_status(&status);
}
Expand All @@ -617,34 +622,37 @@ - (void)rimeUpdate
NSUInteger end = [[NSString alloc] initWithBytes:preedit length:(NSUInteger)ctx.composition.sel_end encoding:NSUTF8StringEncoding].length;
NSUInteger caretPos = [[NSString alloc] initWithBytes:preedit length:(NSUInteger)ctx.composition.cursor_pos encoding:NSUTF8StringEncoding].length;
NSUInteger length = [[NSString alloc] initWithBytes:preedit length:(NSUInteger)ctx.composition.length encoding:NSUTF8StringEncoding].length;
if (_inlineCandidate && !switcherMenu) {
if (_inlineCandidate && !_showingSwitcherMenu) {
const char *candidatePreview = ctx.commit_text_preview;
NSString *candidatePreviewText = candidatePreview ? @(candidatePreview) : @"";
if (_inlinePreedit) {
if ((caretPos >= end) && (caretPos < preeditText.length)) {
candidatePreviewText = [candidatePreviewText stringByAppendingString:[preeditText substringWithRange:NSMakeRange(caretPos, preeditText.length - caretPos)]];
if ((caretPos >= end) && (caretPos < length)) {
candidatePreviewText = [candidatePreviewText stringByAppendingString:
[preeditText substringWithRange:NSMakeRange(caretPos, length - caretPos)]];
}
[self showPreeditString:candidatePreviewText
selRange:NSMakeRange(start, candidatePreviewText.length - (preeditText.length - end) - start)
caretPos:caretPos <= start ? caretPos : candidatePreviewText.length - (preeditText.length - caretPos)];
selRange:NSMakeRange(start, candidatePreviewText.length - (length - end) - start)
caretPos:caretPos <= start ? caretPos - 1 : candidatePreviewText.length - (length - caretPos)];
} else {
if ((end < caretPos) && (caretPos > start)) {
candidatePreviewText = [candidatePreviewText substringWithRange:NSMakeRange(0, candidatePreviewText.length - (caretPos - end))];
} else if ((end < preeditText.length) && (caretPos < end)) {
candidatePreviewText = [candidatePreviewText substringWithRange:NSMakeRange(0, candidatePreviewText.length - (preeditText.length - end))];
candidatePreviewText = [candidatePreviewText substringWithRange:
NSMakeRange(0, candidatePreviewText.length - (caretPos - end))];
} else if ((end < length) && (caretPos < end)) {
candidatePreviewText = [candidatePreviewText substringWithRange:
NSMakeRange(0, candidatePreviewText.length - (length - end))];
}
[self showPreeditString:candidatePreviewText
selRange:NSMakeRange(start - (caretPos < end), candidatePreviewText.length - start + (caretPos < end))
caretPos:caretPos < end ? caretPos - 1 : candidatePreviewText.length];
caretPos:caretPos == 0 ? 0 : (caretPos < end ? caretPos - 1 : candidatePreviewText.length)];
}
} else {
if (_inlinePreedit && !switcherMenu) {
if (_inlinePreedit && !_showingSwitcherMenu) {
[self showPreeditString:preeditText selRange:NSMakeRange(start, end - start) caretPos:caretPos];
} else {
// TRICKY: display a non-empty string to prevent iTerm2 from echoing each character in preedit.
// note this is a full-width EM space U+2003; using narrow characters like "..." will result in
// note this is a full-width space U+3000; using narrow characters like "..." will result in
// an unstable baseline when composing Chinese characters.
[self showPreeditString:(preedit ? @"" : @"") selRange:NSMakeRange(0, 0) caretPos:0];
[self showPreeditString:(preedit ? @" " : @"") selRange:NSMakeRange(0, 0) caretPos:0];
}
}
// update candidates
Expand All @@ -654,9 +662,9 @@ - (void)rimeUpdate
[candidates addObject:@(ctx.menu.candidates[i].text)];
[comments addObject:@(ctx.menu.candidates[i].comment ? : "")];
}
[self showPanelWithPreedit:(_inlinePreedit && !switcherMenu ? nil : preeditText)
[self showPanelWithPreedit:(_inlinePreedit && !_showingSwitcherMenu ? nil : preeditText)
selRange:NSMakeRange(start, end - start)
caretPos:(switcherMenu ? NSNotFound : caretPos)
caretPos:(_showingSwitcherMenu ? NSNotFound : caretPos)
candidates:candidates
comments:comments
highlighted:(NSUInteger)ctx.menu.highlighted_candidate_index
Expand Down
12 changes: 8 additions & 4 deletions SquirrelPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
@class SquirrelConfig;
@class SquirrelOptionSwitcher;

typedef enum {
defaultAppear = 0,
lightAppear = 0,
darkAppear = 1
} SquirrelAppear;

@interface SquirrelPanel : NSPanel

// Linear candidate list, as opposed to stacked candidate list.
Expand All @@ -29,17 +35,15 @@
comments:(NSArray<NSString *> *)comments
highlighted:(NSUInteger)index
pageNum:(NSUInteger)pageNum
lastPage:(BOOL)lastPage
turnPage:(NSUInteger)turnPage
update:(BOOL)update;
lastPage:(BOOL)lastPage;

- (void)hide;

- (void)updateStatusLong:(NSString *)messageLong
statusShort:(NSString *)messageShort;

- (void)loadConfig:(SquirrelConfig *)config
forDarkMode:(BOOL)isDark;
forAppearance:(SquirrelAppear)appear;

- (void)loadLabelConfig:(SquirrelConfig *)config;

Expand Down
Loading

0 comments on commit 532a2d0

Please sign in to comment.