300 lines
11 KiB
Swift
300 lines
11 KiB
Swift
import AppKit
|
||
import Foundation
|
||
import Observation
|
||
import SwiftUI
|
||
import ShotdeckCore
|
||
|
||
@MainActor
|
||
public protocol SendCapable: AnyObject {
|
||
func send(anchor: NSView?) async
|
||
}
|
||
|
||
@MainActor
|
||
public protocol SettingsWindowPresenting: AnyObject {
|
||
func presentSettingsWindow()
|
||
}
|
||
|
||
@MainActor
|
||
public protocol ReturnsSectionProviding: AnyObject {
|
||
@ViewBuilder func returnsSection() -> AnyView
|
||
}
|
||
|
||
@MainActor
|
||
@Observable
|
||
public final class AppModel {
|
||
public private(set) var session: CaptureSession
|
||
public private(set) var region: CaptureRegion?
|
||
public private(set) var screenRecordingGranted: Bool
|
||
public private(set) var allReturns: [ReturnedDocument] = []
|
||
public private(set) var commentedReturns: [ReturnedDocument] = []
|
||
public private(set) var statusLine: String?
|
||
public private(set) var isCapturing: Bool = false
|
||
public private(set) var isSending: Bool = false
|
||
public private(set) var outboxDisplayName: String
|
||
public private(set) var watchFolderDisplayName: String
|
||
/// Live outbox; WP-4b reads this (not `paths.outbox`) so Settings folder changes take effect.
|
||
public private(set) var outboxURL: URL
|
||
/// Live watch folder; WP-4c updates this alongside `ReturnWatcher.updateWatchFolder`.
|
||
public private(set) var watchFolderURL: URL
|
||
/// Currently bound capture combo (the last one Carbon accepted, or the preferred load).
|
||
private(set) var captureHotkey: HotkeyPreference
|
||
var hotkeyDisplayString: String { captureHotkey.displayString }
|
||
|
||
let paths: AppSupportPaths
|
||
let spool: SpoolStore
|
||
let composer: PDFComposer
|
||
let capturer: ScreenCapturer
|
||
let hotkeys: HotkeyCenter
|
||
let picker: RegionPickerController
|
||
let ledger: ReturnLedger
|
||
let watcher: ReturnWatcher
|
||
|
||
public init(
|
||
paths: AppSupportPaths,
|
||
spool: SpoolStore,
|
||
composer: PDFComposer,
|
||
capturer: ScreenCapturer,
|
||
hotkeys: HotkeyCenter,
|
||
picker: RegionPickerController,
|
||
ledger: ReturnLedger,
|
||
watcher: ReturnWatcher
|
||
) {
|
||
self.paths = paths
|
||
self.spool = spool
|
||
self.composer = composer
|
||
self.capturer = capturer
|
||
self.hotkeys = hotkeys
|
||
self.picker = picker
|
||
self.ledger = ledger
|
||
self.watcher = watcher
|
||
self.session = CaptureSession(
|
||
id: UUID(),
|
||
createdAt: Date(),
|
||
state: .open,
|
||
captures: [],
|
||
pdfFileName: nil
|
||
)
|
||
self.region = Self.loadPersistedRegion()
|
||
self.screenRecordingGranted = ScreenCapturer.isScreenRecordingGranted
|
||
// Seeded from FolderSettings.resolve() via resolvedAppSupportPaths — never .standard().
|
||
let folders = FolderSettings.resolve()
|
||
self.outboxURL = folders.outbox
|
||
self.watchFolderURL = folders.watch
|
||
self.outboxDisplayName = folders.outbox.lastPathComponent
|
||
self.watchFolderDisplayName = folders.watch.lastPathComponent
|
||
self.captureHotkey = HotkeyPreference.load()
|
||
}
|
||
|
||
// MARK: Seam mutators — the only way a WP-4b/4c extension changes state.
|
||
|
||
func setStatus(_ text: String?) { statusLine = text }
|
||
func setSending(_ value: Bool) { isSending = value }
|
||
func setCapturing(_ value: Bool) { isCapturing = value }
|
||
func replaceSession(_ new: CaptureSession) { session = new }
|
||
func replaceRegion(_ new: CaptureRegion?) { region = new }
|
||
func setReturns(all: [ReturnedDocument], commented: [ReturnedDocument]) {
|
||
allReturns = all
|
||
commentedReturns = commented
|
||
}
|
||
func setFolderDisplayNames(outbox: String, watch: String) {
|
||
outboxDisplayName = outbox
|
||
watchFolderDisplayName = watch
|
||
}
|
||
func setFolderURLs(outbox: URL, watch: URL) {
|
||
outboxURL = outbox
|
||
watchFolderURL = watch
|
||
setFolderDisplayNames(outbox: outbox.lastPathComponent, watch: watch.lastPathComponent)
|
||
}
|
||
|
||
public var iconState: MenuIconState {
|
||
if !screenRecordingGranted { return .recordingMissing }
|
||
if isCapturing { return .capturing }
|
||
if region == nil { return .noRegion }
|
||
if session.captures.isEmpty { return .regionEmpty }
|
||
return .hasCaptures(session.captures.count)
|
||
}
|
||
|
||
public func bootstrap() async {
|
||
if let data = UserDefaults.standard.data(forKey: CaptureRegion.defaultsKey),
|
||
let decoded = try? JSONDecoder().decode(CaptureRegion.self, from: data),
|
||
decoded.isStillValid {
|
||
replaceRegion(decoded)
|
||
}
|
||
|
||
do {
|
||
let recovered = try await spool.currentSession()
|
||
replaceSession(recovered)
|
||
} catch {
|
||
setStatus((error as? ShotdeckError)?.errorDescription ?? "Could not open the spool.")
|
||
}
|
||
|
||
do {
|
||
let initial = try await ledger.all()
|
||
let commented = try await ledger.commented()
|
||
setReturns(all: initial, commented: commented)
|
||
} catch {
|
||
// Empty ledger on first run is not an error.
|
||
}
|
||
|
||
do {
|
||
try await watcher.start { [weak self] _ in
|
||
Task { @MainActor in
|
||
guard let self else { return }
|
||
let all = (try? await self.ledger.all()) ?? []
|
||
let commented = (try? await self.ledger.commented()) ?? []
|
||
self.setReturns(all: all, commented: commented)
|
||
}
|
||
}
|
||
} catch {
|
||
setStatus((error as? ShotdeckError)?.errorDescription ?? "Could not watch the return folder.")
|
||
}
|
||
|
||
let pref = HotkeyPreference.load()
|
||
captureHotkey = pref
|
||
if !bindCaptureHotkey(pref) {
|
||
setStatus("\(pref.displayString) is already used by another app — capture only works from the menu.")
|
||
}
|
||
}
|
||
|
||
/// Unregisters `capture` and binds `HotkeyPreference.load()`. If Carbon rejects the new
|
||
/// combo, restores the previous preference (UserDefaults + Carbon) so the old one keeps working.
|
||
func reRegisterHotkey() {
|
||
let previous = captureHotkey
|
||
let next = HotkeyPreference.load()
|
||
hotkeys.unregister(id: "capture")
|
||
if bindCaptureHotkey(next) {
|
||
captureHotkey = next
|
||
return
|
||
}
|
||
setStatus("That combination is taken — pick another.")
|
||
previous.save()
|
||
if bindCaptureHotkey(previous) {
|
||
captureHotkey = previous
|
||
}
|
||
}
|
||
|
||
@discardableResult
|
||
private func bindCaptureHotkey(_ pref: HotkeyPreference) -> Bool {
|
||
hotkeys.register(
|
||
id: "capture",
|
||
keyCode: pref.keyCode,
|
||
modifiers: pref.modifiers
|
||
) { [weak self] in
|
||
Task { await self?.captureNow() }
|
||
}
|
||
}
|
||
|
||
public func captureNow() async {
|
||
guard !isCapturing else { return }
|
||
setCapturing(true)
|
||
defer { setCapturing(false) }
|
||
|
||
var target = region
|
||
if target == nil {
|
||
target = await withCheckedContinuation { (cont: CheckedContinuation<CaptureRegion?, Never>) in
|
||
picker.pick { picked in cont.resume(returning: picked) }
|
||
}
|
||
guard let picked = target else {
|
||
setStatus("No region selected.")
|
||
return
|
||
}
|
||
persistRegion(picked)
|
||
}
|
||
guard let region = target else { return }
|
||
|
||
do {
|
||
let image = try await capturer.capture(region)
|
||
let capture = try await spool.append(
|
||
pngData: image.pngData,
|
||
pixelWidth: image.pixelWidth,
|
||
pixelHeight: image.pixelHeight,
|
||
scale: image.scale,
|
||
capturedAt: Date()
|
||
)
|
||
replaceSession(try await spool.currentSession())
|
||
setStatus("Captured page \(capture.sequence).")
|
||
} catch {
|
||
screenRecordingGranted = ScreenCapturer.isScreenRecordingGranted
|
||
setStatus((error as? ShotdeckError)?.errorDescription ?? "The screenshot could not be taken.")
|
||
}
|
||
}
|
||
|
||
public func rePickRegion() async {
|
||
let picked = await withCheckedContinuation { (cont: CheckedContinuation<CaptureRegion?, Never>) in
|
||
picker.pick { cont.resume(returning: $0) }
|
||
}
|
||
guard let picked else { return }
|
||
persistRegion(picked)
|
||
setStatus("Region set: \(Int(picked.rect.width)) × \(Int(picked.rect.height)).")
|
||
}
|
||
|
||
public func removeCapture(id: UUID) async {
|
||
do {
|
||
// D-11: SpoolStore.remove MOVES the PNG to <session>/removed/; it is never unlinked.
|
||
replaceSession(try await spool.remove(captureID: id))
|
||
} catch {
|
||
setStatus((error as? ShotdeckError)?.errorDescription ?? "Could not remove that capture.")
|
||
}
|
||
}
|
||
|
||
public func copyCommentedLinks() async {
|
||
do {
|
||
let text = try await ledger.clipboardText()
|
||
let pasteboard = NSPasteboard.general
|
||
pasteboard.clearContents()
|
||
pasteboard.setString(text, forType: .string)
|
||
let count = commentedReturns.count
|
||
setStatus("Copied \(count) link\(count == 1 ? "" : "s").")
|
||
} catch {
|
||
setStatus((error as? ShotdeckError)?.errorDescription ?? "Nothing to copy.")
|
||
}
|
||
}
|
||
|
||
public func openSpoolFolder() {
|
||
NSWorkspace.shared.open(paths.spool)
|
||
}
|
||
|
||
public func openScreenRecordingSettings() {
|
||
guard let url = URL(string:
|
||
"x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenCapture") else {
|
||
setStatus("Could not open System Settings.")
|
||
return
|
||
}
|
||
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) {
|
||
replaceRegion(picked)
|
||
if let encoded = try? JSONEncoder().encode(picked) {
|
||
UserDefaults.standard.set(encoded, forKey: CaptureRegion.defaultsKey)
|
||
}
|
||
}
|
||
}
|
||
|
||
public enum MenuIconState: Equatable {
|
||
case noRegion, regionEmpty, hasCaptures(Int), capturing, recordingMissing
|
||
|
||
public var symbolName: String {
|
||
switch self {
|
||
case .noRegion: return "viewfinder"
|
||
case .regionEmpty: return "viewfinder.rectangular"
|
||
case .hasCaptures: return "viewfinder.rectangular"
|
||
case .capturing: return "viewfinder.circle.fill"
|
||
case .recordingMissing: return "exclamationmark.triangle"
|
||
}
|
||
}
|
||
|
||
public var countText: String? {
|
||
if case .hasCaptures(let n) = self { return "\(n)" }
|
||
return nil
|
||
}
|
||
}
|