]> git.r.bdr.sh - rbdr/captura/blob - Captura/CapturaApp.swift
67709a5df051374cad5322d3fe54b20fe7f7e972
[rbdr/captura] / Captura / CapturaApp.swift
1 import SwiftUI
2 import Cocoa
3 import Combine
4 import AVFoundation
5
6 @main
7 struct CapturaApp: App {
8
9 @NSApplicationDelegateAdaptor(CapturaAppDelegate.self) var appDelegate
10
11 var body: some Scene {
12 WindowGroup {
13 PreferencesScreen()
14 .handlesExternalEvents(preferring: Set(arrayLiteral: "PreferencesScreen"), allowing: Set(arrayLiteral: "*"))
15 .frame(width: 650, height: 450)
16 }
17 .handlesExternalEvents(matching: Set(arrayLiteral: "PreferencesScreen"))
18 //.modelContainer(for: CapturaRemoteFile.self)
19 }
20 }
21
22 @objc(CapturaAppDelegate) class CapturaAppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
23
24 @Environment(\.openURL) var openURL
25 var statusItem: NSStatusItem!
26 var captureState: CaptureState = .idle
27 var recordingWindow: RecordingWindow? = nil
28 var preferencesWindow: PreferencesWindow? = nil
29 var boxListener: AnyCancellable? = nil
30 var popover: NSPopover? = nil
31 var helpShown = false
32 var captureSession: CapturaCaptureSession? = nil
33 var images: [CGImage] = []
34 var outputFile: CapturaFile? = nil
35 var gifCallbackTimer = ContinuousClock.now
36 var pixelDensity: CGFloat = 1.0
37 var stopTimer: DispatchWorkItem?
38 var remoteFiles: [CapturaRemoteFile] = []
39 var captureSessionConfiguration: CaptureSessionConfiguration = CaptureSessionConfiguration()
40
41 @objc dynamic var scriptedPreferences: ScriptedPreferences = ScriptedPreferences()
42
43 func applicationDidFinishLaunching(_ notification: Notification) {
44 setupStatusBar()
45 NotificationCenter.default.addObserver(
46 self,
47 selector: #selector(self.didReceiveNotification(_:)),
48 name: nil,
49 object: nil)
50 closeWindow()
51 fetchRemoteItems()
52 }
53
54 // MARK: - Setup Functions
55
56
57 private func setupStatusBar() {
58 statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
59
60 if let button = statusItem.button {
61 button.image = NSImage(systemSymbolName: "rectangle.dashed.badge.record", accessibilityDescription: "Captura")
62 }
63
64 statusItem.isVisible = true
65 statusItem.menu = NSMenu()
66 statusItem.menu?.delegate = self
67
68 // Create the Popover
69 popover = NSPopover()
70 popover?.contentViewController = HelpPopoverViewController()
71 popover?.behavior = .transient
72
73 setupMenu()
74 }
75
76 private func setupMenu() {
77
78 statusItem.menu?.removeAllItems()
79
80 statusItem.menu?.addItem(NSMenuItem(title: "Record", action: #selector(CapturaAppDelegate.onClickStartRecording), keyEquivalent: ""))
81 if (remoteFiles.count > 0) {
82 statusItem.menu?.addItem(NSMenuItem.separator())
83 for remoteFile in remoteFiles {
84 let remoteFileItem = NSMenuItem(title: remoteFile.name, action: #selector(CapturaAppDelegate.onClickRemoteFile), keyEquivalent: "")
85 remoteFileItem.representedObject = remoteFile
86 statusItem.menu?.addItem(remoteFileItem)
87 }
88 }
89 statusItem.menu?.addItem(NSMenuItem.separator())
90 statusItem.menu?.addItem(NSMenuItem(title: "Open Local Folder", action: #selector(CapturaAppDelegate.onOpenFolder), keyEquivalent: ""))
91 statusItem.menu?.addItem(NSMenuItem.separator())
92 statusItem.menu?.addItem(NSMenuItem(title: "Preferences", action: #selector(CapturaAppDelegate.onOpenPreferences), keyEquivalent: ""))
93 statusItem.menu?.addItem(NSMenuItem(title: "Quit", action: #selector(CapturaAppDelegate.onQuit), keyEquivalent: ""))
94 }
95
96 private func closeWindow() {
97 if let window = NSApplication.shared.windows.first {
98 window.close()
99 }
100 }
101
102 // MARK: - URL Event Handler
103
104 func application(_ application: NSApplication, open urls: [URL]) {
105 if (CapturaSettings.shouldAllowURLAutomation) {
106 for url in urls {
107 if let action = CapturaURLDecoder.decodeParams(url: url) {
108 switch action {
109 case let .configure(config):
110 NotificationCenter.default.post(name: .setConfiguration, object: nil, userInfo: [
111 "config": config
112 ])
113 case let .record(config):
114 NotificationCenter.default.post(name: .setCaptureSessionConfiguration, object: nil, userInfo: [
115 "config": config
116 ])
117 NotificationCenter.default.post(name: .startAreaSelection, object: nil, userInfo: nil)
118 }
119 }
120 }
121 } else {
122 let alert = NSAlert()
123 alert.messageText = "URL Automation Prevented"
124 alert.informativeText = "A website or application attempted to record your screen using URL Automation. If you want to allow this, enable it in Preferences."
125 alert.alertStyle = .warning
126 alert.addButton(withTitle: "OK")
127 alert.runModal()
128 }
129 }
130
131 // MARK: - UI Event Handlers
132
133 func menuWillOpen(_ menu: NSMenu) {
134 if captureState != .idle {
135 menu.cancelTracking()
136 if captureState == .recording {
137 NotificationCenter.default.post(name: .stopRecording, object: nil, userInfo: nil)
138 }
139 }
140 }
141
142 @objc private func onClickStartRecording() {
143 NotificationCenter.default.post(name: .startAreaSelection, object: nil, userInfo: nil)
144 }
145
146 @objc private func onOpenPreferences() {
147 NSApp.activate(ignoringOtherApps: true)
148 if preferencesWindow == nil {
149 preferencesWindow = PreferencesWindow()
150 } else {
151 preferencesWindow?.makeKeyAndOrderFront(nil)
152 preferencesWindow?.orderFrontRegardless()
153 }
154 }
155
156 @objc private func onOpenFolder() {
157 if let directory = FileManager.default.urls(for: .picturesDirectory, in: .userDomainMask).first?.appendingPathComponent("captura") {
158 NSWorkspace.shared.open(directory)
159 }
160 }
161
162 @objc private func onClickRemoteFile(_ sender: NSMenuItem) {
163 if let remoteFile = sender.representedObject as? CapturaRemoteFile {
164 if let urlString = remoteFile.url {
165 if let url = URL(string: urlString) {
166 NSWorkspace.shared.open(url)
167 }
168 }
169 }
170 }
171
172 @objc private func onQuit() {
173 NSApplication.shared.terminate(self)
174 }
175
176 // MARK: - App State Event Listeners
177
178 @objc func didReceiveNotification(_ notification: Notification) {
179 switch(notification.name) {
180 case .startAreaSelection:
181 startAreaSelection()
182 case .startRecording:
183 startRecording()
184 case .stopRecording:
185 stopRecording()
186 case .finalizeRecording:
187 DispatchQueue.main.async {
188 self.finalizeRecording()
189 }
190 case .reset:
191 reset()
192 case .failedToStart:
193 DispatchQueue.main.async {
194 self.failed(true)
195 }
196 case .failedtoUpload:
197 DispatchQueue.main.async {
198 self.failed()
199 }
200 case .receivedFrame:
201 if let frame = notification.userInfo?["frame"] {
202 receivedFrame(frame as! CVImageBuffer)
203 }
204 case .setConfiguration:
205 DispatchQueue.main.async {
206 if let userInfo = notification.userInfo {
207 if let config = userInfo["config"] as? ConfigureAction {
208 self.setConfiguration(config)
209 }
210 }
211 }
212 case .reloadConfiguration:
213 reloadConfiguration()
214 case .setCaptureSessionConfiguration:
215 if let userInfo = notification.userInfo {
216 if let config = userInfo["config"] as? RecordAction {
217 setCaptureSessionConfiguration(config)
218 }
219 }
220 case .NSManagedObjectContextObjectsDidChange:
221 DispatchQueue.main.async {
222 self.fetchRemoteItems()
223 self.setupMenu()
224 }
225 default:
226 return
227 }
228 }
229
230
231 func startAreaSelection() {
232 helpShown = false
233 if captureState != .selectingArea {
234 captureState = .selectingArea
235 if let button = statusItem.button {
236 let rectInWindow = button.convert(button.bounds, to: nil)
237 let rectInScreen = button.window?.convertToScreen(rectInWindow)
238 NSApp.activate(ignoringOtherApps: true)
239 recordingWindow = RecordingWindow(captureSessionConfiguration, rectInScreen)
240 recordingWindow?.makeKeyAndOrderFront(nil)
241 recordingWindow?.orderFrontRegardless()
242 boxListener = recordingWindow?.recordingContentView.$box
243 .debounce(for: .seconds(0.3), scheduler: RunLoop.main)
244 .sink { newValue in
245 if newValue != nil {
246 self.updateImage()
247 if !self.helpShown {
248 self.helpShown = true
249 self.showPopoverWithMessage("Click here when you're ready to record.")
250 }
251 }
252 }
253 }
254 }
255 }
256
257 func startRecording() {
258 captureState = .recording
259 updateImage()
260 outputFile = nil
261 images = [];
262 pixelDensity = recordingWindow?.pixelDensity ?? 1.0
263 recordingWindow?.recordingContentView.startRecording()
264 if let box = recordingWindow?.recordingContentView.box {
265 if let screen = recordingWindow?.screen {
266 captureSession = CapturaCaptureSession(screen, box: box)
267
268 if let captureSession {
269
270 stopTimer = DispatchWorkItem {
271 self.stopRecording()
272 }
273 DispatchQueue.main.asyncAfter(deadline: .now() + Double(captureSessionConfiguration.maxLength), execute: stopTimer!)
274
275 outputFile = CapturaFile()
276 if captureSessionConfiguration.shouldSaveMp4 {
277 captureSession.startRecording(to: outputFile!.mp4URL)
278 } else {
279 captureSession.startRunning()
280 }
281 return
282 }
283 }
284 }
285 NotificationCenter.default.post(name: .failedToStart, object: nil, userInfo: nil)
286 }
287
288 func stopRecording() {
289 captureState = .uploading
290 updateImage()
291 stop()
292
293 Task.detached {
294 if self.captureSessionConfiguration.shouldSaveGif {
295 if let outputFile = self.outputFile {
296 await GifRenderer.render(self.images, at: self.captureSessionConfiguration.frameRate, to: outputFile.gifURL)
297 }
298 }
299 let wasSuccessful = await self.uploadOrCopy()
300 if wasSuccessful {
301 NotificationCenter.default.post(name: .finalizeRecording, object: nil, userInfo: nil)
302 } else {
303 NotificationCenter.default.post(name: .failedtoUpload, object: nil, userInfo: nil)
304 }
305 }
306 }
307
308 func finalizeRecording() {
309 captureState = .uploaded
310 updateImage()
311 DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
312 NotificationCenter.default.post(name: .reset, object: nil, userInfo: nil)
313 }
314 }
315
316 func reset() {
317 captureState = .idle
318 updateImage()
319 captureSessionConfiguration = CaptureSessionConfiguration()
320 stop()
321 }
322
323 func receivedFrame(_ frame: CVImageBuffer) {
324 let now = ContinuousClock.now
325
326 if now - gifCallbackTimer > .nanoseconds(1_000_000_000 / UInt64(captureSessionConfiguration.frameRate)) {
327 gifCallbackTimer = now
328 DispatchQueue.main.async {
329 if var cgImage = frame.cgImage {
330 if self.pixelDensity > 1 {
331 cgImage = cgImage.resize(by: self.pixelDensity) ?? cgImage
332 }
333 self.images.append(cgImage)
334 }
335 }
336 }
337 }
338
339 func failed(_ requestPermission: Bool = false) {
340 captureState = .error
341 updateImage()
342 if requestPermission {
343 requestPermissionToRecord()
344 }
345 stop()
346 DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
347 NotificationCenter.default.post(name: .reset, object: nil, userInfo: nil)
348 }
349 }
350
351 func setConfiguration(_ config: ConfigureAction) {
352 CapturaSettings.apply(config)
353 }
354
355 func reloadConfiguration() {
356 self.captureSessionConfiguration = CaptureSessionConfiguration()
357 }
358
359 func setCaptureSessionConfiguration(_ config: RecordAction) {
360 self.captureSessionConfiguration = CaptureSessionConfiguration(from: config)
361 }
362
363 // MARK: - CoreData
364
365 private func fetchRemoteItems() {
366 let viewContext = PersistenceController.shared.container.viewContext
367 let fetchRequest = NSFetchRequest<CapturaRemoteFile>(entityName: "CapturaRemoteFile")
368 fetchRequest.fetchLimit = 5
369 fetchRequest.sortDescriptors = [NSSortDescriptor(key: "timestamp", ascending: false)]
370
371 let results = try? viewContext.fetch(fetchRequest)
372 remoteFiles = results ?? []
373 }
374
375 // MARK: - Presentation Helpers
376
377
378 private func requestPermissionToRecord() {
379 showPopoverWithMessage("Please grant Captura permission to record")
380 if let url = URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenRecording") {
381 NSWorkspace.shared.open(url)
382 }
383 }
384
385 private func showPopoverWithMessage(_ message: String) {
386 if let button = statusItem.button {
387 (self.popover?.contentViewController as? HelpPopoverViewController)?.updateLabel(message)
388 self.popover?.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
389 DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
390 self.popover?.performClose(nil)
391 }
392 }
393 }
394
395 private func updateImage() {
396 if let button = statusItem.button {
397 let image: String = switch captureState {
398 case .idle:
399 "rectangle.dashed.badge.record"
400 case .selectingArea:
401 "circle.rectangle.dashed"
402 case .recording:
403 "checkmark.rectangle"
404 case .uploading:
405 "dock.arrow.up.rectangle"
406 case .uploaded:
407 "checkmark.rectangle.fill"
408 case .error:
409 "xmark.rectangle.fill"
410 }
411 button.image = NSImage(systemSymbolName: image, accessibilityDescription: "Captura")
412 }
413 }
414
415 private func stop() {
416 stopTimer?.cancel()
417 captureSession?.stopRunning()
418 captureSession = nil
419 boxListener?.cancel()
420 recordingWindow?.close()
421 recordingWindow = nil
422 }
423
424 private func uploadOrCopy() async -> Bool {
425 if captureSessionConfiguration.shouldUseBackend {
426 let result = await uploadToBackend()
427 if result && !captureSessionConfiguration.shouldKeepLocalFiles {
428 deleteLocalFiles()
429 }
430 return result
431 } else {
432 copyLocalToClipboard()
433 return true
434 }
435 }
436
437 private func copyLocalToClipboard() {
438 let fileType: NSPasteboard.PasteboardType = .init(rawValue: captureSessionConfiguration.shouldSaveGif ? "com.compuserve.gif" : "public.mpeg-4")
439 if let url = captureSessionConfiguration.shouldSaveGif ? outputFile?.gifURL : outputFile?.mp4URL {
440 if let data = try? Data(contentsOf: url) {
441 let pasteboard = NSPasteboard.general
442 pasteboard.declareTypes([fileType], owner: nil)
443 pasteboard.setData(data, forType: fileType)
444 }
445 }
446 }
447
448 private func uploadToBackend() async -> Bool {
449 let contentType = captureSessionConfiguration.shouldUploadGif ? "image/gif" : "video/mp4"
450 if let url = captureSessionConfiguration.shouldUploadGif ? outputFile?.gifURL : outputFile?.mp4URL {
451 if let data = try? Data(contentsOf: url) {
452 if let remoteUrl = captureSessionConfiguration.backend {
453 var request = URLRequest(url: remoteUrl)
454 request.httpMethod = "POST"
455 request.httpBody = data
456 request.setValue(contentType, forHTTPHeaderField: "Content-Type")
457 request.setValue("Captura/1.0", forHTTPHeaderField: "User-Agent")
458
459 do {
460 let (data, response) = try await URLSession.shared.data(for: request)
461
462 if let httpResponse = response as? HTTPURLResponse {
463 if httpResponse.statusCode == 201 {
464 let answer = try JSONDecoder().decode(BackendResponse.self, from: data)
465 createRemoteFile(answer.url)
466 return true
467 }
468 }
469 } catch {}
470 }
471 }
472 }
473 return false
474 }
475
476 private func createRemoteFile(_ url: URL) {
477 let viewContext = PersistenceController.shared.container.viewContext
478 let remoteFile = CapturaRemoteFile(context: viewContext)
479 remoteFile.url = url.absoluteString
480 remoteFile.timestamp = Date()
481 try? viewContext.save()
482 let pasteboard = NSPasteboard.general
483 pasteboard.declareTypes([.URL], owner: nil)
484 pasteboard.setString(url.absoluteString, forType: .string)
485 }
486
487 private func deleteLocalFiles() {
488 if captureSessionConfiguration.shouldSaveGif {
489 if let url = outputFile?.gifURL {
490 try? FileManager.default.removeItem(at: url)
491 }
492 }
493 if captureSessionConfiguration.shouldSaveMp4 {
494 if let url = outputFile?.mp4URL {
495 try? FileManager.default.removeItem(at: url)
496 }
497 }
498 }
499 }