forked from rime/squirrel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SquirrelPanel.m
736 lines (633 loc) · 25.8 KB
/
SquirrelPanel.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
//
// SquirrelPanel.m
// Squirrel
//
// Created by 弓辰 on 2012/2/13.
// Copyright 2012 __MyCompanyName__. All rights reserved.
//
#import "SquirrelPanel.h"
@implementation SquirrelUIStyle
@synthesize horizontal = _horizontal;
@synthesize inlinePreedit = _inlinePreedit;
@synthesize labelFontName = _labelFontName;
@synthesize labelFontSize = _labelFontSize;
@synthesize fontName = _fontName;
@synthesize fontSize = _fontSize;
@synthesize alpha = _alpha;
@synthesize cornerRadius = _cornerRadius;
@synthesize borderHeight = _borderHeight;
@synthesize borderWidth = _borderWidth;
@synthesize lineSpacing = _lineSpacing;
@synthesize spacing = _spacing;
@synthesize backgroundColor = _backgroundColor;
@synthesize textColor = _textColor;
@synthesize candidateLabelColor = _candidateLabelColor;
@synthesize candidateTextColor = _candidateTextColor;
@synthesize highlightedTextColor = _highlightedTextColor;
@synthesize highlightedBackColor = _highlightedBackColor;
@synthesize highlightedCandidateLabelColor = _highlightedCandidateLabelColor;
@synthesize highlightedCandidateTextColor = _highlightedCandidateTextColor;
@synthesize highlightedCandidateBackColor = _highlightedCandidateBackColor;
@synthesize commentTextColor = _commentTextColor;
@synthesize highlightedCommentTextColor = _highlightedCommentTextColor;
@synthesize candidateFormat = _candidateFormat;
- (void)dealloc
{
[_labelFontName release];
[_fontName release];
[_backgroundColor release];
[_textColor release];
[_candidateLabelColor release];
[_candidateTextColor release];
[_highlightedTextColor release];
[_highlightedBackColor release];
[_highlightedCandidateLabelColor release];
[_highlightedCandidateTextColor release];
[_highlightedCandidateBackColor release];
[_commentTextColor release];
[_highlightedCommentTextColor release];
[_candidateFormat release];
[super dealloc];
}
- (id)copyWithZone:(NSZone *)zone
{
SquirrelUIStyle* style = [[SquirrelUIStyle allocWithZone:zone] init];
style.horizontal = _horizontal;
style.inlinePreedit = _inlinePreedit;
style.labelFontName = _labelFontName;
style.labelFontSize = _labelFontSize;
style.fontName = _fontName;
style.fontSize = _fontSize;
style.alpha = _alpha;
style.cornerRadius = _cornerRadius;
style.borderHeight = _borderHeight;
style.borderWidth = _borderWidth;
style.lineSpacing = _lineSpacing;
style.spacing = _spacing;
style.backgroundColor = _backgroundColor;
style.textColor = _textColor;
style.candidateLabelColor = _candidateLabelColor;
style.candidateTextColor = _candidateTextColor;
style.highlightedTextColor = _highlightedTextColor;
style.highlightedBackColor = _highlightedBackColor;
style.highlightedCandidateLabelColor = _highlightedCandidateLabelColor;
style.highlightedCandidateTextColor = _highlightedCandidateTextColor;
style.highlightedCandidateBackColor = _highlightedCandidateBackColor;
style.commentTextColor = _commentTextColor;
style.highlightedCommentTextColor = _highlightedCommentTextColor;
style.candidateFormat = _candidateFormat;
return style;
}
@end
static const int kOffsetHeight = 5;
static const int kFontSize = 24;
static const double kAlpha = 1.0;
@interface SquirrelView : NSView
{
NSAttributedString* _content;
}
@property (nonatomic, retain) NSColor *backgroundColor;
@property (nonatomic, assign) double cornerRadius;
@property (nonatomic, assign) double borderHeight;
@property (nonatomic, assign) double borderWidth;
-(NSSize)contentSize;
-(void)setContent:(NSAttributedString*)content;
@end
@implementation SquirrelView
@synthesize backgroundColor = _backgroundColor;
@synthesize cornerRadius = _cornerRadius;
@synthesize borderHeight = _borderHeight;
@synthesize borderWidth = _borderWidth;
-(double)borderHeight
{
return MAX(_borderHeight, _cornerRadius);
}
-(double)borderWidth
{
return MAX(_borderWidth, _cornerRadius);
}
-(NSSize)contentSize
{
if (!_content) return NSMakeSize(0, 0);
return [_content size];
}
-(void)setContent:(NSAttributedString*)content
{
[content retain];
[_content release];
_content = content;
[self setNeedsDisplay:YES];
}
-(void)drawRect:(NSRect)rect
{
if (!_content) {
return;
}
if ([self backgroundColor]) {
[[self backgroundColor] setFill];
}
else {
[[NSColor windowBackgroundColor] setFill];
}
[[NSBezierPath bezierPathWithRoundedRect:rect xRadius:_cornerRadius yRadius:_cornerRadius] fill];
NSPoint point = rect.origin;
point.x += [self borderWidth];
point.y += [self borderHeight];
[_content drawAtPoint:point];
}
@end
@implementation SquirrelPanel
-(id)init
{
// NSLog(@"SqurrelPanel init");
_position = NSMakeRect(0, 0, 0, 0);
_window = [[NSWindow alloc] initWithContentRect:_position
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[_window setAlphaValue:kAlpha];
[_window setLevel:NSScreenSaverWindowLevel + 1];
[_window setHasShadow:YES];
[_window setOpaque:NO];
[_window setBackgroundColor:[NSColor clearColor]];
_view = [[SquirrelView alloc] initWithFrame:[[_window contentView] frame]];
[_window setContentView:_view];
_attrs = [[NSMutableDictionary alloc] init];
[_attrs setObject:[NSColor controlTextColor] forKey:NSForegroundColorAttributeName];
[_attrs setObject:[NSFont userFontOfSize:kFontSize] forKey:NSFontAttributeName];
_highlightedAttrs = [[NSMutableDictionary alloc] init];
[_highlightedAttrs setObject:[NSColor selectedControlTextColor] forKey:NSForegroundColorAttributeName];
[_highlightedAttrs setObject:[NSColor selectedTextBackgroundColor] forKey:NSBackgroundColorAttributeName];
[_highlightedAttrs setObject:[NSFont userFontOfSize:kFontSize] forKey:NSFontAttributeName];
_labelAttrs = [_attrs mutableCopy];
_labelHighlightedAttrs = [_highlightedAttrs mutableCopy];
_commentAttrs = [[NSMutableDictionary alloc] init];
[_commentAttrs setObject:[NSColor disabledControlTextColor] forKey:NSForegroundColorAttributeName];
[_commentAttrs setObject:[NSFont userFontOfSize:kFontSize] forKey:NSFontAttributeName];
_commentHighlightedAttrs = [_commentAttrs mutableCopy];
[_commentHighlightedAttrs setObject:[NSColor selectedTextBackgroundColor] forKey:NSBackgroundColorAttributeName];
_preeditAttrs = [[NSMutableDictionary alloc] init];
[_preeditAttrs setObject:[NSColor disabledControlTextColor] forKey:NSForegroundColorAttributeName];
[_preeditAttrs setObject:[NSFont userFontOfSize:kFontSize] forKey:NSFontAttributeName];
_preeditHighlightedAttrs = [[NSMutableDictionary alloc] init];
[_preeditHighlightedAttrs setObject:[NSColor controlTextColor] forKey:NSForegroundColorAttributeName];
[_preeditHighlightedAttrs setObject:[NSFont userFontOfSize:kFontSize] forKey:NSFontAttributeName];
_horizontal = NO;
_inlinePreedit = NO;
_candidateFormat = @"%c. %@ ";
_paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] retain];
_preeditParagraphStyle = [[NSParagraphStyle defaultParagraphStyle] retain];
_numCandidates = 0;
_message = nil;
_statusTimer = nil;
return self;
}
-(BOOL)horizontal
{
return _horizontal;
}
-(BOOL)inlinePreedit
{
return _inlinePreedit;
}
-(void)show
{
// NSLog(@"show: %d %@", _numCandidates, _message);
NSRect window_rect = [_window frame];
// resize frame
NSSize content_size = [(SquirrelView*)_view contentSize];
window_rect.size.height = content_size.height + [(SquirrelView*)_view borderHeight] * 2;
window_rect.size.width = content_size.width + [(SquirrelView*)_view borderWidth] * 2;
// reposition window
window_rect.origin.x = NSMinX(_position);
window_rect.origin.y = NSMinY(_position) - kOffsetHeight - NSHeight(window_rect);
// fit in current screen
NSRect screen_rect = [[NSScreen mainScreen] frame];
NSArray* screens = [NSScreen screens];
NSUInteger i;
for (i = 0; i < [screens count]; ++i) {
NSRect rect = [[screens objectAtIndex:i] frame];
if (NSPointInRect(_position.origin, rect)) {
screen_rect = rect;
break;
}
}
if (NSMaxX(window_rect) > NSMaxX(screen_rect)) {
window_rect.origin.x = NSMaxX(screen_rect) - NSWidth(window_rect);
}
if (NSMinX(window_rect) < NSMinX(screen_rect)) {
window_rect.origin.x = NSMinX(screen_rect);
}
if (NSMinY(window_rect) < NSMinY(screen_rect)) {
window_rect.origin.y = NSMaxY(_position) + kOffsetHeight;
}
if (NSMaxY(window_rect) > NSMaxY(screen_rect)) {
window_rect.origin.y = NSMaxY(screen_rect) - NSHeight(window_rect);
}
if (NSMinY(window_rect) < NSMinY(screen_rect)) {
window_rect.origin.y = NSMinY(screen_rect);
}
// voila !
[_window setFrame:window_rect display:YES];
[_window orderFront:nil];
}
-(void)hide
{
// NSLog(@"hide:");
[_window orderOut:nil];
}
-(void)updatePosition:(NSRect)caretPos
{
_position = caretPos;
}
-(void)updatePreedit:(NSString*)preedit
withSelRange:(NSRange)selRange
atCaretPos:(NSUInteger)caretPos
andCandidates:(NSArray*)candidates
andComments:(NSArray*)comments
withLabels:(NSString*)labels
highlighted:(NSUInteger)index
{
_numCandidates = [candidates count];
// NSLog(@"updatePreedit: ... andCandidates: %d %@", _numCandidates, _message);
if (_numCandidates || (preedit && [preedit length])) {
[self updateMessage:nil];
if (_statusTimer) {
[_statusTimer invalidate];
_statusTimer = nil;
}
}
else {
if (_message) {
[self showStatus:_message];
[self updateMessage:nil];
}
else if (!_statusTimer) {
[self hide];
}
return;
}
NSRange labelRange, labelRange2, pureCandidateRange;
NSString *labelFormat, *labelFormat2, *pureCandidateFormat;
{
// in our candiate format, everything other than '%@' is
// considered as a part of the label
labelRange = [_candidateFormat rangeOfString:@"%c"];
if (labelRange.location == NSNotFound) {
labelRange2 = labelRange;
labelFormat2 = labelFormat = nil;
pureCandidateRange = NSMakeRange(0, [_candidateFormat length]);
pureCandidateFormat = _candidateFormat;
}
else {
pureCandidateRange = [_candidateFormat rangeOfString:@"%@"];
if (pureCandidateRange.location == NSNotFound) {
// this should never happen, but just ensure that Squirrel
// would not crash when such edge case occurs...
labelFormat = _candidateFormat;
labelRange2 = pureCandidateRange;
labelFormat2 = nil;
pureCandidateFormat = @"";
}
else {
if (NSMaxRange(pureCandidateRange) >= [_candidateFormat length]) {
// '%@' is at the end, so label2 does not exist
labelRange2 = NSMakeRange(NSNotFound, 0);
labelFormat2 = nil;
// fix label1, everything other than '%@' is label1
labelRange.location = 0;
labelRange.length = pureCandidateRange.location;
}
else {
labelRange = NSMakeRange(0, pureCandidateRange.location);
labelRange2 = NSMakeRange(NSMaxRange(pureCandidateRange), [_candidateFormat length] - NSMaxRange(pureCandidateRange));
labelFormat2 = [_candidateFormat substringWithRange:labelRange2];
}
pureCandidateFormat = @"%@";
labelFormat = [_candidateFormat substringWithRange:labelRange];
}
}
}
NSMutableAttributedString* text = [[NSMutableAttributedString alloc] init];
size_t candidate_start_pos = 0;
// preedit
if (preedit) {
NSMutableAttributedString *line = [[NSMutableAttributedString alloc] init];
if (selRange.location > 0) {
[line appendAttributedString:[[[NSAttributedString alloc] initWithString:[preedit substringToIndex:selRange.location]
attributes:_preeditAttrs] autorelease]];
}
if (selRange.length > 0) {
[line appendAttributedString:[[[NSAttributedString alloc] initWithString:[preedit substringWithRange:selRange]
attributes:_preeditHighlightedAttrs] autorelease]];
}
if (selRange.location + selRange.length < [preedit length]) {
[line appendAttributedString:[[[NSAttributedString alloc] initWithString:[preedit substringFromIndex:selRange.location + selRange.length]
attributes:_preeditAttrs] autorelease]];
}
[text appendAttributedString:line];
[line release];
if (_numCandidates) {
[text appendAttributedString:[[[NSAttributedString alloc] initWithString:@"\n"
attributes: _preeditAttrs] autorelease]];
}
[text addAttribute:NSParagraphStyleAttributeName
value:_preeditParagraphStyle
range:NSMakeRange(0, [text length])];
candidate_start_pos = [text length];
}
// candidates
NSUInteger i;
for (i = 0; i < [candidates count]; ++i) {
NSMutableAttributedString *line = [[NSMutableAttributedString alloc] init];
// default: 1. 2. 3... custom: A. B. C...
char label_character = (i < [labels length]) ? [labels characterAtIndex:i] : ((i + 1) % 10 + '0');
NSDictionary *attrs = _attrs, *labelAttrs = _labelAttrs, *commentAttrs = _commentAttrs;
if (i == index) {
attrs = _highlightedAttrs;
labelAttrs = _labelHighlightedAttrs;
commentAttrs = _commentHighlightedAttrs;
}
if (labelRange.location != NSNotFound) {
[line appendAttributedString:[[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:labelFormat, label_character]
attributes:labelAttrs] autorelease]];
}
[line appendAttributedString:[[[NSAttributedString alloc] initWithString:[candidates objectAtIndex:i]
attributes:attrs] autorelease]];
if (labelRange2.location != NSNotFound) {
[line appendAttributedString:[[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:labelFormat2, label_character]
attributes:labelAttrs] autorelease]];
}
if (i < [comments count] && [[comments objectAtIndex:i] length] != 0) {
[line appendAttributedString:[[[NSAttributedString alloc] initWithString: [comments objectAtIndex:i]
attributes:commentAttrs] autorelease]];
}
if (i > 0) {
[text appendAttributedString:[[[NSAttributedString alloc] initWithString: (_horizontal ? @" " : @"\n")
attributes:_attrs] autorelease]];
}
[text appendAttributedString:line];
[line release];
}
[text addAttribute:NSParagraphStyleAttributeName
value:(id)_paragraphStyle
range:NSMakeRange(candidate_start_pos, [text length] - candidate_start_pos)];
[(SquirrelView*)_view setContent:text];
[text release];
[self show];
}
-(void)updateMessage:(NSString *)msg
{
// NSLog(@"updateMessage: %@ -> %@", _message, msg);
[msg retain];
[_message release];
_message = msg;
}
-(void)showStatus:(NSString *)msg
{
// NSLog(@"showStatus: %@", msg);
NSMutableAttributedString* text = [[NSMutableAttributedString alloc] init];
[text appendAttributedString:[[[NSAttributedString alloc] initWithString: msg
attributes:_commentAttrs] autorelease]];
[(SquirrelView*)_view setContent:text];
[text release];
[self show];
if (_statusTimer) {
[_statusTimer invalidate];
}
_statusTimer = [NSTimer scheduledTimerWithTimeInterval:1.2
target:self
selector:@selector(hideStatus:)
userInfo:nil
repeats:NO];
}
-(void)hideStatus:(NSTimer *)timer
{
// NSLog(@"hideStatus: %@", _message);
[_message release];
_message = nil;
[_statusTimer invalidate];
_statusTimer = nil;
[self hide];
}
-(NSColor *)colorFromString:(NSString *)string
{
if (string == nil) {
return nil;
}
int r = 0, g = 0, b =0, a = 0xff;
if ([string length] == 10) {
// 0xffccbbaa
sscanf([string UTF8String], "0x%02x%02x%02x%02x", &a, &b, &g, &r);
}
else if ([string length] == 8) {
// 0xccbbaa
sscanf([string UTF8String], "0x%02x%02x%02x", &b, &g, &r);
}
return [NSColor colorWithDeviceRed:(CGFloat)r / 255. green:(CGFloat)g / 255. blue:(CGFloat)b / 255. alpha:(CGFloat)a / 255.];
}
static inline NSColor *blendColors(NSColor *foregroundColor, NSColor *backgroundColor)
{
if (!backgroundColor) {
//return foregroundColor;
backgroundColor = [NSColor lightGrayColor];
}
struct {
CGFloat r, g, b, a;
} f, b;
[[foregroundColor colorUsingColorSpaceName:NSDeviceRGBColorSpace] getRed:&f.r
green:&f.g
blue:&f.b
alpha:&f.a];
//NSLog(@"fg: %f %f %f %f", f.r, f.g, f.b, f.a);
[[backgroundColor colorUsingColorSpaceName:NSDeviceRGBColorSpace] getRed:&b.r
green:&b.g
blue:&b.b
alpha:&b.a];
//NSLog(@"bg: %f %f %f %f", b.r, b.g, b.b, b.a);
#define blend_value(f, b) (((f) * 2.0 + (b)) / 3.0)
return [NSColor colorWithDeviceRed:blend_value(f.r, b.r)
green:blend_value(f.g, b.g)
blue:blend_value(f.b, b.b)
alpha:f.a];
#undef blend_value
}
static NSFontDescriptor* getFontDescriptor(NSString *fullname)
{
if (fullname == nil) {
return nil;
}
NSArray *fontNames = [fullname componentsSeparatedByString:@","];
NSMutableArray *validFontDescriptors = [NSMutableArray arrayWithCapacity:[fontNames count]];
for (NSString *fontName in fontNames) {
fontName = [fontName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if ([NSFont fontWithName:fontName size:0.0] != nil) {
// If the font name is not valid, NSFontDescriptor will still create something for us.
// However, when we draw the actual text, Squirrel will crash if there is any font descriptor
// with invalid font name.
[validFontDescriptors addObject:[NSFontDescriptor fontDescriptorWithName:fontName size:0.0]];
}
}
if ([validFontDescriptors count] == 0) {
return nil;
}
else if ([validFontDescriptors count] == 1) {
return [validFontDescriptors objectAtIndex:0];
}
NSFontDescriptor *initialFontDescriptor = [validFontDescriptors objectAtIndex:0];
NSArray *fallbackDescriptors = [validFontDescriptors subarrayWithRange:NSMakeRange(1, [validFontDescriptors count] - 1)];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:fallbackDescriptors forKey:NSFontCascadeListAttribute];
return [initialFontDescriptor fontDescriptorByAddingAttributes:attributes];
}
-(void)updateUIStyle:(SquirrelUIStyle *)style
{
_horizontal = style.horizontal;
_inlinePreedit = style.inlinePreedit;
if (style.fontSize == 0) { // default size
style.fontSize = kFontSize;
}
if (style.labelFontSize == 0) {
style.labelFontSize = style.fontSize;
}
NSFontDescriptor* fontDescriptor = nil;
NSFont* font = nil;
if (style.fontName != nil) {
fontDescriptor = getFontDescriptor(style.fontName);
if (fontDescriptor != nil) {
font = [NSFont fontWithDescriptor:fontDescriptor size:style.fontSize];
}
}
if (font == nil) {
// use default font
font = [NSFont userFontOfSize:style.fontSize];
}
NSFontDescriptor* labelFontDescriptor = nil;
NSFont* labelFont = nil;
if (style.labelFontName != nil) {
labelFontDescriptor = getFontDescriptor(style.labelFontName);
if (labelFontDescriptor == nil) {
labelFontDescriptor = fontDescriptor;
}
if (labelFontDescriptor != nil) {
labelFont = [NSFont fontWithDescriptor:labelFontDescriptor size:style.labelFontSize];
}
}
if (labelFont == nil) {
if (fontDescriptor != nil) {
labelFont = [NSFont fontWithDescriptor:fontDescriptor size:style.labelFontSize];
}
else {
labelFont = [NSFont fontWithName:[font fontName] size:style.labelFontSize];
}
}
[_attrs setObject:font forKey:NSFontAttributeName];
[_highlightedAttrs setObject:font forKey:NSFontAttributeName];
[_labelAttrs setObject:labelFont forKey:NSFontAttributeName];
[_labelHighlightedAttrs setObject:labelFont forKey:NSFontAttributeName];
[_commentAttrs setObject:font forKey:NSFontAttributeName];
[_commentHighlightedAttrs setObject:font forKey:NSFontAttributeName];
[_preeditAttrs setObject:font forKey:NSFontAttributeName];
[_preeditHighlightedAttrs setObject:font forKey:NSFontAttributeName];
if (style.backgroundColor != nil) {
NSColor *color = [self colorFromString:style.backgroundColor];
[(SquirrelView *) _view setBackgroundColor:(color)];
}
else {
// default color
[(SquirrelView *) _view setBackgroundColor:nil];
}
if (style.candidateTextColor != nil) {
NSColor *color = [self colorFromString:style.candidateTextColor];
[_attrs setObject:color forKey:NSForegroundColorAttributeName];
}
else {
[_attrs setObject:[NSColor controlTextColor] forKey:NSForegroundColorAttributeName];
}
if (style.candidateLabelColor != nil) {
NSColor *color = [self colorFromString:style.candidateLabelColor];
[_labelAttrs setObject:color forKey:NSForegroundColorAttributeName];
}
else {
NSColor *color = blendColors([_attrs objectForKey:NSForegroundColorAttributeName], [(SquirrelView *)_view backgroundColor]);
[_labelAttrs setObject:color forKey:NSForegroundColorAttributeName];
}
if (style.highlightedCandidateTextColor != nil) {
NSColor *color = [self colorFromString:style.highlightedCandidateTextColor];
[_highlightedAttrs setObject:color forKey:NSForegroundColorAttributeName];
}
else {
[_highlightedAttrs setObject:[NSColor selectedControlTextColor] forKey:NSForegroundColorAttributeName];
}
if (style.highlightedCandidateBackColor != nil) {
NSColor *color = [self colorFromString:style.highlightedCandidateBackColor];
[_highlightedAttrs setObject:color forKey:NSBackgroundColorAttributeName];
}
else {
[_highlightedAttrs setObject:[NSColor selectedTextBackgroundColor] forKey:NSBackgroundColorAttributeName];
}
if (style.highlightedCandidateLabelColor != nil) {
NSColor *color = [self colorFromString:style.highlightedCandidateLabelColor];
[_labelHighlightedAttrs setObject:color forKey:NSForegroundColorAttributeName];
}
else {
NSColor *color = blendColors([_highlightedAttrs objectForKey:NSForegroundColorAttributeName],
[_highlightedAttrs objectForKey:NSBackgroundColorAttributeName]);
[_labelHighlightedAttrs setObject:color forKey:NSForegroundColorAttributeName];
}
[_labelHighlightedAttrs setObject:[_highlightedAttrs objectForKey:NSBackgroundColorAttributeName] forKey:NSBackgroundColorAttributeName];
if (style.commentTextColor != nil) {
NSColor *color = [self colorFromString:style.commentTextColor];
[_commentAttrs setObject:color forKey:NSForegroundColorAttributeName];
}
else {
[_commentAttrs setObject:[NSColor disabledControlTextColor] forKey:NSForegroundColorAttributeName];
}
if (style.highlightedCommentTextColor != nil) {
NSColor *color = [self colorFromString:style.highlightedCommentTextColor];
[_commentHighlightedAttrs setObject:color forKey:NSForegroundColorAttributeName];
}
else {
[_commentHighlightedAttrs setObject:[_commentAttrs objectForKey:NSForegroundColorAttributeName] forKey:NSForegroundColorAttributeName];
}
[_commentHighlightedAttrs setObject:[_highlightedAttrs objectForKey:NSBackgroundColorAttributeName] forKey:NSBackgroundColorAttributeName];
if (style.textColor != nil) {
NSColor *color = [self colorFromString:style.textColor];
[_preeditAttrs setObject:color forKey:NSForegroundColorAttributeName];
}
else {
[_preeditAttrs setObject:[NSColor disabledControlTextColor] forKey:NSForegroundColorAttributeName];
}
if (style.highlightedTextColor != nil) {
NSColor *color = [self colorFromString:style.highlightedTextColor];
[_preeditHighlightedAttrs setObject:color forKey:NSForegroundColorAttributeName];
}
else {
[_preeditHighlightedAttrs setObject:[NSColor controlTextColor] forKey:NSForegroundColorAttributeName];
}
if (style.highlightedBackColor != nil) {
NSColor *color = [self colorFromString:style.highlightedBackColor];
[_preeditHighlightedAttrs setObject:color forKey:NSBackgroundColorAttributeName];
}
else {
[_preeditHighlightedAttrs removeObjectForKey:NSBackgroundColorAttributeName];
}
[(SquirrelView *) _view setCornerRadius:style.cornerRadius];
[(SquirrelView *) _view setBorderHeight:style.borderHeight];
[(SquirrelView *) _view setBorderWidth:style.borderWidth];
if (style.alpha == 0.0) {
style.alpha = 1.0;
}
[_window setAlphaValue:style.alpha];
[style.candidateFormat retain];
[_candidateFormat release];
_candidateFormat = style.candidateFormat ? style.candidateFormat : @"%c. %@ ";
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setParagraphSpacing:style.lineSpacing];
[_paragraphStyle release];
_paragraphStyle = paragraphStyle;
paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setParagraphSpacing:style.spacing];
[_preeditParagraphStyle release];
_preeditParagraphStyle = paragraphStyle;
}
@end