// The seek line: the whole book as its page edge, just above the bar, full// width, in the reader face's own ink. A thin line for the pages, a short// tick where each chapter starts, and a ribbon where the reader is. No// slider chrome: nothing is filled, nothing is round.//// A touch anywhere on the line, or on the ribbon, and a drag moves the// ribbon; a slip floats above the finger with the core's words for the// place under it. The page behind stands still until the finger lifts.// What the line holds (the ticks, where the ribbon rests) and every word// are the core's (`reader.seek`); this file draws them and says where the// finger is as a share of the line's length.importCottonPaperimportSwiftUIenumReaderSeekKnobs {/// The line's touch height; the drawn edge is a hair of it.staticlet touch: CGFloat = 28staticlet line: CGFloat = 1staticlet tick: CGFloat = 5staticlet ribbonWidth: CGFloat = 5staticlet ribbonHeight: CGFloat = 15staticlet ribbonNotch: CGFloat = 3staticlet slipSize: CGFloat = 14/// The slip's foot stands this far above the line's touch row.staticlet slipRise: CGFloat = 10staticlet slipPadX: CGFloat = 12staticlet slipPadY: CGFloat = 7/// The slip keeps this far from the screen's sides.staticlet slipGutter: CGFloat = 12/// A touch that moved less than this is a tap.staticlet tap: CGFloat = 4}/// One slip set above a point of its row: centred on `x`, kept whole/// inside the row's sides, its foot `rise` above the row's top; given the/// row's width less the gutters to wrap in, so it fits at the largest/// text. The row itself has no height: the slip floats over what is above.structSlipPlacement: Layout {var x: CGFloatvar rise: CGFloat = ReaderSeekKnobs.slipRisevar gutter: CGFloat = ReaderSeekKnobs.slipGutterfunc sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize { CGSize(width: proposal.width ?? 0, height: 0) }func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {guardlet slip = subviews.first else { return }let offered = ProposedViewSize(width: max(0, bounds.width - gutter * 2), height: nil)let size = slip.sizeThatFits(offered) slip.place(at: CGPoint(x: bounds.minX + Self.centre(x, slip: size.width, row: bounds.width, gutter: gutter), y: bounds.minY - rise), anchor: .bottom, proposal: offered) }/// The slip's centre: over the point, never nearer a side than its own half and the gutter.staticfunc centre(_ x: CGFloat, slip: CGFloat, row: CGFloat, gutter: CGFloat) -> CGFloat {let half = slip / 2 + gutterguard row > half * 2else { return row / 2 }return min(row - half, max(half, x)) }}#if os(iOS)structReaderSeekLine: View {let face: ReaderFace/// Where each chapter starts, as a share of the line.let ticks: [Double]/// Where the reader is.let at: Double/// The slip's words while a finger is down, else nil.let slip: String?/// VoiceOver's value (the words for where the reader is) and the line's name.let value: Stringlet label: String/// The finger moved to this share of the line.let drag: (Double) -> Void/// The finger lifted here; the line's length in points when it was a tap.let release: (Double, Double?) -> Void/// VoiceOver's step: one chapter on (+1) or back (-1).let step: (Int) -> Void/// Where the finger is, while it is down. @State privatevar held: Double?var body: someView {GeometryReader { proxy inlet side = ReaderChromeKnobs.sidelet width = max(1, proxy.size.width - side * 2)let middle = proxy.size.height / 2let shown = Self.clamp(held ?? at) ZStack(alignment: .topLeading) {Rectangle().fill(face.secondary.opacity(0.6)) .frame(width: width, height: ReaderSeekKnobs.line) .offset(x: side, y: middle - ReaderSeekKnobs.line / 2)ForEach(Array(ticks.enumerated()), id: \.offset) { _, tick inRectangle().fill(face.secondary) .frame(width: ReaderSeekKnobs.line, height: ReaderSeekKnobs.tick) .offset(x: side + width * Self.clamp(tick), y: middle - ReaderSeekKnobs.tick) }SeekRibbon().fill(face.ink) .frame(width: ReaderSeekKnobs.ribbonWidth, height: ReaderSeekKnobs.ribbonHeight) .offset(x: side + width * shown - ReaderSeekKnobs.ribbonWidth / 2, y: middle - ReaderSeekKnobs.ribbonHeight + ReaderSeekKnobs.ribbonNotch) } .frame(width: proxy.size.width, height: proxy.size.height, alignment: .topLeading) .contentShape(Rectangle()) .gesture(DragGesture(minimumDistance: 0) .onChanged { touch inlet part = Self.clamp(Double((touch.location.x - side) / width)) held = part drag(part) } .onEnded { touch inlet part = Self.clamp(Double((touch.location.x - side) / width))let tapped = abs(touch.translation.width) < ReaderSeekKnobs.tap && abs(touch.translation.height) < ReaderSeekKnobs.tap held = nil release(part, tapped ? Double(width) : nil) }) .overlay(alignment: .top) {iflet slip, held != nil {SlipPlacement(x: side + width * shown) {ReaderSlip(words: slip, face: face) } .allowsHitTesting(false) .accessibilityIdentifier("reader-seek-slip") } } } .frame(height: ReaderSeekKnobs.touch) .accessibilityElement() .accessibilityLabel(label) .accessibilityValue(value) .accessibilityAdjustableAction { direction inswitch direction {case .increment: step(1)case .decrement: step(-1) @unknown default: break } } .accessibilityIdentifier("reader-seek") }staticfunc clamp(_ part: Double) -> Double { min(1, max(0, part)) }}/// A ribbon's end: a narrow strip with a swallowtail cut into its foot.structSeekRibbon: Shape {func path(in rect: CGRect) -> Path {var path = Path() path.move(to: CGPoint(x: rect.minX, y: rect.minY)) path.addLine(to: CGPoint(x: rect.maxX, y: rect.minY)) path.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY)) path.addLine(to: CGPoint(x: rect.midX, y: rect.maxY - ReaderSeekKnobs.ribbonNotch)) path.addLine(to: CGPoint(x: rect.minX, y: rect.maxY)) path.closeSubpath()return path }}/// A slip of the face's own paper with the core's words on it: over the/// finger while seeking, and above the bar after a jump ("Back to page 41").structReaderSlip: View {let words: Stringlet face: ReaderFacevar body: someView {Text(words) .font(CottonFonts.caslon(ReaderSeekKnobs.slipSize).monospacedDigit()) .foregroundStyle(face.ink) .lineLimit(3) .multilineTextAlignment(.center) .padding(.horizontal, ReaderSeekKnobs.slipPadX) .padding(.vertical, ReaderSeekKnobs.slipPadY) .background(face.ground) .overlay(Rectangle().stroke(face.hairline, lineWidth: 1)) .shadow(color: .black.opacity(face.dark ? 0.5 : 0.14), radius: 5, y: 2) }}#endif