SPB Git

Repository Public License

ClockDialPainter.swift 14.0 KB 244 lines swift

1// ORIX Alarm2// File: App/Features/Clock/ClockDialPainter.swift3// Description: Canvas drawing of the analog, minimal, chronograph and hybrid clock dials.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import SwiftUI89/// Draws clock dials into a `GraphicsContext`. Every length is proportional to the dial radius so a face10/// reads the same at 120 pt and at 300 pt; small details are dropped below `isCompact`.11struct ClockDialPainter {12    let ctx: GraphicsContext13    let center: CGPoint14    /// Radius of the available square.15    let r: CGFloat1617    /// One design unit: 1 pt on a 260 pt dial.18    private var u: CGFloat { r / 130 }19    private var isCompact: Bool { r < 90 }2021    // MARK: Primitives22    /// Point at `fraction` of a turn (0 = top, clockwise) and `radius` from `origin` (dial centre by default).23    private func point(_ fraction: Double, _ radius: CGFloat, around origin: CGPoint? = nil) -> CGPoint {24        let angle = fraction * 2 * .pi - .pi / 225        let o = origin ?? center26        return CGPoint(x: o.x + cos(angle) * radius, y: o.y + sin(angle) * radius)27    }2829    private func disc(_ c: CGPoint, _ radius: CGFloat) -> Path {30        Path(ellipseIn: CGRect(x: c.x - radius, y: c.y - radius, width: radius * 2, height: radius * 2))31    }3233    private func line(_ fraction: Double, from inner: CGFloat, to outer: CGFloat, width: CGFloat, color: Color, around origin: CGPoint? = nil, shadow: Bool = false) {34        var path = Path()35        path.move(to: point(fraction, inner, around: origin))36        path.addLine(to: point(fraction, outer, around: origin))37        var c = ctx38        if shadow { c.addFilter(.shadow(color: .black.opacity(0.5), radius: 3 * u, x: 0, y: 2 * u)) }39        c.stroke(path, with: .color(color), style: StrokeStyle(lineWidth: width, lineCap: .round))40    }4142    private func label(_ text: String, at p: CGPoint, size: CGFloat, weight: Font.Weight = .semibold, color: Color, kerning: CGFloat = 0) {43        ctx.draw(Text(text).font(.system(size: size, weight: weight, design: .rounded).monospacedDigit()).kerning(kerning).foregroundStyle(color), at: p)44    }4546    private func symbol(_ name: String, at p: CGPoint, size: CGFloat, color: Color) {47        ctx.draw(Text(Image(systemName: name)).font(.system(size: size, weight: .semibold)).foregroundStyle(color), at: p)48    }4950    /// Glass-like dial plate with a lit rim.51    private func plate(radius: CGFloat) {52        let rect = CGRect(x: center.x - radius, y: center.y - radius, width: radius * 2, height: radius * 2)53        ctx.fill(Path(ellipseIn: rect), with: .radialGradient(54            Gradient(colors: [Theme.indigo.opacity(0.55), Theme.deepIndigo.opacity(0.80), Theme.midnight.opacity(0.88)]),55            center: CGPoint(x: center.x, y: center.y - radius * 0.35), startRadius: 0, endRadius: radius * 1.35))56        ctx.stroke(Path(ellipseIn: rect.insetBy(dx: 0.5, dy: 0.5)), with: .linearGradient(57            Gradient(colors: [.white.opacity(0.42), .white.opacity(0.05), Theme.sunrise.opacity(0.35)]),58            startPoint: CGPoint(x: rect.minX, y: rect.minY), endPoint: CGPoint(x: rect.maxX, y: rect.maxY)), lineWidth: max(1, 1.2 * u))59    }6061    /// Next-alarm marker: a gold bead with a dark outline on the rim (shape + colour, not colour alone).62    private func alarmBead(_ fraction: Double?, at radius: CGFloat) {63        guard let fraction else { return }64        let p = point(fraction, radius)65        let size = max(4.6 * u, 3.4)66        var glow = ctx67        glow.addFilter(.shadow(color: Theme.gold.opacity(0.9), radius: 5 * u))68        glow.fill(disc(p, size), with: .color(Theme.gold))69        ctx.stroke(disc(p, size), with: .color(Theme.midnight), lineWidth: max(1.4 * u, 1))70        ctx.fill(disc(p, size * 0.34), with: .color(Theme.midnight))71    }7273    private func hub(secondColor: Color) {74        ctx.fill(disc(center, 6 * u), with: .color(.white))75        ctx.fill(disc(center, 3.6 * u), with: .color(secondColor))76        ctx.fill(disc(center, 1.3 * u), with: .color(Theme.midnight))77    }7879    // MARK: Analog80    func analog(_ m: ClockMoment, alarm: Double?) {81        let face = r * 0.96582        plate(radius: face)83        for i in 0..<60 {84            let f = Double(i) / 6085            if i % 5 == 0 {86                let quarter = i % 15 == 087                line(f, from: face * (quarter ? 0.80 : 0.84), to: face * 0.93, width: (quarter ? 3.2 : 2.2) * u,88                     color: quarter ? Theme.gold : .white.opacity(0.9))89            } else {90                line(f, from: face * 0.895, to: face * 0.93, width: max(0.9 * u, 0.6), color: .white.opacity(0.34))91            }92        }93        if !isCompact {94            for (text, f) in [("12", 0.0), ("3", 0.25), ("6", 0.5), ("9", 0.75)] {95                label(text, at: point(f, face * 0.655), size: 21 * u, weight: .medium, color: Theme.textSecondary)96            }97        }98        alarmBead(alarm, at: face)99        line(m.hourFraction, from: -face * 0.07, to: face * 0.50, width: 7.5 * u, color: .white, shadow: true)100        line(m.minuteFraction, from: -face * 0.07, to: face * 0.77, width: 4.6 * u, color: .white, shadow: true)101        line(m.secondFraction, from: -face * 0.20, to: face * 0.87, width: max(1.7 * u, 1.1), color: Theme.sunrise, shadow: true)102        ctx.fill(disc(point(m.secondFraction, -face * 0.20), 3.2 * u), with: .color(Theme.sunrise))103        hub(secondColor: Theme.sunrise)104    }105106    // MARK: Minimal107    func minimal(_ m: ClockMoment, alarm: Double?) {108        let face = r * 0.94109        ctx.stroke(disc(center, face), with: .color(.white.opacity(0.10)), lineWidth: max(1, u))110        for i in 0..<12 where i % 3 == 0 {111            ctx.fill(disc(point(Double(i) / 12, face * 0.88), max(1.8 * u, 1.3)), with: .color(.white.opacity(i == 0 ? 0.9 : 0.45)))112        }113        if let alarm {114            ctx.stroke(disc(point(alarm, face), max(5 * u, 3.6)), with: .color(Theme.gold), lineWidth: max(1.8 * u, 1.3))115        }116        line(m.hourFraction, from: 0, to: face * 0.46, width: max(3.4 * u, 2.2), color: .white, shadow: true)117        line(m.minuteFraction, from: 0, to: face * 0.76, width: max(2.2 * u, 1.5), color: .white.opacity(0.92), shadow: true)118        // The second is a small ember bead travelling on the rim.119        var glow = ctx120        glow.addFilter(.shadow(color: Theme.sunrise.opacity(0.9), radius: 6 * u))121        glow.fill(disc(point(m.secondFraction, face), max(3.6 * u, 2.6)), with: .color(Theme.sunrise))122        ctx.fill(disc(center, max(3 * u, 2.2)), with: .color(.white))123    }124125    // MARK: Chronograph126    func chronograph(_ m: ClockMoment, alarm: Double?) {127        let face = r * 0.965128        plate(radius: face)129        // Bezel with a minute track.130        ctx.stroke(disc(center, face * 0.925), with: .color(.black.opacity(0.28)), lineWidth: face * 0.13)131        ctx.stroke(disc(center, face * 0.855), with: .color(.white.opacity(0.14)), lineWidth: max(0.8 * u, 0.5))132        for i in 0..<60 {133            let f = Double(i) / 60134            if i % 5 == 0 {135                line(f, from: face * 0.875, to: face * 0.975, width: 2.2 * u, color: i == 0 ? Theme.sunrise : .white.opacity(0.92))136            } else {137                line(f, from: face * 0.93, to: face * 0.975, width: max(0.9 * u, 0.6), color: .white.opacity(0.36))138            }139        }140        // Hour batons.141        for i in 0..<12 where i % 3 != 0 || isCompact {142            line(Double(i) / 12, from: face * 0.72, to: face * 0.81, width: 3 * u, color: .white.opacity(0.85))143        }144        if !isCompact {145            label("12", at: point(0, face * 0.72), size: 17 * u, weight: .bold, color: .white.opacity(0.92))146            label("ORIX", at: CGPoint(x: center.x, y: center.y - face * 0.42), size: 9.5 * u, weight: .heavy, color: Theme.textSecondary, kerning: 2.4 * u)147            // Date window at 6 o'clock.148            let box = CGRect(x: center.x - 15 * u, y: center.y + face * 0.47, width: 30 * u, height: 21 * u)149            ctx.fill(Path(roundedRect: box, cornerRadius: 5 * u), with: .color(.black.opacity(0.42)))150            ctx.stroke(Path(roundedRect: box, cornerRadius: 5 * u), with: .color(.white.opacity(0.22)), lineWidth: max(0.8 * u, 0.5))151            label("\(m.day)", at: CGPoint(x: box.midX, y: box.midY), size: 13.5 * u, weight: .bold, color: Theme.gold)152        }153154        // Sub-dial at 9 o'clock: running seconds.155        let subRadius = face * 0.245156        let left = CGPoint(x: center.x - face * 0.40, y: center.y)157        subDial(at: left, radius: subRadius)158        for i in 0..<12 {159            line(Double(i) / 12, from: subRadius * (i % 3 == 0 ? 0.70 : 0.80), to: subRadius * 0.92, width: max((i % 3 == 0 ? 1.6 : 1.0) * u, 0.6),160                 color: .white.opacity(i % 3 == 0 ? 0.9 : 0.45), around: left)161        }162        if !isCompact {163            label("60", at: point(0, subRadius * 0.48, around: left), size: 8 * u, color: Theme.textSecondary)164            label("30", at: point(0.5, subRadius * 0.48, around: left), size: 8 * u, color: Theme.textSecondary)165        }166        line(m.secondFraction, from: -subRadius * 0.22, to: subRadius * 0.86, width: max(1.5 * u, 1), color: Theme.sunrise, around: left)167        ctx.fill(disc(left, max(2.4 * u, 1.6)), with: .color(Theme.sunrise))168169        // Sub-dial at 3 o'clock: 24 h hand, day on the upper half (noon at the top), night on the lower half.170        let right = CGPoint(x: center.x + face * 0.40, y: center.y)171        subDial(at: right, radius: subRadius)172        var dayHalf = Path()173        dayHalf.move(to: right)174        dayHalf.addArc(center: right, radius: subRadius * 0.93, startAngle: .degrees(180), endAngle: .degrees(360), clockwise: false)175        dayHalf.closeSubpath()176        ctx.fill(dayHalf, with: .linearGradient(Gradient(colors: [Theme.gold.opacity(0.42), Theme.sunrise.opacity(0.20)]),177                                                 startPoint: CGPoint(x: right.x, y: right.y - subRadius), endPoint: right))178        var nightHalf = Path()179        nightHalf.move(to: right)180        nightHalf.addArc(center: right, radius: subRadius * 0.93, startAngle: .degrees(0), endAngle: .degrees(180), clockwise: false)181        nightHalf.closeSubpath()182        ctx.fill(nightHalf, with: .linearGradient(Gradient(colors: [Theme.violet.opacity(0.22), Theme.indigo.opacity(0.55)]),183                                                   startPoint: right, endPoint: CGPoint(x: right.x, y: right.y + subRadius)))184        for i in 0..<8 {185            line(Double(i) / 8, from: subRadius * 0.82, to: subRadius * 0.93, width: max(1.0 * u, 0.6), color: .white.opacity(0.5), around: right)186        }187        if !isCompact {188            symbol("sun.max.fill", at: point(0, subRadius * 0.52, around: right), size: 9 * u, color: Theme.gold)189            symbol("moon.fill", at: point(0.5, subRadius * 0.52, around: right), size: 8 * u, color: Theme.azure)190        }191        line(m.dayFraction + 0.5, from: -subRadius * 0.18, to: subRadius * 0.84, width: max(1.7 * u, 1.1), color: .white, around: right)192        ctx.fill(disc(right, max(2.4 * u, 1.6)), with: .color(.white))193194        alarmBead(alarm, at: face)195        // Main hands: white batons with a dark inlay.196        line(m.hourFraction, from: -face * 0.06, to: face * 0.48, width: 8 * u, color: .white, shadow: true)197        line(m.hourFraction, from: face * 0.12, to: face * 0.42, width: 2.4 * u, color: Theme.deepIndigo)198        line(m.minuteFraction, from: -face * 0.06, to: face * 0.74, width: 5.4 * u, color: .white, shadow: true)199        line(m.minuteFraction, from: face * 0.12, to: face * 0.68, width: 1.6 * u, color: Theme.deepIndigo)200        hub(secondColor: Theme.gold)201    }202203    private func subDial(at c: CGPoint, radius: CGFloat) {204        ctx.fill(disc(c, radius), with: .color(.black.opacity(0.36)))205        ctx.stroke(disc(c, radius), with: .color(.white.opacity(0.24)), lineWidth: max(0.9 * u, 0.6))206    }207208    // MARK: Hybrid209    /// 24 h ring: midnight at the top, noon at the bottom. The arc is the part of the day already elapsed.210    func hybrid(_ m: ClockMoment, alarm: Double?) {211        let ring = r * 0.88212        let width = max(11 * u, 6)213        ctx.stroke(disc(center, ring), with: .color(.white.opacity(0.09)), lineWidth: width)214        for i in 0..<24 {215            let major = i % 6 == 0216            line(Double(i) / 24, from: ring - width / 2 - (major ? 9 : 5) * u, to: ring - width / 2 - 2.5 * u, width: max((major ? 1.8 : 1.0) * u, 0.6),217                 color: .white.opacity(major ? 0.7 : 0.28))218        }219        if !isCompact {220            symbol("moon.stars.fill", at: point(0, ring * 0.70), size: 12 * u, color: Theme.azure.opacity(0.9))221            symbol("sun.max.fill", at: point(0.5, ring * 0.70), size: 12 * u, color: Theme.gold.opacity(0.9))222        }223        // Elapsed part of the day, coloured like the sky at that hour.224        var arc = Path()225        arc.addArc(center: center, radius: ring, startAngle: .degrees(-90), endAngle: .degrees(-90 + 360 * max(m.dayFraction, 0.002)), clockwise: false)226        let sky = Gradient(colors: [Theme.indigo, Theme.violet, Theme.ember, Theme.sunrise, Theme.gold, Theme.sunrise, Theme.ember, Theme.violet, Theme.indigo])227        ctx.stroke(arc, with: .conicGradient(sky, center: center, angle: .degrees(-90)), style: StrokeStyle(lineWidth: width, lineCap: .round))228        // Time left until the next alarm: a dotted trail from now to the marker.229        if let alarm {230            let end = alarm > m.dayFraction ? alarm : alarm + 1231            var trail = Path()232            trail.addArc(center: center, radius: ring, startAngle: .degrees(-90 + 360 * m.dayFraction), endAngle: .degrees(-90 + 360 * end), clockwise: false)233            ctx.stroke(trail, with: .color(Theme.peach.opacity(0.9)), style: StrokeStyle(lineWidth: max(2.2 * u, 1.4), lineCap: .round, dash: [0.1, max(7 * u, 4.5)]))234        }235        // Seconds: a small bead running inside the ring.236        ctx.fill(disc(point(m.secondFraction, ring - width / 2 - 15 * u), max(2.6 * u, 1.8)), with: .color(Theme.sunrise))237        // Head of the arc.238        var glow = ctx239        glow.addFilter(.shadow(color: .white.opacity(0.8), radius: 6 * u))240        glow.fill(disc(point(m.dayFraction, ring), width * 0.62), with: .color(.white))241        alarmBead(alarm, at: ring)242    }243}244
Files main
Menu

Theme