Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持命令行切换中英文模式 #797

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions SquirrelApplicationDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down Expand Up @@ -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];

}

Expand Down
3 changes: 3 additions & 0 deletions SquirrelInputController.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
@interface SquirrelInputController : IMKInputController
- (BOOL)selectCandidate:(NSInteger)index;
- (BOOL)pageUp:(BOOL)up;
-(void)changeToAscii;
-(void)changeToAsciiPrev;

@end
12 changes: 12 additions & 0 deletions SquirrelInputController.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ @implementation SquirrelInputController {
NSTimer *_chordTimer;
NSTimeInterval _chordDuration;
NSString *_currentApp;
NSNumber *_prev;
}

/*!
Expand Down Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_prev does not sync with the current status from the rime session.

Between last call to changeToAscii and this one, the ascii_mode value could have changed by the user.

rime_get_api()->set_option(_session, "ascii_mode", [_prev boolValue]);
_prev = nil;
}
}

-(BOOL)processKey:(int)rime_keycode modifiers:(int)rime_modifiers
{
Expand Down
3 changes: 3 additions & 0 deletions SquirrelPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@
-(void)loadConfig:(SquirrelConfig*)config
forDarkMode:(BOOL)isDark;

-(void)changeToAscii;
-(void)changeToAsciiPrev;

@end
7 changes: 7 additions & 0 deletions SquirrelPanel.m
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,13 @@ - (instancetype)init {
}
return self;
}
-(void)changeToAscii {
[self.inputController changeToAscii];
}

-(void)changeToAsciiPrev {
[self.inputController changeToAsciiPrev];
}

- (NSPoint)mousePosition {
NSPoint point = NSEvent.mouseLocation;
Expand Down
25 changes: 20 additions & 5 deletions main.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ int main(int argc, char *argv[]) {
return 0;
}

if (argc > 1 && !strcmp("--ascii_mode", argv[1])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a one-way setting ascii_mode to true, I think we need both ways, by supplying a value:

--ascii_mode=true|fales or numeric value --ascii_mode=0|1, to be coherent with the swtich option's reset values.

[[NSDistributedNotificationCenter defaultCenter]
postNotificationName:@"SquirrelChangeToAsciiModeNotification"
object: nil];
return 0;
}

if (argc > 1 && !strcmp("--ascii_mode_prev", argv[1])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the caller keep track of which state should be put back in?

[[NSDistributedNotificationCenter defaultCenter]
postNotificationName:@"SquirrelChangeToAsciiModePrevNotification"
object: nil];
return 0;
}


@autoreleasepool {
// find the bundle identifier and then initialize the input method server
NSBundle *main = [NSBundle mainBundle];
Expand All @@ -70,19 +85,19 @@ 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]
changeCurrentDirectoryPath:main.sharedSupportPath];

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];
Expand Down