// THE RAIL (owner, 15 Nov 2026): a thin strip down the right edge of a chat// in Messages' stream. Dragging its thumb moves through the chat by date, the// date said beside the thumb; under the thumb's run, at the strip's foot,// stand Previous and Next, the pair that cuts to the chat before and after.//// It is a strip of its own. A touch on it is never ink (the stream reads no// stroke begun on it) and never the chat's scroll, which stays as it is// everywhere else on the chat; at rest the thumb stands where the scroll does.//// The rule is the core's (`shell.rail`, `cotton_core::messages::rail`): one// stop a day, where each stands on the rail, the words it says and the// message the chat is turned to; the pair's words; the strip's width and the// thumb's length. This file draws that and carries the finger.importCottonKitimportCottonPaperimportCottonValuesimportSwiftUI/// `shell.rail`'s answer.structRailAnswer: Decodable, Equatable, Sendable {structStop: Decodable, Equatable, Sendable {let day: String/// The day as the chat's own day line says it.let label: String/// Where it stands: 0 the rail's top (the oldest held), 1 its foot.let at: Double/// The day's first message: what the chat is turned to.let message: String }let stops: [Stop]let previous: Stringlet next: Stringlet width: Doublelet thumb: Double}@MainActor@ObservablefinalclassChatRailModel {private(set) var answer: RailAnswer?/// Where the chat's scroll stands: 0 its top, 1 its foot.var scrolled: Double = 1/// Where the finger holds the thumb, while it drags.var heldAt: Double?/// The stop the finger is on.var heldStop: RailAnswer.Stop?structMessage: Encodable, Equatable, Sendable {let id: Stringlet day: String?let label: String? }privatestructAsk: Encodable, Sendable { let messages: [Message] }privatevar asked: [Message] = []/// The rail for the messages the chat holds, oldest first, as the core answers it.func read(core: (any CottonCalling)?, messages: [Message]) async {guardlet core, messages != asked || answer == nilelse { return }guardlet next = try? await core.call("shell.rail", Ask(messages: messages), as: RailAnswer.self), !Task.isCancelled else { return } asked = messages answer = next }/// The stop the thumb is on at `at`, read off the core's stops: the/// last at or above it (`rail::stop_at`).func stop(at: Double) -> RailAnswer.Stop? {guardlet stops = answer?.stops, !stops.isEmpty else { returnnil }return stops.last { $0.at <= at } ?? stops[0] }/// The stop `by` days from the one the scroll stands on (VoiceOver's step).func stop(stepping by: Int) -> RailAnswer.Stop? {guardlet stops = answer?.stops, let on = stop(at: scrolled), let place = stops.firstIndex(of: on), stops.indices.contains(place + by) else { returnnil }return stops[place + by] }}structChatRail: View {let model: ChatRailModellet previous: String?let next: String?/// The chat cut to.let go: (String) -> Void/// The chat turned to a stop's message.let turn: (RailAnswer.Stop) -> Void/// The thumb reached the rail's top: older messages are asked for.let reachedTop: () -> Void @Environment(\.theme) privatevar themeprivatevar width: CGFloat { CGFloat(model.answer?.width ?? 30) }privatevar thumb: CGFloat { CGFloat(model.answer?.thumb ?? 44) }var body: someView { VStack(spacing: 0) { track pair("chevron.left", model.answer?.previous ?? ShellLines.line("chat", "stream.previous"), previous, "stream-previous") pair("chevron.right", model.answer?.next ?? ShellLines.line("chat", "stream.next"), next, "stream-next") } .frame(width: width) }/// The thumb's run: a hairline, the thumb where the scroll stands (or/// where the finger holds it), and the day beside it while it is held.privatevar track: someView {GeometryReader { geometry inlet room = max(1, geometry.size.height - thumb)let at = model.heldAt ?? model.scrolledlet moves = (model.answer?.stops.count ?? 0) > 1 ZStack(alignment: .topTrailing) {Capsule().fill(theme.print(.pencil).opacity(0.22)) .frame(width: 2) .frame(maxHeight: .infinity) .padding(.trailing, 8)if moves {Capsule().fill(theme.print(.ink).opacity(model.heldAt == nil ? 0.38 : 0.8)) .frame(width: 5, height: thumb) .padding(.trailing, 6.5) .offset(y: at * room) } } .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topTrailing) .overlay(alignment: .topTrailing) {iflet stop = model.heldStop {Text(stop.label) .paperHand(17) .lineLimit(1) .fixedSize() .padding(.horizontal, 12).padding(.vertical, 7) .background(RoundedRectangle(cornerRadius: 10).fill(theme.colors.groundMuted.color)) .offset(x: -(width + 2), y: at * room + thumb / 2 - 18) .allowsHitTesting(false) .accessibilityIdentifier("stream-rail-day") } } .contentShape(Rectangle()) .gesture(DragGesture(minimumDistance: 0, coordinateSpace: .local) .onChanged { value inguard moves else { return }let at = min(1, max(0, (value.location.y - thumb / 2) / room)) model.heldAt = atiflet stop = model.stop(at: at), stop != model.heldStop { model.heldStop = stop turn(stop) }if at <= 0.02 { reachedTop() } } .onEnded { _ in model.heldAt = nil model.heldStop = nil }) .accessibilityElement() .accessibilityLabel(model.stop(at: model.scrolled)?.label ?? "") .accessibilityAdjustableAction { direction iniflet stop = model.stop(stepping: direction == .increment ? 1 : -1) { turn(stop) } } .accessibilityIdentifier("stream-rail") } }privatefunc pair(_ symbol: String, _ label: String, _ chat: String?, _ identifier: String) -> someView {Button { iflet chat { go(chat) } } label: {Image(systemName: symbol) .font(.system(size: 15, weight: .semibold)) .foregroundStyle(theme.print(.ink).opacity(chat == nil ? 0.25 : 0.8)) .frame(width: width, height: 40) .contentShape(Rectangle()) } .buttonStyle(.plain) .disabled(chat == nil) .accessibilityLabel(label) .accessibilityIdentifier(identifier) }}