// COTTON CHAT'S ROOT in the Warm look (assets/design/chat/DECISIONS.md): the // whole app, mounted by the root in place of the fan and the board // (`RootView.hosts`), as Story, Notes and Days are. Sign-in comes first: // nothing works signed out. Signed in, three places on a floating pill at the // foot: Chats, Calls and You. Mail, Senders and Sites are Cotton Post's and // Cotton Sites' now (COMMS WITHOUT MAIL, 26 Nov 2026). #if os(iOS) import CottonKit import SwiftUI /// Comms' places, in the floating switch's order: the core's `chat.list` answers Chats, Calls and You only. enum ChatPlace: String, CaseIterable, Identifiable { case chats, calls, you var id: String { rawValue } } /// Chat's words: the chat line set's `app.*`. enum ChatWords { static func line(_ path: String, _ fills: [String: String] = [:]) -> String { ShellLines.line("chat", "app.\(path)", fills) } } struct ChatRoot: View { /// Cotton Chat's own build (the Messages flavour or its Dev twin). static var applies: Bool { ChatBooks.isChat } let core: any CottonCalling @Environment(PersonalObject.self) private var personal: PersonalObject? @Environment(\.chatCalls) private var calls @Environment(\.cottonChat) private var chat @Environment(\.colorScheme) private var scheme @State private var place: ChatPlace = .chats /// A panel stands on the place (Blocked, Change picture, Delete's ask): the pill steps away so the panel rises whole. @State private var panelUp = false /// A page of You stands (Sync, Privacy…): it has Back, and no place switch under it. @State private var youPageUp = false /// The floating pill's height (with its gap to the foot) and its top in the window, as last laid out: every place scrolls clear of it. @State private var pillHeight: CGFloat = 56 @State private var pillTop: CGFloat = .infinity private var look: ChatLook { ChatLook.warm(dark: scheme == .dark) } private var book: any ChatBook { ChatBooks.make(core: core, chat: chat) } /// The core the screens ask; in Chat Dev's look seed, the invented people in front of it. private var asked: any CottonCalling { #if COTTON_DEV if ChatWarmSeed.asked { return ChatWarmSeedCore(inner: core) } #endif return core } /// Signed in, signed out, or not yet known (the first read is out). private enum Door { case unknown, out, open } private var door: Door { #if COTTON_DEV if ChatWarmSeed.asked { return .open } #endif guard let personal else { return .open } guard personal.home != nil else { return .unknown } return personal.stand == .local ? .out : .open } var body: some View { ZStack { look.colours.ground.ignoresSafeArea() switch door { case .unknown: Color.clear case .out: ChatSignInScreen(personal: personal) case .open: places } } .environment(\.chatLook, look) // Look (shared/DECISIONS.md "Look"): every screen in the paper, ink, face and size chosen. .lookStanding(LookModel.standing("messages")) .task { await LookModel.standing("messages").read(core) } // A recording the app was closed on is kept, and the words wait to be made (CONTRACT.md "Recordings"). .task { await ChatRecorder.shared.attach(asked) } #if COTTON_DEV // Two changes that cannot be sent, for a walk of the Sync page's Send again and Let go (`-chat-stuck YES`). .task { if UserDefaults.standard.bool(forKey: "chat-stuck") { _ = try? await core.callJSON("developer.stuckChange") } } #endif // A recording filed or brought in from outside (an audio file opened into Comms) opens in Calls. .onChange(of: ChatRecorder.shared.saved) { _, saved in if saved != nil { place = .calls } } .environment(\.chatBook, book) .environment(\.cottonCore, asked) // Every web link goes to Cotton Sites when it is on the phone, else Safari's sheet (`chat.link`, ChatLinks.swift). .modifier(ChatLinksHost(core: asked, look: look)) // A group's call is told to the group by the next `chat.flush` (CONTRACT.md "Group calls"): the call engine // asks it through the book, which holds the chat key. .onAppear { [book] in calls?.flush = { await book.flush() } } .task { WarmFonts.register() FanLaunch.firstContent() // You draws whole on its first frame from the rows last read; a phone with none yet reads them now. Task { await ChatYouRows.warm(asked) } #if COTTON_DEV if ChatWarmSeed.asked, let thread = ChatWarmSeed.open { ChatOpening.shared.open(thread: thread) } if ChatWarmSeed.asked, let asked = ChatWarmSeed.place.flatMap(ChatPlace.init(rawValue:)) { place = asked } #endif } } private var places: some View { ZStack(alignment: .bottom) { Group { switch place { case .chats: ChatHome(filter: "all") case .calls: ChatCallsScreen(book: book, look: look) case .you: ChatYouScreen(core: asked, known: ChatYouRemembered.known(personal)) { chat in place = .chats ChatOpening.shared.open(thread: chat) } } } .frame(maxWidth: .infinity, maxHeight: .infinity) // Every place's scrolled content ends above the floating pill (its // last row reachable over it) and passes under it while scrolling; // a UIKit list reads the pill's top itself (`chatPillTop`). .safeAreaPadding(.bottom, youPageUp ? 0 : pillHeight) .environment(\.chatPillTop, pillTop) // Scrolled content stops below the status bar (owner's iPhone, // Comms build 19): what scrolls above the safe area's top is cut // away, and the ground behind shows under the clock. A cut, not a // view laid over the bar: a band there was read by the // accessibility audit as an unnamed bar across the screen. .mask { Rectangle().ignoresSafeArea(edges: [.horizontal, .bottom]) } .onPreferenceChange(LookPanelUp.self) { panelUp = $0 } // The home's sync line was tapped: You, on its Sync page. .onReceive(NotificationCenter.default.publisher(for: SyncSeenOpens.notice)) { _ in place = .you } // A page of You, or a recording's page or the recording screen over Calls: Back instead of the switch. .onPreferenceChange(ChatYouPageUp.self) { youPageUp = $0 && (place == .you || place == .calls) } if !panelUp, !(youPageUp && (place == .you || place == .calls)) { ChatPlacePill(place: $place) .onGeometryChange(for: CGRect.self) { $0.frame(in: .global) } action: { frame in pillHeight = frame.height pillTop = frame.minY } } #if COTTON_DEV if ChatWarmSeed.asked, let state = ChatWarmSeed.call { if state == "notes-bar" { // The call folded to its bar while notes are taken. let screen = ChatWarmSeed.callScreen("notes") VStack { ChatCallBar(text: screen.bar ?? "", liveAt: screen.liveAt, notes: screen.notes) {}; Spacer() } } else { ChatCallScreenView(screen: ChatWarmSeed.callScreen(state), calls: ChatWarmSeedCalls()) {} } } #endif } } } /// The floating pill at the foot: Chats, Calls, You. struct ChatPlacePill: View { @Binding var place: ChatPlace var places: [ChatPlace] = ChatPlace.allCases @Environment(\.chatLook) private var look @Environment(\.dynamicTypeSize) private var typeSize var body: some View { HStack(spacing: 4) { ForEach(places) { one in let on = one == place Button { place = one } label: { Text(verbatim: ChatWords.line("place.\(one.rawValue)")) .font(look.font(15, on ? .semibold : .regular)) // The places stay on one line within the screen at the largest sizes. .lineLimit(1) .minimumScaleFactor(0.5) .padding(.horizontal, typeSize.isAccessibilitySize ? 8 : 14) .foregroundStyle(on ? look.colours.text : look.colours.dim) .frame(minHeight: 40) .background(Capsule().fill(on ? look.colours.bar : Color.clear)) .contentShape(Capsule()) } .buttonStyle(.plain) .accessibilityLabel(ChatWords.line("place.\(one.rawValue)")) .accessibilityAddTraits(on ? [.isButton, .isSelected] : .isButton) .accessibilityIdentifier("chat-place-\(one.rawValue)") } } .padding(4) .background(Capsule().fill(look.colours.ground)) .accessibilityElement(children: .contain) .accessibilityIdentifier("chat-places") .padding(.bottom, Warm.pillGap) } } private struct ChatPillTopKey: EnvironmentKey { static let defaultValue: CGFloat = .infinity } extension EnvironmentValues { /// The floating pill's top in the window (`.infinity` where none floats): a list drawn by UIKit ends its rows above it. var chatPillTop: CGFloat { get { self[ChatPillTopKey.self] } set { self[ChatPillTopKey.self] = newValue } } } /// Signed out: Chat says what it is and offers the one way in. struct ChatSignInScreen: View { let personal: PersonalObject? @Environment(\.chatLook) private var look var body: some View { VStack(alignment: .leading, spacing: 14) { Spacer() Text(verbatim: ChatWords.line("welcome.title")) .font(look.font(40, .bold)) .foregroundStyle(look.colours.text) .accessibilityAddTraits(.isHeader) Text(verbatim: ChatWords.line("welcome.line")) .font(look.font(17)) .foregroundStyle(look.colours.dim) .fixedSize(horizontal: false, vertical: true) Spacer() Button { Task { await personal?.backUp() } } label: { Text(verbatim: ChatWords.line("welcome.sign_in")) .font(look.font(17, .semibold)) .foregroundStyle(look.colours.onAccent) .frame(maxWidth: .infinity, minHeight: 54) .background(Capsule().fill(look.colours.accent)) } .buttonStyle(.plain) .accessibilityIdentifier("chat-sign-in") } .padding(.horizontal, 28) .padding(.bottom, 24) } } #endif