From 9da4e94ad6c8bb3ad78ed3dab89e01d1c3fd3e33 Mon Sep 17 00:00:00 2001 From: Joshua Sattler <34030048+jsattler@users.noreply.github.com> Date: Mon, 9 Feb 2026 21:48:30 +0100 Subject: [PATCH] fix(recording): gracefully handle when user stops sharing This commit is addressing an issue where the recording is not saved properly, when the user clicks on "Stop Sharing" via the native macOS sharing window in the menu bar. --- .../xcschemes/xcschememanagement.plist | 14 ------------ .../ViewModel/RecorderViewModel.swift | 22 ++++++++++++++----- 2 files changed, 17 insertions(+), 19 deletions(-) delete mode 100644 BetterCapture.xcodeproj/xcuserdata/jsattler.xcuserdatad/xcschemes/xcschememanagement.plist diff --git a/BetterCapture.xcodeproj/xcuserdata/jsattler.xcuserdatad/xcschemes/xcschememanagement.plist b/BetterCapture.xcodeproj/xcuserdata/jsattler.xcuserdatad/xcschemes/xcschememanagement.plist deleted file mode 100644 index ccfb225..0000000 --- a/BetterCapture.xcodeproj/xcuserdata/jsattler.xcuserdatad/xcschemes/xcschememanagement.plist +++ /dev/null @@ -1,14 +0,0 @@ - - - - - SchemeUserState - - BetterCapture.xcscheme_^#shared#^_ - - orderHint - 0 - - - - diff --git a/BetterCapture/ViewModel/RecorderViewModel.swift b/BetterCapture/ViewModel/RecorderViewModel.swift index 5e51b60..be83a2c 100644 --- a/BetterCapture/ViewModel/RecorderViewModel.swift +++ b/BetterCapture/ViewModel/RecorderViewModel.swift @@ -265,17 +265,29 @@ extension RecorderViewModel: CaptureEngineDelegate { } func captureEngine(_ engine: CaptureEngine, didStopWithError error: Error?) { - if let error { + // Check if user clicked "Stop Sharing" in the menu bar + let isUserStopped = (error as? SCStreamError)?.code == .userStopped + + if let error, !isUserStopped { lastError = error logger.error("Capture stopped with error: \(error.localizedDescription)") } // Clean up if we were recording if isRecording { - stopTimer() - assetWriter.cancel() - state = .idle - notificationService.sendRecordingStoppedNotification(error: error) + if isUserStopped { + // User clicked "Stop Sharing" - gracefully save the recording + logger.info("User stopped sharing via system UI, saving recording...") + Task { + await stopRecording() + } + } else { + // Unexpected error - cancel the recording + stopTimer() + assetWriter.cancel() + state = .idle + notificationService.sendRecordingStoppedNotification(error: error) + } } }