Repository Public License
1// ORIX Convert — App/Sources/DesignSystem/FormatBadge.swift2// Description: Format badges drawn in code: a rounded tile with the format's short label, its category colour and3// an SF Symbol. Colour is never the only signal (label + symbol + accessibility text).4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import ConvertCore6import SwiftUI78struct FormatBadge: View {9 enum Size { case small, regular, large10 var side: CGFloat { switch self { case .small: 28; case .regular: 40; case .large: 56 } }11 var labelSize: CGFloat { switch self { case .small: 8; case .regular: 10.5; case .large: 14 } }12 var symbolSize: CGFloat { switch self { case .small: 9; case .regular: 12; case .large: 17 } }13 var radius: CGFloat { switch self { case .small: 7; case .regular: 10; case .large: 14 } }14 }1516 /// `nil` draws the "unknown format" badge.17 var format: FormatID?18 var size: Size = .regular19 var selected = false2021 private var category: FormatCategory { format?.category ?? .other }22 private var tint: Color { format == nil ? ORIX.Palette.textTertiary : category.color }23 private var label: String { format?.shortLabel ?? "?" }24 private var symbol: String { format == nil ? "questionmark" : (format == .folder ? "folder" : category.symbol) }2526 var body: some View {27 let shape = RoundedRectangle(cornerRadius: size.radius, style: .continuous)28 VStack(spacing: size == .small ? 1 : 2) {29 Image(systemName: symbol)30 .font(.system(size: size.symbolSize, weight: .semibold))31 if !label.isEmpty {32 Text(label)33 .font(.system(size: size.labelSize, weight: .heavy, design: .rounded))34 .minimumScaleFactor(0.6)35 .lineLimit(1)36 }37 }38 .foregroundStyle(tint)39 .padding(.horizontal, 2)40 .frame(width: size.side, height: size.side)41 .background(tint.opacity(0.13), in: shape)42 .overlay(shape.strokeBorder(selected ? AnyShapeStyle(ORIX.Palette.transformationDiagonal) : AnyShapeStyle(tint.opacity(0.35)), lineWidth: selected ? 2 : 1))43 .accessibilityElement(children: .ignore)44 .accessibilityLabel(format.map { L("%@ format, %@", $0.displayName, category.title) } ?? L("Unknown format"))45 }46}4748/// "PDF → PNG" pair used in rows and chips.49struct ConversionPairBadge: View {50 var source: FormatID?51 var destination: FormatID52 var size: FormatBadge.Size = .small53 var body: some View {54 HStack(spacing: 5) {55 FormatBadge(format: source, size: size)56 Image(systemName: "arrow.right").font(.system(size: 9, weight: .bold)).foregroundStyle(ORIX.Palette.textTertiary).accessibilityHidden(true)57 FormatBadge(format: destination, size: size)58 }59 .accessibilityElement(children: .ignore)60 .accessibilityLabel(L("%@ to %@", source?.displayName ?? L("Unknown format"), destination.displayName))61 }62}6364extension Fidelity {65 var title: String {66 switch self {67 case .lossless: L("Lossless")68 case .lossy: L("Lossy")69 case .structural: L("Structural")70 case .partial: L("Partial")71 }72 }7374 var explanation: String {75 switch self {76 case .lossless: L("The content is kept exactly.")77 case .lossy: L("Some quality is lost by the destination format.")78 case .structural: L("The content is kept but its layout or structure is rebuilt.")79 case .partial: L("Only part of the source is carried over.")80 }81 }8283 var symbol: String {84 switch self {85 case .lossless: "checkmark.seal"86 case .lossy: "circle.lefthalf.filled"87 case .structural: "square.stack.3d.up"88 case .partial: "circle.dashed"89 }90 }9192 var color: Color {93 switch self {94 case .lossless: ORIX.Palette.success95 case .lossy: ORIX.Palette.caution96 case .structural: ORIX.Palette.info97 case .partial: ORIX.Palette.caution98 }99 }100}101102struct FidelityBadge: View {103 var fidelity: Fidelity104 var body: some View {105 Pill(text: fidelity.title, symbol: fidelity.symbol, color: fidelity.color)106 .help(fidelity.explanation)107 .accessibilityLabel(L("Fidelity: %@. %@", fidelity.title, fidelity.explanation))108 }109}110111struct ExperimentalBadge: View {112 var body: some View {113 Pill(text: L("Experimental"), symbol: "flask", color: ORIX.Palette.caution)114 .help(L("Tested on a narrow set of files; results can vary."))115 }116}117118/// Small capsule with a symbol and a word.119struct Pill: View {120 var text: String121 var symbol: String?122 var color: Color = ORIX.Palette.textSecondary123 var body: some View {124 HStack(spacing: 4) {125 if let symbol { Image(systemName: symbol).font(.system(size: 9, weight: .bold)).accessibilityHidden(true) }126 Text(text).font(.caption2.weight(.semibold)).lineLimit(1)127 }128 .foregroundStyle(color)129 .padding(.horizontal, 7).padding(.vertical, 3)130 .background(color.opacity(0.12), in: Capsule())131 .fixedSize()132 }133}134