// // NSImage+NSImage_Badges.h // Things Plugin // // Created by Ruben Beltran del Rio on 2025-09-12. // #import "NSImage+Badge.h" NS_ASSUME_NONNULL_BEGIN @implementation NSImage (Badge) - (NSImage *)imageOfSize:(NSSize)size withSystemBadge:(NSString *)symbolName ofColor:(NSColor *)color{ if (@available(macOS 12.0, *)) { NSImageRep *baseRep = [self bestRepresentationForRect:NSMakeRect(0, 0, size.width, size.height) context:nil hints:nil]; NSImage *badgeImage = [NSImage imageWithSystemSymbolName:symbolName accessibilityDescription:nil]; [badgeImage setTemplate:YES]; NSArray *colors = [NSArray arrayWithObjects:color, nil]; NSImageSymbolConfiguration *iconConfig = [NSImageSymbolConfiguration configurationWithPaletteColors:colors]; // Create composite image at the requested size NSImage *compositImage = [[NSImage alloc] initWithSize:size]; [compositImage lockFocus]; // Draw the base image [baseRep drawInRect:NSMakeRect(0, 0, size.width, size.height)]; // Calculate badge position (bottom right corner) CGFloat badgeSize = size.width * 0.4; CGFloat badgeX = size.width - badgeSize; CGFloat badgeY = 0; NSRect badgeRect = NSMakeRect(badgeX, badgeY, badgeSize, badgeSize); // Draw white circle background [[NSColor whiteColor] setFill]; NSBezierPath *circlePath = [NSBezierPath bezierPathWithOvalInRect:badgeRect]; [circlePath fill]; // Draw the SF Symbol in blue [[NSColor blueColor] setFill]; [[badgeImage imageWithSymbolConfiguration:iconConfig] drawInRect:badgeRect]; [compositImage unlockFocus]; // Preserve template status if applicable if ([self isTemplate]) { [compositImage setTemplate:YES]; } return compositImage; } else { return nil; } } @end NS_ASSUME_NONNULL_END