oficina / base Public

Clone
git clone https://amadan.net/oficina/base

Plain git — no account needed to clone.

Download

Download this file

1#if os(macOS)
2import RastrilloApple
3import SwiftUI
4
5/// Settings (⌘,), mockup `mac-settings`: General, Account and Ask Base.
6/// The shortcut's recorder and the login item are the Mac app's own
7/// (KeyboardShortcuts, SMAppService); this draws them.
8public struct MacSettingsView<Recorder: View>: View {
9 public enum Tab: Hashable, Sendable { case general, account, ask }
10
11 public struct Model {
12 public var me: Me?
13 public var identity: String
14 public var askPhase: AskPhase
15 /// The Oficina apps installed on this Mac, by name.
16 public var installed: [String]
17 public var shortcutSet: Bool
18 public init(me: Me?, identity: String, askPhase: AskPhase, installed: [String], shortcutSet: Bool) {
19 self.me = me; self.identity = identity; self.askPhase = askPhase; self.installed = installed; self.shortcutSet = shortcutSet
20 }
21 }
22
23 let model: Model
24 @Binding var showMenuBar: Bool
25 @Binding var openAtLogin: Bool
26 @Binding var openIn: OpenPreference
27 let recorder: Recorder
28 let setAppearance: (BaseAppearance) -> Void
29 let disconnect: () -> Void
30 let actions: BaseActions
31 @State private var tab: Tab
32 /// Snapshots draw one tab's page alone: a TabView drawn off screen
33 /// draws its tab bar blank in the dark.
34 let tabBar: Bool
35 @State private var switching = false
36 @State private var confirmingSignOut = false
37 @Environment(\.colorScheme) private var scheme
38
39 public init(model: Model, showMenuBar: Binding<Bool>, openAtLogin: Binding<Bool>, openIn: Binding<OpenPreference>, tab: Tab = .general, tabBar: Bool = true,
40 setAppearance: @escaping (BaseAppearance) -> Void, disconnect: @escaping () -> Void, actions: BaseActions,
41 @ViewBuilder recorder: () -> Recorder) {
42 self.model = model; _showMenuBar = showMenuBar; _openAtLogin = openAtLogin; _openIn = openIn
43 self.recorder = recorder(); self.setAppearance = setAppearance; self.disconnect = disconnect; self.actions = actions
44 _tab = State(initialValue: tab)
45 self.tabBar = tabBar
46 }
47
48 public var body: some View {
49 if tabBar { tabs } else {
50 Group {
51 switch tab {
52 case .general: page(general)
53 case .account: page(account)
54 case .ask: page(askTab)
55 }
56 }
57 .frame(width: 560)
58 }
59 }
60
61 private var tabs: some View {
62 TabView(selection: $tab) {
63 page(general).tabItem { Label(BaseText.text("native.settings.tab.general"), systemImage: "slider.horizontal.3") }.tag(Tab.general)
64 page(account).tabItem { Label(BaseText.text("native.settings.tab.account"), systemImage: "person") }.tag(Tab.account)
65 page(askTab).tabItem { Label(BaseText.text("native.settings.tab.ask"), systemImage: "sparkle") }.tag(Tab.ask)
66 }
67 .frame(width: 560)
68 .accountChoices(actions: actions, switching: $switching, confirmingSignOut: $confirmingSignOut)
69 }
70
71 private func page(_ content: some View) -> some View {
72 ZStack(alignment: .top) {
73 Sky()
74 content.padding(22)
75 }
76 .fixedSize(horizontal: false, vertical: true)
77 }
78
79 // MARK: General
80
81 private var general: some View {
82 VStack(spacing: 14) {
83 SettingsGroup {
84 SettingsRow(BaseText.text("native.settings.appearance")) {
85 Segmented(selection: Binding(get: { model.me?.appearance ?? .system }, set: setAppearance),
86 choices: [(.system, "native.settings.appearance.system"), (.light, "native.settings.appearance.light"), (.dark, "native.settings.appearance.dark")])
87 }
88 SettingsRow(BaseText.text("native.settings.menubar"), detail: BaseText.text("native.settings.menubar.detail")) {
89 Toggle(isOn: $showMenuBar) { Text(BaseText.text("native.settings.menubar")) }.labelsHidden().toggleStyle(BaseSwitchStyle())
90 }
91 SettingsRow(BaseText.text("native.settings.shortcut"), detail: model.shortcutSet ? nil : BaseText.text("native.settings.shortcut.detail")) {
92 recorder
93 }
94 SettingsRow(BaseText.text("native.settings.login")) {
95 Toggle(isOn: $openAtLogin) { Text(BaseText.text("native.settings.login")) }.labelsHidden().toggleStyle(BaseSwitchStyle())
96 }
97 }
98 SettingsGroup {
99 SettingsRow(BaseText.text("native.settings.open_in"), detail: installedLine) {
100 Segmented(selection: $openIn, choices: [(.app, "native.settings.open_in.app"), (.browser, "native.settings.open_in.browser")])
101 }
102 }
103 }
104 }
105
106 /// "Docs, Sheets and Calls are on this Mac"; nil with none installed.
107 private var installedLine: String? {
108 guard !model.installed.isEmpty else { return nil }
109 let names = model.installed.formatted(.list(type: .and).locale(Locale(identifier: "en_GB")))
110 return BaseText.format(model.installed.count == 1 ? "native.settings.open_in.detail.one" : "native.settings.open_in.detail", ["apps": names])
111 }
112
113 // MARK: Account
114
115 private var account: some View {
116 VStack(alignment: .leading, spacing: 14) {
117 SettingsGroup {
118 SettingsRow(BaseText.text("native.settings.account.signed_in_as"), detail: nil) {
119 VStack(alignment: .trailing, spacing: 0) {
120 Text(verbatim: model.me?.member.name ?? "").font(BaseFont.body(14.5))
121 Text(verbatim: model.me?.member.email ?? model.identity).font(BaseFont.body(12.5)).foregroundStyle(Palette(scheme)[.haze])
122 }
123 }
124 SettingsRow(BaseText.text("native.settings.account.team"), detail: model.me?.base.name) {
125 Button(BaseText.text("native.account.switch")) { switching = true }.buttonStyle(MiniButtonStyle())
126 }
127 }
128 Button(BaseText.text("native.account.signout")) { confirmingSignOut = true }.buttonStyle(MiniButtonStyle())
129 }
130 }
131
132 // MARK: Ask Base
133
134 private var askTab: some View {
135 SettingsGroup {
136 if model.askPhase.chatting {
137 SettingsRow(BaseText.text("native.settings.tab.ask"), detail: nil) {
138 HStack(spacing: 12) {
139 Text(BaseText.text("native.settings.ask.connected")).font(BaseFont.body(13, .semibold)).foregroundStyle(Palette(scheme)[.signal])
140 Button(BaseText.text("native.settings.ask.disconnect"), action: disconnect).buttonStyle(MiniButtonStyle())
141 }
142 }
143 } else {
144 let note = AskActions.note(model.askPhase)
145 SettingsRow(BaseText.text(note.lede), detail: nil) {
146 switch note.way {
147 case .here(let key)?: Button(BaseText.text(key), action: actions.setUpAsk).buttonStyle(MiniButtonStyle())
148 case .web(let key)?: Button(BaseText.text(key), action: actions.setUpAskOnWeb).buttonStyle(MiniButtonStyle())
149 case nil: EmptyView()
150 }
151 }
152 }
153 }
154 }
155}
156
157/// A group of Settings' rows on glass, ruled between rows.
158struct SettingsGroup<Content: View>: View {
159 @ViewBuilder let content: Content
160 @Environment(\.colorScheme) private var scheme
161 var body: some View {
162 VStack(spacing: 0) {
163 Group(subviews: content) { rows in
164 ForEach(Array(rows.enumerated()), id: \.offset) { index, row in
165 if index > 0 { Rectangle().fill(Palette(scheme)[.rule]).frame(height: 1) }
166 row
167 }
168 }
169 }
170 .padding(.horizontal, 16).padding(.vertical, 2)
171 .glass(cornerRadius: 14)
172 }
173}
174
175/// One row: its name, a smaller line under it, and its control.
176struct SettingsRow<Control: View>: View {
177 let label: String
178 let detail: String?
179 @ViewBuilder let control: Control
180 @Environment(\.colorScheme) private var scheme
181 init(_ label: String, detail: String? = nil, @ViewBuilder control: () -> Control) {
182 self.label = label; self.detail = detail; self.control = control()
183 }
184 var body: some View {
185 let p = Palette(scheme)
186 HStack(spacing: 12) {
187 VStack(alignment: .leading, spacing: 1) {
188 Text(verbatim: label).font(BaseFont.body(14.5))
189 if let detail { Text(verbatim: detail).font(BaseFont.body(12.5, relativeTo: .footnote)).foregroundStyle(p[.haze]) }
190 }
191 .frame(maxWidth: .infinity, alignment: .leading)
192 control
193 }
194 .foregroundStyle(p[.frost])
195 .frame(minHeight: 50)
196 .accessibilityElement(children: .contain)
197 }
198}
199
200/// A segmented choice, the chosen one filled (`.seg`).
201struct Segmented<Value: Hashable>: View {
202 @Binding var selection: Value
203 let choices: [(Value, String)]
204 @Environment(\.colorScheme) private var scheme
205 var body: some View {
206 let p = Palette(scheme)
207 HStack(spacing: 0) {
208 ForEach(choices, id: \.0) { value, key in
209 let on = value == selection
210 Button { selection = value } label: {
211 Text(BaseText.text(key)).font(BaseFont.body(13, on ? .semibold : .regular))
212 .padding(.horizontal, 12).padding(.vertical, 4)
213 .foregroundStyle(on ? p[.btnInk] : p[.frost])
214 .background(on ? p[.btn] : .clear, in: RoundedRectangle(cornerRadius: 7, style: .continuous))
215 .contentShape(Rectangle())
216 }
217 .buttonStyle(.plain)
218 .accessibilityAddTraits(on ? .isSelected : [])
219 }
220 }
221 .padding(2)
222 .background(p[.chip], in: RoundedRectangle(cornerRadius: 9, style: .continuous))
223 .overlay { RoundedRectangle(cornerRadius: 9, style: .continuous).strokeBorder(p[.chipEdge], lineWidth: 1) }
224 }
225}
226
227/// The mockup's switch (`.switch`): the accent when on, the track's
228/// outline when off.
229struct BaseSwitchStyle: ToggleStyle {
230 @Environment(\.colorScheme) private var scheme
231 func makeBody(configuration: Configuration) -> some View {
232 let p = Palette(scheme)
233 Button { configuration.isOn.toggle() } label: {
234 ZStack(alignment: configuration.isOn ? .trailing : .leading) {
235 Capsule().fill(configuration.isOn ? p[.signal] : p[.track])
236 .overlay { if !configuration.isOn { Capsule().strokeBorder(p[.trackEdge], lineWidth: 1) } }
237 Circle().fill(configuration.isOn ? Color.white : p[.frost]).frame(width: 18, height: 18).padding(2)
238 }
239 .frame(width: 38, height: 22)
240 .animation(.easeOut(duration: 0.15), value: configuration.isOn)
241 }
242 .buttonStyle(.plain)
243 .accessibilityRepresentation { Toggle(isOn: configuration.$isOn) { configuration.label } }
244 }
245}
246
247/// Settings' small buttons (`.mini`).
248struct MiniButtonStyle: ButtonStyle {
249 @Environment(\.colorScheme) private var scheme
250 func makeBody(configuration: Configuration) -> some View {
251 let p = Palette(scheme)
252 configuration.label
253 .font(BaseFont.body(13, .semibold))
254 .foregroundStyle(p[.frost])
255 .padding(.horizontal, 12).frame(minHeight: 30)
256 .background(p[.ghost], in: RoundedRectangle(cornerRadius: 8, style: .continuous))
257 .overlay { RoundedRectangle(cornerRadius: 8, style: .continuous).strokeBorder(p[.ghostEdge], lineWidth: 1) }
258 .opacity(configuration.isPressed ? 0.8 : 1)
259 }
260}
261#endif
262