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)
54 private func closeWindow() {
55 if let window = NSApplication.shared.windows.first {
60 // MARK: - UI Event Handlers
62 @objc private func onClickStartRecording() {
63 NotificationCenter.default.post(name: .startAreaSelection, object: nil, userInfo: nil)
67 // MARK: - App State Event Listeners
69 @objc func didReceiveNotification(_ notification: Notification) {
70 switch(notification.name) {
71 case .startAreaSelection:
77 case .finalizeRecording:
85 if let data = notification.userInfo?["data"] as? String {
86 print("Data received: \(data)")
92 @objc func startAreaSelection() {
93 if captureState != .selectingArea {
94 captureState = .selectingArea
95 recordingWindow = RecordingWindow()
100 func startRecording() {
101 captureState = .recording
104 func stopRecording() {
105 captureState = .uploading
108 func finalizeRecording() {
109 captureState = .uploaded