// 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.importCottonKitimportCottonValuesimportSwiftUI/// 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`).structIndexRail: View {/// The rail, top to bottom; `#` holds every name that starts with no letter.staticlet 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.staticfunc 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.staticfunc target(for entry: String, present: [String]) -> String? {let have = Set(present)guard !have.isEmpty, let start = rail.firstIndex(of: entry) else { returnnil }iflet after = rail[start...].first(where: have.contains) { return after }return rail[..<start].last(where: have.contains) } @Environment(\.theme) privatevar theme/// The letters the list has runs for.let present: [String]let onScrub: (String) -> Void @Environment(\.cottonHaptics) privatevar haptics @State privatevar touched: String?staticlet 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.staticlet edgeInset: CGFloat = 2/// One entry's width: the rail's own.staticlet 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.staticlet 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.staticlet topAir: CGFloat = 120var body: someView {let have = Set(present) VStack(spacing: 0) {ForEach(Self.rail, id: \.self) { entry inText(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 inlet railHeight = Self.entryHeight * CGFloat(Self.rail.count)let entry = Self.entry(atFraction: Double((drag.location.y - 6) / railHeight))guard entry != touched else { return } touched = entryiflet 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 inlet letters = Self.rail.filter(have.contains)let current = touched.flatMap { letters.firstIndex(of: $0) } ?? -1let next = direction == .increment ? current + 1 : current - 1guard letters.indices.contains(next) else { return } touched = letters[next] onScrub(letters[next]) } }}