// While reading, the whole bottom of Home collapses into one small circle // (lane fancollapse, 15 Oct 2026; the owner's idea, the architect's design). // A finger scrolling the page down past `fan_collapse_scroll` folds the // tiles' arc, the caps' arc and the record button into the record button // alone, smaller (`fan_collapsed_size`), at the bottom centre. Only the // circle brings it back — a swipe up from it or a tap on it; a scroll back up // never does (owner, 21 Oct 2026: reading back up is still reading). It // replaces the fan stepping aside (fan2 part 4). // // The state is the session's, not a page's: a long-press jump, voice // navigation, a database switch, a programmatic scroll and a content reload // never change it, and at a page's very top it is left as it is. A page // opened over a tile's own folds the dial and its close blooms it in the same // motion — the fan is under the finger the moment the page goes (owner's // rule 8; architect, 16 Oct 2026: it never came back after a record closed). // While a page stands there no scroll blooms the fan: the fan is turned to // the tile, never to the page over it. A fresh launch starts bloomed. Only `scrolled` (folding), // `page`, `fold` (the X of a fan rooted on one tool's views, `shell.fan`) and // `swipedUp` (the circle's swipe or tap) write it (FanCollapseTests // holds who calls them). import os import SwiftUI /// The collapse, as a rule over the page's scroll: pure, so it is a test. struct FanCollapseRule: Equatable { var collapsed = false /// The offset last read, nil until a page is watched. var last: Double? /// A finger's travel in its present direction: down positive, up negative. var travel = 0.0 /// A page stands over the tile's own. var paged = false /// The ring over a record page is tucked away: a scroll down tucks it, /// only the circle brings it back. var ringTucked = false /// Whether a page stands over the tile's own now: one opening folds the /// dial, its close blooms it, and either ends any travel. With no page /// before or after, the state is left as it is. mutating func pageStands(_ open: Bool) { defer { paged = open } guard open || paged else { return } collapsed = open ringTucked = false travel = 0 } /// The offset and end a page reports, from its scroll view's offset /// below its top and how far it can scroll (negative: too short to). A /// page too short to scroll is at its top: its bounce up under a finger /// changes nothing, but a pull down is a scroll up — so a page that /// scrolled only while the fan took its room, and stopped once it folded, /// still blooms it (fancollapse review, 15 Oct 2026: Mood folded and no /// scroll could bloom it). static func reading(below top: Double, scrollable: Double) -> (offset: Double, end: Double) { (scrollable > 0 ? top : min(0, top), max(0, scrollable)) } /// A new page is watched: its offset starts afresh, the state stays. mutating func watching() { last = nil travel = 0 } /// The page stands `offset` points below its top, of `end` it can /// scroll; `byFinger` when a person moved it (dragging, or their fling /// running on). Anything else — a reload, a page growing, a programmatic /// scroll, the room changing under the page — only resets the travel. mutating func read(offset: Double, end: Double, byFinger: Bool, collapseAfter: Double) { defer { last = offset } // Past the bottom the page is bouncing back, or its end moved as the // fan's room changed: nobody is reading on. guard let last, byFinger, offset <= end, last <= end else { return travel = 0 } let delta = offset - last guard delta != 0 else { return } // Above the top a finger pulling the page down is scrolling up; the // page settling back to its top is nobody reading on. if last < 0, delta > 0 { return travel = 0 } if travel != 0, (delta > 0) != (travel > 0) { travel = 0 } travel += delta // A scroll down folds it; no scroll blooms it — only the circle does. if !collapsed, travel >= collapseAfter { collapsed = true travel = 0 } else if collapsed, paged, !ringTucked, travel >= collapseAfter { // Folded over a record page, reading on tucks its ring too. ringTucked = true travel = 0 } } } /// The session's collapse. One per run of the app (`session`): a fresh launch /// starts bloomed, and every page, record and database of the run shares it. @MainActor @Observable final class FanCollapse { static let session = FanCollapse() /// The fan is folded into the circle. private(set) var collapsed = false /// The ring over a record page is tucked away (`FanCollapseRule.ringTucked`). private(set) var ringTucked = false /// The rule's travel changes with every scrolled point; only a change of /// state redraws anything. @ObservationIgnored private var rule = FanCollapseRule() /// One line per fold and bloom, for the fan's perf sweep (scripts/perf). private static let log = Logger(subsystem: "com.cottondb.fan", category: "fold") /// A new page is watched (another tile, a record opened or closed on /// it): the state is kept. func watching() { rule.watching() } /// A reading of the watched page's scroll (`FanScrollWatch`). The change, /// when there is one, runs in `animation` — the one spring. func scrolled(offset: Double, end: Double, byFinger: Bool, scale: ThemeScale, animation: Animation?) { rule.read(offset: offset, end: end, byFinger: byFinger, collapseAfter: Double(scale[.fanCollapseScroll])) if rule.ringTucked != ringTucked { withAnimation(animation) { ringTucked = rule.ringTucked } } guard rule.collapsed != collapsed else { return } set(rule.collapsed, animation: animation) } /// A page opened over a tile's own: the dial collapses — the caps, the /// record button and the fan's circle — and the page has the screen /// (owner's rule 8, 16 Oct 2026). Its close blooms the dial in the same /// frame, in the one motion, whatever folded it before the page opened: /// nothing waits on a read, a timer or a scroll (architect, 16 Oct 2026). func page(open: Bool, animation: Animation?) { rule.pageStands(open) if rule.ringTucked != ringTucked { withAnimation(animation) { ringTucked = rule.ringTucked } } guard rule.collapsed != collapsed else { return } set(rule.collapsed, animation: animation) } /// A tap on the circle: a tucked ring comes back over it, the fan stays /// folded; with nothing tucked, it blooms the fan. func tapped(animation: Animation?) { guard rule.ringTucked else { return swipedUp(animation: animation) } rule.ringTucked = false rule.travel = 0 withAnimation(animation) { ringTucked = false } } /// The hub's X on a fan whose root is one tool's views (`shell.fan`): no /// tier stands under it, so it closes the fan into the circle, and the /// circle blooms it again. func fold(animation: Animation?) { guard !collapsed else { return } rule.collapsed = true rule.travel = 0 set(true, animation: animation) } /// A swipe up from the circle blooms the fan, under its ring. func swipedUp(animation: Animation?) { if rule.ringTucked { rule.ringTucked = false withAnimation(animation) { ringTucked = false } } guard collapsed else { return } rule.collapsed = false rule.travel = 0 set(false, animation: animation) } private func set(_ folded: Bool, animation: Animation?) { withAnimation(animation) { collapsed = folded } Self.log.notice("fold: \(folded ? "collapsed" : "bloomed", privacy: .public)") } /// The one motion of a collapse and a bloom: a spring, or with Reduce /// Motion a crossfade. static func animation(_ scale: ThemeScale, reduceMotion: Bool) -> Animation { reduceMotion ? .easeInOut(duration: Double(scale[.fanTurnMsMin]) / 1000) : .spring(response: Double(scale[.fanCollapseResponse]), dampingFraction: Double(scale[.fanCollapseDamping])) } }