// A numbered pin on a picture, and the press that places one. // // Both halves used to live in the version reader, because a published version was // the only page in the app with pins on it. A note's attachment is the second, // and a pin has to look and behave the SAME on both — the fraction stored is // the fraction the share page draws from, so two implementations of "where did // the thumb land" would be two different pictures of one comment. // // So the marker and the gesture are scaffold now: one row shape // (`id`, `pinNumber`, `x`, `y`, `status`), one clamp, one 44pt target. import CottonKit import SwiftUI /// A numbered pin, at its own fraction of the screen it sits on. /// /// A CLOSED pin stays ON the screen and steps back: the owner reads their own /// verdicts in place, and a pin that vanished when it was answered would put /// gaps in a numbering the client's page still shows. struct PinMarker: View { let pin: JSONValue let width: CGFloat let height: CGFloat let onTap: ((String) -> Void)? @Environment(\.theme) private var theme /// A 24pt dot inside a 44pt target. private let dot: CGFloat = 24 private var muted: Bool { pin.text("status") != "open" } private var number: Int { pin.number("pinNumber") ?? 0 } var body: some View { let label = muted ? ShellLines.line("chrome", "pin.pin_closed", ["number": "\(number)"]) : "Pin \(number)" Group { if let onTap, let id = pin.text("id") { Button { onTap(id) } label: { marker } .buttonStyle(OpacityButtonStyle()) .accessibilityLabel(label) } else { // A marker with nowhere to go is a LABEL, not a button — the // pager draws these for context and has no list to jump to. // Announcing a button that does nothing when pressed is the // worse of the two. marker .accessibilityLabel(label) } } .frame(width: 44, height: 44) .contentShape(Rectangle()) .position( x: CGFloat(pin["x"]?.double ?? 0) * width, y: CGFloat(pin["y"]?.double ?? 0) * height ) } /// A small red pencil ring with the number inside — a closed pin's ring /// is pencil, and steps back. private var marker: some View { ZStack { PencilCircle(seed: pin.text("id") ?? String(number), theme: theme) .stroke( theme.print(muted ? .pencil : .red), style: StrokeStyle(lineWidth: 1.6, lineCap: .round) ) .frame(width: dot, height: dot) Text(String(number)) .font(CottonFonts.hand(12)) .foregroundStyle(theme.print(muted ? .pencil : .red)) } .background(Circle().fill(PaperPaint.printFrame.opacity(0.85)).padding(2)) .opacity(muted ? 0.7 : 1) } } // MARK: - where the thumb landed /// The point a long press landed on, as a fraction of the box it landed in. enum PinPlacement { /// `nil` for a box with no area — there is no fraction of nothing, and an /// anchor at (0, 0) would be a pin in the corner nobody pressed. /// /// **Clamped rather than refused**: a long press a pixel outside the box is /// a thumb, not an intention to file against nothing. The crate refuses /// anything outside 0…1 anyway, and being refused for a rounding error /// would be the app's fault, not the owner's. static func fraction(of point: CGPoint, in size: CGSize) -> (x: Double, y: Double)? { guard size.width > 0, size.height > 0 else { return nil } return (clamped(Double(point.x / size.width)), clamped(Double(point.y / size.height))) } /// Into 0…1. An infinity clamps like any other number too big or too /// small; a NaN compares false with everything, so `min`/`max` would let it /// straight through and SwiftUI would draw the pin nowhere — it is the near /// edge instead. static func clamped(_ value: Double) -> Double { guard !value.isNaN else { return 0 } return min(1, max(0, value)) } }