From d3f54eba88154a3b5295cf914bddc472c201d14c Mon Sep 17 00:00:00 2001 From: wangyefeng Date: Tue, 5 Sep 2023 10:57:51 +0800 Subject: [PATCH 1/3] init --- SquirrelApplicationDelegate.m | 26 ++++++++++++++++++++++++++ SquirrelInputController.h | 3 +++ SquirrelInputController.m | 13 +++++++++++++ SquirrelPanel.h | 4 ++++ SquirrelPanel.m | 7 +++++++ main.m | 28 +++++++++++++++++++++++----- 6 files changed, 76 insertions(+), 5 deletions(-) diff --git a/SquirrelApplicationDelegate.m b/SquirrelApplicationDelegate.m index db1479ab6..d975bc333 100644 --- a/SquirrelApplicationDelegate.m +++ b/SquirrelApplicationDelegate.m @@ -1,4 +1,5 @@ #import "SquirrelApplicationDelegate.h" +#include #import #import "SquirrelConfig.h" @@ -22,6 +23,12 @@ -(IBAction)syncUserData:(id)sender rime_get_api()->sync_user_data(); } +-(void)userChange:(NSDictionary *)args +{ + NSLog(@"Sync user data"); + [_panel changeToAscii:args]; +} + -(IBAction)configure:(id)sender { [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[@"file://" stringByAppendingString:(@"~/Library/Rime").stringByStandardizingPath]]]; @@ -214,6 +221,17 @@ -(void)rimeNeedsSync:(NSNotification *)aNotification [self syncUserData:nil]; } +-(void)rimeNeedsChange:(NSNotification *)aNotification +{ + NSLog(@"Sync rime on demand."); + [self userChange:aNotification.userInfo]; +} + +-(void)rimeNeedsPrev:(NSNotification *)aNotification +{ + [_panel changeToPrev]; +} + -(NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender { NSLog(@"Squirrel is quitting."); @@ -242,6 +260,14 @@ -(void)awakeFromNib selector:@selector(rimeNeedsSync:) name:@"SquirrelSyncNotification" object:nil]; + [notifCenter addObserver:self + selector:@selector(rimeNeedsChange:) + name:@"SquirrelChangeNotification" + object:nil]; + [notifCenter addObserver:self + selector:@selector(rimeNeedsPrev:) + name:@"SquirrelChangePrevNotification" + object:nil]; } diff --git a/SquirrelInputController.h b/SquirrelInputController.h index 5e718b325..c3c81e0cf 100644 --- a/SquirrelInputController.h +++ b/SquirrelInputController.h @@ -4,4 +4,7 @@ @interface SquirrelInputController : IMKInputController - (BOOL)selectCandidate:(NSInteger)index; - (BOOL)pageUp:(BOOL)up; +-(void)changeToAscii:(NSDictionary *)args; +-(void)changeToPrev; + @end diff --git a/SquirrelInputController.m b/SquirrelInputController.m index 02f0ef8a4..82fc2b3b8 100644 --- a/SquirrelInputController.m +++ b/SquirrelInputController.m @@ -1,4 +1,5 @@ #import "SquirrelInputController.h" +#include #import "SquirrelApplicationDelegate.h" #import "SquirrelConfig.h" @@ -36,6 +37,7 @@ @implementation SquirrelInputController { NSTimer *_chordTimer; NSTimeInterval _chordDuration; NSString *_currentApp; + NSNumber *_prev; } /*! @@ -164,6 +166,17 @@ - (BOOL)handleEvent:(NSEvent*)event client:(id)sender return handled; } +-(void)changeToAscii:(NSDictionary *)args { + _prev = [NSNumber numberWithBool:rime_get_api()->get_option(_session, "ascii_mode")]; + rime_get_api()->set_option(_session, "ascii_mode", True); +} + +-(void)changeToPrev { + if (_prev) { + rime_get_api()->set_option(_session, "ascii_mode", [_prev boolValue]); + _prev = nil; + } +} -(BOOL)processKey:(int)rime_keycode modifiers:(int)rime_modifiers { diff --git a/SquirrelPanel.h b/SquirrelPanel.h index 159b2a8ad..a1db34917 100644 --- a/SquirrelPanel.h +++ b/SquirrelPanel.h @@ -1,4 +1,5 @@ #import +#include #import "SquirrelInputController.h" @class SquirrelConfig; @@ -35,4 +36,7 @@ -(void)loadConfig:(SquirrelConfig*)config forDarkMode:(BOOL)isDark; +-(void)changeToAscii:(NSDictionary *)args; +-(void)changeToPrev; + @end diff --git a/SquirrelPanel.m b/SquirrelPanel.m index 892471dd4..ca6ba3ac4 100644 --- a/SquirrelPanel.m +++ b/SquirrelPanel.m @@ -1030,6 +1030,13 @@ - (instancetype)init { } return self; } +-(void)changeToAscii:(NSDictionary *)args { + [self.inputController changeToAscii:args]; +} + +-(void)changeToPrev { + [self.inputController changeToPrev]; +} - (NSPoint)mousePosition { NSPoint point = NSEvent.mouseLocation; diff --git a/main.m b/main.m index 2a1a556f3..ff40f23b4 100644 --- a/main.m +++ b/main.m @@ -1,5 +1,6 @@ #import "SquirrelApplicationDelegate.h" +#include #import #import #import @@ -61,6 +62,22 @@ int main(int argc, char *argv[]) { return 0; } + if (argc > 2 && !strcmp("--ascii_mode", argv[1])) { + [[NSDistributedNotificationCenter defaultCenter] + postNotificationName:@"SquirrelChangeNotification" + object: nil + userInfo:@{@"ascii_mode" : [NSString stringWithFormat:@"%s", argv[2]]}]; + return 0; + } + + if (argc > 1 && !strcmp("--ascii_mode_prev", argv[1])) { + [[NSDistributedNotificationCenter defaultCenter] + postNotificationName:@"SquirrelChangePrevNotification" + object: nil]; + return 0; + } + + @autoreleasepool { // find the bundle identifier and then initialize the input method server NSBundle *main = [NSBundle mainBundle]; @@ -70,7 +87,9 @@ int main(int argc, char *argv[]) { // load the bundle explicitly because in this case the input method is a // background only application - [main loadNibNamed:@"MainMenu" owner:[NSApplication sharedApplication] topLevelObjects:NULL]; + [main loadNibNamed:@"MainMenu" + owner:[NSApplication sharedApplication] + topLevelObjects:NULL]; // opencc will be configured with relative dictionary paths [[NSFileManager defaultManager] @@ -78,11 +97,9 @@ int main(int argc, char *argv[]) { if (NSApp.squirrelAppDelegate.problematicLaunchDetected) { NSLog(@"Problematic launch detected!"); - NSArray *args = @[ - @"Problematic launch detected! \ + NSArray *args = @[ @"Problematic launch detected! \ Squirrel may be suffering a crash due to imporper configuration. \ - Revert previous modifications to see if the problem recurs." - ]; + Revert previous modifications to see if the problem recurs." ]; [NSTask launchedTaskWithLaunchPath:@"/usr/bin/say" arguments:args]; } else { [NSApp.squirrelAppDelegate setupRime]; @@ -94,6 +111,7 @@ int main(int argc, char *argv[]) { // finally run everything [[NSApplication sharedApplication] run]; + NSLog(@"Squirrel is quitting..."); rime_get_api()->finalize(); } From 955b8e735529f5e49549434535493d526fe09100 Mon Sep 17 00:00:00 2001 From: wangyefeng Date: Thu, 7 Sep 2023 20:45:51 +0800 Subject: [PATCH 2/3] clean code --- SquirrelApplicationDelegate.m | 24 ++++++++---------------- SquirrelInputController.h | 4 ++-- SquirrelInputController.m | 4 ++-- SquirrelPanel.h | 4 ++-- SquirrelPanel.m | 8 ++++---- main.m | 9 ++++----- 6 files changed, 22 insertions(+), 31 deletions(-) diff --git a/SquirrelApplicationDelegate.m b/SquirrelApplicationDelegate.m index d975bc333..ae1f5fffe 100644 --- a/SquirrelApplicationDelegate.m +++ b/SquirrelApplicationDelegate.m @@ -1,5 +1,4 @@ #import "SquirrelApplicationDelegate.h" -#include #import #import "SquirrelConfig.h" @@ -23,12 +22,6 @@ -(IBAction)syncUserData:(id)sender rime_get_api()->sync_user_data(); } --(void)userChange:(NSDictionary *)args -{ - NSLog(@"Sync user data"); - [_panel changeToAscii:args]; -} - -(IBAction)configure:(id)sender { [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[@"file://" stringByAppendingString:(@"~/Library/Rime").stringByStandardizingPath]]]; @@ -221,15 +214,14 @@ -(void)rimeNeedsSync:(NSNotification *)aNotification [self syncUserData:nil]; } --(void)rimeNeedsChange:(NSNotification *)aNotification +-(void)rimeChangeToAsciiMode:(NSNotification *)aNotification { - NSLog(@"Sync rime on demand."); - [self userChange:aNotification.userInfo]; + [_panel changeToAscii]; } --(void)rimeNeedsPrev:(NSNotification *)aNotification +-(void)rimeChangeToAsciiModePrev:(NSNotification *)aNotification { - [_panel changeToPrev]; + [_panel changeToAsciiPrev]; } -(NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender @@ -261,12 +253,12 @@ -(void)awakeFromNib name:@"SquirrelSyncNotification" object:nil]; [notifCenter addObserver:self - selector:@selector(rimeNeedsChange:) - name:@"SquirrelChangeNotification" + selector:@selector(rimeChangeToAsciiMode:) + name:@"SquirrelChangeToAsciiModeNotification" object:nil]; [notifCenter addObserver:self - selector:@selector(rimeNeedsPrev:) - name:@"SquirrelChangePrevNotification" + selector:@selector(rimeChangeToAsciiModePrev:) + name:@"SquirrelChangeToAsciiModePrevNotification" object:nil]; } diff --git a/SquirrelInputController.h b/SquirrelInputController.h index c3c81e0cf..e11e0e49b 100644 --- a/SquirrelInputController.h +++ b/SquirrelInputController.h @@ -4,7 +4,7 @@ @interface SquirrelInputController : IMKInputController - (BOOL)selectCandidate:(NSInteger)index; - (BOOL)pageUp:(BOOL)up; --(void)changeToAscii:(NSDictionary *)args; --(void)changeToPrev; +-(void)changeToAscii; +-(void)changeToAsciiPrev; @end diff --git a/SquirrelInputController.m b/SquirrelInputController.m index 82fc2b3b8..e6b638a4d 100644 --- a/SquirrelInputController.m +++ b/SquirrelInputController.m @@ -166,12 +166,12 @@ - (BOOL)handleEvent:(NSEvent*)event client:(id)sender return handled; } --(void)changeToAscii:(NSDictionary *)args { +-(void)changeToAscii { _prev = [NSNumber numberWithBool:rime_get_api()->get_option(_session, "ascii_mode")]; rime_get_api()->set_option(_session, "ascii_mode", True); } --(void)changeToPrev { +-(void)changeToAsciiPrev { if (_prev) { rime_get_api()->set_option(_session, "ascii_mode", [_prev boolValue]); _prev = nil; diff --git a/SquirrelPanel.h b/SquirrelPanel.h index a1db34917..d5750cd13 100644 --- a/SquirrelPanel.h +++ b/SquirrelPanel.h @@ -36,7 +36,7 @@ -(void)loadConfig:(SquirrelConfig*)config forDarkMode:(BOOL)isDark; --(void)changeToAscii:(NSDictionary *)args; --(void)changeToPrev; +-(void)changeToAscii; +-(void)changeToAsciiPrev; @end diff --git a/SquirrelPanel.m b/SquirrelPanel.m index ca6ba3ac4..20bec556e 100644 --- a/SquirrelPanel.m +++ b/SquirrelPanel.m @@ -1030,12 +1030,12 @@ - (instancetype)init { } return self; } --(void)changeToAscii:(NSDictionary *)args { - [self.inputController changeToAscii:args]; +-(void)changeToAscii { + [self.inputController changeToAscii]; } --(void)changeToPrev { - [self.inputController changeToPrev]; +-(void)changeToAsciiPrev { + [self.inputController changeToAsciiPrev]; } - (NSPoint)mousePosition { diff --git a/main.m b/main.m index ff40f23b4..89a87321e 100644 --- a/main.m +++ b/main.m @@ -62,17 +62,16 @@ int main(int argc, char *argv[]) { return 0; } - if (argc > 2 && !strcmp("--ascii_mode", argv[1])) { + if (argc > 1 && !strcmp("--ascii_mode", argv[1])) { [[NSDistributedNotificationCenter defaultCenter] - postNotificationName:@"SquirrelChangeNotification" - object: nil - userInfo:@{@"ascii_mode" : [NSString stringWithFormat:@"%s", argv[2]]}]; + postNotificationName:@"SquirrelChangeToAsciiModeNotification" + object: nil]; return 0; } if (argc > 1 && !strcmp("--ascii_mode_prev", argv[1])) { [[NSDistributedNotificationCenter defaultCenter] - postNotificationName:@"SquirrelChangePrevNotification" + postNotificationName:@"SquirrelChangeToAsciiModePrevNotification" object: nil]; return 0; } From 8aa709c90276adc08d5ec8d95ec11ab434859c48 Mon Sep 17 00:00:00 2001 From: wangyefeng Date: Thu, 7 Sep 2023 20:48:23 +0800 Subject: [PATCH 3/3] clean code --- SquirrelInputController.m | 1 - SquirrelPanel.h | 1 - main.m | 2 -- 3 files changed, 4 deletions(-) diff --git a/SquirrelInputController.m b/SquirrelInputController.m index e6b638a4d..690e396f9 100644 --- a/SquirrelInputController.m +++ b/SquirrelInputController.m @@ -1,5 +1,4 @@ #import "SquirrelInputController.h" -#include #import "SquirrelApplicationDelegate.h" #import "SquirrelConfig.h" diff --git a/SquirrelPanel.h b/SquirrelPanel.h index d5750cd13..299d72a0d 100644 --- a/SquirrelPanel.h +++ b/SquirrelPanel.h @@ -1,5 +1,4 @@ #import -#include #import "SquirrelInputController.h" @class SquirrelConfig; diff --git a/main.m b/main.m index 89a87321e..0cecc9142 100644 --- a/main.m +++ b/main.m @@ -1,6 +1,5 @@ #import "SquirrelApplicationDelegate.h" -#include #import #import #import @@ -110,7 +109,6 @@ int main(int argc, char *argv[]) { // finally run everything [[NSApplication sharedApplication] run]; - NSLog(@"Squirrel is quitting..."); rime_get_api()->finalize(); }