// The databases a person holds, as caps on a second arc under a fan Home's // tiles (fan2 part 6, the Home board's `databases` part; fan3 part 1: the row // became an arc). One cap per held database, in the Databases screen's own // order; none when only one is held. The arc is concentric with the tiles' — // the same pivot below the screen, a smaller radius, one band gap under the // raised tile (`DatabaseCapRow.geometry`) — and // turned as the tiles are: the same arc, motion, fling, rubber band and // haptic per cap crossing (`FanArcDeck`, `FanMotion`), with its own size and // step. The cap at the centre is the chosen one, raised and larger — its own // printed name and the header say which; the arc heading for another cap // switches to that database: its kept Home is read while the old one stands, // then shown whole in one frame (`DatabaseScene.show`), and the thought the // record button keeps goes there. import CottonKit import SwiftUI /// The rules of the caps, apart from how they are drawn. @MainActor enum DatabaseCapRow { /// One cap on the arc: a held database, or the last cap, which opens the /// Databases screen (new or join). enum Cap: Equatable { case database(HeldDatabase) case databases /// The cap's key on the arc, and the database it shows if any. var key: String { switch self { case .database(let held): return held.databaseId case .databases: return "databases" } } var database: HeldDatabase? { if case .database(let held) = self { return held } return nil } } /// The caps the arc draws (defaults, night plan, 16 Oct 2026): the /// personal database at the centre, the others either side by name, and /// last the cap that opens the Databases screen — the head draws no /// database symbol, so the arc stands whenever a database is held. static func caps(_ model: DatabasesModel?) -> [Cap] { let held = model?.ordered ?? [] guard !held.isEmpty else { return [] } return arc(held).map(Cap.database) + [.databases] } /// The held databases in the arc's order: the others by name, split /// either side of the personal database so that it stands at the centre /// of the whole arc, the last cap included (the odd one goes left). static func arc(_ held: [HeldDatabase]) -> [HeldDatabase] { let others = held.filter { !$0.isPersonal } .sorted { $0.name.localizedStandardCompare($1.name) == .orderedAscending } let left = (others.count + 1) / 2 return Array(others.prefix(left)) + held.filter(\.isPersonal) + Array(others.dropFirst(left)) } /// A cap chosen: its database is shown, as a pick on the Databases screen /// is — the window's calls, the page and the record button's thoughts /// all move to it. The cap of the database shown keeps it, and a switch /// still reading for another cap turns nothing. static func choose(_ id: String, scene: DatabaseScene?, in model: DatabasesModel?) async { guard let scene, let model, model.database(id) != nil else { return } guard id != scene.shownId else { return scene.stay() } await scene.pick(id, in: model) } /// A database an act brought back (`CommandOpened.database`, a Restore /// in the Bin): whatever was pushed over Home closes, the held list is /// read again so its cap stands on the arc, and it is shown as a chosen /// cap is (architect, 16 Oct 2026). static func show(_ id: String, scene: DatabaseScene?, in model: DatabasesModel?, navigator: Navigator?) async { navigator?.popToRoot() await model?.refresh() await choose(id, scene: scene, in: model) } /// The caps' arc about the tiles' pivot: a cap is `fan_cap_size` square, /// its step the cap and the gap between two caps along the arc, the /// chosen one raised by `fan_cap_lift` and larger by as much. `fan` is /// the tiles' arc and `above` the height of the tiles' band over this /// one, so the two arcs share one pivot. The band and the radius come /// from the tile's size (architect, 15 Oct 2026, 16:50 IST): the band is /// the raised chosen cap, whose top stands at the band's top — one band /// gap under the raised tile — so the radius is the pivot's depth less /// the raised cap's lift and half its size. static func geometry(scale: ThemeScale, fan: FanGeometry, above: Double) -> FanGeometry { let side = Double(scale[.fanCapSize]) let lift = Double(scale[.fanCapLift]) let pivot = fan.top + fan.tileHeight / 2 + fan.radius - above let radius = pivot - lift - (side + lift) / 2 let pointsPerDegree = Double.pi * radius / 180 return FanGeometry( tileWidth: side, tileHeight: side, radius: radius, step: (side + Double(scale[.fanCapsGap])) / pointsPerDegree, visible: fan.visible, height: side + lift, top: pivot - radius - side / 2, lift: lift, settledScale: (side + lift) / side, overscroll: fan.overscroll, flingMin: fan.flingMin, flingFull: fan.flingFull, flingReach: fan.flingReach, swipeLean: fan.swipeLean, tapSlop: fan.tapSlop, turnPerDegree: fan.turnPerDegree, turnLongest: fan.turnLongest, turnShortest: fan.turnShortest) } } #if os(iOS) /// The caps on their arc. struct DatabaseCapArc: View { /// The tiles' arc, and the height of their band over this one. let fan: FanGeometry let above: Double @Environment(DatabasesModel.self) private var databases: DatabasesModel? @Environment(DatabaseScene.self) private var scene: DatabaseScene? @Environment(\.theme) private var theme @Environment(\.cottonHaptics) private var haptics @Environment(\.accessibilityReduceMotion) private var reduceMotion @Environment(\.displayScale) private var displayScale @Environment(\.self) private var environment @State private var motion: FanMotion? @State private var prints = FanPrints() /// What a cap's picture shows. struct CapPrint: Equatable { let name: String let colour: CapColour let side: Double } private var geometry: FanGeometry { DatabaseCapRow.geometry(scale: theme.scale, fan: fan, above: above) } var body: some View { let caps = DatabaseCapRow.caps(databases) let scale = theme.scale let geometry = geometry ZStack(alignment: .topLeading) { if !caps.isEmpty, let motion { FanArcDeck( items: caps.map { cap in FanArcItem(key: cap.key, label: cap.database?.name ?? DatabaseCapWords.databases, picture: prints.picture(cap.key, look(of: cap)), identifier: cap.database.map { "fan-database-\($0.databaseId)" } ?? "fan-databases") }, motion: motion, geometry: geometry, reach: geometry.lift, faintest: Double(scale[.fanCapsFaint]), reduceMotion: reduceMotion ) { index in haptics.press() motion.turn(to: index, animated: !reduceMotion, byPerson: true) } } } .frame(maxWidth: .infinity) .frame(height: caps.isEmpty ? 0 : geometry.height, alignment: .top) .onAppear { start(caps) } .onChange(of: caps.map(\.key)) { _, _ in start(DatabaseCapRow.caps(databases)) } // The database shown is known once the launch has read it: the arc // stands on its cap then, not on whichever cap it mounted at. .onChange(of: scene?.shownId) { _, _ in start(DatabaseCapRow.caps(databases)) } .onChange(of: theme) { _, _ in prints.clear() drawPrints(DatabaseCapRow.caps(databases)) } // The arc heading for another cap — a tap, or a finger lifting: the // switch to its database happens now (`SwitchClock`), not when the // arc comes to rest — the fan stood on the old database's cards under // the new cap for the whole of the caps' spring (292, walker291). // The old Home stands while the new one is read from the phone's // copy, then the new one stands whole (`DatabaseScene.show`); // heading back to the database shown keeps it. Not the arc standing // on the chosen cap as it mounts: nobody chose that. // A swipe chooses where it rests, never on its way (`ArcChoice.chooses`). .onChange(of: motion?.heading) { was, heading in guard was != nil, let heading else { return } choose(at: heading, .heading) } // The arc at rest on the last cap: the Databases screen opens and the // arc turns back to the database shown (a database's cap was shown // as the arc headed for it). Only a rest a person brought: the arc // mounting, its caps arriving and a turn back are nobody's choice, // and a launch draws Me with no sheet over it (walker pass 4). .onChange(of: motion?.settled) { _, settled in let caps = DatabaseCapRow.caps(databases) guard motion?.byPerson == true, let settled, caps.indices.contains(settled) else { return } guard caps[settled].database == nil else { return choose(at: settled, .settled) } scene?.openDatabases() if let shown = caps.firstIndex(where: { $0.database?.databaseId == scene?.shownId }) { motion?.turn(to: shown, animated: !reduceMotion) } } } /// The cap at `index` chosen, when the arc's move is a person's choice. private func choose(at index: Int, _ move: ArcChoice.Move) { let caps = DatabaseCapRow.caps(databases) guard let motion, caps.indices.contains(index), let chosen = caps[index].database?.databaseId, ArcChoice.chooses(move, byPerson: motion.byPerson, swiped: motion.swiped) else { return } if chosen != scene?.shownId { SwitchClock.asked(chosen) } Task { await DatabaseCapRow.choose(chosen, scene: scene, in: databases) } } private func look(of cap: DatabaseCapRow.Cap) -> CapPrint { guard let held = cap.database else { return CapPrint(name: DatabaseCapWords.databases, colour: .navy, side: geometry.tileWidth) } return CapPrint( name: databases?.capName(held) ?? held.name, colour: databases?.colours[held.databaseId] ?? .navy, side: geometry.tileWidth) } /// The arc stands on the chosen cap, its pictures drawn. private func start(_ caps: [DatabaseCapRow.Cap]) { let motion = self.motion ?? FanMotion(geometry: geometry, spring: FanSpring(scale: theme.scale)) motion.geometry = geometry motion.onCross = { [haptics] in haptics.select() } motion.recount(caps.count) if let chosen = caps.firstIndex(where: { $0.database?.databaseId == scene?.shownId }) { motion.turn(to: chosen, animated: false) } self.motion = motion prints.keepOnly(Set(caps.map(\.key))) drawPrints(caps) } private func drawPrints(_ caps: [DatabaseCapRow.Cap]) { let geometry = geometry for cap in caps where prints.picture(cap.key, look(of: cap)) == nil { let look = look(of: cap) let scale = Double(displayScale) * geometry.settledScale if cap.database == nil { prints.draw(cap.key, look, scale: scale, environment: environment, content: EmptyCap(side: CGFloat(look.side)).padding(geometry.lift)) } else { prints.draw(cap.key, look, scale: scale, environment: environment, content: DatabaseCap(name: look.name, colour: look.colour, side: CGFloat(look.side)) .padding(geometry.lift)) } } } } #endif /// The words the caps' arc says. enum DatabaseCapWords { /// The last cap: it opens the Databases screen, where one is made or joined. static var databases: String { ShellLines.line("board", "caps.databases") } }