summaryrefslogtreecommitdiff
path: root/Sources/BubombUI/Icons/BombIconRenderer.swift
blob: d68de1f0ffddf7877cd66bffd60d775e772276b7 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import SwiftUI
#if canImport(AppKit)
import AppKit
#endif

/// State for the menu bar icon rendering.
public enum MenuBarIconState: Equatable, Sendable {
    case idle
    case unconfigured
    case burning(frame: Int)
    case exploded(frame: Int)
    case failed
}

/// Renders native NSImages for menu bar status item and app icons.
public final class BombIconRenderer: @unchecked Sendable {
    public static let shared = BombIconRenderer()

    #if canImport(AppKit)
    /// Creates an NSImage sized for the macOS menu bar (18x18 pt @ 2x).
    public static func menuBarImage(state: MenuBarIconState) -> NSImage {
        let size = NSSize(width: 18, height: 18)
        let image = NSImage(size: size, flipped: false) { bounds in
            let ctx = NSGraphicsContext.current?.cgContext
            
            switch state {
            case .idle:
                drawIdleMenuBarBomb(in: bounds, context: ctx, template: true)
            case .unconfigured:
                drawIdleMenuBarBomb(in: bounds, context: ctx, template: true, unconfiguredBadge: true)
            case .burning(let frame):
                drawBurningMenuBarBomb(in: bounds, context: ctx, frame: frame)
            case .exploded(let frame):
                drawExplodingMenuBarBomb(in: bounds, context: ctx, frame: frame)
            case .failed:
                drawFailedMenuBarBomb(in: bounds, context: ctx)
            }
            return true
        }

        if case .idle = state {
            image.isTemplate = true
        } else if case .unconfigured = state {
            image.isTemplate = true
        } else {
            image.isTemplate = false
        }
        return image
    }

    // MARK: - Menu Bar Drawing Routines

    private static func drawIdleMenuBarBomb(in bounds: NSRect, context: CGContext?, template: Bool, unconfiguredBadge: Bool = false) {
        guard let ctx = context else { return }
        ctx.saveGState()

        let baseColor = template ? NSColor.black : NSColor(calibratedRed: 0.15, green: 0.15, blue: 0.18, alpha: 1.0)
        baseColor.setFill()
        baseColor.setStroke()

        // 1. Bomb Body (Circle)
        // Center at (9, 7.5), radius 5.5
        let bodyRect = NSRect(x: 3.5, y: 1.5, width: 11, height: 11)
        let bodyPath = NSBezierPath(ovalIn: bodyRect)
        bodyPath.fill()

        // 2. Collar / Cap at top of bomb
        let collarRect = NSRect(x: 7.0, y: 11.5, width: 4.0, height: 2.0)
        let collarPath = NSBezierPath(roundedRect: collarRect, xRadius: 0.5, yRadius: 0.5)
        collarPath.fill()

        // 3. Fuse curve
        let fusePath = NSBezierPath()
        fusePath.move(to: NSPoint(x: 9.0, y: 13.0))
        fusePath.curve(to: NSPoint(x: 13.0, y: 15.0),
                       controlPoint1: NSPoint(x: 9.0, y: 15.0),
                       controlPoint2: NSPoint(x: 11.5, y: 16.0))
        fusePath.lineWidth = 1.2
        fusePath.lineCapStyle = .round
        fusePath.stroke()

        // 4. Spark at fuse tip
        if !template {
            let sparkColor = NSColor(calibratedRed: 1.0, green: 0.5, blue: 0.0, alpha: 1.0)
            sparkColor.setFill()
            let sparkRect = NSRect(x: 12.5, y: 14.5, width: 2.5, height: 2.5)
            NSBezierPath(ovalIn: sparkRect).fill()
        }

        // 5. If unconfigured, draw small hollow dot or subtle badge
        if unconfiguredBadge {
            let dotRect = NSRect(x: 8.0, y: 6.0, width: 2.0, height: 2.0)
            NSColor.white.setFill()
            NSBezierPath(ovalIn: dotRect).fill()
        }

        ctx.restoreGState()
    }

    private static func drawBurningMenuBarBomb(in bounds: NSRect, context: CGContext?, frame: Int) {
        guard let ctx = context else { return }
        ctx.saveGState()

        // Dark charcoal bomb body
        let bodyColor = NSColor(calibratedRed: 0.20, green: 0.20, blue: 0.23, alpha: 1.0)
        bodyColor.setFill()
        let bodyRect = NSRect(x: 3.5, y: 1.5, width: 11, height: 11)
        NSBezierPath(ovalIn: bodyRect).fill()

        // Collar
        let collarRect = NSRect(x: 7.0, y: 11.5, width: 4.0, height: 2.0)
        NSBezierPath(roundedRect: collarRect, xRadius: 0.5, yRadius: 0.5).fill()

        // Fuse
        let fuseColor = NSColor(calibratedRed: 0.7, green: 0.6, blue: 0.4, alpha: 1.0)
        fuseColor.setStroke()
        let fusePath = NSBezierPath()
        fusePath.move(to: NSPoint(x: 9.0, y: 13.0))
        fusePath.curve(to: NSPoint(x: 12.5, y: 14.5),
                       controlPoint1: NSPoint(x: 9.0, y: 14.5),
                       controlPoint2: NSPoint(x: 11.0, y: 15.5))
        fusePath.lineWidth = 1.2
        fusePath.stroke()

        // Burning spark: oscillates in size and color based on frame
        let isLarge = (frame % 2 == 0)
        let sparkCenter = NSPoint(x: 13.0, y: 15.0)
        let sparkRadius: CGFloat = isLarge ? 2.8 : 1.8

        // Outer warm glow
        let glowColor = NSColor(calibratedRed: 1.0, green: 0.35, blue: 0.0, alpha: 0.8)
        glowColor.setFill()
        let glowRect = NSRect(x: sparkCenter.x - sparkRadius, y: sparkCenter.y - sparkRadius, width: sparkRadius * 2, height: sparkRadius * 2)
        NSBezierPath(ovalIn: glowRect).fill()

        // Inner bright spark
        let coreColor = NSColor(calibratedRed: 1.0, green: 0.95, blue: 0.2, alpha: 1.0)
        coreColor.setFill()
        let coreRect = NSRect(x: sparkCenter.x - 1.0, y: sparkCenter.y - 1.0, width: 2.0, height: 2.0)
        NSBezierPath(ovalIn: coreRect).fill()

        ctx.restoreGState()
    }

    private static func drawExplodingMenuBarBomb(in bounds: NSRect, context: CGContext?, frame: Int) {
        guard let ctx = context else { return }
        ctx.saveGState()

        // Starburst explosion effect
        let center = NSPoint(x: 9.0, y: 9.0)
        let burstColor = NSColor(calibratedRed: 1.0, green: 0.45, blue: 0.05, alpha: 1.0)
        burstColor.setFill()
        burstColor.setStroke()

        // Draw radial burst rays
        let rayCount = 8
        let outerR: CGFloat = min(8.5, 5.0 + CGFloat(frame) * 1.5)
        let innerR: CGFloat = max(2.0, 3.5 - CGFloat(frame) * 0.5)

        let starPath = NSBezierPath()
        for i in 0..<(rayCount * 2) {
            let angle = (CGFloat(i) * CGFloat.pi / CGFloat(rayCount)) + (CGFloat(frame) * 0.2)
            let radius = (i % 2 == 0) ? outerR : innerR
            let pt = NSPoint(x: center.x + cos(angle) * radius, y: center.y + sin(angle) * radius)
            if i == 0 {
                starPath.move(to: pt)
            } else {
                starPath.line(to: pt)
            }
        }
        starPath.close()
        starPath.fill()

        // Bright yellow core
        let coreColor = NSColor(calibratedRed: 1.0, green: 0.95, blue: 0.4, alpha: 1.0)
        coreColor.setFill()
        let coreRect = NSRect(x: center.x - 2.5, y: center.y - 2.5, width: 5.0, height: 5.0)
        NSBezierPath(ovalIn: coreRect).fill()

        ctx.restoreGState()
    }

    private static func drawFailedMenuBarBomb(in bounds: NSRect, context: CGContext?, template: Bool = false) {
        guard let ctx = context else { return }
        ctx.saveGState()

        // Bomb body in muted charcoal
        let bodyColor = NSColor(calibratedRed: 0.35, green: 0.35, blue: 0.38, alpha: 1.0)
        bodyColor.setFill()
        let bodyRect = NSRect(x: 3.5, y: 1.5, width: 11, height: 11)
        NSBezierPath(ovalIn: bodyRect).fill()

        // Collar
        let collarRect = NSRect(x: 7.0, y: 11.5, width: 4.0, height: 2.0)
        NSBezierPath(roundedRect: collarRect, xRadius: 0.5, yRadius: 0.5).fill()

        // Small red warning indicator badge on upper right
        let redBadge = NSColor(calibratedRed: 0.95, green: 0.25, blue: 0.25, alpha: 1.0)
        redBadge.setFill()
        let badgeRect = NSRect(x: 12.0, y: 12.0, width: 3.5, height: 3.5)
        NSBezierPath(ovalIn: badgeRect).fill()

        ctx.restoreGState()
    }
    #endif
}