// 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)importCottonKitimportSwiftUI/// Comms' places, in the floating switch's order: the core's `chat.list` answers Chats, Calls and You only.enumChatPlace: String, CaseIterable, Identifiable {case chats, calls, youvar id: String { rawValue }}/// Chat's words: the chat line set's `app.*`.enumChatWords {staticfunc line(_ path: String, _ fills: [String: String] = [:]) -> String {ShellLines.line("chat", "app.\(path)", fills) }}structChatRoot: View {/// Cotton Chat's own build (the Messages flavour or its Dev twin).staticvar applies: Bool { ChatBooks.isChat }let core: any CottonCalling @Environment(PersonalObject.self) privatevar personal: PersonalObject? @Environment(\.chatCalls) privatevar calls @Environment(\.cottonChat) privatevar chat @Environment(\.colorScheme) privatevar scheme @State privatevar 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 privatevar panelUp = false/// A page of You stands (Sync, Privacy…): it has Back, and no place switch under it. @State privatevar 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 privatevar pillHeight: CGFloat = 56 @State privatevar pillTop: CGFloat = .infinityprivatevar look: ChatLook { ChatLook.warm(dark: scheme == .dark) }privatevar 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.privatevar asked: any CottonCalling { #if COTTON_DEVifChatWarmSeed.asked { returnChatWarmSeedCore(inner: core) } #endifreturn core }/// Signed in, signed out, or not yet known (the first read is out).privateenumDoor { case unknown, out, open }privatevar door: Door { #if COTTON_DEVifChatWarmSeed.asked { return .open } #endifguardlet personal else { return .open }guard personal.home != nilelse { return .unknown }return personal.stand == .local ? .out : .open }var body: someView { ZStack { look.colours.ground.ignoresSafeArea()switch door {case .unknown: Color.clearcase .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 { awaitLookModel.standing("messages").read(core) }// A recording the app was closed on is kept, and the words wait to be made (CONTRACT.md "Recordings"). .task { awaitChatRecorder.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 { ifUserDefaults.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 inif 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 { awaitChatYouRows.warm(asked) } #if COTTON_DEVifChatWarmSeed.asked, let thread = ChatWarmSeed.open { ChatOpening.shared.open(thread: thread) }ifChatWarmSeed.asked, let asked = ChatWarmSeed.place.flatMap(ChatPlace.init(rawValue:)) { place = asked } #endif } }privatevar places: someView { 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 = .chatsChatOpening.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_DEVifChatWarmSeed.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.structChatPlacePill: View { @Binding var place: ChatPlacevar places: [ChatPlace] = ChatPlace.allCases @Environment(\.chatLook) privatevar look @Environment(\.dynamicTypeSize) privatevar typeSizevar body: someView { HStack(spacing: 4) {ForEach(places) { one inlet on = one == placeButton { 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) }}privatestructChatPillTopKey: EnvironmentKey {staticlet defaultValue: CGFloat = .infinity}extensionEnvironmentValues {/// 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.structChatSignInScreen: View {let personal: PersonalObject? @Environment(\.chatLook) privatevar lookvar body: someView { 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