forked from rime/squirrel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquirrelApplicationDelegate.m
More file actions
218 lines (194 loc) · 7.88 KB
/
Copy pathSquirrelApplicationDelegate.m
File metadata and controls
218 lines (194 loc) · 7.88 KB
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
#import "SquirrelApplicationDelegate.h"
#import "SquirrelPanel.h"
#import <rime_api.h>
@implementation SquirrelApplicationDelegate
-(NSMenu*)menu
{
return _menu;
}
-(SquirrelPanel*)panel
{
return _panel;
}
-(id)updater
{
return _updater;
}
-(BOOL)useUSKeyboardLayout
{
return _useUSKeyboardLayout;
}
-(IBAction)deploy:(id)sender
{
NSLog(@"Start maintenace...");
// restart
RimeFinalize();
[self startRimeWithFullCheck:YES];
[self loadSquirrelConfig];
}
-(IBAction)configure:(id)sender
{
[[NSWorkspace sharedWorkspace] openFile:[@"~/Library/Rime" stringByStandardizingPath]];
}
-(IBAction)openWiki:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://code.google.com/p/rimeime/w/list"]];
}
-(void)startRimeWithFullCheck:(BOOL)fullCheck
{
RimeTraits squirrel_traits;
squirrel_traits.shared_data_dir = [[[NSBundle mainBundle] sharedSupportPath] UTF8String];
squirrel_traits.user_data_dir = [[@"~/Library/Rime" stringByStandardizingPath] UTF8String];
squirrel_traits.distribution_code_name = "Squirrel";
squirrel_traits.distribution_name = "鼠鬚管";
squirrel_traits.distribution_version = [[[[NSBundle mainBundle] infoDictionary]
objectForKey:@"CFBundleVersion"] UTF8String];
NSLog(@"Initializing la rime...");
RimeInitialize(&squirrel_traits);
if (fullCheck) {
// update squirrel config
RimeDeployConfigFile("squirrel.yaml", "config_version");
}
// check for configuration updates
if (RimeStartMaintenance((Bool)fullCheck)) {
// TODO: notification
NSArray* args = [NSArray arrayWithObjects:@"Preparing Rime for updates; patience.", nil];
[NSTask launchedTaskWithLaunchPath:@"/usr/bin/say" arguments:args];
}
}
-(void)loadSquirrelConfig
{
RimeConfig config;
if (!RimeConfigOpen("squirrel", &config)) {
return;
}
NSLog(@"Loading squirrel specific config...");
_useUSKeyboardLayout = NO;
Bool value;
if (RimeConfigGetBool(&config, "us_keyboard_layout", &value)) {
_useUSKeyboardLayout = (BOOL)value;
}
SquirrelUIStyle style = { NO, nil, 0, 1.0, 0.0, 0.0, 0.0, nil, nil, nil, nil, nil };
if (RimeConfigGetBool(&config, "style/horizontal", &value)) {
style.horizontal = (BOOL)value;
}
char font_face[100] = {0};
if (RimeConfigGetString(&config, "style/font_face", font_face, sizeof(font_face))) {
style.fontName = [[NSString alloc] initWithUTF8String:font_face];
}
RimeConfigGetInt(&config, "style/font_point", &style.fontSize);
RimeConfigGetDouble(&config, "style/alpha", &style.alpha);
if (style.alpha > 1.0) {
style.alpha = 1.0;
} else if (style.alpha < 0.1) {
style.alpha = 0.1;
}
RimeConfigGetDouble(&config, "style/corner_radius", &style.cornerRadius);
RimeConfigGetDouble(&config, "style/border_height", &style.borderHeight);
RimeConfigGetDouble(&config, "style/border_width", &style.borderWidth);
char color_scheme[100] = {0};
if (RimeConfigGetString(&config, "style/color_scheme", color_scheme, sizeof(color_scheme))) {
NSMutableString* key = [[NSMutableString alloc] initWithString:@"preset_color_schemes/"];
[key appendString:[NSString stringWithUTF8String:color_scheme]];
NSUInteger prefix_length = [key length];
// 0xaabbggrr or 0xbbggrr
char color[20] = {0};
[key appendString:@"/back_color"];
if (RimeConfigGetString(&config, [key UTF8String], color, sizeof(color))) {
style.backgroundColor = [[NSString alloc] initWithUTF8String:color];
}
NSString* fallback_text_color = nil;
NSString* fallback_hilited_text_color = nil;
NSString* fallback_hilited_back_color = nil;
[key replaceCharactersInRange:NSMakeRange(prefix_length, [key length] - prefix_length) withString:@"/text_color"];
if (RimeConfigGetString(&config, [key UTF8String], color, sizeof(color))) {
fallback_text_color = [[NSString alloc] initWithUTF8String:color];
}
[key replaceCharactersInRange:NSMakeRange(prefix_length, [key length] - prefix_length) withString:@"/hilited_text_color"];
if (RimeConfigGetString(&config, [key UTF8String], color, sizeof(color))) {
fallback_hilited_text_color = [[NSString alloc] initWithUTF8String:color];
}
else {
fallback_hilited_text_color = [fallback_text_color retain];
}
[key replaceCharactersInRange:NSMakeRange(prefix_length, [key length] - prefix_length) withString:@"/hilited_back_color"];
if (RimeConfigGetString(&config, [key UTF8String], color, sizeof(color))) {
fallback_hilited_back_color = [[NSString alloc] initWithUTF8String:color];
}
else {
fallback_hilited_back_color = [style.backgroundColor retain];
}
[key replaceCharactersInRange:NSMakeRange(prefix_length, [key length] - prefix_length) withString:@"/candidate_text_color"];
if (RimeConfigGetString(&config, [key UTF8String], color, sizeof(color))) {
style.candidateTextColor = [[NSString alloc] initWithUTF8String:color];
}
else {
// weasel panel has an additional line at the top to show preedit text, that `text_color` is for.
// if not otherwise specified, candidate text is rendered in this color.
// in other words, `candidate_text_color` inherits this.
style.candidateTextColor = [fallback_text_color retain];
}
[key replaceCharactersInRange:NSMakeRange(prefix_length, [key length] - prefix_length) withString:@"/hilited_candidate_text_color"];
if (RimeConfigGetString(&config, [key UTF8String], color, sizeof(color))) {
style.highlightedCandidateTextColor = [[NSString alloc] initWithUTF8String:color];
}
else {
style.highlightedCandidateTextColor = [fallback_hilited_text_color retain];
}
[key replaceCharactersInRange:NSMakeRange(prefix_length, [key length] - prefix_length) withString:@"/hilited_candidate_back_color"];
if (RimeConfigGetString(&config, [key UTF8String], color, sizeof(color))) {
style.highlightedCandidateBackColor = [[NSString alloc] initWithUTF8String:color];
}
else {
style.highlightedCandidateBackColor = [fallback_hilited_back_color retain];
}
// new in squirrel
[key replaceCharactersInRange:NSMakeRange(prefix_length, [key length] - prefix_length) withString:@"/comment_text_color"];
if (RimeConfigGetString(&config, [key UTF8String], color, sizeof(color))) {
style.commentTextColor = [[NSString alloc] initWithUTF8String:color];
}
[key release];
[fallback_text_color release];
[fallback_hilited_text_color release];
[fallback_hilited_back_color release];
}
RimeConfigClose(&config);
[_panel updateUIStyle:&style];
[style.fontName release];
[style.backgroundColor release];
[style.candidateTextColor release];
[style.highlightedCandidateTextColor release];
[style.highlightedCandidateBackColor release];
[style.commentTextColor release];
}
// prevent freezing the system when squirrel suffers crashes and thus is launched repeatedly by IMK.
-(BOOL)problematicLaunchDetected
{
BOOL detected = NO;
NSString* logfile = [NSTemporaryDirectory() stringByAppendingPathComponent:@"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;
}
//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
{
//NSLog(@"SquirrelApplicationDelegate awakeFromNib");
}
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
@end //SquirrelApplicationDelegate