Skip to content

Commit f4680b8

Browse files
committed
Tabs: Fix MMHoverButton poor usage of images and potential stack overflow
Fix hover buttons to not keep setting image on itself everytime a fg color is changed, leading to each image referring to the last one. If a display changed happens and macOS triggers a redraw, this could sometimes lead to a stack overflow crash. Just simplify the code and properly separate out the image template and the derived images. This also makes sure we properly free the old images when we change fg color where we discard the last image and make a new one.
1 parent 9b30caf commit f4680b8

4 files changed

Lines changed: 7 additions & 14 deletions

File tree

src/MacVim/MMTabline/MMHoverButton.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
@interface MMHoverButton : NSButton
66

77
@property (nonatomic, retain) NSColor *fgColor;
8+
@property (nonatomic, retain) NSImage *imageTemplate;
89

910
typedef enum : NSUInteger {
1011
MMHoverButtonImageAddTab = 0,

src/MacVim/MMTabline/MMHoverButton.m

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,12 @@ - (instancetype)initWithFrame:(NSRect)frameRect
9090
- (void)setFgColor:(NSColor *)color
9191
{
9292
_fgColor = color;
93-
self.image = super.image;
93+
[self setImageTemplate:_imageTemplate];
9494
}
9595

96-
- (void)setImage:(NSImage *)imageTemplate
96+
- (void)setImageTemplate:(NSImage *)imageTemplate
9797
{
98+
_imageTemplate = imageTemplate;
9899
_circle.cornerRadius = imageTemplate.size.width / 2.0;
99100
NSColor *fillColor = self.fgColor ?: NSColor.controlTextColor;
100101
NSImage *image = [NSImage imageWithSize:imageTemplate.size
@@ -105,16 +106,7 @@ - (void)setImage:(NSImage *)imageTemplate
105106
NSRectFillUsingOperation(dstRect, NSCompositingOperationSourceAtop);
106107
return YES;
107108
}];
108-
NSImage *alternateImage = [NSImage imageWithSize:imageTemplate.size
109-
flipped:NO
110-
drawingHandler:^BOOL(NSRect dstRect) {
111-
[[fillColor colorWithAlphaComponent:0.2] set];
112-
[[NSBezierPath bezierPathWithOvalInRect:dstRect] fill];
113-
[image drawInRect:dstRect];
114-
return YES;
115-
}];
116-
super.image = image;
117-
self.alternateImage = alternateImage;
109+
self.image = image;
118110
}
119111

120112
- (void)setEnabled:(BOOL)enabled

src/MacVim/MMTabline/MMTab.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ - (instancetype)initWithFrame:(NSRect)frameRect tabline:(MMTabline *)tabline
4040
_tabline = tabline;
4141

4242
_closeButton = [MMHoverButton new];
43-
_closeButton.image = [MMHoverButton imageFromType:MMHoverButtonImageCloseTab];
43+
_closeButton.imageTemplate = [MMHoverButton imageFromType:MMHoverButtonImageCloseTab];
4444
_closeButton.target = self;
4545
_closeButton.action = @selector(closeTab:);
4646
_closeButton.translatesAutoresizingMaskIntoConstraints = NO;

src/MacVim/MMTabline/MMTabline.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
static MMHoverButton* MakeHoverButton(MMTabline *tabline, MMHoverButtonImage imageType, NSString *tooltip, SEL action, BOOL continuous) {
1919
MMHoverButton *button = [MMHoverButton new];
20-
button.image = [MMHoverButton imageFromType:imageType];
20+
button.imageTemplate = [MMHoverButton imageFromType:imageType];
2121
button.translatesAutoresizingMaskIntoConstraints = NO;
2222
button.target = tabline;
2323
button.action = action;

0 commit comments

Comments
 (0)