// 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) import CottonKit import CottonPaper import ReadiumShared import SwiftUI import UIKit extension BookReading { /// 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() } } private func readSeekNow() async { do { let answer = try await 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) if let before = seekTicksPassed, before != passed { UIImpactFeedbackGenerator(style: .light).impactOccurred(intensity: 0.5) } seekTicksPassed = passed seekWanted = part guard seekSaying == nil else { return } seekSaying = Task { [weak self] in while let self, let part = self.seekWanted { self.seekWanted = nil let said = try? await self.core.call(ReaderCalls.seekSay, ReaderSeekAsk(book: self.book, at: part), as: ReaderSeekSaid.self) // Words for a finger that has since lifted are not drawn. if let 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) { guard let 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)) } private func jump(_ ask: ReaderSeekAsk) { Task { do { let went = try await core.call(ReaderCalls.seekGo, ask, as: ReaderSeekWent.self) await land(went) } catch { seekLanding = nil Log.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 = try await 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. private func land(_ went: ReaderSeekWent) async { stands(went.back) guard let text = went.locator, let locator = try? Locator(jsonString: text) else { seekLanding = nil return } 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 = answer guard let answer else { return } backClock = Timer.scheduledTimer(withTimeInterval: Double(max(answer.msLeft, 0)) / 1000 + 0.05, repeats: false) { [weak self] _ in MainActor.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 == false else { return } contentsShown = true } } } // MARK: - The contents enum ReaderContentsKnobs { static let headSize: CGFloat = 13 static let rowSize: CGFloat = 17 static let row: CGFloat = 48 static let 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. struct ReaderContents: View { let reading: BookReading let seek: ReaderSeekAnswer var body: some View { 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 in ScrollView { LazyVStack(spacing: 0) { ForEach(Array(seek.ticks.enumerated()), id: \.offset) { index, tick in row(index, tick, face) } } .padding(.vertical, 8) } .onAppear { if let here = seek.ticks.firstIndex(where: \.here) { scroll.scrollTo(here, anchor: .center) } } } } .background(face.ground.ignoresSafeArea()) .accessibilityElement(children: .contain) .accessibilityIdentifier("reader-contents") } private func row(_ index: Int, _ tick: ReaderSeekAnswer.Tick, _ face: ReaderFace) -> some View { 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