Repository Public License
1// ORIX Convert — App/Sources/Features/SmartDrop/OutputBar.swift2// Description: Bottom bar of the Prepare pane: where the results go, the naming template with a live preview, the3// conflict policy, a truthful batch summary and the Convert button.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import AppKit6import ConvertCore7import SwiftUI89struct OutputBar: View {10 @Environment(AppModel.self) private var model11 /// Two rows instead of one (narrow windows).12 var narrow = false13 @State private var showNaming = false1415 var body: some View {16 let workspace = model.workspace17 VStack(spacing: 0) {18 ORIX.Palette.hairline.frame(height: 1)19 Group {20 if narrow {21 VStack(alignment: .leading, spacing: ORIX.Space.s) {22 HStack(spacing: ORIX.Space.s) { controls; Spacer(minLength: 0) }23 HStack(spacing: ORIX.Space.m) { summary; Spacer(minLength: 0); convertButton }24 }25 } else {26 HStack(spacing: ORIX.Space.s) { controls; Spacer(minLength: ORIX.Space.s); summary; convertButton }27 }28 }29 .padding(.horizontal, ORIX.Space.xl).padding(.vertical, ORIX.Space.m)30 }31 .background(ORIX.Palette.surface)32 .onChange(of: workspace.output) { _, new in33 // The choices of the bar become the defaults of the next batch.34 model.settings.stored.outputMode = new.mode35 model.settings.stored.nameTemplate = new.nameTemplate36 model.settings.stored.conflictPolicy = new.conflictPolicy37 if let folder = new.folder { model.settings.outputFolder = folder }38 }39 }4041 // MARK: Controls4243 @ViewBuilder private var controls: some View {44 @Bindable var workspace = model.workspace45 Menu {46 Button { workspace.output.mode = .sameFolder } label: { check(L("Same folder as the source"), workspace.output.mode == .sameFolder) }47 Button { chooseFolder() } label: { check(L("Choose Folder…"), workspace.output.mode == .chosenFolder) }48 Button { workspace.output.mode = .askEachTime } label: { check(L("Ask each time"), workspace.output.mode == .askEachTime) }49 if workspace.output.mode == .chosenFolder, let folder = workspace.output.folder {50 Divider()51 Button(L("Reveal “%@” in Finder", folder.lastPathComponent)) { FileActions.reveal([folder]) }52 }53 } label: {54 barLabel(caption: L("Save to"), value: locationTitle, symbol: "folder")55 }56 .menuStyle(.button).buttonStyle(.plain).fixedSize()57 .accessibilityLabel(L("Output location"))58 .accessibilityValue(locationTitle)5960 Button { showNaming = true } label: {61 barLabel(caption: L("Name"), value: workspace.namePreview(for: workspace.selectedGroup ?? workspace.groups.first(where: \.willRun)), symbol: "character.cursor.ibeam")62 }63 .buttonStyle(.plain)64 .popover(isPresented: $showNaming, arrowEdge: .top) { NamingEditor() }65 .accessibilityLabel(L("Output name"))66 .accessibilityHint(L("Opens the naming template editor"))6768 Menu {69 ForEach([ConflictPolicy.rename, .askUser, .skip, .replace], id: \.self) { policy in70 Button { workspace.output.conflictPolicy = policy; if policy != .replace { workspace.output.allowReplacingSource = false } } label: {71 check(policy.title + " — " + policy.explanation, workspace.output.conflictPolicy == policy)72 }73 }74 if workspace.output.conflictPolicy == .replace {75 Divider()76 Toggle(L("Also allow replacing the original files"), isOn: $workspace.output.allowReplacingSource)77 }78 } label: {79 barLabel(caption: L("If it exists"), value: workspace.output.conflictPolicy.title + (workspace.output.allowReplacingSource ? " + " + L("originals") : ""),80 symbol: workspace.output.conflictPolicy == .replace ? "exclamationmark.triangle" : "doc.on.doc",81 tint: workspace.output.conflictPolicy == .replace ? ORIX.Palette.caution : nil)82 }83 .menuStyle(.button).buttonStyle(.plain).fixedSize()84 .accessibilityLabel(L("If the file already exists"))85 .accessibilityValue(workspace.output.conflictPolicy.title)86 }8788 private func check(_ title: String, _ on: Bool) -> some View {89 HStack { Text(title); if on { Spacer(); Image(systemName: "checkmark") } }90 }9192 private var locationTitle: String {93 let output = model.workspace.output94 switch output.mode {95 case .sameFolder: return L("Same folder as the source")96 case .askEachTime: return L("Ask each time")97 case .chosenFolder: return output.folder?.lastPathComponent ?? L("Choose Folder…")98 }99 }100101 private func barLabel(caption: String, value: String, symbol: String, tint: Color? = nil) -> some View {102 HStack(spacing: 8) {103 Image(systemName: symbol).font(.callout).foregroundStyle(tint ?? ORIX.Palette.textSecondary).frame(width: 18)104 VStack(alignment: .leading, spacing: 0) {105 Text(caption).font(.caption2).foregroundStyle(ORIX.Palette.textTertiary)106 Text(value).font(.callout.weight(.medium)).foregroundStyle(tint ?? ORIX.Palette.textPrimary).lineLimit(1).truncationMode(.middle)107 }108 Image(systemName: "chevron.up.chevron.down").font(.system(size: 8, weight: .bold)).foregroundStyle(ORIX.Palette.textTertiary)109 }110 .padding(.horizontal, 10).padding(.vertical, 6)111 .frame(maxWidth: 230, alignment: .leading)112 .background(ORIX.Palette.canvas, in: RoundedRectangle(cornerRadius: ORIX.Radius.m, style: .continuous))113 .overlay(RoundedRectangle(cornerRadius: ORIX.Radius.m, style: .continuous).strokeBorder(ORIX.Palette.hairline, lineWidth: 1))114 .contentShape(Rectangle())115 }116117 private func chooseFolder() {118 let panel = NSOpenPanel()119 panel.canChooseFiles = false120 panel.canChooseDirectories = true121 panel.canCreateDirectories = true122 panel.prompt = L("Choose")123 if panel.runModal() == .OK, let url = panel.url {124 model.workspace.output.folder = url125 model.workspace.output.mode = .chosenFolder126 }127 }128129 // MARK: Summary and action130131 private var summary: some View {132 let workspace = model.workspace133 let jobs = workspace.jobCount, loss = workspace.dataLossFileCount134 return VStack(alignment: .trailing, spacing: 1) {135 Text(jobs == 0 ? L("Nothing to convert") : (jobs == 1 ? L("1 conversion") : L("%lld conversions", jobs)))136 .font(.callout.weight(.medium).monospacedDigit()).foregroundStyle(ORIX.Palette.textPrimary)137 if loss > 0 {138 Label(loss == 1 ? L("1 file will lose data") : L("%lld files will lose data", loss), systemImage: "xmark.octagon.fill")139 .font(.caption).foregroundStyle(ORIX.Palette.danger)140 }141 }142 .lineLimit(1)143 .accessibilityElement(children: .combine)144 }145146 private var convertButton: some View {147 Button { Task { await model.startConversion() } } label: {148 HStack(spacing: 8) {149 Text(L("Convert"))150 Text("⌘↩").font(.caption.weight(.medium)).opacity(0.8).accessibilityHidden(true)151 }152 }153 .buttonStyle(PrimaryButtonStyle())154 .disabled(model.workspace.jobCount == 0)155 .accessibilityLabel(L("Convert"))156 .accessibilityHint(L("Sends the staged files to the queue"))157 }158}159160/// Naming template editor with token buttons and a live preview on the real first file.161struct NamingEditor: View {162 @Environment(AppModel.self) private var model163164 private static let tokens: [(String, String)] = [("{name}", "Original name"), ("{ext}", "Original extension"), ("{date}", "Date"),165 ("{counter:3}", "Counter"), ("{width}x{height}", "Dimensions")]166167 var body: some View {168 @Bindable var workspace = model.workspace169 VStack(alignment: .leading, spacing: ORIX.Space.m) {170 Text(L("Output Name")).font(.headline)171 TextField(L("Template"), text: $workspace.output.nameTemplate).textFieldStyle(.roundedBorder).font(.body.monospaced())172 .accessibilityLabel(L("Naming template"))173 FlowLayout(spacing: 6, lineSpacing: 6) {174 ForEach(Self.tokens, id: \.0) { token, title in175 Button { workspace.output.nameTemplate += (workspace.output.nameTemplate.isEmpty ? "" : "-") + token } label: {176 HStack(spacing: 4) {177 Image(systemName: "plus").font(.system(size: 8, weight: .bold))178 Text(tokenTitle(title)).font(.caption)179 }180 .foregroundStyle(ORIX.Palette.accent)181 .padding(.horizontal, 8).padding(.vertical, 4)182 .background(ORIX.Palette.accentSoft, in: Capsule())183 }184 .buttonStyle(.plain)185 .help(token)186 .accessibilityLabel(L("Insert %@", tokenTitle(title)))187 }188 }189 VStack(alignment: .leading, spacing: 3) {190 SectionLabel(L("Preview"))191 Text(workspace.namePreview(for: workspace.selectedGroup ?? workspace.groups.first(where: \.willRun)))192 .font(.callout.monospaced()).foregroundStyle(ORIX.Palette.textPrimary).textSelection(.enabled).lineLimit(2)193 }194 Text(L("Dimensions are left out when they are not known. Characters that are not allowed in file names are replaced."))195 .font(.caption).foregroundStyle(ORIX.Palette.textTertiary).fixedSize(horizontal: false, vertical: true)196 HStack {197 Button(L("Reset")) { workspace.output.nameTemplate = OutputNameTemplate.default }.buttonStyle(SecondaryButtonStyle())198 Spacer()199 }200 }201 .padding(ORIX.Space.l)202 .frame(width: 360)203 }204205 private func tokenTitle(_ english: String) -> String {206 switch english {207 case "Original name": L("Original name")208 case "Original extension": L("Original extension")209 case "Date": L("Date")210 case "Counter": L("Counter")211 default: L("Dimensions")212 }213 }214}215