// 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. import CottonKit import CottonPaper import CottonValues import SwiftUI /// `shell.rail`'s answer. struct RailAnswer: Decodable, Equatable, Sendable { struct Stop: 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: String let next: String let width: Double let thumb: Double } @MainActor @Observable final class ChatRailModel { 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? struct Message: Encodable, Equatable, Sendable { let id: String let day: String? let label: String? } private struct Ask: Encodable, Sendable { let messages: [Message] } private var 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 { guard let core, messages != asked || answer == nil else { return } guard let 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? { guard let stops = answer?.stops, !stops.isEmpty else { return nil } 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? { guard let stops = answer?.stops, let on = stop(at: scrolled), let place = stops.firstIndex(of: on), stops.indices.contains(place + by) else { return nil } return stops[place + by] } } struct ChatRail: View { let model: ChatRailModel let 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) private var theme private var width: CGFloat { CGFloat(model.answer?.width ?? 30) } private var thumb: CGFloat { CGFloat(model.answer?.thumb ?? 44) } var body: some View { 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. private var track: some View { GeometryReader { geometry in let room = max(1, geometry.size.height - thumb) let at = model.heldAt ?? model.scrolled let 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) { if let 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 in guard moves else { return } let at = min(1, max(0, (value.location.y - thumb / 2) / room)) model.heldAt = at if let 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 in if let stop = model.stop(stepping: direction == .increment ? 1 : -1) { turn(stop) } } .accessibilityIdentifier("stream-rail") } } private func pair(_ symbol: String, _ label: String, _ chat: String?, _ identifier: String) -> some View { Button { if let 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) } }