// Seeking through the open book: the reading's side of the seek line// (`ReaderSeekLine`), the Back slip and the contents.//// Every rule and word is the core's (`phone::reader_seek`): where the ticks// stand, the slip's words for a dragged place, where a tap or a release// lands, the one Back and when it retires. The reading asks, hands the// Locator answered to the navigator, and holds what was answered for the// chrome to draw. The page does not move while a finger is on the line.#if os(iOS)importCottonKitimportCottonPaperimportReadiumSharedimportSwiftUIimport UIKitextensionBookReading {/// Where the ribbon rests: where a jump was let go until it lands, else/// where the core says the page stands.var seekAt: Double { seekLanding ?? seek?.here ?? 0 }// MARK: The line/// The line read again: the ticks, where the page stands, the words, Back.func readSeek() {Task { await readSeekNow() } }privatefunc readSeekNow() async {do {let answer = tryawait core.call(ReaderCalls.seek, ReaderSeekAsk(book: book), as: ReaderSeekAnswer.self) seek = answer seekLanding = nil stands(answer.back) } catch {Log.write("reader: the seek line was not read: \(String(describing: error))") } }/// The finger is down on the line at `part`: the slip says the place,/// one ask at a time, the newest place winning; a chapter start crossed/// is felt, lightly.func seeking(_ part: Double) {if seekSlip == nil { seekSlip = seek?.words.here }let passed = ReaderSeekMeasures.ticksPassed(seek?.ticks.map(\.at) ?? [], at: part)iflet before = seekTicksPassed, before != passed { UIImpactFeedbackGenerator(style: .light).impactOccurred(intensity: 0.5) } seekTicksPassed = passed seekWanted = partguard seekSaying == nilelse { return } seekSaying = Task { [weakself] inwhileletself, let part = self.seekWanted {self.seekWanted = nillet said = try? awaitself.core.call(ReaderCalls.seekSay, ReaderSeekAsk(book: self.book, at: part), as: ReaderSeekSaid.self)// Words for a finger that has since lifted are not drawn.iflet said, self.seekTicksPassed != nil { self.seekSlip = said.words } }self?.seekSaying = nil } }/// The finger lifted at `part`: the reader goes there. `width` is the/// line's length in points when the touch was a tap, for the core to/// land a tap near a tick on its chapter's start.func seekReleased(_ part: Double, width: Double?) { seekSlip = nil seekWanted = nil seekTicksPassed = nil seekLanding = part jump(ReaderSeekAsk(book: book, at: part, width: width)) }/// VoiceOver's step: one chapter on or back, as the core says.func seekStep(_ direction: Int) {guardlet at = direction > 0 ? seek?.steps.next : seek?.steps.previous else { return } seekLanding = at jump(ReaderSeekAsk(book: book, at: at)) }/// The contents' tap: its chapter's start.func seekTick(_ index: Int) { contentsShown = false jump(ReaderSeekAsk(book: book, tick: index)) }privatefunc jump(_ ask: ReaderSeekAsk) {Task {do {let went = tryawait core.call(ReaderCalls.seekGo, ask, as: ReaderSeekWent.self)await land(went) } catch { seekLanding = nilLog.write("reader: the jump was not made: \(String(describing: error))") } } }/// The Back slip's tap: the place the jump left.func seekBack() {Task {do {let went = tryawait core.call(ReaderCalls.seekBack, ReaderSeekAsk(book: book), as: ReaderSeekWent.self)await land(went) } catch {Log.write("reader: Back was not gone to: \(String(describing: error))") } } }/// The navigator goes where the core answered, with one soft tap; the/// place it lands on is reported as any move is (`reader.moved`), and/// the line is read again then.privatefunc land(_ went: ReaderSeekWent) async { stands(went.back)guardlet text = went.locator, let locator = try? Locator(jsonString: text) else { seekLanding = nilreturn } UIImpactFeedbackGenerator(style: .soft).impactOccurred() _ = await navigator.go(to: locator) readSeek() }// MARK: Back/// Back as the core now says it stands; its time up, the core is asked again.func stands(_ answer: ReaderBackAnswer?) { backClock?.invalidate() backClock = nil backSlip = answerguardlet answer else { return } backClock = Timer.scheduledTimer(withTimeInterval: Double(max(answer.msLeft, 0)) / 1000 + 0.05, repeats: false) { [weakself] _ inMainActor.assumeIsolated { self?.readSeek() } } }// MARK: The contents/// The chapter's name in the bar: the contents, read fresh.func openContents() { closeFan()Task {await readSeekNow()guard seek?.ticks.isEmpty == falseelse { return } contentsShown = true } }}// MARK: - The contentsenumReaderContentsKnobs {staticlet headSize: CGFloat = 13staticlet rowSize: CGFloat = 17staticlet row: CGFloat = 48staticlet mark: CGFloat = 14}/// The book's chapters over the page, in the face's ground and inks: the/// one the page stands in marked by the ribbon, a tap going to its start.structReaderContents: View {let reading: BookReadinglet seek: ReaderSeekAnswervar body: someView {let face = reading.face VStack(spacing: 0) { ZStack {Text(seek.words.contents) .font(CottonFonts.caslon(ReaderContentsKnobs.headSize)) .textCase(.uppercase) .tracking(ReaderChromeKnobs.headTracking) .foregroundStyle(face.secondary) .accessibilityAddTraits(.isHeader)ReaderBackButton(ink: face.ink) { reading.contentsShown = false } .frame(maxWidth: .infinity, alignment: .leading) } .padding(.horizontal, ReaderChromeKnobs.side - 14)Rectangle().fill(face.hairline).frame(height: 1) .padding(.horizontal, ReaderChromeKnobs.side)ScrollViewReader { scroll inScrollView {LazyVStack(spacing: 0) {ForEach(Array(seek.ticks.enumerated()), id: \.offset) { index, tick in row(index, tick, face) } } .padding(.vertical, 8) } .onAppear {iflet here = seek.ticks.firstIndex(where: \.here) { scroll.scrollTo(here, anchor: .center) } } } } .background(face.ground.ignoresSafeArea()) .accessibilityElement(children: .contain) .accessibilityIdentifier("reader-contents") }privatefunc row(_ index: Int, _ tick: ReaderSeekAnswer.Tick, _ face: ReaderFace) -> someView {Button { reading.seekTick(index) } label: { HStack(spacing: 10) {SeekRibbon().fill(tick.here ? face.ink : .clear) .frame(width: ReaderSeekKnobs.ribbonWidth, height: ReaderContentsKnobs.mark)Text(tick.title) .font(CottonFonts.caslon(ReaderContentsKnobs.rowSize)) .foregroundStyle(tick.here ? face.ink : face.secondary) .multilineTextAlignment(.leading) .frame(maxWidth: .infinity, alignment: .leading) } .padding(.horizontal, ReaderChromeKnobs.side) .frame(minHeight: ReaderContentsKnobs.row) .contentShape(Rectangle()) } .buttonStyle(.plain) .id(index) .accessibilityAddTraits(tick.here ? .isSelected : []) .accessibilityIdentifier("reader-contents-\(index)") }}#endif