Merge pull request 'fix: load persisted capture region at launch' (#15) from fix/region-persist-20260901 into feat/shotdeck-20260830
This commit was merged in pull request #15.
This commit is contained in:
@@ -72,7 +72,7 @@ public final class AppModel {
|
|||||||
captures: [],
|
captures: [],
|
||||||
pdfFileName: nil
|
pdfFileName: nil
|
||||||
)
|
)
|
||||||
self.region = nil
|
self.region = Self.loadPersistedRegion()
|
||||||
self.screenRecordingGranted = ScreenCapturer.isScreenRecordingGranted
|
self.screenRecordingGranted = ScreenCapturer.isScreenRecordingGranted
|
||||||
// Seeded from FolderSettings.resolve() via resolvedAppSupportPaths — never .standard().
|
// Seeded from FolderSettings.resolve() via resolvedAppSupportPaths — never .standard().
|
||||||
let folders = FolderSettings.resolve()
|
let folders = FolderSettings.resolve()
|
||||||
@@ -237,6 +237,14 @@ public final class AppModel {
|
|||||||
NSWorkspace.shared.open(url)
|
NSWorkspace.shared.open(url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static func loadPersistedRegion() -> CaptureRegion? {
|
||||||
|
guard let data = UserDefaults.standard.data(forKey: CaptureRegion.defaultsKey),
|
||||||
|
let decoded = try? JSONDecoder().decode(CaptureRegion.self, from: data),
|
||||||
|
decoded.isStillValid
|
||||||
|
else { return nil }
|
||||||
|
return decoded
|
||||||
|
}
|
||||||
|
|
||||||
private func persistRegion(_ picked: CaptureRegion) {
|
private func persistRegion(_ picked: CaptureRegion) {
|
||||||
replaceRegion(picked)
|
replaceRegion(picked)
|
||||||
if let encoded = try? JSONEncoder().encode(picked) {
|
if let encoded = try? JSONEncoder().encode(picked) {
|
||||||
|
|||||||
@@ -111,9 +111,52 @@ enum PickerSelfTest {
|
|||||||
|
|
||||||
print("PICKER-SELFTEST PASS rect=\(format(got.rect))")
|
print("PICKER-SELFTEST PASS rect=\(format(got.rect))")
|
||||||
fflush(stdout)
|
fflush(stdout)
|
||||||
|
|
||||||
|
runRegionPersistPhase()
|
||||||
exit(0)
|
exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Phase 2: writes a known region under `CaptureRegion.defaultsKey`, reloads it through
|
||||||
|
/// `AppModel.loadPersistedRegion()` (the same path init uses), then restores whatever
|
||||||
|
/// value was stored before so a real picked region is untouched.
|
||||||
|
private static func runRegionPersistPhase() {
|
||||||
|
let defaults = UserDefaults.standard
|
||||||
|
let previous = defaults.data(forKey: CaptureRegion.defaultsKey)
|
||||||
|
|
||||||
|
let known = CaptureRegion(
|
||||||
|
displayID: CGMainDisplayID(),
|
||||||
|
rect: CGRect(x: 10, y: 10, width: 100, height: 100),
|
||||||
|
capturedScale: 2.0
|
||||||
|
)
|
||||||
|
var failure: String?
|
||||||
|
if let encoded = try? JSONEncoder().encode(known) {
|
||||||
|
defaults.set(encoded, forKey: CaptureRegion.defaultsKey)
|
||||||
|
if let loaded = AppModel.loadPersistedRegion() {
|
||||||
|
if loaded.rect != known.rect {
|
||||||
|
failure = "expected=\(format(known.rect)) got=\(format(loaded.rect))"
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
failure = "loadPersistedRegion returned nil"
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
failure = "could not encode CaptureRegion"
|
||||||
|
}
|
||||||
|
|
||||||
|
if let previous {
|
||||||
|
defaults.set(previous, forKey: CaptureRegion.defaultsKey)
|
||||||
|
} else {
|
||||||
|
defaults.removeObject(forKey: CaptureRegion.defaultsKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
if let failure {
|
||||||
|
print("REGION-PERSIST FAIL \(failure)")
|
||||||
|
fflush(stdout)
|
||||||
|
exit(1)
|
||||||
|
}
|
||||||
|
print("REGION-PERSIST PASS")
|
||||||
|
fflush(stdout)
|
||||||
|
}
|
||||||
|
|
||||||
private static func interpolate(_ step: Int) -> NSPoint {
|
private static func interpolate(_ step: Int) -> NSPoint {
|
||||||
let t = CGFloat(step) / CGFloat(dragSteps)
|
let t = CGFloat(step) / CGFloat(dragSteps)
|
||||||
return NSPoint(
|
return NSPoint(
|
||||||
|
|||||||
Reference in New Issue
Block a user