// 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. import CottonPaper import SwiftUI enum ReaderSeekKnobs { /// The line's touch height; the drawn edge is a hair of it. static let touch: CGFloat = 28 static let line: CGFloat = 1 static let tick: CGFloat = 5 static let ribbonWidth: CGFloat = 5 static let ribbonHeight: CGFloat = 15 static let ribbonNotch: CGFloat = 3 static let slipSize: CGFloat = 14 /// The slip's foot stands this far above the line's touch row. static let slipRise: CGFloat = 10 static let slipPadX: CGFloat = 12 static let slipPadY: CGFloat = 7 /// The slip keeps this far from the screen's sides. static let slipGutter: CGFloat = 12 /// A touch that moved less than this is a tap. static let 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. struct SlipPlacement: Layout { var x: CGFloat var rise: CGFloat = ReaderSeekKnobs.slipRise var gutter: CGFloat = ReaderSeekKnobs.slipGutter func 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 ()) { guard let 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. static func centre(_ x: CGFloat, slip: CGFloat, row: CGFloat, gutter: CGFloat) -> CGFloat { let half = slip / 2 + gutter guard row > half * 2 else { return row / 2 } return min(row - half, max(half, x)) } } #if os(iOS) struct ReaderSeekLine: 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: String let 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 private var held: Double? var body: some View { GeometryReader { proxy in let side = ReaderChromeKnobs.side let width = max(1, proxy.size.width - side * 2) let middle = proxy.size.height / 2 let 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 in Rectangle().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 in let part = Self.clamp(Double((touch.location.x - side) / width)) held = part drag(part) } .onEnded { touch in let 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) { if let 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 in switch direction { case .increment: step(1) case .decrement: step(-1) @unknown default: break } } .accessibilityIdentifier("reader-seek") } static func clamp(_ part: Double) -> Double { min(1, max(0, part)) } } /// A ribbon's end: a narrow strip with a swallowtail cut into its foot. struct SeekRibbon: 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"). struct ReaderSlip: View { let words: String let face: ReaderFace var body: some View { 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