Cotton code
52 lines · 1.8 KB · SwiftRaw
// The hold that fires an act which cannot be taken back.//// The owner's ruling of 12 Oct 2026: no confirm popups. An irreversible act —// bin, cancel, stop repeating, leave — fires on a HOLD of three quarters of a// second, while an ink line draws itself round its card; let go early and the// line unwinds and nothing has happened.//// The rule is this small value, so it is a test and not a gesture's side// effect: a hold fires only when it has been held for the whole duration, and// a release before that fires nothing however close it came.import Foundationstruct ActHold: Equatable, Sendable {    /// How long this hold lasts: the worn theme's    /// `card_hold_duration_seconds`, which the card sets before a press.    var duration: TimeInterval    /// When the finger came down; nil when nothing is held.    private(set) var began: Date?    var isHeld: Bool { began != nil }    init(duration: TimeInterval) {        self.duration = duration    }    mutating func press(at now: Date) {        if began == nil { began = now }    }    /// How far round the card the line has drawn, 0 to 1.    func progress(at now: Date) -> Double {        guard let began else { return 0 }        return min(1, max(0, now.timeIntervalSince(began) / duration))    }    /// Whether the hold has lasted the full duration. The view asks this when    /// its own timer says the time is up, and fires only on a yes.    func completes(at now: Date) -> Bool {        guard let began else { return false }        return now.timeIntervalSince(began) >= duration    }    /// The finger lifted. The hold is over either way; the answer is whether    /// the act fires.    mutating func release(at now: Date) -> Bool {        let fires = completes(at: now)        began = nil        return fires    }}