diff --git a/SquirrelApplicationDelegate.m b/SquirrelApplicationDelegate.m index db1479ab6..ae1f5fffe 100644 --- a/SquirrelApplicationDelegate.m +++ b/SquirrelApplicationDelegate.m @@ -214,6 +214,16 @@ -(void)rimeNeedsSync:(NSNotification *)aNotification [self syncUserData:nil]; } +-(void)rimeChangeToAsciiMode:(NSNotification *)aNotification +{ + [_panel changeToAscii]; +} + +-(void)rimeChangeToAsciiModePrev:(NSNotification *)aNotification +{ + [_panel changeToAsciiPrev]; +} + -(NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender { NSLog(@"Squirrel is quitting."); @@ -242,6 +252,14 @@ -(void)awakeFromNib selector:@selector(rimeNeedsSync:) name:@"SquirrelSyncNotification" object:nil]; + [notifCenter addObserver:self + selector:@selector(rimeChangeToAsciiMode:) + name:@"SquirrelChangeToAsciiModeNotification" + object:nil]; + [notifCenter addObserver:self + selector:@selector(rimeChangeToAsciiModePrev:) + name:@"SquirrelChangeToAsciiModePrevNotification" + object:nil]; } diff --git a/SquirrelInputController.h b/SquirrelInputController.h index 5e718b325..e11e0e49b 100644 --- a/SquirrelInputController.h +++ b/SquirrelInputController.h @@ -4,4 +4,7 @@ @interface SquirrelInputController : IMKInputController - (BOOL)selectCandidate:(NSInteger)index; - (BOOL)pageUp:(BOOL)up; +-(void)changeToAscii; +-(void)changeToAsciiPrev; + @end diff --git a/SquirrelInputController.m b/SquirrelInputController.m index 02f0ef8a4..690e396f9 100644 --- a/SquirrelInputController.m +++ b/SquirrelInputController.m @@ -36,6 +36,7 @@ @implementation SquirrelInputController { NSTimer *_chordTimer; NSTimeInterval _chordDuration; NSString *_currentApp; + NSNumber *_prev; } /*! @@ -164,6 +165,17 @@ - (BOOL)handleEvent:(NSEvent*)event client:(id)sender return handled; } +-(void)changeToAscii { + _prev = [NSNumber numberWithBool:rime_get_api()->get_option(_session, "ascii_mode")]; + rime_get_api()->set_option(_session, "ascii_mode", True); +} + +-(void)changeToAsciiPrev { + 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..299d72a0d 100644 --- a/SquirrelPanel.h +++ b/SquirrelPanel.h @@ -35,4 +35,7 @@ -(void)loadConfig:(SquirrelConfig*)config forDarkMode:(BOOL)isDark; +-(void)changeToAscii; +-(void)changeToAsciiPrev; + @end diff --git a/SquirrelPanel.m b/SquirrelPanel.m index 892471dd4..20bec556e 100644 --- a/SquirrelPanel.m +++ b/SquirrelPanel.m @@ -1030,6 +1030,13 @@ - (instancetype)init { } return self; } +-(void)changeToAscii { + [self.inputController changeToAscii]; +} + +-(void)changeToAsciiPrev { + [self.inputController changeToAsciiPrev]; +} - (NSPoint)mousePosition { NSPoint point = NSEvent.mouseLocation; diff --git a/main.m b/main.m index 2a1a556f3..0cecc9142 100644 --- a/main.m +++ b/main.m @@ -61,6 +61,21 @@ int main(int argc, char *argv[]) { return 0; } + if (argc > 1 && !strcmp("--ascii_mode", argv[1])) { + [[NSDistributedNotificationCenter defaultCenter] + postNotificationName:@"SquirrelChangeToAsciiModeNotification" + object: nil]; + return 0; + } + + if (argc > 1 && !strcmp("--ascii_mode_prev", argv[1])) { + [[NSDistributedNotificationCenter defaultCenter] + postNotificationName:@"SquirrelChangeToAsciiModePrevNotification" + object: nil]; + return 0; + } + + @autoreleasepool { // find the bundle identifier and then initialize the input method server NSBundle *main = [NSBundle mainBundle]; @@ -70,7 +85,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 +95,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];