]> git.r.bdr.sh - rbdr/captura/blame - Captura/CapturaApp.swift
Add multimonitor support
[rbdr/captura] / Captura / CapturaApp.swift
CommitLineData
a4e80427 1import SwiftUI
a4e80427
RBR
2import Cocoa
3import Combine
c9b9e1d6 4import AVFoundation
a4e80427
RBR
5
6@main
7struct CapturaApp: App {
153f3309 8
a4e80427
RBR
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"))
533cd932 18 //.modelContainer(for: CapturaRemoteFile.self)
a4e80427
RBR
19 }
20}
21
153f3309 22@objc(CapturaAppDelegate) class CapturaAppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
a4e80427
RBR
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
c9b9e1d6 32 var captureSession: CapturaCaptureSession? = nil
a4e80427 33 var images: [CGImage] = []
f5d16c1c 34 var outputFile: CapturaFile? = nil
a4e80427 35 var gifCallbackTimer = ContinuousClock.now
a4e80427
RBR
36 var pixelDensity: CGFloat = 1.0
37 var stopTimer: DispatchWorkItem?
533cd932 38 var remoteFiles: [CapturaRemoteFile] = []
ba17de89 39 var captureSessionConfiguration: CaptureSessionConfiguration = CaptureSessionConfiguration()
a4e80427 40
153f3309 41 @objc dynamic var scriptedPreferences: ScriptedPreferences = ScriptedPreferences()
377442f2 42
a4e80427 43 func applicationDidFinishLaunching(_ notification: Notification) {
533cd932 44 setupStatusBar()
a4e80427
RBR
45 NotificationCenter.default.addObserver(
46 self,
47 selector: #selector(self.didReceiveNotification(_:)),
48 name: nil,
49 object: nil)
50 closeWindow()
533cd932 51 fetchRemoteItems()
a4e80427
RBR
52 }
53
54 // MARK: - Setup Functions
55
56
533cd932 57 private func setupStatusBar() {
a4e80427
RBR
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
533cd932
RBR
73 setupMenu()
74 }
75
76 private func setupMenu() {
a4e80427 77
533cd932 78 statusItem.menu?.removeAllItems()
a4e80427 79
533cd932
RBR
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: ""))
a4e80427
RBR
94 }
95
96 private func closeWindow() {
97 if let window = NSApplication.shared.windows.first {
98 window.close()
99 }
100 }
153f3309 101
ba17de89
RBR
102 // MARK: - URL Event Handler
103
104 func application(_ application: NSApplication, open urls: [URL]) {
ba17de89
RBR
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):
e42019cd
RBR
110 NotificationCenter.default.post(name: .setConfiguration, object: nil, userInfo: [
111 "config": config
112 ])
ba17de89 113 case let .record(config):
153f3309 114 NotificationCenter.default.post(name: .setCaptureSessionConfiguration, object: nil, userInfo: [
377442f2
RBR
115 "config": config
116 ])
153f3309 117 NotificationCenter.default.post(name: .startAreaSelection, object: nil, userInfo: nil)
ba17de89
RBR
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
a4e80427
RBR
131 // MARK: - UI Event Handlers
132
133 func menuWillOpen(_ menu: NSMenu) {
134 if captureState != .idle {
135 menu.cancelTracking()
136 if captureState == .recording {
f5d16c1c 137 NotificationCenter.default.post(name: .stopRecording, object: nil, userInfo: nil)
a4e80427
RBR
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)
533cd932
RBR
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 }
a4e80427
RBR
169 }
170 }
171
172 @objc private func onQuit() {
173 NSApplication.shared.terminate(self)
174 }
175
a4e80427
RBR
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:
f5d16c1c
RBR
187 DispatchQueue.main.async {
188 self.finalizeRecording()
189 }
a4e80427
RBR
190 case .reset:
191 reset()
c9b9e1d6 192 case .failedToStart:
533cd932
RBR
193 DispatchQueue.main.async {
194 self.failed(true)
195 }
196 case .failedtoUpload:
197 DispatchQueue.main.async {
198 self.failed()
199 }
c9b9e1d6
RBR
200 case .receivedFrame:
201 if let frame = notification.userInfo?["frame"] {
202 receivedFrame(frame as! CVImageBuffer)
203 }
e42019cd
RBR
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 }
377442f2 212 case .reloadConfiguration:
e42019cd 213 reloadConfiguration()
377442f2
RBR
214 case .setCaptureSessionConfiguration:
215 if let userInfo = notification.userInfo {
216 if let config = userInfo["config"] as? RecordAction {
217 setCaptureSessionConfiguration(config)
218 }
219 }
533cd932
RBR
220 case .NSManagedObjectContextObjectsDidChange:
221 DispatchQueue.main.async {
222 self.fetchRemoteItems()
223 self.setupMenu()
224 }
a4e80427
RBR
225 default:
226 return
227 }
a4e80427
RBR
228 }
229
230
c9b9e1d6 231 func startAreaSelection() {
a4e80427 232 helpShown = false
a4e80427
RBR
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)
533cd932 238 NSApp.activate(ignoringOtherApps: true)
8e932130 239 recordingWindow = RecordingWindow(captureSessionConfiguration, rectInScreen)
533cd932
RBR
240 recordingWindow?.makeKeyAndOrderFront(nil)
241 recordingWindow?.orderFrontRegardless()
c9b9e1d6
RBR
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.")
a4e80427
RBR
250 }
251 }
c9b9e1d6 252 }
a4e80427
RBR
253 }
254 }
255 }
256
257 func startRecording() {
258 captureState = .recording
c9b9e1d6 259 updateImage()
f5d16c1c 260 outputFile = nil
a4e80427
RBR
261 images = [];
262 pixelDensity = recordingWindow?.pixelDensity ?? 1.0
c9b9e1d6
RBR
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 }
8e932130 273 DispatchQueue.main.asyncAfter(deadline: .now() + Double(captureSessionConfiguration.maxLength), execute: stopTimer!)
a4e80427 274
c9b9e1d6 275 outputFile = CapturaFile()
ba17de89 276 if captureSessionConfiguration.shouldSaveMp4 {
c9b9e1d6
RBR
277 captureSession.startRecording(to: outputFile!.mp4URL)
278 } else {
a4e80427 279 captureSession.startRunning()
a4e80427 280 }
c9b9e1d6 281 return
a4e80427
RBR
282 }
283 }
284 }
c9b9e1d6 285 NotificationCenter.default.post(name: .failedToStart, object: nil, userInfo: nil)
a4e80427
RBR
286 }
287
288 func stopRecording() {
a4e80427 289 captureState = .uploading
c9b9e1d6
RBR
290 updateImage()
291 stop()
f5d16c1c 292
a4e80427 293 Task.detached {
ba17de89 294 if self.captureSessionConfiguration.shouldSaveGif {
533cd932 295 if let outputFile = self.outputFile {
ba17de89 296 await GifRenderer.render(self.images, at: self.captureSessionConfiguration.frameRate, to: outputFile.gifURL)
533cd932
RBR
297 }
298 }
299 let wasSuccessful = await self.uploadOrCopy()
300 if wasSuccessful {
f5d16c1c 301 NotificationCenter.default.post(name: .finalizeRecording, object: nil, userInfo: nil)
533cd932
RBR
302 } else {
303 NotificationCenter.default.post(name: .failedtoUpload, object: nil, userInfo: nil)
a4e80427
RBR
304 }
305 }
a4e80427
RBR
306 }
307
308 func finalizeRecording() {
309 captureState = .uploaded
c9b9e1d6 310 updateImage()
f5d16c1c 311 DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
c9b9e1d6 312 NotificationCenter.default.post(name: .reset, object: nil, userInfo: nil)
f5d16c1c 313 }
a4e80427
RBR
314 }
315
316 func reset() {
a4e80427 317 captureState = .idle
c9b9e1d6 318 updateImage()
8e932130 319 captureSessionConfiguration = CaptureSessionConfiguration()
c9b9e1d6 320 stop()
a4e80427
RBR
321 }
322
c9b9e1d6 323 func receivedFrame(_ frame: CVImageBuffer) {
a4e80427
RBR
324 let now = ContinuousClock.now
325
ba17de89 326 if now - gifCallbackTimer > .nanoseconds(1_000_000_000 / UInt64(captureSessionConfiguration.frameRate)) {
a4e80427
RBR
327 gifCallbackTimer = now
328 DispatchQueue.main.async {
c9b9e1d6
RBR
329 if let cgImage = frame.cgImage?.resize(by: self.pixelDensity) {
330 self.images.append(cgImage)
a4e80427
RBR
331 }
332 }
333 }
334 }
335
533cd932 336 func failed(_ requestPermission: Bool = false) {
c9b9e1d6
RBR
337 captureState = .error
338 updateImage()
533cd932
RBR
339 if requestPermission {
340 requestPermissionToRecord()
341 }
c9b9e1d6
RBR
342 stop()
343 DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
344 NotificationCenter.default.post(name: .reset, object: nil, userInfo: nil)
345 }
346 }
347
e42019cd
RBR
348 func setConfiguration(_ config: ConfigureAction) {
349 CapturaSettings.apply(config)
350 }
351
377442f2
RBR
352 func reloadConfiguration() {
353 self.captureSessionConfiguration = CaptureSessionConfiguration()
354 }
355
356 func setCaptureSessionConfiguration(_ config: RecordAction) {
357 self.captureSessionConfiguration = CaptureSessionConfiguration(from: config)
358 }
359
533cd932
RBR
360 // MARK: - CoreData
361
362 private func fetchRemoteItems() {
363 let viewContext = PersistenceController.shared.container.viewContext
364 let fetchRequest = NSFetchRequest<CapturaRemoteFile>(entityName: "CapturaRemoteFile")
365 fetchRequest.fetchLimit = 5
366 fetchRequest.sortDescriptors = [NSSortDescriptor(key: "timestamp", ascending: false)]
367
368 let results = try? viewContext.fetch(fetchRequest)
369 remoteFiles = results ?? []
370 }
371
c9b9e1d6
RBR
372 // MARK: - Presentation Helpers
373
374
375 private func requestPermissionToRecord() {
376 showPopoverWithMessage("Please grant Captura permission to record")
377 if let url = URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenRecording") {
378 NSWorkspace.shared.open(url)
379 }
380 }
381
a4e80427
RBR
382 private func showPopoverWithMessage(_ message: String) {
383 if let button = statusItem.button {
384 (self.popover?.contentViewController as? HelpPopoverViewController)?.updateLabel(message)
385 self.popover?.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
386 DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
387 self.popover?.performClose(nil)
388 }
389 }
390 }
391
c9b9e1d6
RBR
392 private func updateImage() {
393 if let button = statusItem.button {
394 let image: String = switch captureState {
395 case .idle:
396 "rectangle.dashed.badge.record"
397 case .selectingArea:
398 "circle.rectangle.dashed"
399 case .recording:
400 "checkmark.rectangle"
401 case .uploading:
402 "dock.arrow.up.rectangle"
403 case .uploaded:
404 "checkmark.rectangle.fill"
405 case .error:
406 "xmark.rectangle.fill"
407 }
408 button.image = NSImage(systemSymbolName: image, accessibilityDescription: "Captura")
409 }
410 }
411
412 private func stop() {
413 stopTimer?.cancel()
414 captureSession?.stopRunning()
415 captureSession = nil
416 boxListener?.cancel()
417 recordingWindow?.close()
418 recordingWindow = nil
419 }
a4e80427 420
533cd932 421 private func uploadOrCopy() async -> Bool {
ba17de89 422 if captureSessionConfiguration.shouldUseBackend {
533cd932 423 let result = await uploadToBackend()
ba17de89 424 if result && !captureSessionConfiguration.shouldKeepLocalFiles {
533cd932
RBR
425 deleteLocalFiles()
426 }
427 return result
428 } else {
429 copyLocalToClipboard()
430 return true
431 }
432 }
433
434 private func copyLocalToClipboard() {
ba17de89
RBR
435 let fileType: NSPasteboard.PasteboardType = .init(rawValue: captureSessionConfiguration.shouldSaveGif ? "com.compuserve.gif" : "public.mpeg-4")
436 if let url = captureSessionConfiguration.shouldSaveGif ? outputFile?.gifURL : outputFile?.mp4URL {
c9b9e1d6
RBR
437 if let data = try? Data(contentsOf: url) {
438 let pasteboard = NSPasteboard.general
439 pasteboard.declareTypes([fileType], owner: nil)
440 pasteboard.setData(data, forType: fileType)
441 }
442 }
443 }
533cd932
RBR
444
445 private func uploadToBackend() async -> Bool {
ba17de89
RBR
446 let contentType = captureSessionConfiguration.shouldUploadGif ? "image/gif" : "video/mp4"
447 if let url = captureSessionConfiguration.shouldUploadGif ? outputFile?.gifURL : outputFile?.mp4URL {
533cd932 448 if let data = try? Data(contentsOf: url) {
ba17de89 449 if let remoteUrl = captureSessionConfiguration.backend {
533cd932
RBR
450 var request = URLRequest(url: remoteUrl)
451 request.httpMethod = "POST"
452 request.httpBody = data
453 request.setValue(contentType, forHTTPHeaderField: "Content-Type")
454 request.setValue("Captura/1.0", forHTTPHeaderField: "User-Agent")
455
456 do {
457 let (data, response) = try await URLSession.shared.data(for: request)
458
459 if let httpResponse = response as? HTTPURLResponse {
460 if httpResponse.statusCode == 201 {
461 let answer = try JSONDecoder().decode(BackendResponse.self, from: data)
462 createRemoteFile(answer.url)
463 return true
464 }
465 }
466 } catch {}
467 }
468 }
469 }
470 return false
471 }
472
473 private func createRemoteFile(_ url: URL) {
474 let viewContext = PersistenceController.shared.container.viewContext
475 let remoteFile = CapturaRemoteFile(context: viewContext)
476 remoteFile.url = url.absoluteString
477 remoteFile.timestamp = Date()
478 try? viewContext.save()
479 let pasteboard = NSPasteboard.general
480 pasteboard.declareTypes([.URL], owner: nil)
481 pasteboard.setString(url.absoluteString, forType: .string)
482 }
483
484 private func deleteLocalFiles() {
ba17de89 485 if captureSessionConfiguration.shouldSaveGif {
533cd932
RBR
486 if let url = outputFile?.gifURL {
487 try? FileManager.default.removeItem(at: url)
488 }
489 }
ba17de89 490 if captureSessionConfiguration.shouldSaveMp4 {
533cd932
RBR
491 if let url = outputFile?.mp4URL {
492 try? FileManager.default.removeItem(at: url)
493 }
494 }
495 }
a4e80427 496}