diff options
| author | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-09-12 21:50:40 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-09-12 21:50:40 +0200 |
| commit | 182872c760d437556d606cc02c6b7c570667a715 (patch) | |
| tree | 11795bdb259445e5c3f8dd0177e9426ec77b0399 /QSThingsPlugin/Core Extensions | |
| parent | 5870e5cf03ceaff7080174b573618631a413110e (diff) | |
Add children
Diffstat (limited to 'QSThingsPlugin/Core Extensions')
| -rw-r--r-- | QSThingsPlugin/Core Extensions/NSImage+Badge.h | 18 | ||||
| -rw-r--r-- | QSThingsPlugin/Core Extensions/NSImage+Badge.m | 64 |
2 files changed, 82 insertions, 0 deletions
diff --git a/QSThingsPlugin/Core Extensions/NSImage+Badge.h b/QSThingsPlugin/Core Extensions/NSImage+Badge.h new file mode 100644 index 0000000..0882620 --- /dev/null +++ b/QSThingsPlugin/Core Extensions/NSImage+Badge.h @@ -0,0 +1,18 @@ +// +// NSImage+NSImage_Badges.h +// Things Plugin +// +// Created by Ruben Beltran del Rio on 2025-09-12. +// + +#import <Foundation/Foundation.h> + +NS_ASSUME_NONNULL_BEGIN + +@interface NSImage (Badge) + +- (NSImage *)imageOfSize:(NSSize)size withSystemBadge:(NSString *)symbolName ofColor:(NSColor *)color; + +@end + +NS_ASSUME_NONNULL_END diff --git a/QSThingsPlugin/Core Extensions/NSImage+Badge.m b/QSThingsPlugin/Core Extensions/NSImage+Badge.m new file mode 100644 index 0000000..e5a2bf1 --- /dev/null +++ b/QSThingsPlugin/Core Extensions/NSImage+Badge.m @@ -0,0 +1,64 @@ +// +// 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 |