]> git.r.bdr.sh - rbdr/captura/blobdiff - Captura/CapturaApp.swift
Add drawing
[rbdr/captura] / Captura / CapturaApp.swift
index b0ccf32d0acadb9073122dcad825049b0eb90eb4..4dc65a478d25a7c8bea47f22a12ed51dbe09fca2 100644 (file)
-//
-//  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
     }
 }