]> git.r.bdr.sh - rbdr/captura/blob - Captura/CapturaApp.swift
Add multimonitor support
[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.cancelTrackingWithoutAnimation()
136 if captureState == .selectingArea {
137 NotificationCenter.default.post(name: .startRecording, object: nil, userInfo: nil)
138 return
139 }
140 if captureState == .recording {
141 NotificationCenter.default.post(name: .stopRecording, object: nil, userInfo: nil)
142 return
143 }
144 }
145 }
146
147 @objc private func onClickStartRecording() {
148 NotificationCenter.default.post(name: .startAreaSelection, object: nil, userInfo: nil)
149 }
150
151 @objc private func onOpenPreferences() {
152 NSApp.activate(ignoringOtherApps: true)
153 if preferencesWindow == nil {
154 preferencesWindow = PreferencesWindow()
155 } else {
156 preferencesWindow?.makeKeyAndOrderFront(nil)
157 preferencesWindow?.orderFrontRegardless()
158 }
159 }
160
161 @objc private func onOpenFolder() {
162 if let directory = FileManager.default.urls(for: .picturesDirectory, in: .userDomainMask).first?.appendingPathComponent("captura") {
163 NSWorkspace.shared.open(directory)
164 }
165 }
166
167 @objc private func onClickRemoteFile(_ sender: NSMenuItem) {
168 if let remoteFile = sender.representedObject as? CapturaRemoteFile {
169 if let urlString = remoteFile.url {
170 if let url = URL(string: urlString) {
171 NSWorkspace.shared.open(url)
172 }
173 }
174 }
175 }
176
177 @objc private func onQuit() {
178 NSApplication.shared.terminate(self)
179 }
180
181 // MARK: - App State Event Listeners
182
183 @objc func didReceiveNotification(_ notification: Notification) {
184 switch(notification.name) {
185 case .startAreaSelection:
186 startAreaSelection()
187 case .startRecording:
188 startRecording()
189 case .stopRecording:
190 stopRecording()
191 case .finalizeRecording:
192 DispatchQueue.main.async {
193 self.finalizeRecording()
194 }
195 case .reset:
196 reset()
197 case .failedToStart:
198 DispatchQueue.main.async {
199 self.failed(true)
200 }
201 case .failedtoUpload:
202 DispatchQueue.main.async {
203 self.failed()
204 }
205 case .receivedFrame:
206 if let frame = notification.userInfo?["frame"] {
207 receivedFrame(frame as! CVImageBuffer)
208 }
209 case .setConfiguration:
210 DispatchQueue.main.async {
211 if let userInfo = notification.userInfo {
212 if let config = userInfo["config"] as? ConfigureAction {
213 self.setConfiguration(config)
214 }
215 }
216 }
217 case .reloadConfiguration:
218 reloadConfiguration()
219 case .setCaptureSessionConfiguration:
220 if let userInfo = notification.userInfo {
221 if let config = userInfo["config"] as? RecordAction {
222 setCaptureSessionConfiguration(config)
223 }
224 }
225 case .NSManagedObjectContextObjectsDidChange:
226 DispatchQueue.main.async {
227 self.fetchRemoteItems()
228 self.setupMenu()
229 }
230 default:
231 return
232 }
233 }
234
235
236 func startAreaSelection() {
237 helpShown = false
238 if captureState != .selectingArea {
239 captureState = .selectingArea
240 if let button = statusItem.button {
241 let rectInWindow = button.convert(button.bounds, to: nil)
242 let rectInScreen = button.window?.convertToScreen(rectInWindow)
243 NSApp.activate(ignoringOtherApps: true)
244 recordingWindow = RecordingWindow(captureSessionConfiguration, rectInScreen)
245 recordingWindow?.makeKeyAndOrderFront(nil)
246 recordingWindow?.orderFrontRegardless()
247 boxListener = recordingWindow?.recordingContentView.$box
248 .debounce(for: .seconds(0.3), scheduler: RunLoop.main)
249 .sink { newValue in
250 if newValue != nil {
251 self.updateImage()
252 if !self.helpShown {
253 self.helpShown = true
254 self.showPopoverWithMessage("Click here when you're ready to record.")
255 }
256 }
257 }
258 }
259 }
260 }
261
262 func startRecording() {
263 captureState = .recording
264 updateImage()
265 outputFile = nil
266 images = [];
267 pixelDensity = recordingWindow?.pixelDensity ?? 1.0
268 recordingWindow?.recordingContentView.startRecording()
269 if let box = recordingWindow?.recordingContentView.box {
270 if let screen = recordingWindow?.screen {
271 captureSession = CapturaCaptureSession(screen, box: box)
272
273 if let captureSession {
274
275 stopTimer = DispatchWorkItem {
276 self.stopRecording()
277 }
278 DispatchQueue.main.asyncAfter(deadline: .now() + Double(captureSessionConfiguration.maxLength), execute: stopTimer!)
279
280 outputFile = CapturaFile()
281 if captureSessionConfiguration.shouldSaveMp4 {
282 captureSession.startRecording(to: outputFile!.mp4URL)
283 } else {
284 captureSession.startRunning()
285 }
286 return
287 }
288 }
289 }
290 NotificationCenter.default.post(name: .failedToStart, object: nil, userInfo: nil)
291 }
292
293 func stopRecording() {
294 captureState = .uploading
295 updateImage()
296 stop()
297
298 Task.detached {
299 if self.captureSessionConfiguration.shouldSaveGif {
300 if let outputFile = self.outputFile {
301 await GifRenderer.render(self.images, at: self.captureSessionConfiguration.frameRate, to: outputFile.gifURL)
302 }
303 }
304 let wasSuccessful = await self.uploadOrCopy()
305 if wasSuccessful {
306 NotificationCenter.default.post(name: .finalizeRecording, object: nil, userInfo: nil)
307 } else {
308 NotificationCenter.default.post(name: .failedtoUpload, object: nil, userInfo: nil)
309 }
310 }
311 }
312
313 func finalizeRecording() {
314 captureState = .uploaded
315 updateImage()
316 DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
317 NotificationCenter.default.post(name: .reset, object: nil, userInfo: nil)
318 }
319 }
320
321 func reset() {
322 captureState = .idle
323 updateImage()
324 captureSessionConfiguration = CaptureSessionConfiguration()
325 stop()
326 }
327
328 func receivedFrame(_ frame: CVImageBuffer) {
329 let now = ContinuousClock.now
330
331 if now - gifCallbackTimer > .nanoseconds(1_000_000_000 / UInt64(captureSessionConfiguration.frameRate)) {
332 gifCallbackTimer = now
333 DispatchQueue.main.async {
334 if var cgImage = frame.cgImage {
335 if self.pixelDensity > 1 {
336 cgImage = cgImage.resize(by: self.pixelDensity) ?? cgImage
337 }
338 self.images.append(cgImage)
339 }
340 }
341 }
342 }
343
344 func failed(_ requestPermission: Bool = false) {
345 captureState = .error
346 updateImage()
347 if requestPermission {
348 requestPermissionToRecord()
349 }
350 stop()
351 DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
352 NotificationCenter.default.post(name: .reset, object: nil, userInfo: nil)
353 }
354 }
355
356 func setConfiguration(_ config: ConfigureAction) {
357 CapturaSettings.apply(config)
358 }
359
360 func reloadConfiguration() {
361 self.captureSessionConfiguration = CaptureSessionConfiguration()
362 }
363
364 func setCaptureSessionConfiguration(_ config: RecordAction) {
365 self.captureSessionConfiguration = CaptureSessionConfiguration(from: config)
366 }
367
368 // MARK: - CoreData
369
370 private func fetchRemoteItems() {
371 let viewContext = PersistenceController.shared.container.viewContext
372 let fetchRequest = NSFetchRequest<CapturaRemoteFile>(entityName: "CapturaRemoteFile")
373 fetchRequest.fetchLimit = 5
374 fetchRequest.sortDescriptors = [NSSortDescriptor(key: "timestamp", ascending: false)]
375
376 let results = try? viewContext.fetch(fetchRequest)
377 remoteFiles = results ?? []
378 }
379
380 // MARK: - Presentation Helpers
381
382
383 private func requestPermissionToRecord() {
384 showPopoverWithMessage("Please grant Captura permission to record")
385 if let url = URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenRecording") {
386 NSWorkspace.shared.open(url)
387 }
388 }
389
390 private func showPopoverWithMessage(_ message: String) {
391 if let button = statusItem.button {
392 (self.popover?.contentViewController as? HelpPopoverViewController)?.updateLabel(message)
393 self.popover?.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
394 DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
395 self.popover?.performClose(nil)
396 }
397 }
398 }
399
400 private func updateImage() {
401 if let button = statusItem.button {
402 let image: String = switch captureState {
403 case .idle:
404 "rectangle.dashed.badge.record"
405 case .selectingArea:
406 "circle.rectangle.dashed"
407 case .recording:
408 "checkmark.rectangle"
409 case .uploading:
410 "dock.arrow.up.rectangle"
411 case .uploaded:
412 "checkmark.rectangle.fill"
413 case .error:
414 "xmark.rectangle.fill"
415 }
416 button.image = NSImage(systemSymbolName: image, accessibilityDescription: "Captura")
417 }
418 }
419
420 private func stop() {
421 stopTimer?.cancel()
422 captureSession?.stopRunning()
423 captureSession = nil
424 boxListener?.cancel()
425 recordingWindow?.close()
426 recordingWindow = nil
427 }
428
429 private func uploadOrCopy() async -> Bool {
430 if captureSessionConfiguration.shouldUseBackend {
431 let result = await uploadToBackend()
432 if result && !captureSessionConfiguration.shouldKeepLocalFiles {
433 deleteLocalFiles()
434 }
435 return result
436 } else {
437 copyLocalToClipboard()
438 return true
439 }
440 }
441
442 private func copyLocalToClipboard() {
443 let fileType: NSPasteboard.PasteboardType = .init(rawValue: captureSessionConfiguration.shouldSaveGif ? "com.compuserve.gif" : "public.mpeg-4")
444 if let url = captureSessionConfiguration.shouldSaveGif ? outputFile?.gifURL : outputFile?.mp4URL {
445 if let data = try? Data(contentsOf: url) {
446 let pasteboard = NSPasteboard.general
447 pasteboard.declareTypes([fileType], owner: nil)
448 pasteboard.setData(data, forType: fileType)
449 }
450 }
451 }
452
453 private func uploadToBackend() async -> Bool {
454 let contentType = captureSessionConfiguration.shouldUploadGif ? "image/gif" : "video/mp4"
455 if let url = captureSessionConfiguration.shouldUploadGif ? outputFile?.gifURL : outputFile?.mp4URL {
456 if let data = try? Data(contentsOf: url) {
457 if let remoteUrl = captureSessionConfiguration.backend {
458 var request = URLRequest(url: remoteUrl)
459 request.httpMethod = "POST"
460 request.httpBody = data
461 request.setValue(contentType, forHTTPHeaderField: "Content-Type")
462 request.setValue("Captura/1.0", forHTTPHeaderField: "User-Agent")
463
464 do {
465 let (data, response) = try await URLSession.shared.data(for: request)
466
467 if let httpResponse = response as? HTTPURLResponse {
468 if httpResponse.statusCode == 201 {
469 let answer = try JSONDecoder().decode(BackendResponse.self, from: data)
470 createRemoteFile(answer.url)
471 return true
472 }
473 }
474 } catch {}
475 }
476 }
477 }
478 return false
479 }
480
481 private func createRemoteFile(_ url: URL) {
482 let viewContext = PersistenceController.shared.container.viewContext
483 let remoteFile = CapturaRemoteFile(context: viewContext)
484 remoteFile.url = url.absoluteString
485 remoteFile.timestamp = Date()
486 try? viewContext.save()
487 let pasteboard = NSPasteboard.general
488 pasteboard.declareTypes([.URL], owner: nil)
489 pasteboard.setString(url.absoluteString, forType: .string)
490 }
491
492 private func deleteLocalFiles() {
493 if captureSessionConfiguration.shouldSaveGif {
494 if let url = outputFile?.gifURL {
495 try? FileManager.default.removeItem(at: url)
496 }
497 }
498 if captureSessionConfiguration.shouldSaveMp4 {
499 if let url = outputFile?.mp4URL {
500 try? FileManager.default.removeItem(at: url)
501 }
502 }
503 }
504 }