]>
Commit | Line | Data |
---|---|---|
bf063563 RBR |
1 | import SwiftUI |
2 | import SwiftData | |
24220348 | 3 | import Cocoa |
bf063563 RBR |
4 | |
5 | @main | |
6 | struct CapturaApp: App { | |
24220348 RBR |
7 | |
8 | @NSApplicationDelegateAdaptor(CapturaAppDelegate.self) var appDelegate | |
bf063563 RBR |
9 | |
10 | var body: some Scene { | |
11 | WindowGroup { | |
24220348 RBR |
12 | PreferencesWindow() |
13 | .handlesExternalEvents(preferring: Set(arrayLiteral: "PreferencesWindow"), allowing: Set(arrayLiteral: "*")) | |
14 | .frame(width: 650, height: 450) | |
bf063563 | 15 | } |
24220348 | 16 | .handlesExternalEvents(matching: Set(arrayLiteral: "PreferencesWindow")) |
bf063563 | 17 | .modelContainer(for: Item.self) |
24220348 RBR |
18 | } |
19 | } | |
20 | ||
21 | class CapturaAppDelegate: NSObject, NSApplicationDelegate { | |
22 | ||
23 | @Environment(\.openURL) var openURL | |
24 | var statusItem: NSStatusItem! | |
25 | var captureState: CaptureState = .idle | |
26 | var recordingWindow: RecordingWindow? = nil | |
27 | ||
28 | func applicationDidFinishLaunching(_ notification: Notification) { | |
29 | setupMenu() | |
30 | NotificationCenter.default.addObserver( | |
31 | self, | |
32 | selector: #selector(self.didReceiveNotification(_:)), | |
33 | name: nil, | |
34 | object: nil) | |
35 | closeWindow() | |
36 | } | |
37 | ||
38 | // MARK: - Setup Functions | |
39 | ||
40 | ||
41 | private func setupMenu() { | |
42 | statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) | |
43 | ||
44 | statusItem.button!.image = NSImage(systemSymbolName: "rectangle.dashed.badge.record", accessibilityDescription: "Captura") | |
45 | statusItem.isVisible = true | |
46 | statusItem.menu = NSMenu() | |
47 | ||
e834022c | 48 | let recordItem = NSMenuItem(title: "Record", action: #selector(CapturaAppDelegate.onClickStartRecording), keyEquivalent: "6") |
24220348 RBR |
49 | recordItem.keyEquivalentModifierMask = [.command, .shift] |
50 | ||
51 | statusItem.menu?.addItem(recordItem) | |
e834022c RBR |
52 | statusItem.menu?.addItem(NSMenuItem.separator()) |
53 | ||
54 | let preferencesItem = NSMenuItem(title: "Preferences", action: #selector(CapturaAppDelegate.onOpenPreferences), keyEquivalent: "") | |
55 | statusItem.menu?.addItem(preferencesItem) | |
56 | ||
57 | let quitItem = NSMenuItem(title: "Quit", action: #selector(CapturaAppDelegate.onQuit), keyEquivalent: "") | |
58 | statusItem.menu?.addItem(quitItem) | |
24220348 RBR |
59 | } |
60 | ||
61 | private func closeWindow() { | |
62 | if let window = NSApplication.shared.windows.first { | |
63 | window.close() | |
64 | } | |
65 | } | |
66 | ||
67 | // MARK: - UI Event Handlers | |
68 | ||
69 | @objc private func onClickStartRecording() { | |
70 | NotificationCenter.default.post(name: .startAreaSelection, object: nil, userInfo: nil) | |
71 | } | |
72 | ||
e834022c RBR |
73 | @objc private func onOpenPreferences() { |
74 | print("Preferences pressed") | |
75 | } | |
76 | ||
77 | @objc private func onQuit() { | |
78 | NSApplication.shared.terminate(self) | |
79 | } | |
80 | ||
24220348 RBR |
81 | |
82 | // MARK: - App State Event Listeners | |
83 | ||
84 | @objc func didReceiveNotification(_ notification: Notification) { | |
85 | switch(notification.name) { | |
86 | case .startAreaSelection: | |
87 | startAreaSelection() | |
88 | case .startRecording: | |
89 | startRecording() | |
90 | case .stopRecording: | |
91 | stopRecording() | |
92 | case .finalizeRecording: | |
93 | finalizeRecording() | |
94 | case .reset: | |
95 | reset() | |
96 | default: | |
97 | return | |
98 | } | |
99 | /* | |
100 | if let data = notification.userInfo?["data"] as? String { | |
101 | print("Data received: \(data)") | |
102 | } | |
103 | */ | |
104 | } | |
105 | ||
106 | ||
107 | @objc func startAreaSelection() { | |
108 | if captureState != .selectingArea { | |
109 | captureState = .selectingArea | |
110 | recordingWindow = RecordingWindow() | |
24220348 RBR |
111 | } |
112 | } | |
113 | ||
114 | func startRecording() { | |
115 | captureState = .recording | |
116 | } | |
117 | ||
118 | func stopRecording() { | |
119 | captureState = .uploading | |
120 | } | |
121 | ||
122 | func finalizeRecording() { | |
123 | captureState = .uploaded | |
124 | } | |
125 | ||
126 | func reset() { | |
127 | captureState = .idle | |
e834022c RBR |
128 | recordingWindow?.close() |
129 | self.recordingWindow = nil | |
bf063563 RBR |
130 | } |
131 | } |