7 struct CapturaApp: App {
9 @NSApplicationDelegateAdaptor(CapturaAppDelegate.self) var appDelegate
11 var body: some Scene {
14 .handlesExternalEvents(preferring: Set(arrayLiteral: "PreferencesScreen"), allowing: Set(arrayLiteral: "*"))
15 .frame(width: 650, height: 450)
17 .handlesExternalEvents(matching: Set(arrayLiteral: "PreferencesScreen"))
18 //.modelContainer(for: CapturaRemoteFile.self)
22 @objc(CapturaAppDelegate) class CapturaAppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
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
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()
41 @objc dynamic var scriptedPreferences: ScriptedPreferences = ScriptedPreferences()
43 func applicationDidFinishLaunching(_ notification: Notification) {
45 NotificationCenter.default.addObserver(
47 selector: #selector(self.didReceiveNotification(_:)),
54 // MARK: - Setup Functions
57 private func setupStatusBar() {
58 statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
60 if let button = statusItem.button {
61 button.image = NSImage(named: "Idle")
64 statusItem.isVisible = true
65 statusItem.menu = NSMenu()
66 statusItem.menu?.delegate = self
70 popover?.contentViewController = HelpPopoverViewController()
71 popover?.behavior = .transient
76 private func setupMenu() {
78 statusItem.menu?.removeAllItems()
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)
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: ""))
96 private func closeWindow() {
97 if let window = NSApplication.shared.windows.first {
102 // MARK: - URL Event Handler
104 func application(_ application: NSApplication, open urls: [URL]) {
105 if (CapturaSettings.shouldAllowURLAutomation) {
107 if let action = CapturaURLDecoder.decodeParams(url: url) {
109 case let .configure(config):
110 NotificationCenter.default.post(name: .setConfiguration, object: nil, userInfo: [
113 case let .record(config):
114 NotificationCenter.default.post(name: .setCaptureSessionConfiguration, object: nil, userInfo: [
117 NotificationCenter.default.post(name: .startAreaSelection, object: nil, userInfo: nil)
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")
131 // MARK: - UI Event Handlers
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)
140 if captureState == .recording {
141 NotificationCenter.default.post(name: .stopRecording, object: nil, userInfo: nil)
147 @objc private func onClickStartRecording() {
148 NotificationCenter.default.post(name: .startAreaSelection, object: nil, userInfo: nil)
151 @objc private func onOpenPreferences() {
152 NSApp.activate(ignoringOtherApps: true)
153 if preferencesWindow == nil {
154 preferencesWindow = PreferencesWindow()
156 preferencesWindow?.makeKeyAndOrderFront(nil)
157 preferencesWindow?.orderFrontRegardless()
161 @objc private func onOpenFolder() {
162 if let directory = FileManager.default.urls(for: .picturesDirectory, in: .userDomainMask).first?.appendingPathComponent("captura") {
163 NSWorkspace.shared.open(directory)
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)
177 @objc private func onQuit() {
178 NSApplication.shared.terminate(self)
181 // MARK: - App State Event Listeners
183 @objc func didReceiveNotification(_ notification: Notification) {
184 switch(notification.name) {
185 case .startAreaSelection:
187 case .startRecording:
191 case .finalizeRecording:
192 DispatchQueue.main.async {
193 self.finalizeRecording()
198 DispatchQueue.main.async {
201 case .failedtoUpload:
202 DispatchQueue.main.async {
206 if let frame = notification.userInfo?["frame"] {
207 receivedFrame(frame as! CVImageBuffer)
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)
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)
225 case .NSManagedObjectContextObjectsDidChange:
226 DispatchQueue.main.async {
227 self.fetchRemoteItems()
236 func startAreaSelection() {
238 if captureState != .selectingArea {
239 captureState = .selectingArea
241 if let button = statusItem.button {
242 let rectInWindow = button.convert(button.bounds, to: nil)
243 let rectInScreen = button.window?.convertToScreen(rectInWindow)
244 NSApp.activate(ignoringOtherApps: true)
245 recordingWindow = RecordingWindow(captureSessionConfiguration, rectInScreen)
246 recordingWindow?.makeKeyAndOrderFront(nil)
247 recordingWindow?.orderFrontRegardless()
248 boxListener = recordingWindow?.recordingContentView.$box
249 .debounce(for: .seconds(0.3), scheduler: RunLoop.main)
254 self.helpShown = true
255 self.showPopoverWithMessage("Click here when you're ready to record.")
263 func startRecording() {
264 captureState = .recording
268 pixelDensity = recordingWindow?.pixelDensity ?? 1.0
269 recordingWindow?.recordingContentView.startRecording()
270 if let box = recordingWindow?.recordingContentView.box {
271 if let screen = recordingWindow?.screen {
272 captureSession = CapturaCaptureSession(screen, box: box)
274 if let captureSession {
276 stopTimer = DispatchWorkItem {
279 DispatchQueue.main.asyncAfter(deadline: .now() + Double(captureSessionConfiguration.maxLength), execute: stopTimer!)
281 outputFile = CapturaFile()
282 if captureSessionConfiguration.shouldSaveMp4 {
283 captureSession.startRecording(to: outputFile!.mp4URL)
285 captureSession.startRunning()
291 NotificationCenter.default.post(name: .failedToStart, object: nil, userInfo: nil)
294 func stopRecording() {
295 captureState = .uploading
300 if self.captureSessionConfiguration.shouldSaveGif {
301 if let outputFile = self.outputFile {
302 await GifRenderer.render(self.images, at: self.captureSessionConfiguration.frameRate, to: outputFile.gifURL)
305 let wasSuccessful = await self.uploadOrCopy()
307 NotificationCenter.default.post(name: .finalizeRecording, object: nil, userInfo: nil)
309 NotificationCenter.default.post(name: .failedtoUpload, object: nil, userInfo: nil)
314 func finalizeRecording() {
315 captureState = .uploaded
317 DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
318 NotificationCenter.default.post(name: .reset, object: nil, userInfo: nil)
325 captureSessionConfiguration = CaptureSessionConfiguration()
329 func receivedFrame(_ frame: CVImageBuffer) {
330 let now = ContinuousClock.now
332 if now - gifCallbackTimer > .nanoseconds(1_000_000_000 / UInt64(captureSessionConfiguration.frameRate)) {
333 gifCallbackTimer = now
334 DispatchQueue.main.async {
335 if var cgImage = frame.cgImage {
336 if self.pixelDensity > 1 {
337 cgImage = cgImage.resize(by: self.pixelDensity) ?? cgImage
339 self.images.append(cgImage)
345 func failed(_ requestPermission: Bool = false) {
346 captureState = .error
348 if requestPermission {
349 requestPermissionToRecord()
352 DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
353 NotificationCenter.default.post(name: .reset, object: nil, userInfo: nil)
357 func setConfiguration(_ config: ConfigureAction) {
358 CapturaSettings.apply(config)
361 func reloadConfiguration() {
362 self.captureSessionConfiguration = CaptureSessionConfiguration()
365 func setCaptureSessionConfiguration(_ config: RecordAction) {
366 self.captureSessionConfiguration = CaptureSessionConfiguration(from: config)
371 private func fetchRemoteItems() {
372 let viewContext = PersistenceController.shared.container.viewContext
373 let fetchRequest = NSFetchRequest<CapturaRemoteFile>(entityName: "CapturaRemoteFile")
374 fetchRequest.fetchLimit = 5
375 fetchRequest.sortDescriptors = [NSSortDescriptor(key: "timestamp", ascending: false)]
377 let results = try? viewContext.fetch(fetchRequest)
378 remoteFiles = results ?? []
381 // MARK: - Presentation Helpers
384 private func requestPermissionToRecord() {
385 showPopoverWithMessage("Please grant Captura permission to record")
386 if let url = URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenRecording") {
387 NSWorkspace.shared.open(url)
391 private func showPopoverWithMessage(_ message: String) {
392 if let button = statusItem.button {
393 (self.popover?.contentViewController as? HelpPopoverViewController)?.updateLabel(message)
394 self.popover?.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
395 DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
396 self.popover?.performClose(nil)
401 private func updateImage() {
402 if let button = statusItem.button {
403 let image: String = switch captureState {
407 if recordingWindow?.recordingContentView.box != nil {
421 button.image = NSImage(named: image)
425 private func stop() {
427 captureSession?.stopRunning()
429 boxListener?.cancel()
430 recordingWindow?.close()
431 recordingWindow = nil
434 private func uploadOrCopy() async -> Bool {
435 if captureSessionConfiguration.shouldUseBackend {
436 let result = await uploadToBackend()
437 if result && !captureSessionConfiguration.shouldKeepLocalFiles {
442 copyLocalToClipboard()
447 private func copyLocalToClipboard() {
448 let fileType: NSPasteboard.PasteboardType = .init(rawValue: captureSessionConfiguration.shouldSaveGif ? "com.compuserve.gif" : "public.mpeg-4")
449 if let url = captureSessionConfiguration.shouldSaveGif ? outputFile?.gifURL : outputFile?.mp4URL {
450 if let data = try? Data(contentsOf: url) {
451 let pasteboard = NSPasteboard.general
452 pasteboard.declareTypes([fileType], owner: nil)
453 pasteboard.setData(data, forType: fileType)
458 private func uploadToBackend() async -> Bool {
459 let contentType = captureSessionConfiguration.shouldUploadGif ? "image/gif" : "video/mp4"
460 if let url = captureSessionConfiguration.shouldUploadGif ? outputFile?.gifURL : outputFile?.mp4URL {
461 if let data = try? Data(contentsOf: url) {
462 if let remoteUrl = captureSessionConfiguration.backend {
463 var request = URLRequest(url: remoteUrl)
464 request.httpMethod = "POST"
465 request.httpBody = data
466 request.setValue(contentType, forHTTPHeaderField: "Content-Type")
467 request.setValue("Captura/1.0", forHTTPHeaderField: "User-Agent")
470 let (data, response) = try await URLSession.shared.data(for: request)
472 if let httpResponse = response as? HTTPURLResponse {
473 if httpResponse.statusCode == 201 {
474 let answer = try JSONDecoder().decode(BackendResponse.self, from: data)
475 createRemoteFile(answer.url)
486 private func createRemoteFile(_ url: URL) {
487 let viewContext = PersistenceController.shared.container.viewContext
488 let remoteFile = CapturaRemoteFile(context: viewContext)
489 remoteFile.url = url.absoluteString
490 remoteFile.timestamp = Date()
491 try? viewContext.save()
492 let pasteboard = NSPasteboard.general
493 pasteboard.declareTypes([.URL], owner: nil)
494 pasteboard.setString(url.absoluteString, forType: .string)
497 private func deleteLocalFiles() {
498 if captureSessionConfiguration.shouldSaveGif {
499 if let url = outputFile?.gifURL {
500 try? FileManager.default.removeItem(at: url)
503 if captureSessionConfiguration.shouldSaveMp4 {
504 if let url = outputFile?.mp4URL {
505 try? FileManager.default.removeItem(at: url)