Cotton code
46 lines · 2.5 KB · SwiftRaw
#if os(iOS)import OSLogimport UIKit/// THE PHONE'S HALF OF CLIPS: on coming to the front, what was copied in/// another app since is kept as a clip.////// iOS tells an app nothing while it is away, so the front is the moment./// Judged in the order that reads the least: the change count (nothing new,/// nothing read); the item's TYPES (a concealed or transient item is never/// read); and only then the text, which the core refuses when it is/// secret-shaped (a key, a card, a one-time code — `engine::clips`). Every/// skip is logged, because nothing on screen says a copy was passed over./// Under Settings › Cotton › Paste from Other Apps = Ask the read shows the/// system's alert; the Clips list's Paste button never does.@MainActorfinal class PhoneClipboard {    static let shared = PhoneClipboard()    private var keeper: ClipKeeper?    private var seen: Int?    private static let log = Logger(subsystem: "com.cottondb.clips", category: "phone")    /// Keep the copy made since the last look, into the personal database    /// (`personal`: made once, on the first look — the app's own core is the    /// personal database's for the life of the process).    func foreground(personal: () -> any Records, thisDevice: () async -> String?, board: UIPasteboard = .general) async {        let keeper = self.keeper ?? ClipKeeper(store: personal())        self.keeper = keeper        // Off until the person turns it on on this phone's own page (owner,        // 14 Oct 2026): under Ask the read below raises iOS's paste alert.        guard await keeper.deviceAllows("clips_on_open", device: await thisDevice(), otherwise: false) else {            return Self.log.notice("skipped: this phone's clips_on_open is off or unread")        }        let count = board.changeCount        guard count != seen else { return Self.log.notice("skipped: nothing copied since the last look") }        seen = count        guard board.hasStrings || board.hasURLs else { return Self.log.notice("skipped: the copy holds no text") }        let types = Set(board.types)        guard !ClipCapture.refuses(types: types) else { return Self.log.notice("skipped: the copy is marked concealed or transient") }        guard let text = board.string else { return Self.log.notice("skipped: the text was not given (paste declined)") }        let kept = await keeper.keep(CopiedText(text: text, types: types, sourceDevice: UIDevice.current.name))        Self.log.notice("\(kept ? "kept the copy" : "skipped: refused, a duplicate, or no Clips table yet", privacy: .public)")    }}#endif