#if os(macOS) import RastrilloApple import SwiftUI /// Settings (⌘,), mockup `mac-settings`: General, Account and Ask Base. /// The shortcut's recorder and the login item are the Mac app's own /// (KeyboardShortcuts, SMAppService); this draws them. public struct MacSettingsView: View { public enum Tab: Hashable, Sendable { case general, account, ask } public struct Model { public var me: Me? public var identity: String public var askPhase: AskPhase /// The Oficina apps installed on this Mac, by name. public var installed: [String] public var shortcutSet: Bool public init(me: Me?, identity: String, askPhase: AskPhase, installed: [String], shortcutSet: Bool) { self.me = me; self.identity = identity; self.askPhase = askPhase; self.installed = installed; self.shortcutSet = shortcutSet } } let model: Model @Binding var showMenuBar: Bool @Binding var openAtLogin: Bool @Binding var openIn: OpenPreference let recorder: Recorder let setAppearance: (BaseAppearance) -> Void let disconnect: () -> Void let actions: BaseActions @State private var tab: Tab /// Snapshots draw one tab's page alone: a TabView drawn off screen /// draws its tab bar blank in the dark. let tabBar: Bool @State private var switching = false @State private var confirmingSignOut = false @Environment(\.colorScheme) private var scheme public init(model: Model, showMenuBar: Binding, openAtLogin: Binding, openIn: Binding, tab: Tab = .general, tabBar: Bool = true, setAppearance: @escaping (BaseAppearance) -> Void, disconnect: @escaping () -> Void, actions: BaseActions, @ViewBuilder recorder: () -> Recorder) { self.model = model; _showMenuBar = showMenuBar; _openAtLogin = openAtLogin; _openIn = openIn self.recorder = recorder(); self.setAppearance = setAppearance; self.disconnect = disconnect; self.actions = actions _tab = State(initialValue: tab) self.tabBar = tabBar } public var body: some View { if tabBar { tabs } else { Group { switch tab { case .general: page(general) case .account: page(account) case .ask: page(askTab) } } .frame(width: 560) } } private var tabs: some View { TabView(selection: $tab) { page(general).tabItem { Label(BaseText.text("native.settings.tab.general"), systemImage: "slider.horizontal.3") }.tag(Tab.general) page(account).tabItem { Label(BaseText.text("native.settings.tab.account"), systemImage: "person") }.tag(Tab.account) page(askTab).tabItem { Label(BaseText.text("native.settings.tab.ask"), systemImage: "sparkle") }.tag(Tab.ask) } .frame(width: 560) .accountChoices(actions: actions, switching: $switching, confirmingSignOut: $confirmingSignOut) } private func page(_ content: some View) -> some View { ZStack(alignment: .top) { Sky() content.padding(22) } .fixedSize(horizontal: false, vertical: true) } // MARK: General private var general: some View { VStack(spacing: 14) { SettingsGroup { SettingsRow(BaseText.text("native.settings.appearance")) { Segmented(selection: Binding(get: { model.me?.appearance ?? .system }, set: setAppearance), choices: [(.system, "native.settings.appearance.system"), (.light, "native.settings.appearance.light"), (.dark, "native.settings.appearance.dark")]) } SettingsRow(BaseText.text("native.settings.menubar"), detail: BaseText.text("native.settings.menubar.detail")) { Toggle(isOn: $showMenuBar) { Text(BaseText.text("native.settings.menubar")) }.labelsHidden().toggleStyle(BaseSwitchStyle()) } SettingsRow(BaseText.text("native.settings.shortcut"), detail: model.shortcutSet ? nil : BaseText.text("native.settings.shortcut.detail")) { recorder } SettingsRow(BaseText.text("native.settings.login")) { Toggle(isOn: $openAtLogin) { Text(BaseText.text("native.settings.login")) }.labelsHidden().toggleStyle(BaseSwitchStyle()) } } SettingsGroup { SettingsRow(BaseText.text("native.settings.open_in"), detail: installedLine) { Segmented(selection: $openIn, choices: [(.app, "native.settings.open_in.app"), (.browser, "native.settings.open_in.browser")]) } } } } /// "Docs, Sheets and Calls are on this Mac"; nil with none installed. private var installedLine: String? { guard !model.installed.isEmpty else { return nil } let names = model.installed.formatted(.list(type: .and).locale(Locale(identifier: "en_GB"))) return BaseText.format(model.installed.count == 1 ? "native.settings.open_in.detail.one" : "native.settings.open_in.detail", ["apps": names]) } // MARK: Account private var account: some View { VStack(alignment: .leading, spacing: 14) { SettingsGroup { SettingsRow(BaseText.text("native.settings.account.signed_in_as"), detail: nil) { VStack(alignment: .trailing, spacing: 0) { Text(verbatim: model.me?.member.name ?? "").font(BaseFont.body(14.5)) Text(verbatim: model.me?.member.email ?? model.identity).font(BaseFont.body(12.5)).foregroundStyle(Palette(scheme)[.haze]) } } SettingsRow(BaseText.text("native.settings.account.team"), detail: model.me?.base.name) { Button(BaseText.text("native.account.switch")) { switching = true }.buttonStyle(MiniButtonStyle()) } } Button(BaseText.text("native.account.signout")) { confirmingSignOut = true }.buttonStyle(MiniButtonStyle()) } } // MARK: Ask Base private var askTab: some View { SettingsGroup { if model.askPhase.chatting { SettingsRow(BaseText.text("native.settings.tab.ask"), detail: nil) { HStack(spacing: 12) { Text(BaseText.text("native.settings.ask.connected")).font(BaseFont.body(13, .semibold)).foregroundStyle(Palette(scheme)[.signal]) Button(BaseText.text("native.settings.ask.disconnect"), action: disconnect).buttonStyle(MiniButtonStyle()) } } } else { let note = AskActions.note(model.askPhase) SettingsRow(BaseText.text(note.lede), detail: nil) { switch note.way { case .here(let key)?: Button(BaseText.text(key), action: actions.setUpAsk).buttonStyle(MiniButtonStyle()) case .web(let key)?: Button(BaseText.text(key), action: actions.setUpAskOnWeb).buttonStyle(MiniButtonStyle()) case nil: EmptyView() } } } } } } /// A group of Settings' rows on glass, ruled between rows. struct SettingsGroup: View { @ViewBuilder let content: Content @Environment(\.colorScheme) private var scheme var body: some View { VStack(spacing: 0) { Group(subviews: content) { rows in ForEach(Array(rows.enumerated()), id: \.offset) { index, row in if index > 0 { Rectangle().fill(Palette(scheme)[.rule]).frame(height: 1) } row } } } .padding(.horizontal, 16).padding(.vertical, 2) .glass(cornerRadius: 14) } } /// One row: its name, a smaller line under it, and its control. struct SettingsRow: View { let label: String let detail: String? @ViewBuilder let control: Control @Environment(\.colorScheme) private var scheme init(_ label: String, detail: String? = nil, @ViewBuilder control: () -> Control) { self.label = label; self.detail = detail; self.control = control() } var body: some View { let p = Palette(scheme) HStack(spacing: 12) { VStack(alignment: .leading, spacing: 1) { Text(verbatim: label).font(BaseFont.body(14.5)) if let detail { Text(verbatim: detail).font(BaseFont.body(12.5, relativeTo: .footnote)).foregroundStyle(p[.haze]) } } .frame(maxWidth: .infinity, alignment: .leading) control } .foregroundStyle(p[.frost]) .frame(minHeight: 50) .accessibilityElement(children: .contain) } } /// A segmented choice, the chosen one filled (`.seg`). struct Segmented: View { @Binding var selection: Value let choices: [(Value, String)] @Environment(\.colorScheme) private var scheme var body: some View { let p = Palette(scheme) HStack(spacing: 0) { ForEach(choices, id: \.0) { value, key in let on = value == selection Button { selection = value } label: { Text(BaseText.text(key)).font(BaseFont.body(13, on ? .semibold : .regular)) .padding(.horizontal, 12).padding(.vertical, 4) .foregroundStyle(on ? p[.btnInk] : p[.frost]) .background(on ? p[.btn] : .clear, in: RoundedRectangle(cornerRadius: 7, style: .continuous)) .contentShape(Rectangle()) } .buttonStyle(.plain) .accessibilityAddTraits(on ? .isSelected : []) } } .padding(2) .background(p[.chip], in: RoundedRectangle(cornerRadius: 9, style: .continuous)) .overlay { RoundedRectangle(cornerRadius: 9, style: .continuous).strokeBorder(p[.chipEdge], lineWidth: 1) } } } /// The mockup's switch (`.switch`): the accent when on, the track's /// outline when off. struct BaseSwitchStyle: ToggleStyle { @Environment(\.colorScheme) private var scheme func makeBody(configuration: Configuration) -> some View { let p = Palette(scheme) Button { configuration.isOn.toggle() } label: { ZStack(alignment: configuration.isOn ? .trailing : .leading) { Capsule().fill(configuration.isOn ? p[.signal] : p[.track]) .overlay { if !configuration.isOn { Capsule().strokeBorder(p[.trackEdge], lineWidth: 1) } } Circle().fill(configuration.isOn ? Color.white : p[.frost]).frame(width: 18, height: 18).padding(2) } .frame(width: 38, height: 22) .animation(.easeOut(duration: 0.15), value: configuration.isOn) } .buttonStyle(.plain) .accessibilityRepresentation { Toggle(isOn: configuration.$isOn) { configuration.label } } } } /// Settings' small buttons (`.mini`). struct MiniButtonStyle: ButtonStyle { @Environment(\.colorScheme) private var scheme func makeBody(configuration: Configuration) -> some View { let p = Palette(scheme) configuration.label .font(BaseFont.body(13, .semibold)) .foregroundStyle(p[.frost]) .padding(.horizontal, 12).frame(minHeight: 30) .background(p[.ghost], in: RoundedRectangle(cornerRadius: 8, style: .continuous)) .overlay { RoundedRectangle(cornerRadius: 8, style: .continuous).strokeBorder(p[.ghostEdge], lineWidth: 1) } .opacity(configuration.isPressed ? 0.8 : 1) } } #endif