// The A–Z rail on a list's right edge, like Contacts, for a list the core // answers an `index` for (`shell.list`): which letters hold records and where // each lands are the core's; the rail only draws them and reads a finger. // // The rail is a vertical column of letters, and a finger on it scrubs the // list. It always draws all twenty-seven entries (A–Z and `#`) so the column // never changes height or spacing as records come and go; a finger on a letter // no record starts with lands on the NEXT letter one does, the way Contacts // behaves, and past the last one it lands on the last. // // It floats over the page in an overlay, OUTSIDE the list's scroll view, and a // finger on it means a letter from the first touch — so its drag starts at // zero distance, which `ScrollingSweepTests` allows for exactly this file. import CottonKit import CottonValues import SwiftUI /// The A–Z rail on the right edge, like Contacts: a column of serif letters in /// pencil, and a finger anywhere on it scrubs the list to that letter. /// /// Every entry is always drawn, so the column never changes height; a letter /// no record starts with is fainter, and a finger on it lands on the next /// letter that has one (`target`). struct IndexRail: View { /// The rail, top to bottom; `#` holds every name that starts with no letter. static let rail: [String] = (UnicodeScalar("A").value...UnicodeScalar("Z").value) .compactMap { UnicodeScalar($0).map { String(Character($0)) } } + ["#"] /// The rail entry under a finger at `fraction` of the rail's height /// (0 = top). Outside the rail clamps to its ends. static func entry(atFraction fraction: Double) -> String { guard fraction.isFinite else { return rail[0] } let index = Int((fraction * Double(rail.count)).rounded(.down)) return rail[min(rail.count - 1, max(0, index))] } /// The letter a finger on `entry` lands on, given the letters the core /// said hold records: the entry itself, else the next one below it on /// the rail, else the last one above. Nil only when none holds any. static func target(for entry: String, present: [String]) -> String? { let have = Set(present) guard !have.isEmpty, let start = rail.firstIndex(of: entry) else { return nil } if let after = rail[start...].first(where: have.contains) { return after } return rail[.. Void @Environment(\.cottonHaptics) private var haptics @State private var touched: String? static let entryHeight: CGFloat = 15.5 /// The air the rail keeps between its letters and whatever it is pinned /// to. The caller pins it — see `RecordListSurface` — and subtracts this from /// the page's own side margin so the two agree about where content ends. static let edgeInset: CGFloat = 2 /// One entry's width: the rail's own. static let width: CGFloat = 22 /// **WHERE the rail hangs, as a value rather than a habit.** /// /// The overlay that draws it was aligned `.trailing`, which is CENTRED /// vertically. On a phone that reads as "beside the cards": the column is /// barely taller than the rail. On a Mac's page column it is a third of a /// screen of air, then twenty-seven letters floating in the middle of an /// empty page, starting BELOW the single row of cards they index (the /// walk's re-shoot, walk-05-v3 and walk-11-v3). An index belongs at the /// top of what it indexes, the way Contacts hangs one. /// /// Named here, and asserted by `IndexRailTests`, because an alignment is the /// whole of the placement and a wrong one is invisible until somebody /// photographs a tall window. static let anchor: Alignment = .topTrailing /// How far below the page's top edge the letters start — clear of the /// floating circles and level with the first row of objects, which begins /// under the screen's name. static let topAir: CGFloat = 120 var body: some View { let have = Set(present) VStack(spacing: 0) { ForEach(Self.rail, id: \.self) { entry in Text(entry) .font(CottonFonts.serif(touched == entry ? 13 : 10.5)) .foregroundStyle( touched == entry ? theme.print(.ink) : theme.print(.pencil).opacity(have.contains(entry) ? 1 : 0.45) ) .frame(width: Self.width, height: Self.entryHeight) } } .padding(.vertical, 6) .contentShape(Rectangle()) .gesture( DragGesture(minimumDistance: 0, coordinateSpace: .local) .onChanged { drag in let railHeight = Self.entryHeight * CGFloat(Self.rail.count) let entry = Self.entry(atFraction: Double((drag.location.y - 6) / railHeight)) guard entry != touched else { return } touched = entry if let letter = Self.target(for: entry, present: present) { haptics.select() onScrub(letter) } } .onEnded { _ in touched = nil } ) .padding(.trailing, Self.edgeInset) .accessibilityElement(children: .ignore) .accessibilityLabel(ShellLines.line("record", "index_rail")) .accessibilityValue(touched ?? "") .accessibilityAdjustableAction { direction in let letters = Self.rail.filter(have.contains) let current = touched.flatMap { letters.firstIndex(of: $0) } ?? -1 let next = direction == .increment ? current + 1 : current - 1 guard letters.indices.contains(next) else { return } touched = letters[next] onScrub(letters[next]) } } }