SPB Git

Repository Public License

JobRow.swift 8.8 KB 165 lines swift

1// ORIX Convert — App/Sources/Features/Queue/JobRow.swift2// Description: One job in the queue: state, measured progress (indeterminate when the engine reports no fraction),3//              ETA only when the core gives one, sizes, and the actions that apply to the current state.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import ConvertCore6import SwiftUI7import UniformTypeIdentifiers89struct JobRow: View {10    @Environment(AppModel.self) private var model11    let item: JobItem12    @State private var hovering = false1314    var body: some View {15        let job = item.job16        HStack(spacing: ORIX.Space.m) {17            stateIcon(job)18            VStack(alignment: .leading, spacing: 3) {19                HStack(spacing: 6) {20                    Text(job.displayName).font(.callout.weight(.medium)).foregroundStyle(ORIX.Palette.textPrimary).lineLimit(1).truncationMode(.middle)21                    if job.priority != .normal { Pill(text: job.priority.title, symbol: job.priority == .high ? "arrow.up" : "arrow.down", color: ORIX.Palette.textSecondary) }22                    if job.isHeavy { Image(systemName: "scalemass").font(.caption2).foregroundStyle(ORIX.Palette.textTertiary).help(L("Heavy conversion: counted against the heavy-job limit")).accessibilityLabel(L("Heavy conversion")) }23                }24                if job.state.isActive {25                    ORIXProgressBar(fraction: job.state == .running ? job.progress.fraction : nil, height: 5, label: L("Progress of %@", job.displayName))26                        .frame(maxWidth: 420)27                }28                Text(statusLine(job)).font(.caption).foregroundStyle(job.state == .failed ? ORIX.Palette.danger : ORIX.Palette.textSecondary)29                    .lineLimit(job.state == .failed ? 2 : 1)30            }31            .layoutPriority(1)32            Spacer(minLength: ORIX.Space.s)33            trailing(job)34        }35        .padding(.vertical, 7)36        .contentShape(Rectangle())37        .onHover { hovering = $0 }38        .accessibilityElement(children: .combine)39        .accessibilityLabel(job.displayName)40        .accessibilityValue(accessibilityValue(job))41    }4243    /// State first (ring while working, symbol otherwise), then the destination badge.44    @ViewBuilder private func stateIcon(_ job: ConversionJob) -> some View {45        HStack(spacing: ORIX.Space.s) {46            Group {47                if job.state.isActive {48                    ORIXProgressRing(fraction: job.state == .running ? job.progress.fraction : nil, size: 16, lineWidth: 2.5, label: job.state.title)49                } else {50                    Image(systemName: job.state.symbol).font(.system(size: 14, weight: .semibold)).foregroundStyle(job.state.color)51                }52            }53            .frame(width: 18)54            FormatBadge(format: job.request.destinationFormat, size: .small)55        }56        .accessibilityHidden(true)57    }5859    private func statusLine(_ job: ConversionJob) -> String {60        let pair = "\(job.sourceFormat?.shortLabel ?? "…") → \(job.plan?.steps.last?.title.map { LE($0) } ?? job.request.destinationFormat.shortLabel)"61        switch job.state {62        case .failed:63            return job.error.map { LE($0) } ?? L("Failed")64        case .running:65            var parts = [pair, job.state.title]66            if let fraction = job.progress.fraction { parts.append(Fmt.percent(fraction)) }67            if let message = job.progress.message { parts.append(LE(message)) }68            if job.cancelRequested { parts = [pair, L("Cancelling…")] }69            return parts.joined(separator: " · ")70        case .finished:71            var parts = [pair]72            let outputs = job.writtenOutputs73            if let first = outputs.first { parts.append(outputs.count == 1 ? first.lastPathComponent : L("%lld files", outputs.count)) }74            if !job.warnings.isEmpty { parts.append(job.warnings.count == 1 ? L("1 warning") : L("%lld warnings", job.warnings.count)) }75            return parts.joined(separator: " · ")76        case .awaitingDecision:77            return pair + " · " + L("“%@” already exists", job.conflict?.existing.lastPathComponent ?? "")78        default:79            return pair + " · " + job.state.title80        }81    }8283    @ViewBuilder private func trailing(_ job: ConversionJob) -> some View {84        HStack(spacing: ORIX.Space.s) {85            switch job.state {86            case .running:87                VStack(alignment: .trailing, spacing: 1) {88                    if let eta = job.estimatedSecondsRemaining { Text(L("%@ left", Fmt.duration(eta))).font(.orixCaptionNumber).foregroundStyle(ORIX.Palette.textSecondary) }89                    TimelineView(.periodic(from: .now, by: 1)) { context in90                        if let rate = job.throughput(now: context.date) {91                            Text(L("%@/s", Fmt.bytes(Int64(rate)))).font(.orixCaptionNumber).foregroundStyle(ORIX.Palette.textTertiary)92                        }93                    }94                }95            case .finished:96                VStack(alignment: .trailing, spacing: 1) {97                    if let before = job.bytesBefore, let after = job.bytesAfter {98                        Text("\(Fmt.bytes(before)) → \(Fmt.bytes(after))").font(.orixCaptionNumber).foregroundStyle(ORIX.Palette.textSecondary)99                    }100                    if let duration = job.duration { Text(Fmt.duration(duration)).font(.orixCaptionNumber).foregroundStyle(ORIX.Palette.textTertiary) }101                }102                if let output = job.writtenOutputs.first { DragOutHandle(urls: job.writtenOutputs, label: output.lastPathComponent) }103            case .awaitingDecision:104                Button(L("Keep Both")) { model.queue.resolve(job.id, policy: .rename) }.buttonStyle(SecondaryButtonStyle(prominent: true))105                Button(L("Skip")) { model.queue.resolve(job.id, policy: .skip) }.buttonStyle(SecondaryButtonStyle())106                Button(L("Replace…")) { ConflictActions.confirmReplace([job], queue: model.queue) }.buttonStyle(SecondaryButtonStyle())107            case .failed:108                Button(L("Copy Details")) { FileActions.copy(job.detailsText) }.buttonStyle(SecondaryButtonStyle())109            default:110                EmptyView()111            }112            actions(job)113        }114        .lineLimit(1)115    }116117    /// Only truthful controls: a running job has Cancel, never Pause.118    @ViewBuilder private func actions(_ job: ConversionJob) -> some View {119        switch job.state {120        case .queued:121            IconButton(symbol: "pause", label: L("Pause before it starts")) { model.queue.pauseJobs([job.id]) }122            IconButton(symbol: "xmark", label: L("Cancel")) { model.queue.cancel([job.id]) }123        case .paused:124            IconButton(symbol: "play", label: L("Resume")) { model.queue.resumeJobs([job.id]) }125            IconButton(symbol: "xmark", label: L("Cancel")) { model.queue.cancel([job.id]) }126        case .preparing, .running, .validating:127            IconButton(symbol: "xmark", label: L("Cancel")) { model.queue.cancel([job.id]) }128        case .failed, .cancelled:129            IconButton(symbol: "arrow.clockwise", label: L("Retry")) { model.queue.retry([job.id]) }130            IconButton(symbol: "doc.text.magnifyingglass", label: L("Show Log")) { model.queue.logJobID = job.id }131        case .finished:132            IconButton(symbol: "magnifyingglass", label: L("Reveal in Finder")) { FileActions.reveal(job.writtenOutputs) }133        case .skipped, .awaitingDecision:134            EmptyView()135        }136    }137138    private func accessibilityValue(_ job: ConversionJob) -> String {139        var parts = [L("%@ to %@", job.sourceFormat?.displayName ?? L("Unknown format"), job.targetTitle), job.state.title]140        if job.state == .running { parts.append(job.progress.fraction.map { Fmt.percent($0) } ?? L("In progress")) }141        if let error = job.error { parts.append(LE(error)) }142        if !job.warnings.isEmpty { parts.append(L("%lld warnings", job.warnings.count)) }143        return parts.joined(separator: ", ")144    }145}146147/// A finished output that can be dragged to the Finder or another app.148struct DragOutHandle: View {149    let urls: [URL]150    let label: String151    var body: some View {152        Image(systemName: urls.count > 1 ? "doc.on.doc.fill" : "doc.fill")153            .font(.system(size: 13)).foregroundStyle(ORIX.Palette.accent)154            .frame(width: 26, height: 26)155            .background(ORIX.Palette.accentSoft, in: RoundedRectangle(cornerRadius: ORIX.Radius.s, style: .continuous))156            .onDrag {157                guard let first = urls.first(where: FileActions.exists) else { return NSItemProvider() }158                return NSItemProvider(contentsOf: first) ?? NSItemProvider()159            }160            .help(L("Drag “%@” to the Finder or another app", label))161            .accessibilityLabel(L("Result file %@", label))162            .accessibilityHint(L("Drag to the Finder or another app"))163    }164}165
Files main
Menu

Theme