SPB Git

Repository Public License

OptionsForm.swift 8.8 KB 158 lines swift

1// ORIX Convert — App/Sources/Features/SmartDrop/OptionsForm.swift2// Description: Settings form generated from OptionDescriptors (toggle / integer / number / choice / text), grouped,3//              with an "Advanced" disclosure. Keys containing "password" are SecureFields and are never stored.4//              Shared by the options panel and the Metadata Studio sheets.5// Author: Simon-Pierre Boucher — contact@spboucher.ai6import ConvertCore7import SwiftUI89struct OptionsForm: View {10    let descriptors: [OptionDescriptor]11    /// Explicit values; a missing key means "default".12    let values: [String: OptionValue]13    let set: (String, OptionValue?) -> Void14    @State private var showAdvanced = false1516    private func groups(_ list: [OptionDescriptor]) -> [(String, [OptionDescriptor])] {17        var order: [String] = []18        var map: [String: [OptionDescriptor]] = [:]19        for descriptor in list {20            if map[descriptor.group] == nil { order.append(descriptor.group) }21            map[descriptor.group, default: []].append(descriptor)22        }23        return order.map { ($0, map[$0] ?? []) }24    }2526    var body: some View {27        let basic = descriptors.filter { !$0.advanced }, advanced = descriptors.filter(\.advanced)28        VStack(alignment: .leading, spacing: ORIX.Space.l) {29            ForEach(groups(basic), id: \.0) { name, options in group(name, options) }30            if !advanced.isEmpty {31                let changed = advanced.filter { values[$0.id] != nil }.count32                DisclosureGroup(isExpanded: $showAdvanced) {33                    VStack(alignment: .leading, spacing: ORIX.Space.l) {34                        ForEach(groups(advanced), id: \.0) { name, options in group(name, options) }35                    }36                    .padding(.top, ORIX.Space.m)37                } label: {38                    HStack(spacing: 6) {39                        Text(L("Advanced")).font(.callout.weight(.medium))40                        if changed > 0 { Pill(text: L("%lld changed", changed), symbol: nil, color: ORIX.Palette.accent) }41                    }42                }43            }44        }45    }4647    private func group(_ name: String, _ options: [OptionDescriptor]) -> some View {48        VStack(alignment: .leading, spacing: ORIX.Space.m) {49            SectionLabel(LE(name))50            ForEach(options) { OptionControl(descriptor: $0, value: values[$0.id], set: set) }51        }52    }53}5455struct OptionControl: View {56    let descriptor: OptionDescriptor57    let value: OptionValue?58    let set: (String, OptionValue?) -> Void5960    private var label: String { LE(descriptor.label) }61    private var isSecret: Bool { ConversionOptions.isSecretKey(descriptor.id) }6263    var body: some View {64        VStack(alignment: .leading, spacing: 4) {65            control66            if let help = descriptor.help {67                Text(LE(help)).font(.caption).foregroundStyle(ORIX.Palette.textTertiary).fixedSize(horizontal: false, vertical: true)68            }69            if isSecret {70                Label(L("Used for this conversion only. Never saved."), systemImage: "lock").font(.caption2).foregroundStyle(ORIX.Palette.textTertiary)71            }72        }73    }7475    @ViewBuilder private var control: some View {76        switch descriptor.kind {77        case .toggle(let defaultValue):78            let current: Bool = { if case .bool(let v)? = value { return v }; return defaultValue }()79            Toggle(isOn: Binding(get: { current }, set: { set(descriptor.id, .bool($0)) })) {80                Text(label).font(.callout).foregroundStyle(ORIX.Palette.textPrimary).fixedSize(horizontal: false, vertical: true)81            }82            .toggleStyle(.checkbox)83        case .integer(let defaultValue, let range, let unit):84            let current: Int = { switch value { case .int(let v)?: return v; case .double(let v)?: return Int(v); default: return defaultValue } }()85            let binding = Binding<Int>(get: { current }, set: { set(descriptor.id, .int(min(range.upperBound, max(range.lowerBound, $0)))) })86            if unit == "%", range.upperBound - range.lowerBound <= 100 {87                // A percentage is a level, not a count: a slider follows the hand, and the form reacts while it moves.88                VStack(alignment: .leading, spacing: 2) {89                    HStack {90                        Text(label).font(.callout).foregroundStyle(ORIX.Palette.textPrimary)91                        Spacer(minLength: 4)92                        Text((Double(current) / 100).formatted(.percent.precision(.fractionLength(0)))).font(.orixNumber).foregroundStyle(ORIX.Palette.textSecondary)93                    }94                    Slider(value: Binding(get: { Double(current) }, set: { binding.wrappedValue = Int($0.rounded()) }), in: Double(range.lowerBound)...Double(range.upperBound))95                        .accessibilityLabel(label)96                        .accessibilityValue((Double(current) / 100).formatted(.percent.precision(.fractionLength(0))))97                }98            } else {99            HStack(spacing: 6) {100                Text(label).font(.callout).foregroundStyle(ORIX.Palette.textPrimary).fixedSize(horizontal: false, vertical: true)101                Spacer(minLength: 4)102                TextField(label, value: binding, format: .number.grouping(.never))103                    .textFieldStyle(.roundedBorder).multilineTextAlignment(.trailing).font(.orixNumber).frame(width: 70).labelsHidden()104                    .accessibilityLabel(label)105                if let unit { Text(LE(unit)).font(.caption).foregroundStyle(ORIX.Palette.textSecondary) }106                Stepper(label, value: binding, in: range).labelsHidden().accessibilityLabel(label)107            }108            .help(L("From %lld to %lld", range.lowerBound, range.upperBound))109            }110        case .number(let defaultValue, let range, let step, let unit):111            let current: Double = { switch value { case .double(let v)?: return v; case .int(let v)?: return Double(v); default: return defaultValue } }()112            VStack(alignment: .leading, spacing: 2) {113                HStack {114                    Text(label).font(.callout).foregroundStyle(ORIX.Palette.textPrimary)115                    Spacer(minLength: 4)116                    Text(current.formatted(.number.precision(.fractionLength(0...2))) + (unit.map { " " + LE($0) } ?? ""))117                        .font(.orixNumber).foregroundStyle(ORIX.Palette.textSecondary)118                }119                Slider(value: Binding(get: { current }, set: { set(descriptor.id, .double(($0 / step).rounded() * step)) }), in: range)120                    .accessibilityLabel(label)121                    .accessibilityValue(current.formatted(.number.precision(.fractionLength(0...2))))122            }123        case .choice(let defaultValue, let choices):124            let current: String = { if case .string(let v)? = value { return v }; return defaultValue }()125            let picker = Picker(label, selection: Binding(get: { current }, set: { set(descriptor.id, .string($0)) })) {126                ForEach(choices, id: \.value) { Text(LE($0.label)).tag($0.value) }127            }128            .labelsHidden().pickerStyle(.menu)129            .accessibilityLabel(label)130            // Side by side when both fit; otherwise the menu goes under its label (long choices, narrow panels). Under131            // its label the menu takes the width of the panel at most: a long choice (French labels are longer) is132            // then cut by the menu button itself instead of pushing the whole form out of the panel.133            ViewThatFits(in: .horizontal) {134                HStack(spacing: ORIX.Space.s) {135                    Text(label).font(.callout).foregroundStyle(ORIX.Palette.textPrimary).lineLimit(1).fixedSize()136                    Spacer(minLength: 4)137                    picker.fixedSize()138                }139                VStack(alignment: .leading, spacing: 4) {140                    Text(label).font(.callout).foregroundStyle(ORIX.Palette.textPrimary).fixedSize(horizontal: false, vertical: true)141                    picker.frame(maxWidth: .infinity, alignment: .leading)142                }143            }144        case .text(let defaultValue, let placeholder):145            let current: String = { if case .string(let v)? = value { return v }; return defaultValue }()146            let binding = Binding<String>(get: { current }, set: { set(descriptor.id, $0.isEmpty && defaultValue.isEmpty ? nil : .string($0)) })147            VStack(alignment: .leading, spacing: 3) {148                Text(label).font(.callout).foregroundStyle(ORIX.Palette.textPrimary)149                if isSecret {150                    SecureField(LE(placeholder), text: binding).textFieldStyle(.roundedBorder).accessibilityLabel(label)151                } else {152                    TextField(LE(placeholder), text: binding).textFieldStyle(.roundedBorder).accessibilityLabel(label)153                }154            }155        }156    }157}158
Files main
Menu

Theme