forked from macvim-dev/macvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMMHoverButton.m
More file actions
169 lines (153 loc) · 5.31 KB
/
MMHoverButton.m
File metadata and controls
169 lines (153 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#import "MMHoverButton.h"
@implementation MMHoverButton
{
NSTrackingArea *_trackingArea;
NSBox *_circle;
}
+ (NSImage *)imageFromType:(MMHoverButtonImage)imageType
{
if (imageType >= MMHoverButtonImageCount)
return nil;
CGFloat size = imageType == MMHoverButtonImageCloseTab ? 15 : 17;
static __weak NSImage *imageCache[MMHoverButtonImageCount] = { nil };
if (imageCache[imageType] != nil)
return imageCache[imageType];
BOOL (^drawFuncs[MMHoverButtonImageCount])(NSRect) = {
// AddTab
^BOOL(NSRect dstRect) {
NSBezierPath *p = [NSBezierPath new];
[p moveToPoint:NSMakePoint( 8.5, 4.5)];
[p lineToPoint:NSMakePoint( 8.5, 12.5)];
[p moveToPoint:NSMakePoint( 4.5, 8.5)];
[p lineToPoint:NSMakePoint(12.5, 8.5)];
[p setLineWidth:1.2];
[p stroke];
return YES;
},
// CloseTab
^BOOL(NSRect dstRect) {
NSBezierPath *p = [NSBezierPath new];
[p moveToPoint:NSMakePoint( 4.5, 4.5)];
[p lineToPoint:NSMakePoint(10.5, 10.5)];
[p moveToPoint:NSMakePoint( 4.5, 10.5)];
[p lineToPoint:NSMakePoint(10.5, 4.5)];
[p setLineWidth:1.2];
[p stroke];
return YES;
},
// ScrollLeft
^BOOL(NSRect dstRect) {
NSBezierPath *p = [NSBezierPath new];
[p moveToPoint:NSMakePoint( 5.0, 8.5)];
[p lineToPoint:NSMakePoint(10.0, 4.5)];
[p lineToPoint:NSMakePoint(10.0, 12.5)];
[p fill];
return YES;
},
// ScrollRight
^BOOL(NSRect dstRect) {
NSBezierPath *p = [NSBezierPath new];
[p moveToPoint:NSMakePoint(12.0, 8.5)];
[p lineToPoint:NSMakePoint( 7.0, 4.5)];
[p lineToPoint:NSMakePoint( 7.0, 12.5)];
[p fill];
return YES;
}
};
NSImage *img = [NSImage imageWithSize:NSMakeSize(size, size)
flipped:NO
drawingHandler:drawFuncs[imageType]];
imageCache[imageType] = img;
return img;
}
- (instancetype)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
if (self) {
self.buttonType = NSButtonTypeMomentaryChange;
self.bordered = NO;
self.imagePosition = NSImageOnly;
// This view will fade in/out when hovered.
_circle = [NSBox new];
_circle.boxType = NSBoxCustom;
_circle.borderWidth = 0;
_circle.alphaValue = 0.16;
_circle.fillColor = NSColor.clearColor;
_circle.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
_circle.frame = self.bounds;
[self addSubview:_circle positioned:NSWindowBelow relativeTo:nil];
}
return self;
}
- (void)setFgColor:(NSColor *)color
{
_fgColor = color;
self.image = super.image;
}
- (void)setImage:(NSImage *)imageTemplate
{
_circle.cornerRadius = imageTemplate.size.width / 2.0;
NSColor *fillColor = self.fgColor ?: NSColor.controlTextColor;
NSImage *image = [NSImage imageWithSize:imageTemplate.size
flipped:NO
drawingHandler:^BOOL(NSRect dstRect) {
[imageTemplate drawInRect:dstRect];
[fillColor set];
NSRectFillUsingOperation(dstRect, NSCompositingOperationSourceAtop);
return YES;
}];
NSImage *alternateImage = [NSImage imageWithSize:imageTemplate.size
flipped:NO
drawingHandler:^BOOL(NSRect dstRect) {
[[fillColor colorWithAlphaComponent:0.2] set];
[[NSBezierPath bezierPathWithOvalInRect:dstRect] fill];
[image drawInRect:dstRect];
return YES;
}];
super.image = image;
self.alternateImage = alternateImage;
}
- (void)setEnabled:(BOOL)enabled
{
[super setEnabled:enabled];
[self evaluateHover];
}
- (void)updateTrackingAreas
{
[self removeTrackingArea:_trackingArea];
_trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds options:(NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow) owner:self userInfo:nil];
[self addTrackingArea:_trackingArea];
[self evaluateHover];
[super updateTrackingAreas];
}
- (void)backgroundCircleShouldHighlight:(BOOL)shouldHighlight
{
NSColor *fillColor = NSColor.clearColor;
if (shouldHighlight) {
fillColor = self.fgColor ?: NSColor.controlTextColor;
}
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0.1];
_circle.animator.fillColor = fillColor;
[NSAnimationContext endGrouping];
}
- (void)evaluateHover
{
NSPoint mouseLocation = [self.window mouseLocationOutsideOfEventStream];
mouseLocation = [self convertPoint:mouseLocation fromView:nil];
if (NSPointInRect(mouseLocation, self.bounds)) {
if (self.enabled) [self backgroundCircleShouldHighlight:YES];
else [self backgroundCircleShouldHighlight:NO];
} else {
[self backgroundCircleShouldHighlight:NO];
}
}
- (void)mouseEntered:(NSEvent *)event
{
if (self.enabled) [self backgroundCircleShouldHighlight:YES];
}
- (void)mouseExited:(NSEvent *)event
{
[self backgroundCircleShouldHighlight:NO];
}
@end