forked from rime/squirrel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SquirrelPanel.m
1629 lines (1484 loc) · 69.4 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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import "SquirrelPanel.h"
#import "SquirrelConfig.h"
static const CGFloat kOffsetHeight = 5;
static const CGFloat kDefaultFontSize = 24;
static const CGFloat kBlendedBackgroundColorFraction = 1.0 / 5;
static const NSTimeInterval kShowStatusDuration = 1.2;
static NSString *const kDefaultCandidateFormat = @"%c. %@";
@interface SquirrelTheme : NSObject
@property(nonatomic, assign) BOOL native;
@property(nonatomic, strong, readonly) NSColor *backgroundColor;
@property(nonatomic, strong, readonly) NSColor *highlightedStripColor;
@property(nonatomic, strong, readonly) NSColor *highlightedPreeditColor;
@property(nonatomic, strong, readonly) NSColor *preeditBackgroundColor;
@property(nonatomic, strong, readonly) NSColor *borderColor;
@property(nonatomic, readonly) CGFloat cornerRadius;
@property(nonatomic, readonly) CGFloat hilitedCornerRadius;
@property(nonatomic, readonly) NSSize edgeInset;
@property(nonatomic, readonly) CGFloat borderWidth;
@property(nonatomic, readonly) CGFloat linespace;
@property(nonatomic, readonly) CGFloat preeditLinespace;
@property(nonatomic, readonly) CGFloat alpha;
@property(nonatomic, readonly) BOOL linear;
@property(nonatomic, readonly) BOOL vertical;
@property(nonatomic, readonly) BOOL inlinePreedit;
@property(nonatomic, readonly) BOOL inlineCandidate;
@property(nonatomic, strong, readonly) NSDictionary *attrs;
@property(nonatomic, strong, readonly) NSDictionary *highlightedAttrs;
@property(nonatomic, strong, readonly) NSDictionary *labelAttrs;
@property(nonatomic, strong, readonly) NSDictionary *labelHighlightedAttrs;
@property(nonatomic, strong, readonly) NSDictionary *commentAttrs;
@property(nonatomic, strong, readonly) NSDictionary *commentHighlightedAttrs;
@property(nonatomic, strong, readonly) NSDictionary *preeditAttrs;
@property(nonatomic, strong, readonly) NSDictionary *preeditHighlightedAttrs;
@property(nonatomic, strong, readonly) NSParagraphStyle *paragraphStyle;
@property(nonatomic, strong, readonly) NSParagraphStyle *preeditParagraphStyle;
@property(nonatomic, strong, readonly) NSString *prefixLabelFormat, *suffixLabelFormat;
- (void)setCandidateFormat:(NSString *)candidateFormat;
- (void)setBackgroundColor:(NSColor *)backgroundColor
highlightedStripColor:(NSColor *)highlightedStripColor
highlightedPreeditColor:(NSColor *)highlightedPreeditColor
preeditBackgroundColor:(NSColor *)preeditBackgroundColor
borderColor:(NSColor *)borderColor;
- (void)setCornerRadius:(CGFloat)cornerRadius
hilitedCornerRadius:(CGFloat)hilitedCornerRadius
edgeInset:(NSSize)edgeInset
borderWidth:(CGFloat)borderWidth
linespace:(CGFloat)linespace
preeditLinespace:(CGFloat)preeditLinespace
alpha:(CGFloat)alpha
linear:(BOOL)linear
vertical:(BOOL)vertical
inlinePreedit:(BOOL)inlinePreedit
inlineCandidate:(BOOL)inlineCandidate;
- (void) setAttrs:(NSMutableDictionary *)attrs
labelAttrs:(NSMutableDictionary *)labelAttrs
highlightedAttrs:(NSMutableDictionary *)highlightedAttrs
labelHighlightedAttrs:(NSMutableDictionary *)labelHighlightedAttrs
commentAttrs:(NSMutableDictionary *)commentAttrs
commentHighlightedAttrs:(NSMutableDictionary *)commentHighlightedAttrs
preeditAttrs:(NSMutableDictionary *)preeditAttrs
preeditHighlightedAttrs:(NSMutableDictionary *)preeditHighlightedAttrs;
- (void) setParagraphStyle:(NSParagraphStyle *)paragraphStyle
preeditParagraphStyle:(NSParagraphStyle *)preeditParagraphStyle;
@end
@implementation SquirrelTheme
- (void)setCandidateFormat:(NSString *)candidateFormat {
// in the candiate format, everything other than '%@' is considered part of the label
NSRange candidateRange = [candidateFormat rangeOfString:@"%@"];
if (candidateRange.location == NSNotFound) {
_prefixLabelFormat = candidateFormat;
_suffixLabelFormat = nil;
return;
}
if (candidateRange.location > 0) {
// everything before '%@' is prefix label
NSRange prefixLabelRange = NSMakeRange(0, candidateRange.location);
_prefixLabelFormat = [candidateFormat substringWithRange:prefixLabelRange];
} else {
_prefixLabelFormat = nil;
}
if (NSMaxRange(candidateRange) < candidateFormat.length) {
// everything after '%@' is suffix label
NSRange suffixLabelRange = NSMakeRange(NSMaxRange(candidateRange),
candidateFormat.length - NSMaxRange(candidateRange));
_suffixLabelFormat = [candidateFormat substringWithRange:suffixLabelRange];
} else {
// '%@' is at the end, so suffix label does not exist
_suffixLabelFormat = nil;
}
}
- (void)setBackgroundColor:(NSColor *)backgroundColor
highlightedStripColor:(NSColor *)highlightedStripColor
highlightedPreeditColor:(NSColor *)highlightedPreeditColor
preeditBackgroundColor:(NSColor *)preeditBackgroundColor
borderColor:(NSColor *)borderColor {
_backgroundColor = backgroundColor;
_highlightedStripColor = highlightedStripColor;
_highlightedPreeditColor = highlightedPreeditColor;
_preeditBackgroundColor = preeditBackgroundColor;
_borderColor = borderColor;
}
- (void)setCornerRadius:(double)cornerRadius
hilitedCornerRadius:(double)hilitedCornerRadius
edgeInset:(NSSize)edgeInset
borderWidth:(double)borderWidth
linespace:(double)linespace
preeditLinespace:(double)preeditLinespace
alpha:(CGFloat)alpha
linear:(BOOL)linear
vertical:(BOOL)vertical
inlinePreedit:(BOOL)inlinePreedit
inlineCandidate:(BOOL)inlineCandidate {
_cornerRadius = cornerRadius;
_hilitedCornerRadius = hilitedCornerRadius;
_edgeInset = edgeInset;
_borderWidth = borderWidth;
_linespace = linespace;
_alpha = alpha;
_preeditLinespace = preeditLinespace;
_linear = linear;
_vertical = vertical;
_inlinePreedit = inlinePreedit;
_inlineCandidate = inlineCandidate;
}
- (void) setAttrs:(NSMutableDictionary *)attrs
labelAttrs:(NSMutableDictionary *)labelAttrs
highlightedAttrs:(NSMutableDictionary *)highlightedAttrs
labelHighlightedAttrs:(NSMutableDictionary *)labelHighlightedAttrs
commentAttrs:(NSMutableDictionary *)commentAttrs
commentHighlightedAttrs:(NSMutableDictionary *)commentHighlightedAttrs
preeditAttrs:(NSMutableDictionary *)preeditAttrs
preeditHighlightedAttrs:(NSMutableDictionary *)preeditHighlightedAttrs {
_attrs = attrs;
_labelAttrs = labelAttrs;
_highlightedAttrs = highlightedAttrs;
_labelHighlightedAttrs = labelHighlightedAttrs;
_commentAttrs = commentAttrs;
_commentHighlightedAttrs = commentHighlightedAttrs;
_preeditAttrs = preeditAttrs;
_preeditHighlightedAttrs = preeditHighlightedAttrs;
}
- (void) setParagraphStyle:(NSParagraphStyle *)paragraphStyle
preeditParagraphStyle:(NSParagraphStyle *)preeditParagraphStyle {
_paragraphStyle = paragraphStyle;
_preeditParagraphStyle = preeditParagraphStyle;
}
@end
@interface SquirrelView : NSView
@property(nonatomic, readonly) NSTextStorage *text;
@property(nonatomic, readonly) NSRange highlightedRange;
@property(nonatomic, readonly) NSRange preeditRange;
@property(nonatomic, readonly) NSRange highlightedPreeditRange;
@property(nonatomic, readonly) NSRect contentRect;
@property(nonatomic, readonly) BOOL isDark;
@property(nonatomic, strong, readonly) SquirrelTheme *currentTheme;
@property(nonatomic, assign) CGFloat seperatorWidth;
- (BOOL)isFlipped;
- (void)setText:(NSAttributedString *)text;
- (void)drawViewWith:(NSRange)hilightedRange
preeditRange:(NSRange)preeditRange
highlightedPreeditRange:(NSRange)highlightedPreeditRange;
- (NSRect)contentRectForRange:(NSRange)range;
@end
@implementation SquirrelView
SquirrelTheme *_defaultTheme;
SquirrelTheme *_darkTheme;
// Need flipped coordinate system, as required by textStorage
- (BOOL)isFlipped {
return YES;
}
- (BOOL)isDark {
if (@available(macOS 10.14, *)) {
if ([NSApp.effectiveAppearance bestMatchFromAppearancesWithNames:@[NSAppearanceNameAqua, NSAppearanceNameDarkAqua]] == NSAppearanceNameDarkAqua) {
return YES;
}
}
return NO;
}
- (SquirrelTheme *)selectTheme:(BOOL)isDark {
return isDark ? _darkTheme : _defaultTheme;
}
- (SquirrelTheme *)currentTheme {
return [self selectTheme:self.isDark];
}
- (instancetype)initWithFrame:(NSRect)frameRect {
self = [super initWithFrame:frameRect];
if (self) {
self.wantsLayer = YES;
self.layer.masksToBounds = YES;
}
// Use textStorage to store text and manage all text layout and draws
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:NSZeroSize];
textContainer.lineFragmentPadding = 0.0;
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
[layoutManager addTextContainer:textContainer];
_text = [[NSTextStorage alloc] init];
[_text addLayoutManager:layoutManager];
layoutManager.backgroundLayoutEnabled = YES;
_defaultTheme = [[SquirrelTheme alloc] init];
if (@available(macOS 10.14, *)) {
_darkTheme = [[SquirrelTheme alloc] init];
}
return self;
}
// Get the rectangle containing entire contents, expensive to calculate
- (NSRect)contentRect {
NSRange glyphRange = [_text.layoutManagers[0] glyphRangeForTextContainer:_text.layoutManagers[0].textContainers[0]];
NSRect rect = [_text.layoutManagers[0] boundingRectForGlyphRange:glyphRange inTextContainer:_text.layoutManagers[0].textContainers[0]];
return rect;
}
// Get the rectangle containing the range of text, will first convert to glyph range, expensive to calculate
- (NSRect)contentRectForRange:(NSRange)range {
NSRange glyphRange = [_text.layoutManagers[0] glyphRangeForCharacterRange:range actualCharacterRange:NULL];
NSRect rect = [_text.layoutManagers[0] boundingRectForGlyphRange:glyphRange inTextContainer:_text.layoutManagers[0].textContainers[0]];
return rect;
}
- (void)setText:(NSAttributedString *)text {
[_text setAttributedString:[text copy]];
}
// Will triger - (void)drawRect:(NSRect)dirtyRect
- (void)drawViewWith:(NSRange)hilightedRange
preeditRange:(NSRange)preeditRange
highlightedPreeditRange:(NSRange)highlightedPreeditRange {
_highlightedRange = hilightedRange;
_preeditRange = preeditRange;
_highlightedPreeditRange = highlightedPreeditRange;
self.needsDisplay = YES;
}
// A tweaked sign function, to winddown corner radius when the size is small
double sign(double number) {
if (number >= 2) {
return 1;
} else if (number <= -2) {
return -1;
}else {
return number / 2;
}
}
// Bezier cubic curve, which has continuous roundness
NSBezierPath *drawSmoothLines(NSArray<NSValue *> *vertex, CGFloat alpha, CGFloat beta) {
NSBezierPath *path = [NSBezierPath bezierPath];
if (vertex.count < 1)
return path;
NSPoint previousPoint = [vertex[vertex.count-1] pointValue];
NSPoint point = [vertex[0] pointValue];
NSPoint nextPoint;
NSPoint control1;
NSPoint control2;
NSPoint target = previousPoint;
NSPoint diff = NSMakePoint(point.x - previousPoint.x, point.y - previousPoint.y);
if (ABS(diff.x) >= ABS(diff.y)) {
target.x += sign(diff.x/beta)*beta;
} else {
target.y += sign(diff.y/beta)*beta;
}
[path moveToPoint:target];
for (NSUInteger i = 0; i < vertex.count; i += 1) {
previousPoint = [vertex[(vertex.count+i-1)%vertex.count] pointValue];
point = [vertex[i] pointValue];
nextPoint = [vertex[(i+1)%vertex.count] pointValue];
target = point;
control1 = point;
diff = NSMakePoint(point.x - previousPoint.x, point.y - previousPoint.y);
if (ABS(diff.x) >= ABS(diff.y)) {
target.x -= sign(diff.x/beta)*beta;
control1.x -= sign(diff.x/beta)*alpha;
} else {
target.y -= sign(diff.y/beta)*beta;
control1.y -= sign(diff.y/beta)*alpha;
}
[path lineToPoint:target];
target = point;
control2 = point;
diff = NSMakePoint(nextPoint.x - point.x, nextPoint.y - point.y);
if (ABS(diff.x) > ABS(diff.y)) {
control2.x += sign(diff.x/beta)*alpha;
target.x += sign(diff.x/beta)*beta;
} else {
control2.y += sign(diff.y/beta)*alpha;
target.y += sign(diff.y/beta)*beta;
}
[path curveToPoint:target controlPoint1:control1 controlPoint2:control2];
}
[path closePath];
return path;
}
NSArray<NSValue *> *rectVertex(NSRect rect) {
return @[
@(rect.origin),
@(NSMakePoint(rect.origin.x, rect.origin.y+rect.size.height)),
@(NSMakePoint(rect.origin.x+rect.size.width, rect.origin.y+rect.size.height)),
@(NSMakePoint(rect.origin.x+rect.size.width, rect.origin.y))
];
}
void xyTranslation(NSMutableArray<NSValue *> *shape, NSPoint direction) {
for (NSUInteger i = 0; i < shape.count; i += 1) {
NSPoint point = [shape[i] pointValue];
point.x += direction.x;
point.y += direction.y;
[shape replaceObjectAtIndex:i withObject:@(point)];
}
}
BOOL nearEmptyRect(NSRect rect) {
return rect.size.height * rect.size.width < 1;
}
// Calculate 3 boxes containing the text in range. leadingRect and trailingRect are incomplete line rectangle
// bodyRect is complete lines in the middle
- (void)multilineRectForRange:(NSRange)charRange leadingRect:(NSRect *)leadingRect bodyRect:(NSRect *)bodyRect trailingRect:(NSRect *)trailingRect {
NSLayoutManager *layoutManager = _text.layoutManagers[0];
NSTextContainer *textContainer = layoutManager.textContainers[0];
NSRange glyphRange = [layoutManager glyphRangeForCharacterRange:charRange actualCharacterRange:NULL];
NSRect boundingRect = [layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:textContainer];
NSRange fullRangeInBoundingRect = [layoutManager glyphRangeForBoundingRect:boundingRect inTextContainer:textContainer];
*leadingRect = NSZeroRect;
*bodyRect = boundingRect;
*trailingRect = NSZeroRect;
if (boundingRect.origin.x <= 1 && fullRangeInBoundingRect.location < glyphRange.location) {
*leadingRect = [layoutManager boundingRectForGlyphRange:NSMakeRange(fullRangeInBoundingRect.location, glyphRange.location-fullRangeInBoundingRect.location) inTextContainer:textContainer];
if (!nearEmptyRect(*leadingRect)) {
bodyRect->size.height -= leadingRect->size.height;
bodyRect->origin.y += leadingRect->size.height;
}
double rightEdge = NSMaxX(*leadingRect);
leadingRect->origin.x = rightEdge;
leadingRect->size.width = bodyRect->origin.x + bodyRect->size.width - rightEdge;
}
if (fullRangeInBoundingRect.location+fullRangeInBoundingRect.length > glyphRange.location+glyphRange.length) {
*trailingRect = [layoutManager boundingRectForGlyphRange:
NSMakeRange(glyphRange.location+glyphRange.length, fullRangeInBoundingRect.location+fullRangeInBoundingRect.length-glyphRange.location-glyphRange.length)
inTextContainer:textContainer];
if (!nearEmptyRect(*trailingRect)) {
bodyRect->size.height -= trailingRect->size.height;
}
double leftEdge = NSMinX(*trailingRect);
trailingRect->origin.x = bodyRect->origin.x;
trailingRect->size.width = leftEdge - bodyRect->origin.x;
} else if (fullRangeInBoundingRect.location+fullRangeInBoundingRect.length == glyphRange.location+glyphRange.length) {
*trailingRect = [layoutManager lineFragmentUsedRectForGlyphAtIndex:glyphRange.location+glyphRange.length-1 effectiveRange:NULL];
if (NSMaxX(*trailingRect) >= NSMaxX(boundingRect) - 1) {
*trailingRect = NSZeroRect;
} else if (!nearEmptyRect(*trailingRect)) {
bodyRect->size.height -= trailingRect->size.height;
}
}
NSRect lastLineRect = nearEmptyRect(*trailingRect) ? *bodyRect : *trailingRect;
lastLineRect.size.width = textContainer.containerSize.width - lastLineRect.origin.x;
NSRange lastLineRange = [layoutManager glyphRangeForBoundingRect:lastLineRect inTextContainer:textContainer];
NSGlyphProperty glyphProperty = [layoutManager propertyForGlyphAtIndex:lastLineRange.location+lastLineRange.length-1];
while (lastLineRange.length>0 && (glyphProperty == NSGlyphPropertyElastic || glyphProperty == NSGlyphPropertyControlCharacter)) {
lastLineRange.length -= 1;
glyphProperty = [layoutManager propertyForGlyphAtIndex:lastLineRange.location+lastLineRange.length-1];
}
if (lastLineRange.location+lastLineRange.length == glyphRange.location+glyphRange.length) {
if (!nearEmptyRect(*trailingRect)) {
*trailingRect = lastLineRect;
} else {
*bodyRect = lastLineRect;
}
}
NSSize edgeInset = self.currentTheme.edgeInset;
leadingRect->origin.x += edgeInset.width;
leadingRect->origin.y += edgeInset.height;
bodyRect->origin.x += edgeInset.width;
bodyRect->origin.y += edgeInset.height;
trailingRect->origin.x += edgeInset.width;
trailingRect->origin.y += edgeInset.height;
}
// Based on the 3 boxes from multilineRectForRange, calculate the vertex of the polygon containing the text in range
NSArray<NSValue *> * multilineRectVertex(NSRect leadingRect, NSRect bodyRect, NSRect trailingRect) {
if (nearEmptyRect(bodyRect) && !nearEmptyRect(leadingRect) && nearEmptyRect(trailingRect)) {
return rectVertex(leadingRect);
} else if (nearEmptyRect(bodyRect) && nearEmptyRect(leadingRect) && !nearEmptyRect(trailingRect)) {
return rectVertex(trailingRect);
} else if (nearEmptyRect(leadingRect) && nearEmptyRect(trailingRect) && !nearEmptyRect(bodyRect)) {
return rectVertex(bodyRect);
} else if (nearEmptyRect(trailingRect) && !nearEmptyRect(bodyRect)) {
NSArray<NSValue *> * leadingVertex = rectVertex(leadingRect);
NSArray<NSValue *> * bodyVertex = rectVertex(bodyRect);
return @[bodyVertex[0], leadingVertex[1], leadingVertex[0], leadingVertex[3], bodyVertex[2], bodyVertex[1]];
} else if (nearEmptyRect(leadingRect) && !nearEmptyRect(bodyRect)) {
NSArray<NSValue *> * trailingVertex = rectVertex(trailingRect);
NSArray<NSValue *> * bodyVertex = rectVertex(bodyRect);
return @[bodyVertex[0], bodyVertex[3], bodyVertex[2], trailingVertex[3], trailingVertex[2], trailingVertex[1]];
} else if (!nearEmptyRect(leadingRect) && !nearEmptyRect(trailingRect) && nearEmptyRect(bodyRect) && NSMaxX(leadingRect)>NSMinX(trailingRect)) {
NSArray<NSValue *> * leadingVertex = rectVertex(leadingRect);
NSArray<NSValue *> * trailingVertex = rectVertex(trailingRect);
return @[trailingVertex[0], leadingVertex[1], leadingVertex[0], leadingVertex[3], leadingVertex[2], trailingVertex[3], trailingVertex[2], trailingVertex[1]];
} else if (!nearEmptyRect(leadingRect) && !nearEmptyRect(trailingRect) && !nearEmptyRect(bodyRect)) {
NSArray<NSValue *> * leadingVertex = rectVertex(leadingRect);
NSArray<NSValue *> * bodyVertex = rectVertex(bodyRect);
NSArray<NSValue *> * trailingVertex = rectVertex(trailingRect);
return @[bodyVertex[0], leadingVertex[1], leadingVertex[0], leadingVertex[3], bodyVertex[2], trailingVertex[3], trailingVertex[2], trailingVertex[1]];
} else {
return @[];
}
}
// If the point is outside the innerBox, will extend to reach the outerBox
void expand(NSMutableArray<NSValue *> *vertex, NSRect innerBorder, NSRect outerBorder) {
for (NSUInteger i = 0; i < vertex.count; i += 1){
NSPoint point = [vertex[i] pointValue];
if (point.x < innerBorder.origin.x) {
point.x = outerBorder.origin.x;
} else if (point.x > innerBorder.origin.x+innerBorder.size.width) {
point.x = outerBorder.origin.x+outerBorder.size.width;
}
if (point.y < innerBorder.origin.y) {
point.y = outerBorder.origin.y;
} else if (point.y > innerBorder.origin.y+innerBorder.size.height) {
point.y = outerBorder.origin.y+outerBorder.size.height;
}
[vertex replaceObjectAtIndex:i withObject:@(point)];
}
}
// Add gap between horizontal candidates
- (void)addGapBetweenHorizontalCandidates:(NSRect *)rect {
if (_highlightedRange.location+_highlightedRange.length == _text.length) {
if (!nearEmptyRect(*rect)) {
rect->size.width += _seperatorWidth / 2;
rect->origin.x -= _seperatorWidth / 2;
}
} else if (_highlightedRange.location - ((_preeditRange.location == NSNotFound ? 0 : _preeditRange.location)+_preeditRange.length) <= 1) {
if (!nearEmptyRect(*rect)) {
rect->size.width += _seperatorWidth / 2;
}
} else {
if (!nearEmptyRect(*rect)) {
rect->size.width += _seperatorWidth;
rect->origin.x -= _seperatorWidth / 2;
}
}
}
// All draws happen here
- (void)drawRect:(NSRect)dirtyRect {
NSBezierPath *backgroundPath;
NSBezierPath *borderPath;
NSBezierPath *highlightedPath;
NSBezierPath *highlightedPath2;
NSBezierPath *highlightedPreeditPath;
NSBezierPath *highlightedPreeditPath2;
NSBezierPath *preeditPath;
SquirrelTheme * theme = self.currentTheme;
NSRect textField = dirtyRect;
textField.origin.y += theme.edgeInset.height;
textField.origin.x += theme.edgeInset.width;
// Draw preedit Rect
NSRect backgroundRect = dirtyRect;
// Draw preedit Rect
NSRect preeditRect = NSZeroRect;
if (_preeditRange.length > 0) {
preeditRect = [self contentRectForRange:_preeditRange];
preeditRect.size.width = textField.size.width;
preeditRect.size.height += theme.edgeInset.height + theme.preeditLinespace / 2 + theme.hilitedCornerRadius / 2;
preeditRect.origin = NSMakePoint(textField.origin.x - theme.edgeInset.width, textField.origin.y - theme.edgeInset.height);
if (_highlightedRange.length == 0) {
preeditRect.size.height += theme.edgeInset.height - theme.preeditLinespace / 2 - theme.hilitedCornerRadius / 2;
}
if (theme.preeditBackgroundColor != nil) {
preeditPath = drawSmoothLines(rectVertex(preeditRect), 0, 0);
}
}
// Draw highlighted Rect
if (_highlightedRange.length > 0 && theme.highlightedStripColor != nil) {
NSRect innerBox = backgroundRect;
innerBox.size.width -= (theme.edgeInset.width + 1) * 2;
innerBox.origin.x += theme.edgeInset.width + 1;
if (_preeditRange.length == 0) {
innerBox.origin.y += theme.edgeInset.height + 1;
innerBox.size.height -= (theme.edgeInset.height + 1) * 2;
} else {
innerBox.origin.y += preeditRect.size.height + theme.preeditLinespace / 2 + theme.hilitedCornerRadius / 2 + 1;
innerBox.size.height -= theme.edgeInset.height + preeditRect.size.height + theme.preeditLinespace / 2 + theme.hilitedCornerRadius / 2 + 2;
}
NSRect outerBox = backgroundRect;
outerBox.size.height -= theme.hilitedCornerRadius + preeditRect.size.height;
outerBox.size.width -= theme.hilitedCornerRadius;
outerBox.origin.x += theme.hilitedCornerRadius / 2;
outerBox.origin.y += theme.hilitedCornerRadius / 2 + preeditRect.size.height;
CGFloat halfLinespace = theme.linespace / 2;
if (theme.linear){
NSRect leadingRect;
NSRect bodyRect;
NSRect trailingRect;
[self multilineRectForRange:_highlightedRange leadingRect:&leadingRect bodyRect:&bodyRect trailingRect:&trailingRect];
[self addGapBetweenHorizontalCandidates:&leadingRect];
[self addGapBetweenHorizontalCandidates:&bodyRect];
[self addGapBetweenHorizontalCandidates:&trailingRect];
NSMutableArray<NSValue *> *highlightedPoints;
NSMutableArray<NSValue *> *highlightedPoints2;
// Handles the special case where containing boxes are separated
if (nearEmptyRect(bodyRect) && !nearEmptyRect(leadingRect) && !nearEmptyRect(trailingRect) && NSMaxX(trailingRect) < NSMinX(leadingRect)) {
highlightedPoints = [rectVertex(leadingRect) mutableCopy];
highlightedPoints2 = [rectVertex(trailingRect) mutableCopy];
} else {
highlightedPoints = [multilineRectVertex(leadingRect, bodyRect, trailingRect) mutableCopy];
}
xyTranslation(highlightedPoints, NSMakePoint(0, -halfLinespace));
xyTranslation(highlightedPoints2, NSMakePoint(0, -halfLinespace));
innerBox.size.height -= halfLinespace;
// Expand the boxes to reach proper border
expand(highlightedPoints, innerBox, outerBox);
expand(highlightedPoints2, innerBox, outerBox);
highlightedPath = drawSmoothLines(highlightedPoints, 0.3*theme.hilitedCornerRadius, 1.4*theme.hilitedCornerRadius);
if (highlightedPoints2.count > 0) {
highlightedPath2 = drawSmoothLines(highlightedPoints2, 0.3*theme.hilitedCornerRadius, 1.4*theme.hilitedCornerRadius);
}
} else {
NSRect highlightedRect = [self contentRectForRange:_highlightedRange];
highlightedRect.size.width = textField.size.width;
highlightedRect.size.height += theme.linespace;
highlightedRect.origin = NSMakePoint(textField.origin.x - theme.edgeInset.width,
highlightedRect.origin.y + theme.edgeInset.height - halfLinespace);
if (_highlightedRange.location+_highlightedRange.length == _text.length) {
highlightedRect.size.height += theme.edgeInset.height - halfLinespace;
}
if (_highlightedRange.location - ((_preeditRange.location == NSNotFound ? 0 : _preeditRange.location)+_preeditRange.length) <= 1) {
if (_preeditRange.length == 0) {
highlightedRect.size.height += theme.edgeInset.height - halfLinespace;
highlightedRect.origin.y -= theme.edgeInset.height - halfLinespace;
} else {
highlightedRect.size.height += theme.hilitedCornerRadius / 2;
highlightedRect.origin.y -= theme.hilitedCornerRadius / 2;
}
}
NSMutableArray<NSValue *> *highlightedPoints = [rectVertex(highlightedRect) mutableCopy];
expand(highlightedPoints, innerBox, outerBox);
highlightedPath = drawSmoothLines(highlightedPoints, theme.hilitedCornerRadius*0.3, theme.hilitedCornerRadius*1.4);
}
}
// Draw highlighted part of preedit text
if (_highlightedPreeditRange.length > 0 && theme.highlightedPreeditColor != nil) {
NSRect leadingRect;
NSRect bodyRect;
NSRect trailingRect;
[self multilineRectForRange:_highlightedPreeditRange leadingRect:&leadingRect bodyRect:&bodyRect trailingRect:&trailingRect];
NSRect innerBox = preeditRect;
innerBox.size.width -= (theme.edgeInset.width + 1) * 2;
innerBox.origin.x += theme.edgeInset.width + 1;
innerBox.origin.y += theme.edgeInset.height + 1;
if (_highlightedRange.length == 0) {
innerBox.size.height -= (theme.edgeInset.height + 1) * 2;
} else {
innerBox.size.height -= theme.edgeInset.height + theme.preeditLinespace + theme.hilitedCornerRadius / 2 + 2;
}
NSRect outerBox = preeditRect;
outerBox.size.height -= theme.hilitedCornerRadius;
outerBox.size.width -= theme.hilitedCornerRadius;
outerBox.origin.x += theme.hilitedCornerRadius / 2;
outerBox.origin.y += theme.hilitedCornerRadius / 2;
NSMutableArray<NSValue *> *highlightedPreeditPoints;
NSMutableArray<NSValue *> *highlightedPreeditPoints2;
// Handles the special case where containing boxes are separated
if (nearEmptyRect(bodyRect) && !nearEmptyRect(leadingRect) && !nearEmptyRect(trailingRect) && NSMaxX(trailingRect) < NSMinX(leadingRect)) {
highlightedPreeditPoints = [rectVertex(leadingRect) mutableCopy];
highlightedPreeditPoints2 = [rectVertex(trailingRect) mutableCopy];
} else {
highlightedPreeditPoints = [multilineRectVertex(leadingRect, bodyRect, trailingRect) mutableCopy];
}
// Expand the boxes to reach proper border
expand(highlightedPreeditPoints, innerBox, outerBox);
expand(highlightedPreeditPoints2, innerBox, outerBox);
highlightedPreeditPath = drawSmoothLines(highlightedPreeditPoints, 0.3*theme.hilitedCornerRadius, 1.4*theme.hilitedCornerRadius);
if (highlightedPreeditPoints2.count > 0) {
highlightedPreeditPath2 = drawSmoothLines(highlightedPreeditPoints2, 0.3*theme.hilitedCornerRadius, 1.4*theme.hilitedCornerRadius);
}
}
[NSBezierPath setDefaultLineWidth:0];
backgroundPath = drawSmoothLines(rectVertex(backgroundRect), theme.cornerRadius*0.3, theme.cornerRadius*1.4);
// Nothing should extend beyond backgroundPath
borderPath = [backgroundPath copy];
[borderPath addClip];
borderPath.lineWidth = theme.borderWidth;
// This block of code enables independent transparencies in highlighted colour and background colour.
// Disabled because of the flaw: edges or rounded corners of the heighlighted area are rendered with undesirable shadows.
#if 0
// Calculate intersections.
if (![highlightedPath isEmpty]) {
[backgroundPath appendBezierPath:[highlightedPath copy]];
if (![highlightedPath2 isEmpty]) {
[backgroundPath appendBezierPath:[highlightedPath2 copy]];
}
}
if (![preeditPath isEmpty]) {
[backgroundPath appendBezierPath:[preeditPath copy]];
}
if (![highlightedPreeditPath isEmpty]) {
if (preeditPath != nil) {
[preeditPath appendBezierPath:[highlightedPreeditPath copy]];
} else {
[backgroundPath appendBezierPath:[highlightedPreeditPath copy]];
}
if (![highlightedPreeditPath2 isEmpty]) {
if (preeditPath != nil) {
[preeditPath appendBezierPath:[highlightedPreeditPath2 copy]];
} else {
[backgroundPath appendBezierPath:[highlightedPreeditPath2 copy]];
}
}
}
[backgroundPath setWindingRule:NSEvenOddWindingRule];
[preeditPath setWindingRule:NSEvenOddWindingRule];
#endif
[theme.backgroundColor setFill];
[backgroundPath fill];
if (theme.preeditBackgroundColor && ![preeditPath isEmpty]) {
[theme.preeditBackgroundColor setFill];
[preeditPath fill];
}
if (theme.highlightedStripColor && ![highlightedPath isEmpty]) {
[theme.highlightedStripColor setFill];
[highlightedPath fill];
if (![highlightedPath2 isEmpty]) {
[highlightedPath2 fill];
}
}
if (theme.highlightedPreeditColor && ![highlightedPreeditPath isEmpty]) {
[theme.highlightedPreeditColor setFill];
[highlightedPreeditPath fill];
if (![highlightedPreeditPath2 isEmpty]) {
[highlightedPreeditPath2 fill];
}
}
if (theme.borderColor && (theme.borderWidth > 0)) {
[theme.borderColor setStroke];
[borderPath stroke];
}
NSRange glyphRange = [_text.layoutManagers[0] glyphRangeForTextContainer:_text.layoutManagers[0].textContainers[0]];
[_text.layoutManagers[0] drawGlyphsForGlyphRange:glyphRange atPoint:textField.origin];
}
@end
@implementation SquirrelPanel {
SquirrelView *_view;
NSRange _preeditRange;
NSRect _screenRect;
CGFloat _maxHeight;
NSString *_statusMessage;
NSTimer *_statusTimer;
}
- (BOOL)linear {
return _view.currentTheme.linear;
}
- (BOOL)vertical {
return _view.currentTheme.vertical;
}
- (BOOL)inlinePreedit {
return _view.currentTheme.inlinePreedit;
}
- (BOOL)inlineCandidate {
return _view.currentTheme.inlineCandidate;
}
CGFloat minimumHeight(NSDictionary *attribute) {
const NSAttributedString *spaceChar = [[NSAttributedString alloc] initWithString:@" " attributes:attribute];
const CGFloat minimumHeight = [spaceChar boundingRectWithSize:NSZeroSize options:0].size.height;
return minimumHeight;
}
// Use this method to convert charcters to upright position
// Based on the width of the chacter, relative font size matters
void convertToVerticalGlyph(NSMutableAttributedString *originalText, NSRange stringRange) {
NSDictionary *attribute = [originalText attributesAtIndex:stringRange.location effectiveRange:NULL];
double baseOffset = [attribute[NSBaselineOffsetAttributeName] doubleValue];
// Use the width of the character to determin if they should be upright in vertical writing mode.
// Adjust font base line for better alignment.
const NSAttributedString *cjkChar = [[NSAttributedString alloc] initWithString:@"字" attributes:attribute];
const NSRect cjkRect = [cjkChar boundingRectWithSize:NSZeroSize options:0];
const NSAttributedString *hangulChar = [[NSAttributedString alloc] initWithString:@"글" attributes:attribute];
const NSSize hangulSize = [hangulChar boundingRectWithSize:NSZeroSize options:0].size;
stringRange = [originalText.string rangeOfComposedCharacterSequencesForRange:stringRange];
NSUInteger i = stringRange.location;
while (i < stringRange.location+stringRange.length) {
NSRange range = [originalText.string rangeOfComposedCharacterSequenceAtIndex:i];
i = range.location + range.length;
NSRect charRect = [[originalText attributedSubstringFromRange:range] boundingRectWithSize:NSZeroSize options:0];
// Also adjust the baseline so upright and lying charcters are properly aligned
if ((charRect.size.width >= cjkRect.size.width) || (charRect.size.width >= hangulSize.width)) {
[originalText addAttribute:NSVerticalGlyphFormAttributeName value:@(1) range:range];
NSRect uprightCharRect = [[originalText attributedSubstringFromRange:range] boundingRectWithSize:NSZeroSize options:0];
CGFloat widthDiff = charRect.size.width-cjkChar.size.width;
CGFloat offset = (cjkRect.size.height - uprightCharRect.size.height)/2 + (cjkRect.origin.y-uprightCharRect.origin.y) - (widthDiff>0 ? widthDiff/3 : widthDiff/2) +baseOffset;
[originalText addAttribute:NSBaselineOffsetAttributeName value:@(offset) range:range];
} else {
[originalText addAttribute:NSBaselineOffsetAttributeName value:@(baseOffset) range:range];
}
}
}
void fixDefaultFont(NSMutableAttributedString *text) {
[text fixFontAttributeInRange:NSMakeRange(0, text.length)];
NSRange currentFontRange = NSMakeRange(NSNotFound, 0);
long i = 0;
while (i < text.length) {
NSFont *charFont = [text attribute:NSFontAttributeName atIndex:i effectiveRange:¤tFontRange];
if ([charFont.fontName isEqualToString:@"AppleColorEmoji"]) {
NSFont *defaultFont = [NSFont systemFontOfSize:charFont.pointSize];
[text addAttribute:NSFontAttributeName value:defaultFont range:currentFontRange];
}
i = currentFontRange.location + currentFontRange.length;
}
}
+ (NSColor *)secondaryTextColor {
if(@available(macOS 10.10, *)) {
return [NSColor secondaryLabelColor];
} else {
return [NSColor disabledControlTextColor];
}
}
- (void)initializeUIStyleForDarkMode:(BOOL)isDark {
SquirrelTheme *theme = [_view selectTheme:isDark];
theme.native = YES;
theme.candidateFormat = kDefaultCandidateFormat;
NSColor *secondaryTextColor = [[self class] secondaryTextColor];
NSMutableDictionary *attrs = [[NSMutableDictionary alloc] init];
attrs[NSForegroundColorAttributeName] = [NSColor controlTextColor];
attrs[NSFontAttributeName] = [NSFont userFontOfSize:kDefaultFontSize];
NSMutableDictionary *highlightedAttrs = [[NSMutableDictionary alloc] init];
highlightedAttrs[NSForegroundColorAttributeName] = [NSColor selectedControlTextColor];
highlightedAttrs[NSFontAttributeName] = [NSFont userFontOfSize:kDefaultFontSize];
NSMutableDictionary *labelAttrs = [attrs mutableCopy];
NSMutableDictionary *labelHighlightedAttrs = [highlightedAttrs mutableCopy];
NSMutableDictionary *commentAttrs = [[NSMutableDictionary alloc] init];
commentAttrs[NSForegroundColorAttributeName] = secondaryTextColor;
commentAttrs[NSFontAttributeName] = [NSFont userFontOfSize:kDefaultFontSize];
NSMutableDictionary *commentHighlightedAttrs = [commentAttrs mutableCopy];
NSMutableDictionary *preeditAttrs = [[NSMutableDictionary alloc] init];
preeditAttrs[NSForegroundColorAttributeName] = secondaryTextColor;
preeditAttrs[NSFontAttributeName] = [NSFont userFontOfSize:kDefaultFontSize];
NSMutableDictionary *preeditHighlightedAttrs = [[NSMutableDictionary alloc] init];
preeditHighlightedAttrs[NSForegroundColorAttributeName] = [NSColor controlTextColor];
preeditHighlightedAttrs[NSFontAttributeName] = [NSFont userFontOfSize:kDefaultFontSize];
NSParagraphStyle *paragraphStyle = [NSParagraphStyle defaultParagraphStyle];
NSParagraphStyle *preeditParagraphStyle = [NSParagraphStyle defaultParagraphStyle];
[theme setAttrs:attrs
labelAttrs:labelAttrs
highlightedAttrs:highlightedAttrs
labelHighlightedAttrs:labelHighlightedAttrs
commentAttrs:commentAttrs
commentHighlightedAttrs:commentHighlightedAttrs
preeditAttrs:preeditAttrs
preeditHighlightedAttrs:preeditHighlightedAttrs];
[theme setParagraphStyle:paragraphStyle
preeditParagraphStyle:preeditParagraphStyle];
}
- (instancetype)init {
self = [super initWithContentRect:_position
styleMask:NSWindowStyleMaskBorderless
backing:NSBackingStoreBuffered
defer:YES];
if (self) {
self.alphaValue = 1.0;
// _window.level = NSScreenSaverWindowLevel + 1;
// ^ May fix visibility issue in fullscreen games.
self.level = CGShieldingWindowLevel();
self.hasShadow = YES;
self.opaque = NO;
self.backgroundColor = [NSColor clearColor];
_view = [[SquirrelView alloc] initWithFrame:self.contentView.frame];
self.contentView = _view;
[self initializeUIStyleForDarkMode:NO];
if (@available(macOS 10.14, *)) {
[self initializeUIStyleForDarkMode:YES];
}
_maxHeight = 0;
}
return self;
}
- (void)getCurrentScreen {
// get current screen
_screenRect = [NSScreen mainScreen].frame;
NSArray *screens = [NSScreen screens];
NSUInteger i;
for (i = 0; i < screens.count; ++i) {
NSRect rect = [screens[i] frame];
if (NSPointInRect(_position.origin, rect)) {
_screenRect = rect;
break;
}
}
}
// Get the window size, the windows will be the dirtyRect in SquirrelView.drawRect
- (void)show {
[self getCurrentScreen];
SquirrelTheme *theme = _view.currentTheme;
if (@available(macOS 10.14, *)) {
NSAppearance *requestedAppearance = theme.native ? nil : [NSAppearance appearanceNamed:NSAppearanceNameAqua];
if (self.appearance != requestedAppearance) {
self.appearance = requestedAppearance;
}
}
//Break line if the text is too long, based on screen size.
CGFloat textWidth = _view.text.size.width;
NSFont *currentFont = theme.attrs[NSFontAttributeName];
CGFloat fontScale = currentFont.pointSize / 12;
CGFloat textWidthRatio = MIN(1.0, 1.0 / (theme.vertical ? 4 : 3) + fontScale / 12);
CGFloat maxTextWidth = theme.vertical
? NSHeight(_screenRect) * textWidthRatio - theme.edgeInset.height * 2
: NSWidth(_screenRect) * textWidthRatio - theme.edgeInset.width * 2;
if (textWidth > maxTextWidth) {
textWidth = maxTextWidth;
}
_view.text.layoutManagers[0].textContainers[0].containerSize = NSMakeSize(textWidth, 0);
NSRect windowRect;
// in vertical mode, the width and height are interchanged
NSRect contentRect = _view.contentRect;
if ((theme.vertical && NSMidY(_position) / NSHeight(_screenRect) < 0.5) ||
(!theme.vertical && NSMinX(_position)+MAX(contentRect.size.width, _maxHeight)+theme.edgeInset.width*2 > NSMaxX(_screenRect))) {
if (contentRect.size.width >= _maxHeight) {
_maxHeight = contentRect.size.width;
} else {
contentRect.size.width = _maxHeight;
_view.text.layoutManagers[0].textContainers[0].containerSize = NSMakeSize(_maxHeight, 0);
}
}
if (theme.vertical) {
windowRect.size = NSMakeSize(contentRect.size.height + theme.edgeInset.height * 2,
contentRect.size.width + theme.edgeInset.width * 2);
// To avoid jumping up and down while typing, use the lower screen when typing on upper, and vice versa
if (NSMidY(_position) / NSHeight(_screenRect) >= 0.5) {
windowRect.origin.y = NSMinY(_position) - kOffsetHeight - NSHeight(windowRect);
} else {
windowRect.origin.y = NSMaxY(_position) + kOffsetHeight;
}
// Make the first candidate fixed at the left of cursor
windowRect.origin.x = NSMinX(_position) - windowRect.size.width - kOffsetHeight;
if (_preeditRange.length > 0) {
NSSize preeditSize = [_view contentRectForRange:_preeditRange].size;
windowRect.origin.x += preeditSize.height + theme.edgeInset.width;
}
} else {
windowRect.size = NSMakeSize(contentRect.size.width + theme.edgeInset.width * 2,
contentRect.size.height + theme.edgeInset.height * 2);
windowRect.origin = NSMakePoint(NSMinX(_position),
NSMinY(_position) - kOffsetHeight - NSHeight(windowRect));
}
if (NSMaxX(windowRect) > NSMaxX(_screenRect)) {
windowRect.origin.x = NSMaxX(_screenRect) - NSWidth(windowRect);
}
if (NSMinX(windowRect) < NSMinX(_screenRect)) {
windowRect.origin.x = NSMinX(_screenRect);
}
if (NSMinY(windowRect) < NSMinY(_screenRect)) {
if (theme.vertical) {
windowRect.origin.y = NSMinY(_screenRect);
} else {
windowRect.origin.y = NSMaxY(_position) + kOffsetHeight;
}
}
if (NSMaxY(windowRect) > NSMaxY(_screenRect)) {
windowRect.origin.y = NSMaxY(_screenRect) - NSHeight(windowRect);
}
if (NSMinY(windowRect) < NSMinY(_screenRect)) {
windowRect.origin.y = NSMinY(_screenRect);
}
// rotate the view, the core in vertical mode!
if (theme.vertical) {
_view.boundsRotation = 90.0;
[_view setBoundsOrigin:NSMakePoint(0, windowRect.size.width)];
} else {
_view.boundsRotation = 0;
[_view setBoundsOrigin:NSMakePoint(0, 0)];
}
self.alphaValue = theme.alpha;
[self setFrame:windowRect display:YES];
[self invalidateShadow];
[self orderFront:nil];
// voila !
}
- (void)hide {
if (_statusTimer) {
[_statusTimer invalidate];
_statusTimer = nil;
}
[self orderOut:nil];
_maxHeight = 0;
}
// Main function to add attributes to text output from librime
- (void)showPreedit:(NSString *)preedit
selRange:(NSRange)selRange
caretPos:(NSUInteger)caretPos
candidates:(NSArray *)candidates
comments:(NSArray *)comments
labels:(NSArray *)labels
highlighted:(NSUInteger)index {
NSUInteger numCandidates = candidates.count;
if (numCandidates || (preedit && preedit.length)) {
_statusMessage = nil;
if (_statusTimer) {
[_statusTimer invalidate];
_statusTimer = nil;
}
} else {
if (_statusMessage) {
[self showStatus:_statusMessage];
_statusMessage = nil;
} else if (!_statusTimer) {
[self hide];
}
return;
}
SquirrelTheme *theme = _view.currentTheme;
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] init];
NSUInteger candidateStartPos = 0;
_preeditRange = NSMakeRange(NSNotFound, 0);
NSRange highlightedPreeditRange = NSMakeRange(NSNotFound, 0);
// preedit
if (preedit) {