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
Changes from 1 commit
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
Next Next commit
init
  • Loading branch information
wangyefeng committed Sep 5, 2023
commit d3f54eba88154a3b5295cf914bddc472c201d14c
26 changes: 26 additions & 0 deletions SquirrelApplicationDelegate.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import "SquirrelApplicationDelegate.h"
#include <Foundation/Foundation.h>

#import <rime_api.h>
#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];

}

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

@end
13 changes: 13 additions & 0 deletions SquirrelInputController.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import "SquirrelInputController.h"
#include <Foundation/Foundation.h>

#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) {
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
{
4 changes: 4 additions & 0 deletions SquirrelPanel.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import <Cocoa/Cocoa.h>
#include <Foundation/Foundation.h>
#import "SquirrelInputController.h"

@class SquirrelConfig;
@@ -35,4 +36,7 @@
-(void)loadConfig:(SquirrelConfig*)config
forDarkMode:(BOOL)isDark;

-(void)changeToAscii:(NSDictionary *)args;
-(void)changeToPrev;

@end
7 changes: 7 additions & 0 deletions SquirrelPanel.m
Original file line number Diff line number Diff line change
@@ -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;
28 changes: 23 additions & 5 deletions main.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#import "SquirrelApplicationDelegate.h"
#include <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import <InputMethodKit/InputMethodKit.h>
#import <rime_api.h>
@@ -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])) {
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:@"SquirrelChangePrevNotification"
object: nil];
return 0;
}


@autoreleasepool {
// find the bundle identifier and then initialize the input method server
NSBundle *main = [NSBundle mainBundle];
@@ -70,19 +87,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];
@@ -94,6 +111,7 @@ int main(int argc, char *argv[]) {
// finally run everything
[[NSApplication sharedApplication] run];


NSLog(@"Squirrel is quitting...");
rime_get_api()->finalize();
}