-
Notifications
You must be signed in to change notification settings - Fork 421
/
Copy pathSquirrelPanel.m
2007 lines (1836 loc) · 84.9 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"
#import <QuartzCore/QuartzCore.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.\u00A0%@";
@interface SquirrelTheme : NSObject
@property(nonatomic, assign) BOOL native;
@property(nonatomic, assign) BOOL memorizeSize;
@property(nonatomic, strong, readonly) NSColor *backgroundColor;
@property(nonatomic, strong, readonly) NSColor *highlightedBackColor;
@property(nonatomic, strong, readonly) NSColor *candidateBackColor;
@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) CGFloat surroundingExtraExpansion;
@property(nonatomic, readonly) CGFloat shadowSize;
@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 translucency;
@property(nonatomic, readonly) BOOL mutualExclusive;
@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;
@property(nonatomic, strong, readonly) NSString *statusMessageType;
- (void)setCandidateFormat:(NSString *)candidateFormat;
- (void)setStatusMessageType:(NSString *)statusMessageType;
- (void)setBackgroundColor:(NSColor *)backgroundColor
highlightedBackColor:(NSColor *)highlightedBackColor
candidateBackColor:(NSColor *)candidateBackColor
highlightedPreeditColor:(NSColor *)highlightedPreeditColor
preeditBackgroundColor:(NSColor *)preeditBackgroundColor
borderColor:(NSColor *)borderColor;
- (void)setCornerRadius:(CGFloat)cornerRadius
hilitedCornerRadius:(CGFloat)hilitedCornerRadius
srdExtraExpansion:(CGFloat)surroundingExtraExpansion
shadowSize:(CGFloat)shadowSize
edgeInset:(NSSize)edgeInset
borderWidth:(CGFloat)borderWidth
linespace:(CGFloat)linespace
preeditLinespace:(CGFloat)preeditLinespace
alpha:(CGFloat)alpha
translucency:(BOOL)translucency
mutualExclusive:(BOOL)mutualExclusive
linear:(BOOL)linear
vertical:(BOOL)vertical
inlinePreedit:(BOOL)inlinePreedit
inlineCandidate:(BOOL)inlineCandidate;
- (void) setAttrs:(NSMutableDictionary *)attrs
highlightedAttrs:(NSMutableDictionary *)highlightedAttrs
labelAttrs:(NSMutableDictionary *)labelAttrs
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)setStatusMessageType:(NSString *)type {
if ([type isEqualToString: @"long"] || [type isEqualToString: @"short"] || [type isEqualToString: @"mix"]) {
_statusMessageType = type;
} else {
_statusMessageType = @"mix";
}
}
- (void)setBackgroundColor:(NSColor *)backgroundColor
highlightedBackColor:(NSColor *)highlightedBackColor
candidateBackColor:(NSColor *)candidateBackColor
highlightedPreeditColor:(NSColor *)highlightedPreeditColor
preeditBackgroundColor:(NSColor *)preeditBackgroundColor
borderColor:(NSColor *)borderColor {
_backgroundColor = backgroundColor;
_highlightedBackColor = highlightedBackColor;
_candidateBackColor = candidateBackColor;
_highlightedPreeditColor = highlightedPreeditColor;
_preeditBackgroundColor = preeditBackgroundColor;
_borderColor = borderColor;
}
- (void)setCornerRadius:(double)cornerRadius
hilitedCornerRadius:(double)hilitedCornerRadius
srdExtraExpansion:(double)surroundingExtraExpansion
shadowSize:(double)shadowSize
edgeInset:(NSSize)edgeInset
borderWidth:(double)borderWidth
linespace:(double)linespace
preeditLinespace:(double)preeditLinespace
alpha:(double)alpha
translucency:(BOOL)translucency
mutualExclusive:(BOOL)mutualExclusive
linear:(BOOL)linear
vertical:(BOOL)vertical
inlinePreedit:(BOOL)inlinePreedit
inlineCandidate:(BOOL)inlineCandidate {
_cornerRadius = cornerRadius;
_hilitedCornerRadius = hilitedCornerRadius;
_surroundingExtraExpansion = surroundingExtraExpansion;
_shadowSize = shadowSize;
_edgeInset = edgeInset;
_borderWidth = borderWidth;
_linespace = linespace;
_alpha = alpha;
_translucency = translucency;
_mutualExclusive = mutualExclusive;
_preeditLinespace = preeditLinespace;
_linear = linear;
_vertical = vertical;
_inlinePreedit = inlinePreedit;
_inlineCandidate = inlineCandidate;
}
- (void) setAttrs:(NSMutableDictionary *)attrs
highlightedAttrs:(NSMutableDictionary *)highlightedAttrs
labelAttrs:(NSMutableDictionary *)labelAttrs
labelHighlightedAttrs:(NSMutableDictionary *)labelHighlightedAttrs
commentAttrs:(NSMutableDictionary *)commentAttrs
commentHighlightedAttrs:(NSMutableDictionary *)commentHighlightedAttrs
preeditAttrs:(NSMutableDictionary *)preeditAttrs
preeditHighlightedAttrs:(NSMutableDictionary *)preeditHighlightedAttrs {
_attrs = attrs;
_highlightedAttrs = highlightedAttrs;
_labelAttrs = labelAttrs;
_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) NSTextView *textView;
@property(nonatomic, readonly) NSArray<NSValue *> *candidateRanges;
@property(nonatomic, readonly) NSInteger hilightedIndex;
@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, readonly) NSTextLayoutManager *layoutManager;
@property(nonatomic, assign) CGFloat seperatorWidth;
@property(nonatomic, readonly) CAShapeLayer *shape;
- (void) drawViewWith:(NSArray<NSValue *> *)candidateRanges
hilightedIndex:(NSInteger)hilightedIndex
preeditRange:(NSRange)preeditRange
highlightedPreeditRange:(NSRange)highlightedPreeditRange;
- (NSRect)contentRectForRange:(NSTextRange *)range;
@end
@implementation SquirrelView
SquirrelTheme *_defaultTheme;
SquirrelTheme *_darkTheme;
// Need flipped coordinate system, as required by textStorage
- (BOOL)isFlipped {
return YES;
}
- (BOOL)isDark {
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];
}
- (NSTextLayoutManager *)layoutManager {
return _textView.textLayoutManager;
}
- (instancetype)initWithFrame:(NSRect)frameRect {
self = [super initWithFrame:frameRect];
if (self) {
self.wantsLayer = YES;
self.layer.masksToBounds = YES;
}
_textView = [[NSTextView alloc] initWithFrame:frameRect];
_textView.drawsBackground = NO;
_textView.editable = NO;
_textView.selectable = NO;
self.layoutManager.textContainer.lineFragmentPadding = 0.0;
_defaultTheme = [[SquirrelTheme alloc] init];
_darkTheme = [[SquirrelTheme alloc] init];
_shape = [[CAShapeLayer alloc] init];
return self;
}
- (NSTextRange *)convertRange:(NSRange)range {
if (range.location == NSNotFound) {
return nil;
} else {
id<NSTextLocation> startLocation = [self.layoutManager locationFromLocation:[self.layoutManager documentRange].location withOffset:range.location];
id<NSTextLocation> endLocation = [self.layoutManager locationFromLocation:startLocation withOffset:range.length];
return [[NSTextRange alloc] initWithLocation:startLocation endLocation:endLocation];
}
}
// Get the rectangle containing entire contents, expensive to calculate
- (NSRect)contentRect {
NSMutableArray<NSValue *> *ranges = [_candidateRanges mutableCopy];
if (_preeditRange.length > 0) {
[ranges addObject:[NSValue valueWithRange:_preeditRange]];
}
CGFloat x0 = CGFLOAT_MAX;
CGFloat x1 = CGFLOAT_MIN;
CGFloat y0 = CGFLOAT_MAX;
CGFloat y1 = CGFLOAT_MIN;
for (NSUInteger i = 0; i < ranges.count; i += 1) {
NSRange range = [ranges[i] rangeValue];
NSRect rect = [self contentRectForRange:[self convertRange: range]];
x0 = MIN(NSMinX(rect), x0);
x1 = MAX(NSMaxX(rect), x1);
y0 = MIN(NSMinY(rect), y0);
y1 = MAX(NSMaxY(rect), y1);
}
return NSMakeRect(x0, y0, x1-x0, y1-y0);
}
// Get the rectangle containing the range of text, will first convert to glyph range, expensive to calculate
- (NSRect)contentRectForRange:(NSTextRange *)range {
__block CGFloat x0 = CGFLOAT_MAX;
__block CGFloat x1 = CGFLOAT_MIN;
__block CGFloat y0 = CGFLOAT_MAX;
__block CGFloat y1 = CGFLOAT_MIN;
[self.layoutManager enumerateTextSegmentsInRange:range type:NSTextLayoutManagerSegmentTypeStandard options:NSTextLayoutManagerSegmentOptionsRangeNotRequired usingBlock:^(NSTextRange *_, CGRect rect, CGFloat baseline, NSTextContainer *tectContainer) {
x0 = MIN(NSMinX(rect), x0);
x1 = MAX(NSMaxX(rect), x1);
y0 = MIN(NSMinY(rect), y0);
y1 = MAX(NSMaxY(rect), y1);
return YES;
}];
return NSMakeRect(x0, y0, x1-x0, y1-y0);
}
// Will triger - (void)drawRect:(NSRect)dirtyRect
- (void) drawViewWith:(NSArray<NSValue *> *)candidateRanges
hilightedIndex:(NSInteger)hilightedIndex
preeditRange:(NSRange)preeditRange
highlightedPreeditRange:(NSRange)highlightedPreeditRange {
_candidateRanges = candidateRanges;
_hilightedIndex = hilightedIndex;
_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
CGMutablePathRef drawSmoothLines(NSArray<NSValue *> *vertex, NSSet<NSNumber *> * __nullable straightCorner, CGFloat alpha, CGFloat beta) {
beta = MAX(0.00001, beta);
CGMutablePathRef path = CGPathCreateMutable();
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 (!straightCorner || ![straightCorner containsObject:[NSNumber numberWithUnsignedInteger:vertex.count - 1]]) {
target.x += sign(diff.x/beta)*beta;
target.y += sign(diff.y/beta)*beta;
}
CGPathMoveToPoint(path, NULL, target.x, target.y);
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;
if (straightCorner && [straightCorner containsObject:[NSNumber numberWithUnsignedInteger:i]]) {
CGPathAddLineToPoint(path, NULL, target.x, target.y);
} else {
control1 = point;
diff = NSMakePoint(point.x - previousPoint.x, point.y - previousPoint.y);
target.x -= sign(diff.x/beta)*beta;
control1.x -= sign(diff.x/beta)*alpha;
target.y -= sign(diff.y/beta)*beta;
control1.y -= sign(diff.y/beta)*alpha;
CGPathAddLineToPoint(path, NULL, target.x, target.y);
target = point;
control2 = point;
diff = NSMakePoint(nextPoint.x - point.x, nextPoint.y - point.y);
control2.x += sign(diff.x/beta)*alpha;
target.x += sign(diff.x/beta)*beta;
control2.y += sign(diff.y/beta)*alpha;
target.y += sign(diff.y/beta)*beta;
CGPathAddCurveToPoint(path, NULL, control1.x, control1.y, control2.x, control2.y, target.x, target.y);
}
}
CGPathCloseSubpath(path);
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))
];
}
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:(NSTextRange *)range leadingRect:(NSRect *)leadingRect bodyRect:(NSRect *)bodyRect trailingRect:(NSRect *)trailingRect extraSurounding:(CGFloat)extraSurounding bounds:(NSRect)bounds {
NSSize edgeInset = self.currentTheme.edgeInset;
NSMutableArray<NSValue *> *lineRects = [[NSMutableArray alloc] init];
[self.layoutManager enumerateTextSegmentsInRange:range type:NSTextLayoutManagerSegmentTypeStandard options:NSTextLayoutManagerSegmentOptionsRangeNotRequired usingBlock:^(NSTextRange *_, CGRect rect, CGFloat baseline, NSTextContainer *tectContainer) {
if (!nearEmptyRect(rect)) {
NSRect newRect = rect;
newRect.origin.x += edgeInset.width;
newRect.origin.y += edgeInset.height;
newRect.size.height += self.currentTheme.linespace;
newRect.origin.y -= self.currentTheme.linespace / 2;
[lineRects addObject:[NSValue valueWithRect:newRect]];
}
return YES;
}];
*leadingRect = NSZeroRect;
*bodyRect = NSZeroRect;
*trailingRect = NSZeroRect;
if (lineRects.count == 1) {
*bodyRect = [lineRects[0] rectValue];
} else if (lineRects.count == 2) {
*leadingRect = [lineRects[0] rectValue];
*trailingRect = [lineRects[1] rectValue];
} else if (lineRects.count > 2) {
*leadingRect = [lineRects[0] rectValue];
*trailingRect = [lineRects[lineRects.count-1] rectValue];
CGFloat x0 = CGFLOAT_MAX;
CGFloat x1 = CGFLOAT_MIN;
CGFloat y0 = CGFLOAT_MAX;
CGFloat y1 = CGFLOAT_MIN;
for (NSUInteger i = 1; i < lineRects.count-1; i += 1) {
NSRect rect = [lineRects[i] rectValue];
x0 = MIN(NSMinX(rect), x0);
x1 = MAX(NSMaxX(rect), x1);
y0 = MIN(NSMinY(rect), y0);
y1 = MAX(NSMaxY(rect), y1);
}
y0 = MIN(NSMaxY(*leadingRect), y0);
y1 = MAX(NSMinY(*trailingRect), y1);
*bodyRect = NSMakeRect(x0, y0, x1-x0, y1-y0);
}
if (extraSurounding > 0) {
if (nearEmptyRect(*leadingRect) && nearEmptyRect(*trailingRect)) {
expandHighlightWidth(bodyRect, extraSurounding);
} else {
if (!(nearEmptyRect(*leadingRect))) {
expandHighlightWidth(leadingRect, extraSurounding);
}
if (!(nearEmptyRect(*trailingRect))) {
expandHighlightWidth(trailingRect, extraSurounding);
}
}
}
if (!nearEmptyRect(*leadingRect) && !nearEmptyRect(*trailingRect)) {
leadingRect->size.width = NSMaxX(bounds) - leadingRect->origin.x;
trailingRect->size.width = NSMaxX(*trailingRect) - NSMinX(bounds);
trailingRect->origin.x = NSMinX(bounds);
if (!nearEmptyRect(*bodyRect)) {
bodyRect->size.width = bounds.size.width;
bodyRect->origin.x = bounds.origin.x;
} else {
CGFloat diff = NSMinY(*trailingRect) - NSMaxY(*leadingRect);
leadingRect->size.height += diff / 2;
trailingRect->size.height += diff / 2;
trailingRect->origin.y -= diff / 2;
}
}
}
// 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], bodyVertex[1], bodyVertex[2], leadingVertex[3], leadingVertex[0], leadingVertex[1]];
} else if (nearEmptyRect(leadingRect) && !nearEmptyRect(bodyRect)) {
NSArray<NSValue *> * trailingVertex = rectVertex(trailingRect);
NSArray<NSValue *> * bodyVertex = rectVertex(bodyRect);
return @[trailingVertex[1], trailingVertex[2], trailingVertex[3], bodyVertex[2], bodyVertex[3], bodyVertex[0]];
} 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], trailingVertex[1], trailingVertex[2], trailingVertex[3], leadingVertex[2], leadingVertex[3], leadingVertex[0], leadingVertex[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 @[trailingVertex[1], trailingVertex[2], trailingVertex[3], bodyVertex[2], leadingVertex[3], leadingVertex[0], leadingVertex[1], bodyVertex[0]];
} 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)];
}
}
CGPoint direction(CGPoint diff) {
if (diff.y == 0 && diff.x > 0) {
return NSMakePoint(0, 1);
} else if (diff.y == 0 && diff.x < 0) {
return NSMakePoint(0, -1);
} else if (diff.x == 0 && diff.y > 0) {
return NSMakePoint(-1, 0);
} else if (diff.x == 0 && diff.y < 0) {
return NSMakePoint(1, 0);
} else {
return NSMakePoint(0, 0);
}
}
CAShapeLayer *shapeFromPath(CGPathRef path) {
CAShapeLayer *layer = [CAShapeLayer layer];
layer.path = path;
layer.fillRule = kCAFillRuleEvenOdd;
return layer;
}
// Assumes clockwise iteration
void enlarge(NSMutableArray<NSValue *> *vertex, CGFloat by) {
if (by != 0) {
NSPoint previousPoint;
NSPoint point;
NSPoint nextPoint;
NSArray<NSValue *> *original = [[NSArray alloc] initWithArray:vertex];
NSPoint newPoint;
NSPoint displacement;
for (NSUInteger i = 0; i < original.count; i += 1){
previousPoint = [original[(original.count+i-1)%original.count] pointValue];
point = [original[i] pointValue];
nextPoint = [original[(i+1)%original.count] pointValue];
newPoint = point;
displacement = direction(NSMakePoint(point.x - previousPoint.x, point.y - previousPoint.y));
newPoint.x += by * displacement.x;
newPoint.y += by * displacement.y;
displacement = direction(NSMakePoint(nextPoint.x - point.x, nextPoint.y - point.y));
newPoint.x += by * displacement.x;
newPoint.y += by * displacement.y;
[vertex replaceObjectAtIndex:i withObject:@(newPoint)];
}
}
}
// Add gap between horizontal candidates
void expandHighlightWidth(NSRect *rect, CGFloat extraSurrounding) {
if (!nearEmptyRect(*rect)) {
rect->size.width += extraSurrounding;
rect->origin.x -= extraSurrounding / 2;
}
}
void removeCorner(NSMutableArray<NSValue *> *highlightedPoints, NSMutableSet<NSNumber *> *rightCorners, NSRect containingRect) {
if (highlightedPoints && rightCorners) {
NSSet<NSNumber *> *originalRightCorners = [[NSSet<NSNumber *> alloc] initWithSet:rightCorners];
for (NSNumber *cornerIndex in originalRightCorners) {
NSUInteger index = cornerIndex.unsignedIntegerValue;
NSPoint corner = [highlightedPoints[index] pointValue];
CGFloat dist = MIN(NSMaxY(containingRect) - corner.y, corner.y - NSMinY(containingRect));
if (dist < 1e-2) {
[rightCorners removeObject:cornerIndex];
}
}
}
}
- (void) linearMultilineForRect:(NSRect)bodyRect leadingRect:(NSRect)leadingRect trailingRect:(NSRect)trailingRect points1:(NSMutableArray<NSValue *> **)highlightedPoints points2:(NSMutableArray<NSValue *> **)highlightedPoints2 rightCorners:(NSMutableSet<NSNumber *> **)rightCorners rightCorners2:(NSMutableSet<NSNumber *> **)rightCorners2 {
// 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];
*rightCorners = [[NSMutableSet<NSNumber *> alloc] initWithObjects:@(2), @(3), nil];
*rightCorners2 = [[NSMutableSet<NSNumber *> alloc] initWithObjects:@(0), @(1), nil];
} else {
*highlightedPoints = [multilineRectVertex(leadingRect, bodyRect, trailingRect) mutableCopy];
}
}
- (CGPathRef)drawHighlightedWith:(SquirrelTheme *)theme highlightedRange:(NSRange)highlightedRange backgroundRect:(NSRect)backgroundRect preeditRect:(NSRect)preeditRect containingRect:(NSRect)containingRect extraExpansion:(CGFloat)extraExpansion {
NSRect currentContainingRect = containingRect;
currentContainingRect.size.width += extraExpansion * 2;
currentContainingRect.size.height += extraExpansion * 2;
currentContainingRect.origin.x -= extraExpansion;
currentContainingRect.origin.y -= extraExpansion;
CGFloat halfLinespace = theme.linespace / 2;
NSRect innerBox = backgroundRect;
innerBox.size.width -= (theme.edgeInset.width + 1) * 2 - 2 * extraExpansion;
innerBox.origin.x += theme.edgeInset.width + 1 - extraExpansion;
innerBox.size.height += 2 * extraExpansion;
innerBox.origin.y -= extraExpansion;
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;
}
innerBox.size.height -= theme.linespace;
innerBox.origin.y += halfLinespace;
NSRect outerBox = backgroundRect;
outerBox.size.height -= preeditRect.size.height + MAX(0, theme.hilitedCornerRadius + theme.borderWidth) - 2 * extraExpansion;
outerBox.size.width -= MAX(0, theme.hilitedCornerRadius + theme.borderWidth) - 2 * extraExpansion;
outerBox.origin.x += MAX(0, theme.hilitedCornerRadius + theme.borderWidth) / 2 - extraExpansion;
outerBox.origin.y += preeditRect.size.height + MAX(0, theme.hilitedCornerRadius + theme.borderWidth) / 2 - extraExpansion;
double effectiveRadius = MAX(0, theme.hilitedCornerRadius + 2 * extraExpansion / theme.hilitedCornerRadius * MAX(0, theme.cornerRadius - theme.hilitedCornerRadius));
CGMutablePathRef path = CGPathCreateMutable();
if (theme.linear){
NSRect leadingRect;
NSRect bodyRect;
NSRect trailingRect;
[self multilineRectForRange:[self convertRange:highlightedRange] leadingRect:&leadingRect bodyRect:&bodyRect trailingRect:&trailingRect extraSurounding:_seperatorWidth bounds:outerBox];
NSMutableArray<NSValue *> *highlightedPoints;
NSMutableArray<NSValue *> *highlightedPoints2;
NSMutableSet<NSNumber *> *rightCorners;
NSMutableSet<NSNumber *> *rightCorners2;
[self linearMultilineForRect:bodyRect leadingRect:leadingRect trailingRect:trailingRect points1:&highlightedPoints points2:&highlightedPoints2 rightCorners:&rightCorners rightCorners2:&rightCorners2];
// Expand the boxes to reach proper border
enlarge(highlightedPoints, extraExpansion);
expand(highlightedPoints, innerBox, outerBox);
removeCorner(highlightedPoints, rightCorners, currentContainingRect);
path = drawSmoothLines(highlightedPoints, rightCorners, 0.3*effectiveRadius, 1.4*effectiveRadius);
if (highlightedPoints2.count > 0) {
enlarge(highlightedPoints2, extraExpansion);
expand(highlightedPoints2, innerBox, outerBox);
removeCorner(highlightedPoints2, rightCorners2, currentContainingRect);
CGPathRef path2 = drawSmoothLines(highlightedPoints2, rightCorners2, 0.3*effectiveRadius, 1.4*effectiveRadius);
CGPathAddPath(path, NULL, path2);
}
} else {
NSRect highlightedRect = [self contentRectForRange:[self convertRange:highlightedRange]];
if (!nearEmptyRect(highlightedRect)) {
highlightedRect.size.width = backgroundRect.size.width;
highlightedRect.size.height += theme.linespace;
highlightedRect.origin = NSMakePoint(backgroundRect.origin.x, highlightedRect.origin.y + theme.edgeInset.height - halfLinespace);
if (NSMaxRange(highlightedRange) == _textView.string.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];
enlarge(highlightedPoints, extraExpansion);
expand(highlightedPoints, innerBox, outerBox);
path = drawSmoothLines(highlightedPoints, nil, 0.3*effectiveRadius, 1.4*effectiveRadius);
}
}
return path;
}
- (NSRect)carveInset:(NSRect)rect theme:(SquirrelTheme *)theme {
NSRect newRect = rect;
newRect.size.height -= (theme.hilitedCornerRadius + theme.borderWidth) * 2;
newRect.size.width -= (theme.hilitedCornerRadius + theme.borderWidth) * 2;
newRect.origin.x += theme.hilitedCornerRadius + theme.borderWidth;
newRect.origin.y += theme.hilitedCornerRadius + theme.borderWidth;
return newRect;
}
// All draws happen here
- (void)drawRect:(NSRect)dirtyRect {
CGPathRef backgroundPath = CGPathCreateMutable();
CGPathRef highlightedPath = CGPathCreateMutable();
CGMutablePathRef candidatePaths = CGPathCreateMutable();
CGMutablePathRef highlightedPreeditPath = CGPathCreateMutable();
CGPathRef preeditPath = CGPathCreateMutable();
SquirrelTheme * theme = self.currentTheme;
NSPoint textFieldOrigin = dirtyRect.origin;
textFieldOrigin.y += theme.edgeInset.height;
textFieldOrigin.x += theme.edgeInset.width;
// Draw preedit Rect
NSRect backgroundRect = dirtyRect;
NSRect containingRect = dirtyRect;
// Draw preedit Rect
NSRect preeditRect = NSZeroRect;
if (_preeditRange.length > 0) {
preeditRect = [self contentRectForRange:[self convertRange:_preeditRange]];
if (!nearEmptyRect(preeditRect)) {
preeditRect.size.width = backgroundRect.size.width;
preeditRect.size.height += theme.edgeInset.height + theme.preeditLinespace / 2 + theme.hilitedCornerRadius / 2;
preeditRect.origin = backgroundRect.origin;
if (_candidateRanges.count == 0) {
preeditRect.size.height += theme.edgeInset.height - theme.preeditLinespace / 2 - theme.hilitedCornerRadius / 2;
}
containingRect.size.height -= preeditRect.size.height;
containingRect.origin.y += preeditRect.size.height;
if (theme.preeditBackgroundColor != nil) {
preeditPath = drawSmoothLines(rectVertex(preeditRect), nil, 0, 0);
}
}
}
containingRect = [self carveInset:containingRect theme:theme];
// Draw highlighted Rect
for (NSUInteger i = 0; i < _candidateRanges.count; i += 1) {
NSRange candidateRange = [_candidateRanges[i] rangeValue];
if (i == _hilightedIndex) {
// Draw highlighted Rect
if (candidateRange.length > 0 && theme.highlightedBackColor != nil) {
highlightedPath = [self drawHighlightedWith:theme highlightedRange:candidateRange backgroundRect:backgroundRect preeditRect:preeditRect containingRect:containingRect extraExpansion:0];
}
} else {
// Draw other highlighted Rect
if (candidateRange.length > 0 && theme.candidateBackColor != nil) {
CGPathRef candidatePath = [self drawHighlightedWith:theme highlightedRange:candidateRange backgroundRect:backgroundRect preeditRect:preeditRect containingRect:containingRect extraExpansion:theme.surroundingExtraExpansion];
CGPathAddPath(candidatePaths, NULL, candidatePath);
}
}
}
// Draw highlighted part of preedit text
if (_highlightedPreeditRange.length > 0 && theme.highlightedPreeditColor != nil) {
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 (_candidateRanges.count == 0) {
innerBox.size.height -= (theme.edgeInset.height + 1) * 2;
} else {
innerBox.size.height -= theme.edgeInset.height + theme.preeditLinespace / 2 + theme.hilitedCornerRadius / 2 + 2;
}
NSRect outerBox = preeditRect;
outerBox.size.height -= MAX(0, theme.hilitedCornerRadius + theme.borderWidth);
outerBox.size.width -= MAX(0, theme.hilitedCornerRadius + theme.borderWidth);
outerBox.origin.x += MAX(0, theme.hilitedCornerRadius + theme.borderWidth) / 2;
outerBox.origin.y += MAX(0, theme.hilitedCornerRadius + theme.borderWidth) / 2;
NSRect leadingRect;
NSRect bodyRect;
NSRect trailingRect;
[self multilineRectForRange:[self convertRange:_highlightedPreeditRange] leadingRect:&leadingRect bodyRect:&bodyRect trailingRect:&trailingRect extraSurounding:0 bounds:outerBox];
NSMutableArray<NSValue *> *highlightedPreeditPoints;
NSMutableArray<NSValue *> *highlightedPreeditPoints2;
NSMutableSet<NSNumber *> *rightCorners;
NSMutableSet<NSNumber *> *rightCorners2;
[self linearMultilineForRect:bodyRect leadingRect:leadingRect trailingRect:trailingRect points1:&highlightedPreeditPoints points2:&highlightedPreeditPoints2 rightCorners:&rightCorners rightCorners2:&rightCorners2];
containingRect = [self carveInset:preeditRect theme:theme];
expand(highlightedPreeditPoints, innerBox, outerBox);
removeCorner(highlightedPreeditPoints, rightCorners, containingRect);
highlightedPreeditPath = drawSmoothLines(highlightedPreeditPoints, rightCorners, 0.3*theme.hilitedCornerRadius, 1.4*theme.hilitedCornerRadius);
if (highlightedPreeditPoints2.count > 0) {
expand(highlightedPreeditPoints2, innerBox, outerBox);
removeCorner(highlightedPreeditPoints2, rightCorners2, containingRect);
CGPathRef highlightedPreeditPath2 = drawSmoothLines(highlightedPreeditPoints2, rightCorners2, 0.3*theme.hilitedCornerRadius, 1.4*theme.hilitedCornerRadius);
CGPathAddPath(highlightedPreeditPath, NULL, highlightedPreeditPath2);
}
}
[NSBezierPath setDefaultLineWidth:0];
backgroundPath = drawSmoothLines(rectVertex(backgroundRect), nil, theme.cornerRadius*0.3, theme.cornerRadius*1.4);
_shape.path = CGPathCreateMutableCopy(backgroundPath);
[self.layer setSublayers: NULL];
CGMutablePathRef backPath = CGPathCreateMutableCopy(backgroundPath);
if (!CGPathIsEmpty(preeditPath)) {
CGPathAddPath(backPath, NULL, preeditPath);
}
if (theme.mutualExclusive) {
if (!CGPathIsEmpty(highlightedPath)) {
CGPathAddPath(backPath, NULL, highlightedPath);
}
if (!CGPathIsEmpty(candidatePaths)) {
CGPathAddPath(backPath, NULL, candidatePaths);
}
}
CAShapeLayer *panelLayer = shapeFromPath(backPath);
panelLayer.fillColor = theme.backgroundColor.CGColor;
CAShapeLayer *panelLayerMask = shapeFromPath(backgroundPath);
panelLayer.mask = panelLayerMask;
[self.layer addSublayer: panelLayer];
if (theme.preeditBackgroundColor && !CGPathIsEmpty(preeditPath)) {
CAShapeLayer *layer = shapeFromPath(preeditPath);
layer.fillColor = theme.preeditBackgroundColor.CGColor;
CGMutablePathRef maskPath = CGPathCreateMutableCopy(backgroundPath);
if (theme.mutualExclusive && !CGPathIsEmpty(highlightedPreeditPath)) {
CGPathAddPath(maskPath, NULL, highlightedPreeditPath);
}
CAShapeLayer *mask = shapeFromPath(maskPath);
layer.mask = mask;
[panelLayer addSublayer: layer];
}
if (theme.borderWidth > 0 && theme.borderColor) {
CAShapeLayer *borderLayer = shapeFromPath(backgroundPath);
borderLayer.lineWidth = theme.borderWidth * 2;
borderLayer.strokeColor = theme.borderColor.CGColor;
borderLayer.fillColor = NULL;
[panelLayer addSublayer: borderLayer];
}
if (theme.highlightedPreeditColor && !CGPathIsEmpty(highlightedPreeditPath)) {
CAShapeLayer *layer = shapeFromPath(highlightedPreeditPath);
layer.fillColor = theme.highlightedPreeditColor.CGColor;
[panelLayer addSublayer: layer];
}
if (theme.candidateBackColor && !CGPathIsEmpty(candidatePaths)) {
CAShapeLayer *layer = shapeFromPath(candidatePaths);
layer.fillColor = theme.candidateBackColor.CGColor;
[panelLayer addSublayer: layer];
}
if (theme.highlightedBackColor && !CGPathIsEmpty(highlightedPath)) {
CAShapeLayer *layer = shapeFromPath(highlightedPath);
layer.fillColor = theme.highlightedBackColor.CGColor;
if (theme.shadowSize > 0) {
CAShapeLayer *shadowLayer = [CAShapeLayer layer];
shadowLayer.shadowColor = NSColor.blackColor.CGColor;
shadowLayer.shadowOffset = NSMakeSize(theme.shadowSize/2, (theme.vertical ? -1 : 1) * theme.shadowSize/2);
shadowLayer.shadowPath = highlightedPath;
shadowLayer.shadowRadius = theme.shadowSize;
shadowLayer.shadowOpacity = 0.2;
CGMutablePathRef maskPath = CGPathCreateMutableCopy(backgroundPath);
CGPathAddPath(maskPath, NULL, highlightedPath);
if (!CGPathIsEmpty(preeditPath)) {
CGPathAddPath(maskPath, NULL, preeditPath);
}
CAShapeLayer *shadowLayerMask = shapeFromPath(maskPath);
shadowLayer.mask = shadowLayerMask;
layer.strokeColor = [NSColor.blackColor colorWithAlphaComponent:0.15].CGColor;
layer.lineWidth = 0.5;
[layer addSublayer: shadowLayer];
}
[panelLayer addSublayer: layer];
}
[_textView setTextContainerInset:NSMakeSize(textFieldOrigin.x, textFieldOrigin.y)];
}
- (BOOL)clickAtPoint:(NSPoint)_point index:(NSInteger *)_index {
if (CGPathContainsPoint(_shape.path, nil, _point, NO)) {
NSPoint point = NSMakePoint(_point.x - self.textView.textContainerInset.width,
_point.y - self.textView.textContainerInset.height);
NSTextLayoutFragment *fragment = [self.layoutManager textLayoutFragmentForPosition:point];
if (fragment) {
point = NSMakePoint(point.x - NSMinX(fragment.layoutFragmentFrame),
point.y - NSMinY(fragment.layoutFragmentFrame));
NSInteger index = [self.layoutManager offsetFromLocation: self.layoutManager.documentRange.location toLocation: fragment.rangeInElement.location];
for (NSUInteger i = 0; i < fragment.textLineFragments.count; i += 1) {
NSTextLineFragment *lineFragment = fragment.textLineFragments[i];
if (CGRectContainsPoint(lineFragment.typographicBounds, point)) {
point = NSMakePoint(point.x - NSMinX(lineFragment.typographicBounds),
point.y - NSMinY(lineFragment.typographicBounds));
index += [lineFragment characterIndexForPoint:point];
for (NSUInteger i = 0; i < _candidateRanges.count; i += 1) {
NSRange range = [_candidateRanges[i] rangeValue];
if (index >= range.location && index < NSMaxRange(range)) {
*_index = i;
break;
}
}
break;
}
}
}
return YES;
} else {
return NO;
}
}
@end
@implementation SquirrelPanel {
SquirrelView *_view;
NSVisualEffectView *_back;
NSRect _screenRect;
CGFloat _maxHeight;
NSString *_statusMessage;
NSTimer *_statusTimer;
NSString *_preedit;
NSRange _selRange;
NSUInteger _caretPos;
NSArray *_candidates;
NSArray *_comments;
NSArray *_labels;
NSUInteger _index;
NSUInteger _cursorIndex;
NSPoint _scrollDirection;
NSDate *_scrollTime;
}
- (BOOL)linear {
return _view.currentTheme.linear;
}
- (BOOL)vertical {
return _view.currentTheme.vertical;
}
- (BOOL)inlinePreedit {
return _view.currentTheme.inlinePreedit;
}
- (BOOL)inlineCandidate {
return _view.currentTheme.inlineCandidate;
}
NSAttributedString *insert(NSString *separator, NSAttributedString *betweenText) {
NSRange range = [betweenText.string rangeOfComposedCharacterSequenceAtIndex:0];
NSAttributedString *attributedSeperator = [[NSAttributedString alloc] initWithString:separator attributes:[betweenText attributesAtIndex:0 effectiveRange:nil]];
NSUInteger i = NSMaxRange(range);
NSMutableAttributedString *workingString = [[betweenText attributedSubstringFromRange:range] mutableCopy];
while (i < betweenText.length) {
range = [betweenText.string rangeOfComposedCharacterSequenceAtIndex:i];
[workingString appendAttributedString:attributedSeperator];
[workingString appendAttributedString:[betweenText attributedSubstringFromRange:range]];
i = NSMaxRange(range);
}
return workingString;
}
+ (NSColor *)secondaryTextColor {
return [NSColor secondaryLabelColor];
}
- (void)initializeUIStyleForDarkMode:(BOOL)isDark {
SquirrelTheme *theme = [_view selectTheme:isDark];
theme.native = YES;
theme.memorizeSize = 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
highlightedAttrs:highlightedAttrs
labelAttrs:labelAttrs
labelHighlightedAttrs:labelHighlightedAttrs
commentAttrs:commentAttrs
commentHighlightedAttrs:commentHighlightedAttrs
preeditAttrs:preeditAttrs
preeditHighlightedAttrs:preeditHighlightedAttrs];
[theme setParagraphStyle:paragraphStyle