SPB Git

Repository Public License

MonitorOverlays.swift 13.5 KB 307 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Camera9// File             : App/Sources/Features/Camera/MonitorOverlays.swift10// Description      : Vector overlays drawn over the picture rectangle: grid,11//                    aspect mask, safe framing, level, focus reticle,12//                    histogram, capture assistance and status badges.13// Module           : Features14// Repository       : https://git.spboucher.ai/orix-camera15// Project Website  : https://www.orixlab.dev16// Created          : 2026-11-2117// Last Modified    : 2026-11-2118//19// =============================================================================20// AUTHOR & PROFESSIONAL AFFILIATION21// =============================================================================22//23// Author           : Simon-Pierre Boucher24// Position         : Professor of Finance25// Institution      : Université du Québec en Outaouais (UQO)26// Department       : Département des sciences administratives27// Organization     : SP Boucher28//29// Website          : https://spboucher.ai30// Git              : https://git.spboucher.ai31//32// =============================================================================33// OFFICIAL CONTACT DIRECTORY34// =============================================================================35//36// General          : info@spboucher.ai37// Professional     : contact@spboucher.ai38// Direct           : simon@spboucher.ai39// Research         : research@spboucher.ai40// Development      : dev@spboucher.ai41// Support          : support@spboucher.ai42// Security/Admin   : admin@spboucher.ai43// Notifications    : notifications@spboucher.ai44//45// Preferred Contact: support@spboucher.ai46//47// =============================================================================48// COPYRIGHT & INTELLECTUAL PROPERTY49// =============================================================================50//51// Copyright (c) 202652// Simon-Pierre Boucher53//54// All rights reserved where applicable.55//56// This file is governed by the applicable project license,57// copyright notices, institutional agreements and relevant law.58//59// Unless expressly authorized by the applicable license or60// rights holder, copying, redistribution, sublicensing,61// publication and commercial exploitation are not permitted.62//63// Existing third-party rights and notices remain unaffected.64//65// =============================================================================66// CONFIDENTIALITY & SECURITY67// =============================================================================68//69// Classification   : Public70// Distribution     : Public repository; use is governed by the project license71// Security Contact : admin@spboucher.ai72//73// Access to this source does not independently grant permission74// to redistribute or disclose confidential materials.75//76// Do not embed credentials, private keys, access tokens,77// certificates or confidential personal information.78//79// =============================================================================80// LICENSE & MAINTENANCE81// =============================================================================82//83// License          : Proprietary, all rights reserved (see license file)84// License File     : LICENSE85// Maintainer       : Simon-Pierre Boucher86// Technical Contact: dev@spboucher.ai87//88// =============================================================================89// END OF SP BOUCHER SOURCE HEADER90// =============================================================================9192import ORIXDesign93import ORIXImaging94import SwiftUI9596struct GridOverlayView: View {97    let grid: GridOverlay98    var body: some View {99        Canvas { context, size in100            for line in grid.lines {101                var path = Path()102                path.move(to: CGPoint(x: line.start.x * size.width, y: line.start.y * size.height))103                path.addLine(to: CGPoint(x: line.end.x * size.width, y: line.end.y * size.height))104                context.stroke(path, with: .color(.black.opacity(0.25)), lineWidth: 2)105                context.stroke(path, with: .color(.white.opacity(0.55)), lineWidth: 0.75)106            }107        }108        .allowsHitTesting(false)109        .accessibilityHidden(true)110    }111}112113/// Dims what falls outside the chosen aspect ratio. The full sensor image is still what gets saved (the crop is stored114/// as a non-destructive edit).115struct AspectMaskView: View {116    let guide: AspectGuide117    var body: some View {118        GeometryReader { proxy in119            let size = proxy.size120            let r = guide.rect(frameAspect: size.width / max(size.height, 1), portrait: size.height > size.width)121            let rect = CGRect(x: r.minX * size.width, y: r.minY * size.height, width: r.width * size.width, height: r.height * size.height)122            Path { path in123                path.addRect(CGRect(origin: .zero, size: size))124                path.addRect(rect)125            }126            .fill(.black.opacity(0.55), style: FillStyle(eoFill: true))127            Rectangle().path(in: rect).stroke(.white.opacity(0.7), lineWidth: 1)128        }129        .allowsHitTesting(false)130        .accessibilityHidden(true)131    }132}133134struct SafeFramingView: View {135    private static func rect(_ r: CGRect, in s: CGSize) -> CGRect {136        CGRect(x: r.minX * s.width, y: r.minY * s.height, width: r.width * s.width, height: r.height * s.height)137    }138139    var body: some View {140        GeometryReader { proxy in141            Rectangle().path(in: Self.rect(SafeFraming.actionSafe, in: proxy.size)).stroke(.white.opacity(0.5), style: StrokeStyle(lineWidth: 1, dash: [6, 5]))142            Rectangle().path(in: Self.rect(SafeFraming.titleSafe, in: proxy.size)).stroke(Instrument.manual.opacity(0.6), style: StrokeStyle(lineWidth: 1, dash: [2, 5]))143        }144        .allowsHitTesting(false)145        .accessibilityHidden(true)146    }147}148149/// Horizon level from the gravity vector. An aid, not a survey instrument.150struct LevelView: View {151    let reading: LevelReading152    var body: some View {153        let isLevel = reading.isLevel()154        ZStack {155            if reading.isFlat {156                Circle().stroke(.white.opacity(0.6), lineWidth: 1).frame(width: 34, height: 34)157                Circle().fill(Instrument.manual).frame(width: 8, height: 8)158            } else {159                HStack(spacing: 44) {160                    Capsule().fill(.white.opacity(0.7)).frame(width: 26, height: 1.5)161                    Capsule().fill(.white.opacity(0.7)).frame(width: 26, height: 1.5)162                }163                Capsule()164                    .fill(isLevel ? Instrument.good : Instrument.manual)165                    .frame(width: 40, height: 2)166                    .rotationEffect(.degrees(-reading.roll))167            }168        }169        .animation(.linear(duration: 0.08), value: reading.roll)170        .allowsHitTesting(false)171        .accessibilityElement()172        .accessibilityLabel(Text("Level"))173        .accessibilityValue(reading.isFlat ? Text("Camera pointing down or up") : (isLevel ? Text("Level") : Text("Tilted \(Int(reading.roll.rounded())) degrees")))174    }175}176177struct FocusReticle: View {178    let adjusting: Bool179    @State private var scale: CGFloat = 1.5180    var body: some View {181        RoundedRectangle(cornerRadius: 6, style: .continuous)182            .stroke(Instrument.manual, lineWidth: 1.5)183            .frame(width: 72, height: 72)184            .overlay(alignment: .top) { Rectangle().fill(Instrument.manual).frame(width: 1.5, height: 7) }185            .overlay(alignment: .bottom) { Rectangle().fill(Instrument.manual).frame(width: 1.5, height: 7) }186            .scaleEffect(scale)187            .opacity(adjusting ? 1 : 0.75)188            .onAppear { withAnimation(.spring(response: 0.3, dampingFraction: 0.7)) { scale = 1 } }189            .allowsHitTesting(false)190            .accessibilityHidden(true)191    }192}193194/// The histogram of the live frame: counts from HistogramProcessor, normalized and smoothed for drawing only.195struct HistogramView: View {196    let curves: [[Float]]197    let style: HistogramStyle198    let highlightClipping: Double199    let shadowClipping: Double200    var expanded = false201202    var body: some View {203        let size = expanded ? CGSize(width: 200, height: 96) : CGSize(width: 116, height: 54)204        Canvas { context, canvas in205            guard curves.count == 4 else { return }206            func path(_ values: [Float]) -> Path {207                var p = Path()208                p.move(to: CGPoint(x: 0, y: canvas.height))209                for (i, v) in values.enumerated() {210                    // Square-root scale: small counts stay visible next to a large peak.211                    p.addLine(to: CGPoint(x: CGFloat(i) / 255 * canvas.width, y: canvas.height * (1 - CGFloat(v.squareRoot()))))212                }213                p.addLine(to: CGPoint(x: canvas.width, y: canvas.height))214                p.closeSubpath()215                return p216            }217            if style == .rgb {218                context.blendMode = .screen219                context.fill(path(curves[0]), with: .color(Color(red: 1, green: 0.25, blue: 0.25).opacity(0.85)))220                context.fill(path(curves[1]), with: .color(Color(red: 0.25, green: 1, blue: 0.35).opacity(0.85)))221                context.fill(path(curves[2]), with: .color(Color(red: 0.3, green: 0.45, blue: 1).opacity(0.85)))222            } else {223                context.fill(path(curves[3]), with: .color(.white.opacity(0.85)))224            }225        }226        .frame(width: size.width, height: size.height)227        .padding(5)228        .background(.black.opacity(0.5), in: RoundedRectangle(cornerRadius: 8, style: .continuous))229        .overlay(alignment: .topTrailing) {230            if highlightClipping > 0.005 { Image(systemName: "triangle.fill").font(.system(size: 8)).foregroundStyle(Instrument.record).padding(4) }231        }232        .overlay(alignment: .topLeading) {233            if shadowClipping > 0.02 { Image(systemName: "triangle.fill").font(.system(size: 8)).foregroundStyle(Color(red: 0.35, green: 0.55, blue: 1)).padding(4) }234        }235        .overlay(RoundedRectangle(cornerRadius: 8, style: .continuous).strokeBorder(Instrument.hairline, lineWidth: 0.5))236        .accessibilityElement()237        .accessibilityIdentifier("histogram")238        .accessibilityLabel(Text(style == .rgb ? "RGB histogram" : "Luminance histogram"))239        .accessibilityValue(Text("Highlights clipped \(Int((highlightClipping * 100).rounded())) percent, shadows clipped \(Int((shadowClipping * 100).rounded())) percent"))240        .accessibilityHint(Text("Double tap to switch between luminance and RGB"))241    }242}243244/// Auto mode's capture assistance: short, factual hints derived from the measurements.245struct HintChips: View {246    let hints: [SceneAssessment.Hint]247    var body: some View {248        VStack(alignment: .leading, spacing: 6) {249            ForEach(hints.prefix(2), id: \.self) { hint in250                MonitorBadge(text: String(localized: Self.text(hint)), symbol: Self.symbol(hint), color: Instrument.manual)251            }252        }253        .animation(.smooth(duration: 0.25), value: hints)254        .accessibilityElement(children: .combine)255    }256257    static func text(_ hint: SceneAssessment.Hint) -> LocalizedStringResource {258        switch hint {259        case .highlightsClipped: "Highlights clipped"260        case .shadowsCrushed: "Shadows crushed"261        case .lowLight: "Low light"262        case .motionBlurRisk: "Slow shutter: hold steady"263        case .lowDetail: "Low detail: check focus"264        case .tilted: "Horizon tilted"265        }266    }267268    static func symbol(_ hint: SceneAssessment.Hint) -> String {269        switch hint {270        case .highlightsClipped: "sun.max.trianglebadge.exclamationmark"271        case .shadowsCrushed: "moon"272        case .lowLight: "moon.stars"273        case .motionBlurRisk: "hand.raised"274        case .lowDetail: "scope"275        case .tilted: "level"276        }277    }278}279280/// Microphone level meter (dBFS, −60…0) with a clip light.281struct AudioMeter: View {282    let levels: AudioLevels283    var body: some View {284        let rms = CGFloat(min(max((levels.rms + 60) / 60, 0), 1)), peak = CGFloat(min(max((levels.peak + 60) / 60, 0), 1))285        HStack(spacing: 6) {286            Image(systemName: "mic.fill").font(.system(size: 10, weight: .bold)).foregroundStyle(Instrument.textDim)287            GeometryReader { proxy in288                ZStack(alignment: .leading) {289                    Capsule().fill(.white.opacity(0.14))290                    Capsule().fill(LinearGradient(colors: [Instrument.good, Instrument.good, Instrument.warn, Instrument.record], startPoint: .leading, endPoint: .trailing))291                        .mask(alignment: .leading) { Rectangle().frame(width: proxy.size.width * rms) }292                    Rectangle().fill(.white).frame(width: 2).offset(x: proxy.size.width * peak - 1)293                }294            }295            .frame(width: 96, height: 5)296            Circle().fill(levels.clipped ? Instrument.record : .white.opacity(0.18)).frame(width: 6, height: 6)297        }298        .padding(.horizontal, 8).padding(.vertical, 6)299        .background(.black.opacity(0.5), in: Capsule())300        .animation(.linear(duration: 0.06), value: levels)301        .accessibilityElement()302        .accessibilityIdentifier("audioMeter")303        .accessibilityLabel(Text("Microphone level"))304        .accessibilityValue(Text("\(Int(levels.rms.rounded())) decibels"))305    }306}307
Files main
Menu

Theme