// Cotton Chat's settings: a theme for light appearance and one for dark, // each drawn as a small sample thread in its own colours; the text size; // the ground a thread lies on. Every word, theme and choice is the core's // (`chat.themes`; `chat.theme`, `chat.text`, `chat.ground` answer it whole). #if os(iOS) import SwiftUI struct ChatSettingsScreen: View { let book: any ChatBook @State var themes: ChatThemesAnswer? let changed: (ChatThemesAnswer) -> Void @Environment(\.colorScheme) private var scheme @Environment(\.dismiss) private var dismiss @Environment(\.closeCard) private var close init(book: any ChatBook, themes: ChatThemesAnswer?, changed: @escaping (ChatThemesAnswer) -> Void) { self.book = book _themes = State(initialValue: themes) self.changed = changed } private var look: ChatLook { ChatLook.from(themes, dark: scheme == .dark) } var body: some View { // Its own head, in Chat's look: no NavigationStack (NavigationSweepTests). VStack(spacing: 0) { HStack { Spacer() Text(verbatim: themes?.words.settings ?? "") .font(look.font(17, .semibold)) .foregroundStyle(look.colours.text) .accessibilityAddTraits(.isHeader) Spacer() } .overlay(alignment: .trailing) { Button { CardClosing.close(close, else: dismiss) } label: { Image(systemName: "checkmark").font(.system(size: 17, weight: .semibold)) .frame(width: 44, height: 44) .contentShape(Rectangle()) } .accessibilityIdentifier("chat-settings-done") } .padding(.horizontal, 8) .padding(.top, 8) ScrollView { VStack(alignment: .leading, spacing: 22) { if let themes { section(themes.words.light) { samples(themes, dark: false) } section(themes.words.dark) { samples(themes, dark: true) } section(themes.text.title) { chips(themes.text, identifier: "chat-text") { id in Task { await take(try? await book.setText(id)) } } } section(themes.ground.title) { grounds(themes) } } } .padding(16) } } .background(look.colours.ground.ignoresSafeArea()) .environment(\.chatLook, look) .environment(\.chatBook, book) .tint(look.colours.accent) .task { if themes == nil { await take(try? await book.themes()) } } } private func take(_ fresh: ChatThemesAnswer?) async { guard let fresh else { return } themes = fresh changed(fresh) } private func section(_ title: String, @ViewBuilder _ content: () -> Content) -> some View { VStack(alignment: .leading, spacing: 10) { Text(verbatim: title) .font(look.font(13, .semibold)) .foregroundStyle(look.colours.dim) .textCase(.uppercase) .accessibilityAddTraits(.isHeader) content() } } private func chips(_ row: ChatChoosing, identifier: String, choose: @escaping (String) -> Void) -> some View { ScrollView(.horizontal, showsIndicators: false) { HStack(spacing: 8) { ForEach(row.options) { option in let on = option.id == row.chosen Button { choose(option.id) } label: { Text(verbatim: option.label) .font(look.font(15 * (option.scale ?? 1), on ? .semibold : .regular)) .foregroundStyle(on ? look.colours.onAccent : look.colours.text) .padding(.horizontal, 14) .padding(.vertical, 8) .background(Capsule().fill(on ? look.colours.accent : look.colours.pill)) } .buttonStyle(.plain) .accessibilityIdentifier("\(identifier)-\(option.id)") .accessibilityAddTraits(on ? .isSelected : []) } } } } private func grounds(_ themes: ChatThemesAnswer) -> some View { let standing = themes.theme(dark: scheme == .dark)?.ground ?? "plain" return HStack(spacing: 10) { ForEach(themes.ground.options) { option in let on = option.id == themes.ground.chosen Button { Task { await take(try? await book.setGround(option.id)) } } label: { VStack(spacing: 6) { ChatGroundView(ground: option.id == "theme" ? standing : option.id) .frame(height: 70) .clipShape(RoundedRectangle(cornerRadius: 10, style: .continuous)) .overlay(RoundedRectangle(cornerRadius: 10, style: .continuous) .strokeBorder(on ? look.colours.accent : look.colours.rule, lineWidth: on ? 2 : 1)) Text(verbatim: option.label).font(look.font(13)).foregroundStyle(look.colours.text) } } .buttonStyle(.plain) .accessibilityIdentifier("chat-ground-\(option.id)") .accessibilityAddTraits(on ? .isSelected : []) } } } private func samples(_ themes: ChatThemesAnswer, dark: Bool) -> some View { let chosen = dark ? themes.dark : themes.light return ScrollView(.horizontal, showsIndicators: false) { HStack(spacing: 12) { ForEach(themes.themes) { theme in let sampleLook = ChatLook(colours: ChatColours(dark ? theme.dark : theme.light), ground: theme.ground, scale: 1) Button { Task { await take(try? await book.choose(theme: theme.id, dark: dark)) } } label: { VStack(spacing: 8) { ChatSample() .environment(\.chatLook, sampleLook) .frame(width: 150, height: 132) .clipShape(RoundedRectangle(cornerRadius: 14, style: .continuous)) .overlay(RoundedRectangle(cornerRadius: 14, style: .continuous) .strokeBorder(theme.id == chosen ? look.colours.accent : look.colours.rule, lineWidth: theme.id == chosen ? 2.5 : 1)) Text(verbatim: theme.name).font(look.font(14, theme.id == chosen ? .semibold : .regular)) .foregroundStyle(look.colours.text) } } .buttonStyle(.plain) .accessibilityIdentifier("chat-theme-\(dark ? "dark" : "light")-\(theme.id)") .accessibilityAddTraits(theme.id == chosen ? .isSelected : []) } } } } } /// A theme's sample: a small thread on its ground, the words drawn as soft /// lines in each bubble's own ink, so the colours speak and no word is made up. struct ChatSample: View { @Environment(\.chatLook) private var look var body: some View { ZStack(alignment: .bottom) { ChatGroundView(ground: look.ground) VStack(spacing: 4) { bubble(mine: false, lines: [0.75, 0.5], tail: true) bubble(mine: true, lines: [0.6], tail: false) bubble(mine: true, lines: [0.85, 0.4], tail: true) } .padding(10) } .accessibilityHidden(true) } private func bubble(mine: Bool, lines: [CGFloat], tail: Bool) -> some View { let ink = mine ? look.colours.bubbleOutText : look.colours.bubbleInText return HStack { if mine { Spacer(minLength: 24) } VStack(alignment: .leading, spacing: 4) { ForEach(Array(lines.enumerated()), id: \.offset) { _, width in Capsule().fill(ink.opacity(0.55)).frame(width: 84 * width, height: 5) } } .padding(.horizontal, 10) .padding(.vertical, 8) .background(ChatBubbleShape(mine: mine, tail: tail).fill(mine ? look.colours.bubbleOut : look.colours.bubbleIn)) if !mine { Spacer(minLength: 24) } } } } #endif