6 struct CapturaApp: App {
8 @NSApplicationDelegateAdaptor(CapturaAppDelegate.self) var appDelegate
10 var body: some Scene {
13 .handlesExternalEvents(preferring: Set(arrayLiteral: "PreferencesWindow"), allowing: Set(arrayLiteral: "*"))
14 .frame(width: 650, height: 450)
16 .handlesExternalEvents(matching: Set(arrayLiteral: "PreferencesWindow"))
17 .modelContainer(for: Item.self)
21 class CapturaAppDelegate: NSObject, NSApplicationDelegate {
23 @Environment(\.openURL) var openURL
24 var statusItem: NSStatusItem!
25 var captureState: CaptureState = .idle
26 var recordingWindow: RecordingWindow? = nil
28 func applicationDidFinishLaunching(_ notification: Notification) {
30 NotificationCenter.default.addObserver(
32 selector: #selector(self.didReceiveNotification(_:)),
38 // MARK: - Setup Functions
41 private func setupMenu() {
42 statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
44 statusItem.button!.image = NSImage(systemSymbolName: "rectangle.dashed.badge.record", accessibilityDescription: "Captura")
45 statusItem.isVisible = true
46 statusItem.menu = NSMenu()
48 let recordItem = NSMenuItem(title: "Record", action: #selector(CapturaAppDelegate.onClickStartRecording), keyEquivalent: "6")
49 recordItem.keyEquivalentModifierMask = [.command, .shift]
51 statusItem.menu?.addItem(recordItem)
52 statusItem.menu?.addItem(NSMenuItem.separator())
54 let preferencesItem = NSMenuItem(title: "Preferences", action: #selector(CapturaAppDelegate.onOpenPreferences), keyEquivalent: "")
55 statusItem.menu?.addItem(preferencesItem)
57 let quitItem = NSMenuItem(title: "Quit", action: #selector(CapturaAppDelegate.onQuit), keyEquivalent: "")
58 statusItem.menu?.addItem(quitItem)
61 private func closeWindow() {
62 if let window = NSApplication.shared.windows.first {
67 // MARK: - UI Event Handlers
69 @objc private func onClickStartRecording() {
70 NotificationCenter.default.post(name: .startAreaSelection, object: nil, userInfo: nil)
73 @objc private func onOpenPreferences() {
74 print("Preferences pressed")
77 @objc private func onQuit() {
78 NSApplication.shared.terminate(self)
82 // MARK: - App State Event Listeners
84 @objc func didReceiveNotification(_ notification: Notification) {
85 switch(notification.name) {
86 case .startAreaSelection:
92 case .finalizeRecording:
100 if let data = notification.userInfo?["data"] as? String {
101 print("Data received: \(data)")
107 @objc func startAreaSelection() {
108 if captureState != .selectingArea {
109 captureState = .selectingArea
110 recordingWindow = RecordingWindow()
114 func startRecording() {
115 captureState = .recording
118 func stopRecording() {
119 captureState = .uploading
122 func finalizeRecording() {
123 captureState = .uploaded
128 recordingWindow?.close()
129 self.recordingWindow = nil