]> git.r.bdr.sh - rbdr/captura/commitdiff
Add URL parsing for record action
authorRuben Beltran del Rio <redacted>
Mon, 31 Jul 2023 16:32:58 +0000 (18:32 +0200)
committerRuben Beltran del Rio <redacted>
Mon, 31 Jul 2023 16:32:58 +0000 (18:32 +0200)
Captura/CapturaApp.swift
Captura/Data/CapturaURLDecoder.swift
Captura/Domain/CaptureSessionConfiguration.swift
Captura/Presentation/Windows/RecordingWindow.swift

index d9c3ff0bcfffd18786ae622bda386b149bf34733..eb6f02ecb264a24147165745a7944435a176ca9b 100644 (file)
@@ -101,16 +101,15 @@ class CapturaAppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
   // MARK: - URL Event Handler
   
   func application(_ application: NSApplication, open urls: [URL]) {
-    print("AAAH OPENING")
     if (CapturaSettings.shouldAllowURLAutomation) {
       for url in urls {
         if let action = CapturaURLDecoder.decodeParams(url: url) {
           switch action {
             case let .configure(config):
-            print("AAAH CONFIGURING \(config)")
               CapturaSettings.apply(config)
             case let .record(config):
-              print(config)
+              captureSessionConfiguration = CaptureSessionConfiguration(from: config)
+              NotificationCenter.default.post(name: .startAreaSelection, object: nil, userInfo: nil)
           }
         }
       }
@@ -216,7 +215,7 @@ class CapturaAppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
         let rectInWindow = button.convert(button.bounds, to: nil)
         let rectInScreen = button.window?.convertToScreen(rectInWindow)
         NSApp.activate(ignoringOtherApps: true)
-        recordingWindow = RecordingWindow(rectInScreen)
+        recordingWindow = RecordingWindow(captureSessionConfiguration, rectInScreen)
         recordingWindow?.makeKeyAndOrderFront(nil)
         recordingWindow?.orderFrontRegardless()
         boxListener = recordingWindow?.recordingContentView.$box
@@ -250,7 +249,7 @@ class CapturaAppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
           stopTimer = DispatchWorkItem {
             self.stopRecording()
           }
-          DispatchQueue.main.asyncAfter(deadline: .now() + 300, execute: stopTimer!)
+          DispatchQueue.main.asyncAfter(deadline: .now() + Double(captureSessionConfiguration.maxLength), execute: stopTimer!)
           
           outputFile = CapturaFile()
           if captureSessionConfiguration.shouldSaveMp4 {
@@ -296,6 +295,7 @@ class CapturaAppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
   func reset() {
     captureState = .idle
     updateImage()
+    captureSessionConfiguration = CaptureSessionConfiguration()
     stop()
   }
   
@@ -383,7 +383,6 @@ class CapturaAppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
     boxListener?.cancel()
     recordingWindow?.close()
     recordingWindow = nil
-    captureSessionConfiguration = CaptureSessionConfiguration()
   }
   
   private func uploadOrCopy() async -> Bool {
index 3fa6412849a626fcef87e0485706a42fed9a7ac0..f4c325b0d56c7ff9f597716b9b36a415cc867242 100644 (file)
@@ -25,6 +25,8 @@ protocol RecordActionProtocol {
   var backendOutput: OutputFormatSetting? { get }
   var keepLocalFiles: Bool? { get }
   var autoStart: Bool? { get }
+  var skipBackend: Bool { get }
+  var maxLength: Int? { get }
 }
 
 // The concrete implementations
@@ -51,6 +53,8 @@ struct RecordAction: RecordActionProtocol {
   var backendOutput: OutputFormatSetting?
   var keepLocalFiles: Bool?
   var autoStart: Bool?
+  var skipBackend: Bool
+  var maxLength: Int?
 }
 
 enum CapturaAction {
@@ -113,15 +117,28 @@ struct CapturaURLDecoder {
         let keepLocalFiles = Bool(paramsDict["keep_local_files"] as? String ?? "")
         let outputs = OutputFormatSetting(paramsDict["outputs"] as? String ?? "")
         var backendOutput = OutputFormatSetting(paramsDict["backend_output"] as? String ?? "")
+        let autoStart = Bool(paramsDict["auto_start"] as? String ?? "")
+        var maxLength = Int(paramsDict["max_length"] as? String ?? "")
         
         if fps != nil {
           fps = min(10, max(4, fps!))
         }
         
+        if maxLength != nil {
+          maxLength = min(300, max(1, fps!))
+        }
+        
         if backendOutput == .all {
           backendOutput = .gifOnly
         }
         
+        var skipBackend = false
+        if let backendString = paramsDict["backend"] as? String {
+          if backendString == "" {
+            skipBackend = true
+          }
+        }
+        
         return .record(RecordAction(
           action: action,
           x: x,
@@ -134,7 +151,10 @@ struct CapturaURLDecoder {
           outputs: outputs,
           backend: backend,
           backendOutput: backendOutput,
-          keepLocalFiles: keepLocalFiles
+          keepLocalFiles: keepLocalFiles,
+          autoStart: autoStart,
+          skipBackend: skipBackend,
+          maxLength: maxLength
         ))
 
       default:
index 69c1ebf35fbf5a67a038c50ca9015c11d87a76f9..c2ebe75d097b6568d6bf4e6e79adac68d34c78ae 100644 (file)
@@ -6,6 +6,14 @@ struct CaptureSessionConfiguration {
   let backendFormat: OutputFormatSetting
   let backend: URL?
   let shouldKeepLocalFiles: Bool
+  let x: Int?
+  let y: Int?
+  let width: Int?
+  let height: Int?
+  let preventMove: Bool
+  let preventResize: Bool
+  let autoStart: Bool
+  let maxLength: Int
   
   init(
     frameRate: Int? = nil,
@@ -19,6 +27,34 @@ struct CaptureSessionConfiguration {
     self.backendFormat = backendFormat ?? CapturaSettings.backendFormat
     self.backend = backend ?? CapturaSettings.backend
     self.shouldKeepLocalFiles = shouldKeepLocalFiles ?? CapturaSettings.shouldKeepLocalFiles
+    x = nil
+    y = nil
+    width = nil
+    height = nil
+    preventMove = false
+    preventResize = false
+    autoStart = false
+    maxLength = 300
+  }
+  
+  init(from action: RecordAction) {
+    self.frameRate = action.fps ?? CapturaSettings.frameRate
+    self.outputFormats = action.outputs ?? CapturaSettings.outputFormats
+    self.backendFormat = action.backendOutput ?? CapturaSettings.backendFormat
+    if action.skipBackend {
+      self.backend = nil
+    } else {
+      self.backend = action.backend ?? CapturaSettings.backend
+    }
+    self.shouldKeepLocalFiles = action.keepLocalFiles ?? CapturaSettings.shouldKeepLocalFiles
+    self.x = action.x
+    self.y = action.y
+    self.width = action.width
+    self.height = action.height
+    preventMove = action.preventMove ?? false
+    preventResize = action.preventResize ?? false
+    autoStart = action.autoStart ?? false
+    maxLength = action.maxLength ?? 300
   }
   
   var shouldSaveMp4: Bool {
index 3395c549c65ba6a7734772354de906da45442f1e..f5941dc82bdbde22970439157d5088a2572eef23 100644 (file)
@@ -11,7 +11,7 @@ class RecordingWindow: NSWindow {
     self.contentView as! RecordingContentView
   }
   
-  init(_ button: NSRect?) {
+  init(_ configuration: CaptureSessionConfiguration, _ button: NSRect?) {
     
     let screens = NSScreen.screens
     var boundingBox = NSZeroRect
@@ -33,7 +33,7 @@ class RecordingWindow: NSWindow {
     self.titlebarAppearsTransparent = true
     self.setFrame(boundingBox, display: true)
     self.titleVisibility = .hidden
-    let recordingView = RecordingContentView()
+    let recordingView = RecordingContentView(configuration, frame: boundingBox)
     recordingView.frame = boundingBox
     recordingView.button = button
     self.contentView = recordingView
@@ -80,12 +80,41 @@ enum RecordingWindowState {
 
 class RecordingContentView: NSView {
   
+  init(_ configuration: CaptureSessionConfiguration, frame: NSRect) {
+    super.init(frame: frame)
+    preventResize = configuration.preventResize
+    preventMove = configuration.preventMove
+    autoStart = configuration.autoStart
+    
+    if configuration.x != nil || configuration.y != nil || configuration.width != nil || configuration.height != nil {
+      box = NSRect(
+        x: configuration.x ?? Int(frame.width / 2.0),
+        y: configuration.y ?? Int(frame.height / 2.0),
+        width: configuration.width ?? 400,
+        height: configuration.height ?? 400
+      )
+    }
+    
+    if autoStart {
+      DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
+        NotificationCenter.default.post(name: .startRecording, object: nil, userInfo: nil)
+      }
+    }
+  }
+  
+  required init?(coder: NSCoder) {
+    fatalError("init(coder:) has not been implemented")
+  }
+  
   public var button: NSRect? = nil
   @Published public var box: NSRect? = nil
   public var state: RecordingWindowState = .idle
   private var mouseLocation: NSPoint = NSPoint()
   private var origin: NSPoint = NSPoint()
   private var boxOrigin: NSPoint = NSPoint()
+  private var preventResize = false
+  private var preventMove = false
+  private var autoStart = false
   
   private var resizeBox: NSRect? {
     if let box {
@@ -222,18 +251,22 @@ class RecordingContentView: NSView {
         }
       }
       
-      if resizeBox!.contains(origin) {
+      if resizeBox!.contains(origin) && !preventResize {
         self.origin = NSPoint(x: box.minX, y: box.maxY)
         state = .resizing
         return
       }
-      if box.contains(origin) {
+      if box.contains(origin) && !preventMove {
         state = .moving
         self.boxOrigin = NSPoint(x: box.origin.x, y: box.origin.y)
         return
       }
     }
     
+    if preventResize || preventMove {
+      return
+    }
+    
     state = .drawing
   }