SPB Git

Repository Public License

FormatPicker.swift 5.7 KB 109 lines swift

1// ORIX Convert — App/Sources/Features/SmartDrop/FormatPicker.swift2// Description: Searchable destination picker for a group: operations, then formats by category, each with its3//              fidelity and experimental status. Lists only destinations a real plan exists for.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import ConvertCore6import SwiftUI78struct FormatPicker: View {9    @Environment(AppModel.self) private var model10    let group: StagedGroup11    @Binding var isPresented: Bool12    @State private var search = ""13    @FocusState private var searchFocused: Bool1415    private struct Entry: Identifiable {16        let destination: FormatID17        let operation: String?18        let title: String19        let fidelity: Fidelity20        let experimental: Bool21        let chainedVia: [FormatID]22        var id: String { destination.rawValue + "#" + (operation ?? "") }23    }2425    private var operations: [Entry] {26        group.operations.map { Entry(destination: $0.destination, operation: $0.operation, title: LE($0.title), fidelity: $0.best.fidelity,27                                     experimental: $0.best.status == .experimental, chainedVia: []) }.filter(matches)28    }2930    private var formatsByCategory: [(FormatCategory, [Entry])] {31        var entries = group.targets.map { Entry(destination: $0.destination, operation: nil, title: $0.destination.displayName, fidelity: $0.best.fidelity,32                                                experimental: $0.best.status == .experimental, chainedVia: []) }33        if let source = group.format {34            for destination in group.chained {35                // A chained destination is listed only when the planner really finds the two steps.36                guard let plan = try? model.manager.planner.plan(from: source, to: destination) else { continue }37                entries.append(Entry(destination: destination, operation: nil, title: destination.displayName, fidelity: plan.fidelity,38                                     experimental: plan.status == .experimental, chainedVia: plan.intermediates))39            }40        }41        let grouped = Dictionary(grouping: entries.filter(matches)) { $0.destination.category }42        return FormatCategory.allCases.compactMap { category in grouped[category].map { (category, $0) } }43    }4445    private func matches(_ entry: Entry) -> Bool {46        let needle = search.trimmingCharacters(in: .whitespaces).lowercased()47        if needle.isEmpty { return true }48        let extensions = entry.destination.catalogEntry?.extensions ?? []49        return entry.title.lowercased().contains(needle) || entry.destination.rawValue.contains(needle) || extensions.contains { $0.contains(needle) }50    }5152    var body: some View {53        VStack(spacing: 0) {54            SearchField(text: $search, prompt: L("Search formats"), focus: $searchFocused).padding(ORIX.Space.m)55            ORIX.Palette.hairline.frame(height: 1)56            ScrollView {57                LazyVStack(alignment: .leading, spacing: 2) {58                    let ops = operations, formats = formatsByCategory59                    if ops.isEmpty && formats.isEmpty {60                        Text(L("No destination matches “%@”.", search)).font(.callout).foregroundStyle(ORIX.Palette.textSecondary).padding(ORIX.Space.l)61                    }62                    if !ops.isEmpty {63                        SectionLabel(L("Operations")).padding(.horizontal, ORIX.Space.s).padding(.top, ORIX.Space.s)64                        ForEach(ops) { row($0) }65                    }66                    ForEach(formats, id: \.0) { category, entries in67                        SectionLabel(category.title).padding(.horizontal, ORIX.Space.s).padding(.top, ORIX.Space.m)68                        ForEach(entries) { row($0) }69                    }70                }71                .padding(ORIX.Space.s)72            }73        }74        .frame(width: 380, height: 440)75        .background(ORIX.Palette.surface)76        .onAppear { searchFocused = true }77    }7879    private func row(_ entry: Entry) -> some View {80        let selected = group.choice?.destination == entry.destination && group.choice?.operation == entry.operation81        return Button {82            model.workspace.selectedGroupID = group.id83            model.workspace.select(destination: entry.destination, operation: entry.operation, in: group)84            isPresented = false85        } label: {86            HStack(spacing: ORIX.Space.s) {87                FormatBadge(format: entry.destination, size: .small, selected: selected)88                VStack(alignment: .leading, spacing: 1) {89                    Text(entry.title).font(.callout.weight(selected ? .semibold : .regular)).foregroundStyle(ORIX.Palette.textPrimary).lineLimit(1)90                    if !entry.chainedVia.isEmpty {91                        Text(L("In two steps, through %@", entry.chainedVia.map(\.displayName).joined(separator: ", "))).font(.caption2).foregroundStyle(ORIX.Palette.textTertiary)92                    }93                }94                Spacer(minLength: 4)95                if entry.experimental { ExperimentalBadge() }96                FidelityBadge(fidelity: entry.fidelity)97                if selected { Image(systemName: "checkmark").font(.caption.weight(.bold)).foregroundStyle(ORIX.Palette.accent) }98            }99            .padding(.horizontal, ORIX.Space.s).padding(.vertical, 5)100            .background(selected ? ORIX.Palette.accentSoft : .clear, in: RoundedRectangle(cornerRadius: ORIX.Radius.s, style: .continuous))101            .contentShape(Rectangle())102        }103        .buttonStyle(.plain)104        .accessibilityLabel(entry.title)105        .accessibilityValue(entry.fidelity.title + (entry.experimental ? ", " + L("Experimental") : ""))106        .accessibilityAddTraits(selected ? [.isSelected] : [])107    }108}109
Files main
Menu

Theme