SPB Git

Repository Public License

ProgressViews.swift 4.2 KB 92 lines swift

1// ORIX Convert — App/Sources/DesignSystem/ProgressViews.swift2// Description: Progress indicators. Determinate ones show a measured fraction; the indeterminate style (a travelling3//              segment, or a static hatched track with Reduce Motion) is used whenever no fraction was measured.4//              Nothing here ever animates towards a made-up value.5// Author: Simon-Pierre Boucher — contact@spboucher.ai6import SwiftUI78/// Linear progress. `fraction == nil` means "working, amount unknown".9struct ORIXProgressBar: View {10    var fraction: Double?11    var height: CGFloat = 612    var tint: AnyShapeStyle = AnyShapeStyle(ORIX.Palette.transformation)13    var label: String = L("Progress")14    @Environment(\.accessibilityReduceMotion) private var reduceMotion1516    var body: some View {17        GeometryReader { geo in18            ZStack(alignment: .leading) {19                Capsule().fill(ORIX.Palette.hairline)20                if let fraction {21                    Capsule().fill(tint)22                        .frame(width: max(height, geo.size.width * min(1, max(0, fraction))))23                        .animation(reduceMotion ? nil : .easeOut(duration: 0.25), value: fraction)24                } else if reduceMotion {25                    Hatching().stroke(ORIX.Palette.accent.opacity(0.7), lineWidth: 1.5).clipShape(Capsule())26                } else {27                    TimelineView(.animation(minimumInterval: 1 / 30)) { context in28                        let phase = context.date.timeIntervalSinceReferenceDate.truncatingRemainder(dividingBy: 1.6) / 1.629                        let width = geo.size.width * 0.330                        Capsule().fill(ORIX.Palette.accent.opacity(0.75))31                            .frame(width: width)32                            .offset(x: (geo.size.width + width) * phase - width)33                    }34                    .clipShape(Capsule())35                }36            }37        }38        .frame(height: height)39        .accessibilityElement(children: .ignore)40        .accessibilityLabel(label)41        .accessibilityValue(fraction.map { Fmt.percent($0) } ?? L("In progress"))42    }43}4445private nonisolated struct Hatching: Shape {46    func path(in rect: CGRect) -> Path {47        var path = Path()48        var x = -rect.height49        while x < rect.width {50            path.move(to: CGPoint(x: x, y: rect.maxY))51            path.addLine(to: CGPoint(x: x + rect.height, y: rect.minY))52            x += 653        }54        return path55    }56}5758/// Circular progress. Determinate: an arc of the measured fraction. Indeterminate: a rotating short arc (static59/// dashed circle with Reduce Motion).60struct ORIXProgressRing: View {61    var fraction: Double?62    var size: CGFloat = 2263    var lineWidth: CGFloat = 364    var label: String = L("Progress")65    @Environment(\.accessibilityReduceMotion) private var reduceMotion6667    var body: some View {68        ZStack {69            Circle().stroke(ORIX.Palette.hairline, lineWidth: lineWidth)70            if let fraction {71                Circle().trim(from: 0, to: min(1, max(0.02, fraction)))72                    .stroke(AngularGradient(colors: ORIX.Palette.transformationStops, center: .center), style: StrokeStyle(lineWidth: lineWidth, lineCap: .round))73                    .rotationEffect(.degrees(-90))74                    .animation(reduceMotion ? nil : .easeOut(duration: 0.25), value: fraction)75            } else if reduceMotion {76                Circle().stroke(ORIX.Palette.accent, style: StrokeStyle(lineWidth: lineWidth, dash: [3, 3]))77            } else {78                TimelineView(.animation(minimumInterval: 1 / 30)) { context in79                    let turn = context.date.timeIntervalSinceReferenceDate.truncatingRemainder(dividingBy: 1.1) / 1.180                    Circle().trim(from: 0, to: 0.28)81                        .stroke(ORIX.Palette.accent, style: StrokeStyle(lineWidth: lineWidth, lineCap: .round))82                        .rotationEffect(.degrees(360 * turn))83                }84            }85        }86        .frame(width: size, height: size)87        .accessibilityElement(children: .ignore)88        .accessibilityLabel(label)89        .accessibilityValue(fraction.map { Fmt.percent($0) } ?? L("In progress"))90    }91}92
Files main
Menu

Theme