Skip to content

Commit

Permalink
Merge pull request #96 from cparish312/more_robust_screenshot_loop
Browse files Browse the repository at this point in the history
startScreenCapture in loop
  • Loading branch information
jasonjmcghee authored May 16, 2024
2 parents 9e6730d + 23d208f commit f4f6804
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions rem/remApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
private var lastImageData: Data? = nil
private var lastActiveApplication: String? = nil
private var lastDisplayID: UInt32? = nil
private var screenshotRetries: Int = 0
private var screenCaptureRetries: Int = 0


private var imageResizer = ImageResizer(
Expand Down Expand Up @@ -349,15 +349,16 @@ func drawStatusBarIcon(rect: CGRect) -> Bool {
return false
}

private func retryScreenshot(shareableContent: SCShareableContent) {
if screenshotRetries < 3 {
screenshotRetries += 1
screenshotQueue.asyncAfter(deadline: .now() + 2) { [weak self] in
self?.scheduleScreenshot(shareableContent: shareableContent)
private func retryScreenCapture() {
if screenCaptureRetries < 3 {
screenCaptureRetries += 1
Task {
try await Task.sleep(nanoseconds: 2_000_000_000)
await startScreenCapture()
}
} else {
disableRecording()
screenshotRetries = 0
screenCaptureRetries = 0
}
}

Expand Down Expand Up @@ -388,13 +389,13 @@ func drawStatusBarIcon(rect: CGRect) -> Bool {

guard displayID != nil else {
logger.debug("DisplayID is nil")
retryScreenshot(shareableContent: shareableContent)
retryScreenCapture()
return
}

guard let display = shareableContent.displays.first(where: { $0.displayID == displayID }) else {
logger.debug("Display could not be retrieved")
retryScreenshot(shareableContent: shareableContent)
retryScreenCapture()
return
}

Expand Down Expand Up @@ -452,7 +453,7 @@ func drawStatusBarIcon(rect: CGRect) -> Bool {
logger.error("Error taking screenshot: \(error)")
}

screenshotRetries = 0
screenCaptureRetries = 0
screenshotQueue.asyncAfter(deadline: .now() + 2) { [weak self] in
self?.scheduleScreenshot(shareableContent: shareableContent)
}
Expand Down

0 comments on commit f4f6804

Please sign in to comment.