summaryrefslogtreecommitdiff
path: root/Sources/BubombUI/MenuBar/MenuBarController.swift
blob: 6f722b46b05d9f1b0c39b7d729c021c35f99c226 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import SwiftUI
import BubombCore
#if canImport(AppKit)
import AppKit

/// Manages the native macOS NSStatusItem, click routing (left=detonate, right=menu),
/// and dynamic menu bar icon animations.
@MainActor
public final class MenuBarController: NSObject {
    public static let shared = MenuBarController()

    private var statusItem: NSStatusItem?
    private let settingsStore: SettingsStore
    private let executor: PayloadExecutor
    private var animationTimer: Timer?
    private var currentFrame: Int = 0

    public init(settingsStore: SettingsStore = .shared, executor: PayloadExecutor? = nil) {
        self.settingsStore = settingsStore
        self.executor = executor ?? PayloadExecutor.shared
        super.init()
    }

    /// Configures the NSStatusItem in the macOS menu bar.
    public func setup() {
        guard statusItem == nil else { return }

        let item = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
        if let button = item.button {
            button.target = self
            button.action = #selector(statusBarButtonClicked(_:))
            button.sendAction(on: [.leftMouseUp, .rightMouseUp])
            button.imagePosition = .imageOnly
        }

        self.statusItem = item
        updateIcon()

        // Observe state changes on executor
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(handleStateChanged),
            name: UserDefaults.didChangeNotification,
            object: nil
        )

        // Observe executor currentState directly via a polling/timer or binding
        startStateObservation()
    }

    // MARK: - Click Handling

    @objc private func statusBarButtonClicked(_ sender: NSStatusBarButton) {
        guard let event = NSApp.currentEvent else {
            handlePrimaryClick()
            return
        }

        let isRightClick = event.type == .rightMouseUp
        let isControlLeftClick = event.type == .leftMouseUp && event.modifierFlags.contains(.control)

        if isRightClick || isControlLeftClick {
            showContextMenu()
        } else {
            handlePrimaryClick()
        }
    }

    /// Left-click interaction: Detonates configured action instantly, or opens settings if unconfigured.
    public func handlePrimaryClick() {
        guard let payload = settingsStore.settings.activePayload else {
            // Unconfigured: Open settings immediately
            WindowManager.shared.showSettingsWindow(settingsStore: settingsStore, executor: executor)
            return
        }

        // If already running, prevent duplicate or show notice
        if executor.currentState.isRunning {
            return
        }

        // Check if confirmation is required
        let alwaysConfirm = settingsStore.settings.confirmDestructiveActions
        let (requiresConfirm, _) = SafetyPolicy.requiresConfirmation(
            command: commandStringForPayload(payload),
            userSettingAlwaysConfirmDestructive: alwaysConfirm || payload.requiresConfirmation
        )

        if requiresConfirm {
            // Show alert or open settings to confirm
            WindowManager.shared.showSettingsWindow(settingsStore: settingsStore, executor: executor)
            return
        }

        // Detonate directly!
        Task {
            do {
                _ = try await executor.detonate(payload: payload, ignoreSafetyConfirmation: false)
            } catch {
                print("Detonation finished with notice: \(error.localizedDescription)")
            }
        }
    }

    /// Right-click menu with configuration and power options.
    public func showContextMenu() {
        guard let item = statusItem, let button = item.button else { return }

        let menu = NSMenu()

        // App title & Armed status
        let titleItem = NSMenuItem(title: "Bubomb", action: nil, keyEquivalent: "")
        titleItem.attributedTitle = NSAttributedString(
            string: "💣 Bubomb — Configure the boom",
            attributes: [.font: NSFont.boldSystemFont(ofSize: 13)]
        )
        menu.addItem(titleItem)

        if let active = settingsStore.settings.activePayload {
            let armedItem = NSMenuItem(title: "Armed: \(active.name)", action: nil, keyEquivalent: "")
            armedItem.image = NSImage(systemSymbolName: "checkmark.circle.fill", accessibilityDescription: nil)
            menu.addItem(armedItem)
        } else {
            let unarmedItem = NSMenuItem(title: "No Payload Armed", action: nil, keyEquivalent: "")
            unarmedItem.image = NSImage(systemSymbolName: "exclamationmark.circle", accessibilityDescription: nil)
            menu.addItem(unarmedItem)
        }

        menu.addItem(NSMenuItem.separator())

        // Detonate action
        let detonateItem = NSMenuItem(title: "Detonate Payload", action: #selector(menuDetonateClicked), keyEquivalent: "\r")
        detonateItem.target = self
        detonateItem.isEnabled = (settingsStore.settings.activePayload != nil) && !executor.currentState.isRunning
        detonateItem.image = NSImage(systemSymbolName: "flame.fill", accessibilityDescription: nil)
        menu.addItem(detonateItem)

        if executor.currentState.isRunning {
            let cancelItem = NSMenuItem(title: "Cancel Detonation", action: #selector(menuCancelClicked), keyEquivalent: ".")
            cancelItem.target = self
            cancelItem.image = NSImage(systemSymbolName: "xmark.circle", accessibilityDescription: nil)
            menu.addItem(cancelItem)
        }

        menu.addItem(NSMenuItem.separator())

        // Settings
        let settingsMenuItem = NSMenuItem(title: "Settings...", action: #selector(menuSettingsClicked), keyEquivalent: ",")
        settingsMenuItem.target = self
        settingsMenuItem.image = NSImage(systemSymbolName: "gearshape", accessibilityDescription: nil)
        menu.addItem(settingsMenuItem)

        menu.addItem(NSMenuItem.separator())

        // Quit
        let quitItem = NSMenuItem(title: "Quit Bubomb", action: #selector(menuQuitClicked), keyEquivalent: "q")
        quitItem.target = self
        quitItem.image = NSImage(systemSymbolName: "power", accessibilityDescription: nil)
        menu.addItem(quitItem)

        item.menu = menu
        button.performClick(nil)
        item.menu = nil
    }

    // MARK: - Menu Actions

    @objc private func menuDetonateClicked() {
        handlePrimaryClick()
    }

    @objc private func menuCancelClicked() {
        executor.cancel()
    }

    @objc private func menuSettingsClicked() {
        WindowManager.shared.showSettingsWindow(settingsStore: settingsStore, executor: executor)
    }

    @objc private func menuQuitClicked() {
        NSApplication.shared.terminate(nil)
    }

    @objc private func handleStateChanged() {
        updateIcon()
    }

    // MARK: - Animation & State Updates

    private func startStateObservation() {
        // Lightweight polling timer (60ms) to sync menu bar icon with executor state
        Timer.scheduledTimer(withTimeInterval: 0.08, repeats: true) { [weak self] _ in
            Task { @MainActor [weak self] in
                self?.syncStateWithExecutor()
            }
        }
    }

    private var previousState: DetonationState = .idle

    private func syncStateWithExecutor() {
        let state = executor.currentState
        if state != previousState {
            previousState = state
            onStateTransition(to: state)
        }
    }

    private func onStateTransition(to state: DetonationState) {
        animationTimer?.invalidate()
        animationTimer = nil
        currentFrame = 0

        let reduceMotion = NSWorkspace.shared.accessibilityDisplayShouldReduceMotion ||
            (settingsStore.settings.animationPreference == .none)

        switch state {
        case .idle:
            updateIcon()

        case .burning:
            if reduceMotion {
                statusItem?.button?.image = BombIconRenderer.menuBarImage(state: .burning(frame: 0))
            } else {
                animationTimer = Timer.scheduledTimer(withTimeInterval: 0.12, repeats: true) { [weak self] _ in
                    Task { @MainActor [weak self] in
                        guard let self = self else { return }
                        self.currentFrame = (self.currentFrame + 1) % 4
                        self.statusItem?.button?.image = BombIconRenderer.menuBarImage(state: .burning(frame: self.currentFrame))
                    }
                }
            }

        case .exploded:
            if settingsStore.settings.playCompletionFeedback {
                NSSound.beep()
            }
            if reduceMotion {
                statusItem?.button?.image = BombIconRenderer.menuBarImage(state: .exploded(frame: 2))
            } else {
                // 3 quick frames for explosion burst
                var burstFrame = 0
                animationTimer = Timer.scheduledTimer(withTimeInterval: 0.10, repeats: true) { [weak self] timer in
                    Task { @MainActor [weak self] in
                        guard let self = self else { return }
                        burstFrame += 1
                        if burstFrame < 4 {
                            self.statusItem?.button?.image = BombIconRenderer.menuBarImage(state: .exploded(frame: burstFrame))
                        } else {
                            timer.invalidate()
                            self.updateIcon()
                        }
                    }
                }
            }

        case .failed:
            statusItem?.button?.image = BombIconRenderer.menuBarImage(state: .failed)
        }
    }

    private func updateIcon() {
        if settingsStore.settings.activePayload == nil {
            statusItem?.button?.image = BombIconRenderer.menuBarImage(state: .unconfigured)
            statusItem?.button?.toolTip = "Bubomb (Unconfigured) — Click to setup payload"
        } else {
            statusItem?.button?.image = BombIconRenderer.menuBarImage(state: .idle)
            statusItem?.button?.toolTip = "Bubomb: Detonate '\(settingsStore.settings.activePayload?.name ?? "")'"
        }
    }

    private func commandStringForPayload(_ payload: Payload) -> String {
        switch payload.type {
        case .shellCommand(let config):
            return config.command
        case .runScript(let config):
            return config.scriptPath
        default:
            return ""
        }
    }
}
#endif