@@ -6,41 +6,64 @@ @implementation MMHoverButton
6
6
NSBox *_circle;
7
7
}
8
8
9
- + (NSImage *)imageNamed : ( NSString *) name
9
+ + (NSImage *)imageFromType : (MMHoverButtonImage) imageType
10
10
{
11
- CGFloat size = [name isEqualToString: @" CloseTabButton" ] ? 15 : 17 ;
12
- return [NSImage imageWithSize: NSMakeSize (size, size) flipped: NO drawingHandler: ^BOOL (NSRect dstRect) {
13
- NSBezierPath *p = [NSBezierPath new ];
14
- if ([name isEqualToString: @" AddTabButton" ]) {
11
+ if (imageType >= MMHoverButtonImageCount)
12
+ return nil ;
13
+
14
+ CGFloat size = imageType == MMHoverButtonImageCloseTab ? 15 : 17 ;
15
+
16
+ static __weak NSImage *imageCache[MMHoverButtonImageCount] = { nil };
17
+ if (imageCache[imageType] != nil )
18
+ return imageCache[imageType];
19
+
20
+ BOOL (^drawFuncs[MMHoverButtonImageCount])(NSRect ) = {
21
+ // AddTab
22
+ ^BOOL (NSRect dstRect) {
23
+ NSBezierPath *p = [NSBezierPath new ];
15
24
[p moveToPoint: NSMakePoint ( 8.5 , 4.5 )];
16
25
[p lineToPoint: NSMakePoint ( 8.5 , 12.5 )];
17
26
[p moveToPoint: NSMakePoint ( 4.5 , 8.5 )];
18
27
[p lineToPoint: NSMakePoint (12.5 , 8.5 )];
19
28
[p setLineWidth: 1.2 ];
20
29
[p stroke ];
21
- }
22
- else if ([name isEqualToString: @" CloseTabButton" ]) {
30
+ return YES ;
31
+ },
32
+ // CloseTab
33
+ ^BOOL (NSRect dstRect) {
34
+ NSBezierPath *p = [NSBezierPath new ];
23
35
[p moveToPoint: NSMakePoint ( 4.5 , 4.5 )];
24
36
[p lineToPoint: NSMakePoint (10.5 , 10.5 )];
25
37
[p moveToPoint: NSMakePoint ( 4.5 , 10.5 )];
26
38
[p lineToPoint: NSMakePoint (10.5 , 4.5 )];
27
39
[p setLineWidth: 1.2 ];
28
40
[p stroke ];
29
- }
30
- else if ([name isEqualToString: @" ScrollLeftButton" ]) {
41
+ return YES ;
42
+ },
43
+ // ScrollLeft
44
+ ^BOOL (NSRect dstRect) {
45
+ NSBezierPath *p = [NSBezierPath new ];
31
46
[p moveToPoint: NSMakePoint ( 5.0 , 8.5 )];
32
47
[p lineToPoint: NSMakePoint (10.0 , 4.5 )];
33
48
[p lineToPoint: NSMakePoint (10.0 , 12.5 )];
34
49
[p fill ];
35
- }
36
- else if ([name isEqualToString: @" ScrollRightButton" ]) {
50
+ return YES ;
51
+ },
52
+ // ScrollRight
53
+ ^BOOL (NSRect dstRect) {
54
+ NSBezierPath *p = [NSBezierPath new ];
37
55
[p moveToPoint: NSMakePoint (12.0 , 8.5 )];
38
56
[p lineToPoint: NSMakePoint ( 7.0 , 4.5 )];
39
57
[p lineToPoint: NSMakePoint ( 7.0 , 12.5 )];
40
58
[p fill ];
59
+ return YES ;
41
60
}
42
- return YES ;
43
- }];
61
+ };
62
+ NSImage *img = [NSImage imageWithSize: NSMakeSize (size, size)
63
+ flipped: NO
64
+ drawingHandler: drawFuncs[imageType]];
65
+ imageCache[imageType] = img;
66
+ return img;
44
67
}
45
68
46
69
- (instancetype )initWithFrame : (NSRect )frameRect
@@ -70,22 +93,28 @@ - (void)setFgColor:(NSColor *)color
70
93
self.image = super.image ;
71
94
}
72
95
73
- - (void )setImage : (NSImage *)image
96
+ - (void )setImage : (NSImage *)imageTemplate
74
97
{
75
- _circle.cornerRadius = image .size .width / 2.0 ;
98
+ _circle.cornerRadius = imageTemplate .size .width / 2.0 ;
76
99
NSColor *fillColor = self.fgColor ?: NSColor .controlTextColor ;
77
- super.image = [NSImage imageWithSize: image.size flipped: NO drawingHandler: ^BOOL (NSRect dstRect) {
78
- [image drawInRect: dstRect];
100
+ NSImage *image = [NSImage imageWithSize: imageTemplate.size
101
+ flipped: NO
102
+ drawingHandler: ^BOOL (NSRect dstRect) {
103
+ [imageTemplate drawInRect: dstRect];
79
104
[fillColor set ];
80
105
NSRectFillUsingOperation (dstRect, NSCompositingOperationSourceAtop);
81
106
return YES ;
82
107
}];
83
- self.alternateImage = [NSImage imageWithSize: image.size flipped: NO drawingHandler: ^BOOL (NSRect dstRect) {
108
+ NSImage *alternateImage = [NSImage imageWithSize: imageTemplate.size
109
+ flipped: NO
110
+ drawingHandler: ^BOOL (NSRect dstRect) {
84
111
[[fillColor colorWithAlphaComponent: 0.2 ] set ];
85
112
[[NSBezierPath bezierPathWithOvalInRect: dstRect] fill ];
86
- [super . image drawInRect: dstRect];
113
+ [image drawInRect: dstRect];
87
114
return YES ;
88
115
}];
116
+ super.image = image;
117
+ self.alternateImage = alternateImage;
89
118
}
90
119
91
120
- (void )setEnabled : (BOOL )enabled
0 commit comments