SPB Git

Repository Public

SettingsSupport.swift 5.3 KB 148 lines swift

1// ORIX DJ2// File: App/Sources/Features/Settings/SettingsSupport.swift3// Description: Shared pieces of the Settings window: the tab list, navigation to a tab from elsewhere in the app, the4//              launch option that opens the window, and the row/card layout used by every tab.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import SwiftUI910enum SettingsTab: String, CaseIterable, Identifiable, Sendable {11    case audio, mixer, library, appearance, ai, automation, about12    var id: String { rawValue }1314    var title: LocalizedStringKey {15        switch self {16        case .audio: "Audio"17        case .mixer: "Mixer"18        case .library: "Library"19        case .appearance: "Appearance"20        case .ai: "AI"21        case .automation: "Automation"22        case .about: "About"23        }24    }2526    var symbol: String {27        switch self {28        case .audio: "hifispeaker.2"29        case .mixer: "slider.vertical.3"30        case .library: "music.note.list"31        case .appearance: "circle.lefthalf.filled"32        case .ai: "sparkles"33        case .automation: "chevron.left.forwardslash.chevron.right"34        case .about: "info.circle"35        }36    }37}3839/// Lets other screens open the Settings window on a given tab.40@MainActor @Observable41final class SettingsNavigation {42    static let shared = SettingsNavigation()43    var tab: SettingsTab = .audio44}4546/// `-ORIXOpenSettings <tab>` opens the Settings window on a tab at launch (screenshots, UI tests).47struct SettingsLaunchOption: ViewModifier {48    @Environment(\.openSettings) private var openSettings49    @MainActor private static var handled = false5051    func body(content: Content) -> some View {52        content.task {53            guard !Self.handled, let raw = UserDefaults.standard.string(forKey: "ORIXOpenSettings") else { return }54            Self.handled = true55            if let tab = SettingsTab(rawValue: raw) { SettingsNavigation.shared.tab = tab }56            try? await Task.sleep(nanoseconds: 600_000_000)57            openSettings()58        }59    }60}6162/// A titled group of settings.63struct SettingsCard<Content: View>: View {64    var title: LocalizedStringKey65    @ViewBuilder var content: Content6667    var body: some View {68        VStack(alignment: .leading, spacing: 8) {69            SectionLabel(title)70            Panel(padding: 0) {71                VStack(alignment: .leading, spacing: 0) { content }.frame(maxWidth: .infinity, alignment: .leading)72            }73        }74    }75}7677/// One setting: name and explanation on the left, the control on the right.78struct SettingsRow<Control: View>: View {79    var title: LocalizedStringKey80    var detail: LocalizedStringKey?81    var divider = true82    @ViewBuilder var control: Control8384    var body: some View {85        VStack(spacing: 0) {86            HStack(alignment: .center, spacing: ORIX.Space.md) {87                VStack(alignment: .leading, spacing: 3) {88                    Text(title).font(.system(size: 13, weight: .medium)).foregroundStyle(ORIX.Palette.textPrimary)89                    if let detail { Text(detail).font(.system(size: 11.5)).foregroundStyle(ORIX.Palette.textSecondary).fixedSize(horizontal: false, vertical: true) }90                }91                .frame(maxWidth: .infinity, alignment: .leading)92                control93            }94            .padding(.horizontal, 14).padding(.vertical, 10)95            if divider { Rectangle().fill(ORIX.Palette.hairline).frame(height: 1).padding(.leading, 14) }96        }97    }98}99100/// Explanatory text inside a card.101struct SettingsNote: View {102    var text: LocalizedStringKey103    var symbol: String = "info.circle"104    var tint: Color = ORIX.Palette.textSecondary105106    init(_ text: LocalizedStringKey, symbol: String = "info.circle", tint: Color = ORIX.Palette.textSecondary) { self.text = text; self.symbol = symbol; self.tint = tint }107108    var body: some View {109        HStack(alignment: .firstTextBaseline, spacing: 8) {110            Image(systemName: symbol).font(.system(size: 11)).foregroundStyle(tint).accessibilityHidden(true)111            Text(text).font(.system(size: 11.5)).foregroundStyle(ORIX.Palette.textSecondary).fixedSize(horizontal: false, vertical: true)112        }113        .frame(maxWidth: .infinity, alignment: .leading)114        .padding(.horizontal, 14).padding(.vertical, 10)115    }116}117118/// A label with a measured value in monospaced digits.119struct SettingsReadout: View {120    var label: LocalizedStringKey121    var value: String122    var tint: Color = ORIX.Palette.textPrimary123124    var body: some View {125        VStack(alignment: .leading, spacing: 2) {126            Text(label).font(ORIX.Typeface.label).foregroundStyle(ORIX.Palette.textTertiary).textCase(.uppercase)127            Text(verbatim: value).font(ORIX.Typeface.readout(15)).foregroundStyle(tint)128        }129        .frame(maxWidth: .infinity, alignment: .leading)130        .accessibilityElement(children: .ignore)131        .accessibilityLabel(label)132        .accessibilityValue(Text(verbatim: value))133    }134}135136/// The scrolling body of a tab.137struct SettingsPage<Content: View>: View {138    @ViewBuilder var content: Content139    var body: some View {140        ScrollView {141            VStack(alignment: .leading, spacing: ORIX.Space.md) { content }142                .padding(ORIX.Space.lg)143                .frame(maxWidth: .infinity, alignment: .leading)144        }145        .background(ORIX.Palette.canvas)146    }147}148
Files main
Menu

Theme