aboutsummaryrefslogtreecommitdiff
path: root/QSThingsPlugin/Core Extensions/NSImage+Badge.m
blob: e5a2bf159cb84a0ec7281be119edc86e48e09ad3 (plain)
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
//
//  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