]> git.r.bdr.sh - rbdr/captura/blob - Captura/CapturaApp.swift
Add drawing
[rbdr/captura] / Captura / CapturaApp.swift
1 import SwiftUI
2 import SwiftData
3 import Cocoa
4
5 @main
6 struct CapturaApp: App {
7
8 @NSApplicationDelegateAdaptor(CapturaAppDelegate.self) var appDelegate
9
10 var body: some Scene {
11 WindowGroup {
12 PreferencesWindow()
13 .handlesExternalEvents(preferring: Set(arrayLiteral: "PreferencesWindow"), allowing: Set(arrayLiteral: "*"))
14 .frame(width: 650, height: 450)
15 }
16 .handlesExternalEvents(matching: Set(arrayLiteral: "PreferencesWindow"))
17 .modelContainer(for: Item.self)
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
48 let recordItem = NSMenuItem(title: "record", action: #selector(CapturaAppDelegate.onClickStartRecording), keyEquivalent: "6")
49 recordItem.keyEquivalentModifierMask = [.command, .shift]
50
51 statusItem.menu?.addItem(recordItem)
52 }
53
54 private func closeWindow() {
55 if let window = NSApplication.shared.windows.first {
56 window.close()
57 }
58 }
59
60 // MARK: - UI Event Handlers
61
62 @objc private func onClickStartRecording() {
63 NotificationCenter.default.post(name: .startAreaSelection, object: nil, userInfo: nil)
64 }
65
66
67 // MARK: - App State Event Listeners
68
69 @objc func didReceiveNotification(_ notification: Notification) {
70 switch(notification.name) {
71 case .startAreaSelection:
72 startAreaSelection()
73 case .startRecording:
74 startRecording()
75 case .stopRecording:
76 stopRecording()
77 case .finalizeRecording:
78 finalizeRecording()
79 case .reset:
80 reset()
81 default:
82 return
83 }
84 /*
85 if let data = notification.userInfo?["data"] as? String {
86 print("Data received: \(data)")
87 }
88 */
89 }
90
91
92 @objc func startAreaSelection() {
93 if captureState != .selectingArea {
94 captureState = .selectingArea
95 recordingWindow = RecordingWindow()
96 print("Recording")
97 }
98 }
99
100 func startRecording() {
101 captureState = .recording
102 }
103
104 func stopRecording() {
105 captureState = .uploading
106 }
107
108 func finalizeRecording() {
109 captureState = .uploaded
110 }
111
112 func reset() {
113 captureState = .idle
114 }
115 }