From: Ruben Beltran del Rio Date: Mon, 31 Jul 2023 16:32:58 +0000 (+0200) Subject: Add URL parsing for record action X-Git-Tag: 1.0.0~7 X-Git-Url: https://git.r.bdr.sh/rbdr/captura/commitdiff_plain/8e9321304ca80e9ada7ac01058a5f4603cb37fab?ds=inline;hp=ba17de891507da74fb07423803fd636a4457354c Add URL parsing for record action --- diff --git a/Captura/CapturaApp.swift b/Captura/CapturaApp.swift index d9c3ff0..eb6f02e 100644 --- a/Captura/CapturaApp.swift +++ b/Captura/CapturaApp.swift @@ -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 { diff --git a/Captura/Data/CapturaURLDecoder.swift b/Captura/Data/CapturaURLDecoder.swift index 3fa6412..f4c325b 100644 --- a/Captura/Data/CapturaURLDecoder.swift +++ b/Captura/Data/CapturaURLDecoder.swift @@ -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: diff --git a/Captura/Domain/CaptureSessionConfiguration.swift b/Captura/Domain/CaptureSessionConfiguration.swift index 69c1ebf..c2ebe75 100644 --- a/Captura/Domain/CaptureSessionConfiguration.swift +++ b/Captura/Domain/CaptureSessionConfiguration.swift @@ -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 { diff --git a/Captura/Presentation/Windows/RecordingWindow.swift b/Captura/Presentation/Windows/RecordingWindow.swift index 3395c54..f5941dc 100644 --- a/Captura/Presentation/Windows/RecordingWindow.swift +++ b/Captura/Presentation/Windows/RecordingWindow.swift @@ -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 }