X-Git-Url: https://git.r.bdr.sh/rbdr/captura/blobdiff_plain/bf063563436a12c003968ae4d00991f49fac226c..242203487a56df9edd1aa6eceb106461ef62483c:/Captura/CapturaApp.swift?ds=inline diff --git a/Captura/CapturaApp.swift b/Captura/CapturaApp.swift index b0ccf32..4dc65a4 100644 --- a/Captura/CapturaApp.swift +++ b/Captura/CapturaApp.swift @@ -1,20 +1,115 @@ -// -// CapturaApp.swift -// Captura -// -// Created by Ruben Beltran del Rio on 7/24/23. -// - import SwiftUI import SwiftData +import Cocoa @main struct CapturaApp: App { + + @NSApplicationDelegateAdaptor(CapturaAppDelegate.self) var appDelegate var body: some Scene { WindowGroup { - ContentView() + 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) + } + + 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) + } + + + // 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() + print("Recording") + } + } + + func startRecording() { + captureState = .recording + } + + func stopRecording() { + captureState = .uploading + } + + func finalizeRecording() { + captureState = .uploaded + } + + func reset() { + captureState = .idle } }