// How far the live page under Home's fan has scrolled (lane fan, stage 4). // The page is whatever screen the tile opens — a list, a board, Home's own // board — each with its own scroll view, so the fan finds the one scroll view // that fills the page area in the window and watches its offset, rather than // asking every screen to report it. #if os(iOS) import UIKit @MainActor final class FanScrollWatch { private var observation: NSKeyValueObservation? private weak var watched: UIScrollView? /// Bumped by every stop: a transition's end that finds the page again /// does so only for the watch it was asked for. private var generation = 0 /// Watches the scroll view filling `area` (window points); `onOffset` /// gets how far below its top the page is, how far it can scroll, in /// points, and whether a finger moved it (dragging, or its fling running /// on). A page too short to scroll is at its top; only its pull down /// is read (`FanCollapseRule.reading`). func watch(in area: CGRect, onOffset: @escaping @MainActor (_ offset: Double, _ end: Double, _ byFinger: Bool) -> Void) { guard let scroll = Self.scrollView(in: area) else { return stop() } // Mid-push or mid-pop, the page leaving can still cover the area: // the page is found again once the transition ends (fancollapse // review, 15 Oct 2026: after a record closed by the edge swipe, the // fan watched the record's scroll and no scroll up bloomed it). if let transition = Self.transition(of: scroll) { let asked = generation + (scroll === watched ? 0 : 1) transition.animate(alongsideTransition: nil) { [weak self] _ in MainActor.assumeIsolated { guard let self, self.generation == asked else { return } self.watch(in: area, onOffset: onOffset) } } } guard scroll !== watched else { return } stop() watched = scroll observation = scroll.observe(\.contentOffset, options: [.initial, .new]) { scroll, _ in MainActor.assumeIsolated { let inset = scroll.adjustedContentInset let read = FanCollapseRule.reading( below: Double(scroll.contentOffset.y + inset.top), scrollable: Double(scroll.contentSize.height + inset.top + inset.bottom - scroll.bounds.height)) onOffset(read.offset, read.end, scroll.isTracking || scroll.isDecelerating) } } } func stop() { generation += 1 observation?.invalidate() observation = nil watched = nil } /// The navigation transition `view` stands in, while one runs. static func transition(of view: UIView) -> UIViewControllerTransitionCoordinator? { var responder: UIResponder? = view while let next = responder?.next { if let navigation = next as? UINavigationController, let coordinator = navigation.transitionCoordinator { return coordinator } responder = next } return nil } /// The scroll view that covers most of `area`: the page's own. A text /// view scrolls too, and is not the page. Only Home's own presentation is /// searched: a sheet over it (the Databases sheet) stands in the same /// window, and its scroll folded the dial under it and was let go with /// the sheet, so no scroll of the page bloomed it again (walker pass 3, /// 16 Oct 2026). static func scrollView(in area: CGRect) -> UIScrollView? { guard let window = PagePictures.keyWindow, area.width > 0, area.height > 0, let home = window.rootViewController?.view else { return nil } var best: (view: UIScrollView, cover: CGFloat)? func visit(_ view: UIView) { if let scroll = view as? UIScrollView, !(scroll is UITextView), !scroll.isHidden, scroll.isScrollEnabled { let frame = scroll.convert(scroll.bounds, to: window).intersection(area) let cover = frame.isNull ? 0 : frame.width * frame.height if cover > area.width * area.height / 2, cover > (best?.cover ?? 0) { best = (scroll, cover) } } view.subviews.forEach(visit) } visit(home) return best?.view } /// The largest scroll view inside `view`: an off-screen page's own. static func scrollView(within view: UIView) -> UIScrollView? { var best: (view: UIScrollView, area: CGFloat)? func visit(_ view: UIView) { if let scroll = view as? UIScrollView, !(scroll is UITextView), !scroll.isHidden, scroll.isScrollEnabled { let area = scroll.bounds.width * scroll.bounds.height if area > (best?.area ?? 0) { best = (scroll, area) } } view.subviews.forEach(visit) } visit(view) return best?.view } /// The page stands at its top: no scroll view is at its top too. static func atTop(_ scroll: UIScrollView?) -> Bool { guard let scroll else { return true } return scroll.contentOffset.y + scroll.adjustedContentInset.top <= 1 } } #endif