summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDamiano Galassi <[email protected]>2018-08-12 09:56:20 +0200
committerDamiano Galassi <[email protected]>2018-08-12 09:56:20 +0200
commit7b31141ee6910fd5f25408d7d1e6b5859043bfe2 (patch)
treeed3dc9eed780bfb374ecb01cdd597e060788622b
parent05ebf615a8056cca39166b5e2bf2ea05d35eb01c (diff)
MacGui: simplify the toolbar badge drawing code, redraw the icon when switching between dark and light mode.
-rw-r--r--macosx/HBToolbarBadgedItem.m161
1 files changed, 58 insertions, 103 deletions
diff --git a/macosx/HBToolbarBadgedItem.m b/macosx/HBToolbarBadgedItem.m
index bae3ab133..86069b9b9 100644
--- a/macosx/HBToolbarBadgedItem.m
+++ b/macosx/HBToolbarBadgedItem.m
@@ -10,7 +10,7 @@
@interface HBToolbarBadgedItem ()
@property (nonatomic) NSImage *primary;
-@property (nonatomic) NSImage *cache;
+@property (nonatomic) NSImage *badged;
@end
@@ -41,125 +41,49 @@
- (void)setImage:(NSImage *)image
{
+ __weak HBToolbarBadgedItem *weakSelf = self;
_primary = image;
- if (_badgeValue.length)
- {
- _cache = nil;
- [super setImage:[self HB_badgeImage:_badgeValue]];
- }
- else
- {
- [super setImage:image];
- }
-}
-
-- (void)setBadgeValue:(NSString *)badgeValue
-{
- if (![_badgeValue isEqualToString:badgeValue])
- {
- if (badgeValue.length)
- {
- [super setImage:[self HB_badgeImage:badgeValue]];
- }
- else
+ _badged = [NSImage imageWithSize:image.size flipped:NO drawingHandler:^BOOL(NSRect dstRect) {
+ HBToolbarBadgedItem *strongSelf = weakSelf;
+ if (strongSelf == nil)
{
- [super setImage:_primary];
+ return NO;
}
- _badgeValue = [badgeValue copy];
- }
-}
-
-- (void)setBadgeTextColor:(NSColor *)badgeTextColor
-{
- _badgeTextColor = [badgeTextColor copy];
-
- [self HB_refreshBadge];
-}
-
-- (void)setBadgeFillColor:(NSColor *)badgeFillColor
-{
- _badgeFillColor = [badgeFillColor copy];
-
- [self HB_refreshBadge];
-}
-
-#pragma mark -- Private Methods
-
-- (void)HB_refreshBadge
-{
- if (_badgeValue.length)
- {
- _cache = [self HB_renderImage:_primary withBadge:_badgeValue];
- [super setImage:_cache];
- }
-}
-
-- (NSImage *)HB_badgeImage:(NSString *)badgeValue
-{
- if (![_badgeValue isEqualToString:badgeValue] || _cache == nil)
- {
- _cache = [self HB_renderImage:_primary withBadge:badgeValue];
- }
- return _cache;
-}
-
-- (NSImage *)HB_renderImage:(NSImage *)image withBadge:(NSString *)badgeString
-{
- NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
- NSImage *newImage = [[NSImage alloc] initWithSize:image.size];
- for (NSImageRep *rep in image.representations)
- {
- NSSize size = NSMakeSize(rep.pixelsWide, rep.pixelsHigh);
- NSBitmapImageRep *newRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
- pixelsWide:(NSInteger)floor(size.width)
- pixelsHigh:(NSInteger)floor(size.height)
- bitsPerSample:8
- samplesPerPixel:4
- hasAlpha:YES
- isPlanar:NO
- colorSpaceName:NSDeviceRGBColorSpace
- bytesPerRow:(NSInteger)floor(size.width) * 4
- bitsPerPixel:32];
-
- NSGraphicsContext *ctx = [NSGraphicsContext graphicsContextWithBitmapImageRep:newRep];
- [NSGraphicsContext saveGraphicsState];
- [NSGraphicsContext setCurrentContext:ctx];
-
- CGContextRef context = [[NSGraphicsContext currentContext] CGContext];
+ CGContextRef context = NSGraphicsContext.currentContext.CGContext;
CGContextSaveGState(context);
- NSRect imageRect = NSMakeRect(0, 0, size.width, size.height);
+ CGRect deviceRect = CGContextConvertRectToDeviceSpace(context, dstRect);
+ NSSize size = dstRect.size;
+
+ NSRect imageRect = NSMakeRect(0, 0, image.size.width, image.size.height);
CGImageRef ref = [image CGImageForProposedRect:&imageRect context:[NSGraphicsContext currentContext] hints:nil];
CGContextDrawImage(context, imageRect, ref);
// Work out the area
- CGFloat scaleFactor = rep.pixelsWide / rep.size.width;
+ CGFloat scaleFactor = deviceRect.size.width / image.size.width;
CGFloat typeSize = 10;
if (scaleFactor > 1)
{
typeSize = 8;
}
- CGFloat pointSize = typeSize * scaleFactor;
+ CGFloat pointSize = typeSize;
+ NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
NSFont *font = [NSFont boldSystemFontOfSize:pointSize];
NSDictionary *attr = @{NSParagraphStyleAttributeName : paragraphStyle,
NSFontAttributeName : font,
- NSForegroundColorAttributeName : _badgeTextColor };
+ NSForegroundColorAttributeName : strongSelf->_badgeTextColor };
- NSRect textBounds = [badgeString boundingRectWithSize:NSZeroSize
- options:0
- attributes:attr];
+ NSRect textBounds = [strongSelf->_badgeValue boundingRectWithSize:NSZeroSize
+ options:0
+ attributes:attr];
- NSPoint indent = NSMakePoint(typeSize * scaleFactor, 2 * scaleFactor);
+ NSPoint indent = NSMakePoint(typeSize, 2);
CGFloat radius = (textBounds.size.height + indent.y) * 0.5f;
CGFloat offset_x = 0;
CGFloat offset_y = 0;
- if (scaleFactor > 1)
- {
- offset_y = 2 * scaleFactor;
- }
NSRect badgeRect = NSMakeRect(size.width - textBounds.size.width - indent.x - offset_x, offset_y,
textBounds.size.width + indent.x, textBounds.size.height + indent.y);
badgeRect = NSIntegralRect(badgeRect);
@@ -173,7 +97,6 @@
CGFloat maxy = CGRectGetMaxY(badgeRect);
// Fill the ellipse
- CGContextSaveGState(context);
CGContextBeginPath(context);
CGContextMoveToPoint(context, minx, midy);
CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
@@ -181,7 +104,7 @@
CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
CGContextClosePath(context);
- CGColorRef fillColor = _badgeFillColor.CGColor;
+ CGColorRef fillColor = strongSelf->_badgeFillColor.CGColor;
CGContextSetFillColorWithColor(context,fillColor);
CGContextDrawPath(context, kCGPathFill);
@@ -198,19 +121,51 @@
badgeRect.size.width = textBounds.size.width;
badgeRect.size.height = textBounds.size.height;
- [badgeString drawInRect:badgeRect withAttributes:attr];
+ [strongSelf->_badgeValue drawInRect:badgeRect withAttributes:attr];
CGContextRestoreGState(context);
- CGContextFlush(context);
- CGContextRestoreGState(context);
+ return YES;
+ }];
- [NSGraphicsContext restoreGraphicsState];
+ [self HB_refreshBadge];
+}
- [newImage addRepresentation:newRep];
+- (void)setBadgeValue:(NSString *)badgeValue
+{
+ if (![_badgeValue isEqualToString:badgeValue])
+ {
+ _badgeValue = [badgeValue copy];
+ [self HB_refreshBadge];
}
+}
- return newImage;
+- (void)setBadgeTextColor:(NSColor *)badgeTextColor
+{
+ _badgeTextColor = [badgeTextColor copy];
+
+ [self HB_refreshBadge];
+}
+
+- (void)setBadgeFillColor:(NSColor *)badgeFillColor
+{
+ _badgeFillColor = [badgeFillColor copy];
+
+ [self HB_refreshBadge];
+}
+
+#pragma mark -- Private Methods
+
+- (void)HB_refreshBadge
+{
+ if (_badgeValue.length)
+ {
+ [super setImage:_badged];
+ }
+ else
+ {
+ [super setImage:_primary];
+ }
}
@end