Repository Public License
1// ORIX Convert — App/Sources/Features/SmartDrop/OptionsPanel.swift2// Description: Side panel of the selected group: conversion summary, presets, generated options, input order for3// multi-source operations, and every warning and limitation of the plan.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import ConvertCore6import SwiftUI78struct OptionsPanel: View {9 @Environment(AppModel.self) private var model10 let group: StagedGroup11 @State private var presetName = ""12 @State private var showSavePreset = false1314 var body: some View {15 let workspace = model.workspace16 VStack(spacing: 0) {17 HStack(spacing: ORIX.Space.s) {18 if let choice = group.choice { ConversionPairBadge(source: group.format, destination: choice.destination) }19 VStack(alignment: .leading, spacing: 1) {20 Text(group.choice?.title ?? L("Options")).font(.headline).lineLimit(1)21 Text(group.title).font(.caption).foregroundStyle(ORIX.Palette.textSecondary).lineLimit(1)22 }23 Spacer(minLength: 4)24 IconButton(symbol: "sidebar.trailing", label: L("Hide Side Panel")) { model.setSidePanel(visible: false) }25 }26 .padding(.horizontal, ORIX.Space.l).frame(height: 54)27 ORIX.Palette.hairline.frame(height: 1)28 ScrollView {29 VStack(alignment: .leading, spacing: ORIX.Space.xl) {30 previewArea31 presets32 if group.combine && group.canCombine { inputOrder }33 if group.descriptors.isEmpty {34 Text(L("This conversion has no settings.")).font(.callout).foregroundStyle(ORIX.Palette.textSecondary)35 } else {36 OptionsForm(descriptors: group.descriptors, values: group.optionValues) { key, value in workspace.setOption(key, value, in: group) }37 if !group.optionValues.isEmpty {38 Button(L("Reset to Defaults")) { workspace.resetOptions(in: group) }.buttonStyle(SecondaryButtonStyle())39 }40 }41 planDetails42 warnings43 }44 .frame(maxWidth: .infinity, alignment: .leading)45 .padding(ORIX.Space.l)46 }47 }48 .background(ORIX.Palette.surface)49 .accessibilityElement(children: .contain)50 .accessibilityLabel(L("Conversion options"))51 }5253 // MARK: Preview5455 /// The selected file as its engine reads it. Closed by default: nothing is decoded until it is opened.56 @ViewBuilder private var previewArea: some View {57 let workspace = model.workspace58 if !group.isFolderGroup, let file = workspace.previewedFile(in: group) {59 @Bindable var bindable = workspace60 DisclosureGroup(isExpanded: $bindable.showInspectorPreview) {61 VStack(alignment: .leading, spacing: ORIX.Space.s) {62 Text(file.url.lastPathComponent).font(.caption).foregroundStyle(ORIX.Palette.textSecondary).lineLimit(1).truncationMode(.middle)63 FilePreviewView(preview: workspace.inspectorPreview, height: 220)64 }65 .padding(.top, ORIX.Space.s)66 } label: { SectionLabel(L("Preview")) }67 .onChange(of: workspace.showInspectorPreview, initial: true) { workspace.refreshInspectorPreview(for: group) }68 .onChange(of: workspace.selectedFileID) { workspace.refreshInspectorPreview(for: group) }69 .onChange(of: group.id) { workspace.refreshInspectorPreview(for: group) }70 }71 }7273 // MARK: Presets7475 private var matchingPresets: [ConversionPreset] {76 // Presets for this format, and the generic ones (archives) that apply to anything.77 model.presetList.filter { preset in78 if preset.source == .any { return group.operations.contains { $0.destination == preset.destinationFormat && $0.operation == preset.operation } || group.format != nil && !group.isFolderGroup && preset.operation == nil }79 guard let format = group.format else { return false }80 return preset.source.matches(format)81 }82 }8384 private var presets: some View {85 VStack(alignment: .leading, spacing: ORIX.Space.s) {86 SectionLabel(L("Presets"))87 HStack(spacing: ORIX.Space.s) {88 Menu {89 if matchingPresets.isEmpty { Text(L("No preset for this format yet")) }90 ForEach(matchingPresets) { preset in91 Button(preset.name + (preset.isBuiltIn ? "" : " · " + preset.destinationFormat.shortLabel)) { model.workspace.apply(preset, to: group) }92 }93 let mine = matchingPresets.filter { !$0.isBuiltIn }94 if !mine.isEmpty {95 Divider()96 Menu(L("Delete Preset")) {97 ForEach(mine) { preset in98 Button(preset.name, role: .destructive) {99 Task { try? await model.presets.delete(id: preset.id); await model.reloadPresets() }100 }101 }102 }103 }104 } label: { Label(L("Apply Preset"), systemImage: "bookmark") }105 .menuStyle(.button).buttonStyle(SecondaryButtonStyle()).fixedSize()106 Button(L("Save…")) { presetName = defaultPresetName; showSavePreset = true }107 .buttonStyle(SecondaryButtonStyle())108 .popover(isPresented: $showSavePreset, arrowEdge: .bottom) { savePopover }109 }110 }111 }112113 private var defaultPresetName: String {114 guard let choice = group.choice, let format = group.format else { return "" }115 return "\(format.shortLabel) → \(choice.title)"116 }117118 private var savePopover: some View {119 VStack(alignment: .leading, spacing: ORIX.Space.m) {120 Text(L("Save as Preset")).font(.headline)121 TextField(L("Preset name"), text: $presetName).textFieldStyle(.roundedBorder).frame(width: 240)122 if !group.options.secretKeys.isEmpty {123 Label(L("Passwords are not saved in presets."), systemImage: "lock").font(.caption).foregroundStyle(ORIX.Palette.textSecondary)124 }125 HStack {126 Spacer()127 Button(L("Cancel")) { showSavePreset = false }.keyboardShortcut(.cancelAction)128 Button(L("Save")) {129 let name = presetName.trimmingCharacters(in: .whitespaces)130 showSavePreset = false131 Task { await model.workspace.savePreset(named: name, from: group) }132 }133 .keyboardShortcut(.defaultAction)134 .disabled(presetName.trimmingCharacters(in: .whitespaces).isEmpty)135 }136 }137 .padding(ORIX.Space.l)138 }139140 // MARK: Input order141142 private var inputOrder: some View {143 VStack(alignment: .leading, spacing: ORIX.Space.s) {144 SectionLabel(L("Order of the inputs"))145 Text(L("Drag to reorder. The result follows this order.")).font(.caption).foregroundStyle(ORIX.Palette.textTertiary)146 List {147 ForEach(Array(group.files.enumerated()), id: \.element.id) { index, file in148 HStack(spacing: ORIX.Space.s) {149 Text(Fmt.count(index + 1)).font(.orixCaptionNumber).foregroundStyle(ORIX.Palette.textTertiary).frame(width: 22, alignment: .trailing)150 Text(file.url.lastPathComponent).font(.callout).lineLimit(1).truncationMode(.middle)151 Spacer(minLength: 0)152 Image(systemName: "line.3.horizontal").font(.caption).foregroundStyle(ORIX.Palette.textTertiary).accessibilityHidden(true)153 }154 .accessibilityElement(children: .combine)155 .accessibilityLabel(L("Input %lld: %@", index + 1, file.url.lastPathComponent))156 }157 .onMove { model.workspace.moveFiles(in: group, from: $0, to: $1) }158 }159 .listStyle(.plain)160 .scrollContentBackground(.hidden)161 .frame(height: min(220, CGFloat(group.files.count) * 28 + 8))162 .background(ORIX.Palette.surfaceSunken, in: RoundedRectangle(cornerRadius: ORIX.Radius.m, style: .continuous))163 .overlay(RoundedRectangle(cornerRadius: ORIX.Radius.m, style: .continuous).strokeBorder(ORIX.Palette.hairline, lineWidth: 1))164 }165 }166167 // MARK: Plan and warnings168169 @ViewBuilder private var planDetails: some View {170 if let choice = group.choice {171 VStack(alignment: .leading, spacing: ORIX.Space.s) {172 SectionLabel(L("How it is converted"))173 KeyValueRow(key: L("Engine"), value: choice.plan.engines.compactMap { model.registry.engine(id: $0).map { LE($0.displayName) } }.joined(separator: " → "))174 KeyValueRow(key: L("Fidelity"), value: choice.plan.fidelity.title)175 KeyValueRow(key: L("Status"), value: choice.plan.status == .supported ? L("Verified") : L("Experimental"))176 if !choice.plan.preserves.isEmpty {177 KeyValueRow(key: L("Preserves"), value: choice.plan.preserves.map(\.title).sorted().joined(separator: ", "))178 }179 }180 }181 }182183 @ViewBuilder private var warnings: some View {184 if !group.warnings.isEmpty {185 VStack(alignment: .leading, spacing: ORIX.Space.s) {186 SectionLabel(L("Before you convert"))187 ForEach(group.warnings) { warning in188 Banner(kind: warning.severity == .dataLoss ? .danger : (warning.severity == .caution ? .caution : .info), text: LE(warning.message),189 detail: warning.detail(total: group.files.count))190 }191 }192 }193 }194}195196extension Preserved {197 var title: String {198 switch self {199 case .metadata: L("metadata")200 case .transparency: L("transparency")201 case .animation: L("animation")202 case .colorProfile: L("colour profile")203 case .hdr: L("HDR")204 case .layers: L("layers")205 case .vectorGeometry: L("vector geometry")206 case .textStyles: L("text styles")207 case .tables: L("tables")208 case .images: L("images")209 case .links: L("links")210 case .formulas: L("formulas")211 case .multipleSheets: L("multiple sheets")212 case .chapters: L("chapters and outline")213 case .audioTracks: L("audio tracks")214 case .subtitleTracks: L("subtitle tracks")215 case .timing: L("timing")216 case .materials: L("materials")217 case .textures: L("textures")218 case .attributes: L("attributes")219 case .coordinateSystem: L("coordinate system")220 case .dimensions: L("dimensions")221 }222 }223}224