Skip to content

Commit

Permalink
cleanup codes
Browse files Browse the repository at this point in the history
  • Loading branch information
groverlynn committed Nov 18, 2023
1 parent 81cdbfe commit 7a65546
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 180 deletions.
12 changes: 6 additions & 6 deletions SquirrelApplicationDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ - (void)loadSettings {

_enableNotifications =
![[_config getString:@"show_notifications_when"] isEqualToString:@"never"];
[self.panel loadConfig:_config forDarkMode:NO];
[self.panel loadConfig:_config forDarkMode:YES];
[self.panel loadConfig:_config forAppearance:defaultAppear];
[self.panel loadConfig:_config forAppearance:darkAppear];
}

- (void)loadSchemaSpecificSettings:(NSString *)schemaId {
Expand All @@ -159,11 +159,11 @@ - (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 forDarkMode:YES];
[self.panel loadConfig:schema forAppearance:defaultAppear];
[self.panel loadConfig:schema forAppearance:darkAppear];
} else {
[self.panel loadConfig:self.config forDarkMode:NO];
[self.panel loadConfig:self.config forDarkMode:YES];
[self.panel loadConfig:self.config forAppearance:defaultAppear];
[self.panel loadConfig:self.config forAppearance:darkAppear];
}
[schema close];
}
Expand Down
13 changes: 8 additions & 5 deletions SquirrelInputController.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
@interface SquirrelInputController : IMKInputController

typedef enum {
kSELECT = 1,
kDELETE = 2,
kCHOOSE = 3
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;

typedef enum {
// 0 ... 9 are ordinals, 0x21 ... 0x7e are ascii chars
// 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;
- (void)perform:(rimeAction)action
onIndex:(rimeIndex)index;

@end
80 changes: 46 additions & 34 deletions SquirrelInputController.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,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 @@ -93,36 +94,34 @@ - (BOOL)handleEvent:(NSEvent *)event client:(id)sender
CGEventSourceCounterForEventType(kCGEventSourceStateCombinedSessionState, kCGEventOtherMouseDown);
_lastModifier = modifiers;
switch (keyCode) {
case kVK_CapsLock: {
case kVK_CapsLock:
rime_modifiers ^= kLockMask;
[self processKey:rime_keycode modifiers:rime_modifiers];
} break;
break;
case kVK_Shift:
case kVK_RightShift: {
case kVK_RightShift:
release_mask = modifiers & NSEventModifierFlagShift ? 0 : kReleaseMask | (eventCount - _lastEventCount == 1 ? 0 : kIgnoredMask);
[self processKey:rime_keycode modifiers:(rime_modifiers | release_mask)];
} break;
break;
case kVK_Control:
case kVK_RightControl: {
case kVK_RightControl:
release_mask = modifiers & NSEventModifierFlagControl ? 0 : kReleaseMask | (eventCount - _lastEventCount == 1 ? 0 : kIgnoredMask);
[self processKey:rime_keycode modifiers:(rime_modifiers | release_mask)];
} break;
break;
case kVK_Option:
case kVK_RightOption: {
case kVK_RightOption:
release_mask = modifiers & NSEventModifierFlagOption ? 0 : kReleaseMask | (eventCount - _lastEventCount == 1 ? 0 : kIgnoredMask);
[self processKey:rime_keycode modifiers:(rime_modifiers | release_mask)];
} break;
case kVK_Function: {
break;
case kVK_Function:
release_mask = modifiers & NSEventModifierFlagFunction ? 0 : kReleaseMask | (eventCount - _lastEventCount == 1 ? 0 : kIgnoredMask);
[self processKey:rime_keycode modifiers:(rime_modifiers | release_mask)];
} break;
break;
case kVK_Command:
case kVK_RightCommand: {
case kVK_RightCommand:
release_mask = modifiers & NSEventModifierFlagCommand ? 0 : kReleaseMask | (eventCount - _lastEventCount == 1 ? 0 : kIgnoredMask);
[self processKey:rime_keycode modifiers:(rime_modifiers | release_mask)];
goto saveStatus;
}
default:
break;
}
[self rimeUpdate];
Expand Down Expand Up @@ -162,7 +161,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 @@ -209,7 +209,8 @@ - (BOOL)processKey:(int)rime_keycode modifiers:(int)rime_modifiers
return handled;
}

- (void)perform:(rimeAction)action onIndex:(rimeIndex)index {
- (void)perform:(rimeAction)action
onIndex:(rimeIndex)index {
bool handled = false;
if (action == kSELECT && ((index >= '!' && index <= '~') ||
index == kPageUp || index == kPageDown)) {
Expand Down Expand Up @@ -244,7 +245,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 (uint i = 0; i < _chordKeyCount; ++i) {
Expand Down Expand Up @@ -308,7 +310,9 @@ - (void)activateServer:(id)sender
}
}

- (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]) {
Expand Down Expand Up @@ -391,7 +395,7 @@ - (id)composedString:(id)sender
return _composedString;
}

- (NSArray *)candidates:(id)sender
- (NSArray<NSString *> *)candidates:(id)sender
{
return _candidates;
}
Expand Down Expand Up @@ -486,27 +490,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 @@ -633,7 +642,7 @@ - (void)rimeUpdate
if (RimeGetContext(_session, &ctx)) {
// update raw input
const char *raw_input = RimeGetInput(_session);
_originalString = @(raw_input);
_originalString = raw_input ? @(raw_input) : @"";

// update preedit text
const char *preedit = ctx.composition.preedit;
Expand Down Expand Up @@ -662,16 +671,19 @@ - (void)rimeUpdate
NSString *candidatePreviewText = candidatePreview ? @(candidatePreview) : @"";
if (_inlinePreedit) {
if ((caretPos >= end) && (caretPos < length)) {
candidatePreviewText = [candidatePreviewText stringByAppendingString:[preeditText substringWithRange:NSMakeRange(caretPos, length - caretPos)]];
candidatePreviewText = [candidatePreviewText stringByAppendingString:
[preeditText substringWithRange:NSMakeRange(caretPos, length - caretPos)]];
}
[self showPreeditString:candidatePreviewText
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))];
candidatePreviewText = [candidatePreviewText substringWithRange:
NSMakeRange(0, candidatePreviewText.length - (caretPos - end))];
} else if ((end < length) && (caretPos < end)) {
candidatePreviewText = [candidatePreviewText substringWithRange:NSMakeRange(0, candidatePreviewText.length - (length - end))];
candidatePreviewText = [candidatePreviewText substringWithRange:
NSMakeRange(0, candidatePreviewText.length - (length - end))];
}
[self showPreeditString:candidatePreviewText
selRange:NSMakeRange(start - (caretPos < end), candidatePreviewText.length - start + (caretPos < end))
Expand Down Expand Up @@ -703,7 +715,7 @@ - (void)rimeUpdate
lastPage:(BOOL)ctx.menu.is_last_page];
RimeFreeContext(&ctx);
} else {
[NSApp.squirrelAppDelegate.panel hide];
[self hidePalettes];
}
}

Expand Down
12 changes: 8 additions & 4 deletions SquirrelPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,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 @@ -30,17 +36,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 7a65546

Please sign in to comment.