import SwiftUI import SwiftData import Cocoa @main struct CapturaApp: App { @NSApplicationDelegateAdaptor(CapturaAppDelegate.self) var appDelegate var body: some Scene { WindowGroup { PreferencesWindow() .handlesExternalEvents(preferring: Set(arrayLiteral: "PreferencesWindow"), allowing: Set(arrayLiteral: "*")) .frame(width: 650, height: 450) } .handlesExternalEvents(matching: Set(arrayLiteral: "PreferencesWindow")) .modelContainer(for: Item.self) } } class CapturaAppDelegate: NSObject, NSApplicationDelegate { @Environment(\.openURL) var openURL var statusItem: NSStatusItem! var captureState: CaptureState = .idle var recordingWindow: RecordingWindow? = nil func applicationDidFinishLaunching(_ notification: Notification) { setupMenu() NotificationCenter.default.addObserver( self, selector: #selector(self.didReceiveNotification(_:)), name: nil, object: nil) closeWindow() } // MARK: - Setup Functions private func setupMenu() { statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) statusItem.button!.image = NSImage(systemSymbolName: "rectangle.dashed.badge.record", accessibilityDescription: "Captura") statusItem.isVisible = true statusItem.menu = NSMenu() let recordItem = NSMenuItem(title: "Record", action: #selector(CapturaAppDelegate.onClickStartRecording), keyEquivalent: "6") recordItem.keyEquivalentModifierMask = [.command, .shift] statusItem.menu?.addItem(recordItem) statusItem.menu?.addItem(NSMenuItem.separator()) let preferencesItem = NSMenuItem(title: "Preferences", action: #selector(CapturaAppDelegate.onOpenPreferences), keyEquivalent: "") statusItem.menu?.addItem(preferencesItem) let quitItem = NSMenuItem(title: "Quit", action: #selector(CapturaAppDelegate.onQuit), keyEquivalent: "") statusItem.menu?.addItem(quitItem) } private func closeWindow() { if let window = NSApplication.shared.windows.first { window.close() } } // MARK: - UI Event Handlers @objc private func onClickStartRecording() { NotificationCenter.default.post(name: .startAreaSelection, object: nil, userInfo: nil) } @objc private func onOpenPreferences() { print("Preferences pressed") } @objc private func onQuit() { NSApplication.shared.terminate(self) } // MARK: - App State Event Listeners @objc func didReceiveNotification(_ notification: Notification) { switch(notification.name) { case .startAreaSelection: startAreaSelection() case .startRecording: startRecording() case .stopRecording: stopRecording() case .finalizeRecording: finalizeRecording() case .reset: reset() default: return } /* if let data = notification.userInfo?["data"] as? String { print("Data received: \(data)") } */ } @objc func startAreaSelection() { if captureState != .selectingArea { captureState = .selectingArea recordingWindow = RecordingWindow() } } func startRecording() { captureState = .recording } func stopRecording() { captureState = .uploading } func finalizeRecording() { captureState = .uploaded } func reset() { captureState = .idle recordingWindow?.close() self.recordingWindow = nil } }