Skip to content

Commit

Permalink
Merge pull request #95 from cparish312/robust_screenshot_loop
Browse files Browse the repository at this point in the history
retryScreenshot for more Robust screenshot loop
  • Loading branch information
jasonjmcghee authored May 8, 2024
2 parents b36aa1b + 6f9fa7e commit 9e6730d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
3 changes: 3 additions & 0 deletions rem/DB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class DatabaseManager {
db = try! Connection("db.sqlite3")
}

try! db.run("PRAGMA journal_mode = WAL")
try! db.run("PRAGMA synchronous = NORMAL")

createTables()
currentChunkId = getCurrentChunkId()
lastFrameId = getLastFrameId()
Expand Down
30 changes: 24 additions & 6 deletions rem/remApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +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 imageResizer = ImageResizer(
Expand Down Expand Up @@ -347,11 +348,23 @@ 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)
}
} else {
disableRecording()
screenshotRetries = 0
}
}

private func scheduleScreenshot(shareableContent: SCShareableContent) {
Task {
do {
guard isCapturing == .recording else {
guard isCapturing == .recording else {
logger.debug("Stopped Recording")
return }

Expand All @@ -372,14 +385,18 @@ func drawStatusBarIcon(rect: CGRect) -> Bool {
logger.debug("Active Display ID: \(displayID ?? 999)")
}
}

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

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

let activeApplicationName = NSWorkspace.shared.frontmostApplication?.localizedName

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

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

0 comments on commit 9e6730d

Please sign in to comment.