forked from rime/squirrel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSquirrelApplicationDelegate.m
263 lines (234 loc) · 8.32 KB
/
SquirrelApplicationDelegate.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#import "SquirrelApplicationDelegate.h"
#import <rime_api.h>
#import "SquirrelConfig.h"
#import "SquirrelPanel.h"
static NSString *const kRimeWikiURL = @"https://github.com/rime/home/wiki";
@implementation SquirrelApplicationDelegate
-(IBAction)deploy:(id)sender
{
NSLog(@"Start maintenance...");
[self shutdownRime];
[self startRimeWithFullCheck:YES];
[self loadSettings];
}
-(IBAction)syncUserData:(id)sender
{
NSLog(@"Sync user data");
rime_get_api()->sync_user_data();
}
-(IBAction)configure:(id)sender
{
[[NSWorkspace sharedWorkspace] openFile:(@"~/Library/Rime").stringByStandardizingPath];
}
-(IBAction)openWiki:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:kRimeWikiURL]];
}
void show_message(const char* msg_text, const char* msg_id) {
@autoreleasepool {
id notification = [[NSClassFromString(@"NSUserNotification") alloc] init];
[notification performSelector:@selector(setTitle:)
withObject:NSLocalizedString(@"Squirrel", nil)];
[notification performSelector:@selector(setSubtitle:)
withObject:NSLocalizedString(@(msg_text), nil)];
id notificationCenter = [(id)NSClassFromString(@"NSUserNotificationCenter")
performSelector:@selector(defaultUserNotificationCenter)];
[notificationCenter performSelector:@selector(removeAllDeliveredNotifications)];
[notificationCenter performSelector:@selector(deliverNotification:)
withObject:notification];
}
}
static void show_status_message(const char* msg_text, const char* msg_id) {
SquirrelPanel* panel = NSApp.squirrelAppDelegate.panel;
if (panel) {
[panel updateStatus:NSLocalizedString(@(msg_text), nil)];
}
}
void notification_handler(void* context_object, RimeSessionId session_id,
const char* message_type, const char* message_value) {
if (!strcmp(message_type, "deploy")) {
if (!strcmp(message_value, "start")) {
show_message("deploy_start", message_type);
}
else if (!strcmp(message_value, "success")) {
show_message("deploy_success", message_type);
}
else if (!strcmp(message_value, "failure")) {
show_message("deploy_failure", message_type);
}
return;
}
// off?
id app_delegate = (__bridge id)context_object;
if (app_delegate && ![app_delegate enableNotifications]) {
return;
}
// schema change
if (!strcmp(message_type, "schema")) {
const char* schema_name = strchr(message_value, '/');
if (schema_name) {
++schema_name;
show_status_message(schema_name, message_type);
}
return;
}
// option change
if (!strcmp(message_type, "option")) {
if (!strcmp(message_value, "ascii_mode") ||
!strcmp(message_value, "!ascii_mode")) {
static bool was_ascii_mode = false;
bool is_ascii_mode = (message_value[0] != '!');
if (is_ascii_mode != was_ascii_mode) {
was_ascii_mode = is_ascii_mode;
show_status_message(message_value, message_type);
}
}
else if (!strcmp(message_value, "full_shape") ||
!strcmp(message_value, "!full_shape") ||
!strcmp(message_value, "ascii_punct") ||
!strcmp(message_value, "!ascii_punct") ||
!strcmp(message_value, "simplification") ||
!strcmp(message_value, "!simplification") ||
!strcmp(message_value, "extended_charset") ||
!strcmp(message_value, "!extended_charset")) {
show_status_message(message_value, message_type);
}
}
}
-(void)setupRime
{
NSString* userDataDir = (@"~/Library/Rime").stringByStandardizingPath;
NSFileManager* fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:userDataDir]) {
if (![fileManager createDirectoryAtPath:userDataDir
withIntermediateDirectories:YES
attributes:nil
error:NULL]) {
NSLog(@"Error creating user data directory: %@", userDataDir);
}
}
rime_get_api()->set_notification_handler(notification_handler, (__bridge void *)(self));
RIME_STRUCT(RimeTraits, squirrel_traits);
squirrel_traits.shared_data_dir = [NSBundle mainBundle].sharedSupportPath.UTF8String;
squirrel_traits.user_data_dir = userDataDir.UTF8String;
squirrel_traits.distribution_code_name = "Squirrel";
squirrel_traits.distribution_name = "鼠鬚管";
squirrel_traits.distribution_version =
[[NSBundle mainBundle].infoDictionary[@"CFBundleVersion"] UTF8String];
squirrel_traits.app_name = "rime.squirrel";
rime_get_api()->setup(&squirrel_traits);
}
-(void)startRimeWithFullCheck:(BOOL)fullCheck
{
NSLog(@"Initializing la rime...");
rime_get_api()->initialize(NULL);
// check for configuration updates
if (rime_get_api()->start_maintenance((Bool)fullCheck)) {
// update squirrel config
rime_get_api()->deploy_config_file("squirrel.yaml", "config_version");
}
}
- (void)shutdownRime {
[_config close];
rime_get_api()->finalize();
}
-(void)loadSettings {
_config = [[SquirrelConfig alloc] init];
if (![_config openBaseConfig]) {
return;
}
_enableNotifications =
![[_config getString:@"show_notifications_when"] isEqualToString:@"never"];
[self.panel loadConfig:_config forDarkMode:NO];
if (@available(macOS 10.14, *)) {
[self.panel loadConfig:_config forDarkMode:YES];
}
}
-(void)loadSchemaSpecificSettings:(NSString *)schemaId {
if (schemaId.length == 0 || [schemaId characterAtIndex:0] == '.') {
return;
}
SquirrelConfig *schema = [[SquirrelConfig alloc] init];
if ([schema openWithSchemaId:schemaId baseConfig:self.config] &&
[schema hasSection:@"style"]) {
[self.panel loadConfig:schema forDarkMode:NO];
} else {
[self.panel loadConfig:self.config forDarkMode:NO];
}
if (@available(macOS 10.14, *)) {
if ([schema openWithSchemaId:schemaId baseConfig:self.config] &&
[schema hasSection:@"style"]) {
[self.panel loadConfig:schema forDarkMode:YES];
} else {
[self.panel loadConfig:self.config forDarkMode:YES];
}
}
[schema close];
}
// prevent freezing the system
-(BOOL)problematicLaunchDetected
{
BOOL detected = NO;
NSString* logfile =
[NSTemporaryDirectory() stringByAppendingPathComponent:@"squirrel_launch.dat"];
//NSLog(@"[DEBUG] archive: %@", logfile);
NSData* archive = [NSData dataWithContentsOfFile:logfile
options:NSDataReadingUncached
error:nil];
if (archive) {
NSDate* previousLaunch = [NSKeyedUnarchiver unarchiveObjectWithData:archive];
if (previousLaunch && previousLaunch.timeIntervalSinceNow >= -2) {
detected = YES;
}
}
NSDate* now = [NSDate date];
NSData* record = [NSKeyedArchiver archivedDataWithRootObject:now];
[record writeToFile:logfile atomically:NO];
return detected;
}
-(void)workspaceWillPowerOff:(NSNotification *)aNotification
{
NSLog(@"Finalizing before logging out.");
[self shutdownRime];
}
-(void)rimeNeedsReload:(NSNotification *)aNotification
{
NSLog(@"Reloading rime on demand.");
[self deploy:nil];
}
-(NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
NSLog(@"Squirrel is quitting.");
rime_get_api()->cleanup_all_sessions();
return NSTerminateNow;
}
//add an awakeFromNib item so that we can set the action method. Note that
//any menuItems without an action will be disabled when displayed in the Text
//Input Menu.
-(void)awakeFromNib
{
NSNotificationCenter* center = [NSWorkspace sharedWorkspace].notificationCenter;
[center addObserver:self
selector:@selector(workspaceWillPowerOff:)
name:NSWorkspaceWillPowerOffNotification
object:nil];
NSDistributedNotificationCenter* notifCenter = [NSDistributedNotificationCenter defaultCenter];
[notifCenter addObserver:self
selector:@selector(rimeNeedsReload:)
name:@"SquirrelReloadNotification"
object:nil];
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
if (_panel) {
[_panel hide];
}
}
@end //SquirrelApplicationDelegate
@implementation NSApplication (SquirrelApp)
-(SquirrelApplicationDelegate *)squirrelAppDelegate {
return (SquirrelApplicationDelegate *)self.delegate;
}
@end