Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Sources/SPGlassMedia/Media/GlassScrubber.swift8// Module : SPGlassMedia9// Repository : https://git.spboucher.ai/sp-glasskit10// Project Website : https://glass.spboucher.ai11// Created : 2026-12-0312// Last Modified : 2026-12-0313//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import SwiftUI42import SPGlassCore43import SPGlassTokens44import SPGlassThemes4546/// Geometry of a ``GlassScrubber``, separated from the view so that it can be tested.47public enum GlassScrubberMath {48 /// The fraction (0…1) of a horizontal position inside a track of `width` points.49 /// In a right-to-left layout the track is mirrored.50 public static func fraction(at x: CGFloat, width: CGFloat, isRightToLeft: Bool = false) -> Double {51 guard width.isFinite, width > 0, x.isFinite else { return 0 }52 let raw = Double(min(max(x / width, 0), 1))53 return isRightToLeft ? 1 - raw : raw54 }5556 /// The time for a fraction of `duration`; 0 while the duration is unknown.57 public static func time(fraction: Double, duration: TimeInterval) -> TimeInterval {58 let length = GlassPlaybackMath.sanitizedDuration(duration)59 guard !fraction.isNaN else { return 0 }60 return min(max(fraction, 0), 1) * length61 }6263 /// Seconds moved by one accessibility increment or arrow key: 5 % of the duration, at least one second.64 public static func step(duration: TimeInterval) -> TimeInterval {65 max(GlassPlaybackMath.sanitizedDuration(duration) * 0.05, 1)66 }67}6869/// The seek track shared by ``GlassAudioPlayer`` and ``GlassVideoControls``: a thin rail, the played part in70/// the tint colour and a thumb that grows while dragging.71///72/// The position is previewed locally while the user drags and committed once, on release. Assistive73/// technologies and the keyboard see a system `Slider` (`accessibilityRepresentation`), so the adjustable74/// actions, the spoken position and the arrow keys behave as with the native control.75struct GlassScrubber: View {76 @GlassThemed private var theme77 @Environment(\.layoutDirection) private var layoutDirection78 @Environment(\.isEnabled) private var isEnabled79 let currentTime: TimeInterval80 let duration: TimeInterval81 /// Colour of the played part and of the thumb.82 var tint: Color83 /// Colour of the rail.84 var rail: Color85 var trackHeight: CGFloat = 486 /// The previewed time while dragging, `nil` otherwise. The owner displays it in its time labels.87 @Binding var scrubTime: TimeInterval?88 let onSeek: @MainActor (TimeInterval) -> Void8990 private var displayedTime: TimeInterval {91 GlassPlaybackMath.clampedSeek(scrubTime ?? currentTime, duration: duration)92 }9394 var body: some View {95 let fraction = GlassPlaybackMath.fraction(currentTime: displayedTime, duration: duration)96 let isDragging = scrubTime != nil97 GeometryReader { proxy in98 let width = proxy.size.width99 let thumb: CGFloat = isDragging ? 18 : 12100 let filled = width * fraction101 ZStack(alignment: .leading) {102 Capsule()103 .fill(rail)104 .frame(height: trackHeight)105 Capsule()106 .fill(tint)107 .frame(width: max(filled, trackHeight), height: trackHeight)108 .opacity(fraction > 0 ? 1 : 0)109 Circle()110 .fill(tint)111 .frame(width: thumb, height: thumb)112 .shadow(color: .black.opacity(0.18), radius: 2, y: 1)113 .offset(x: min(max(filled - thumb / 2, 0), max(width - thumb, 0)))114 .opacity(isEnabled ? 1 : 0)115 }116 .frame(maxHeight: .infinity)117 .contentShape(Rectangle())118 .gesture(119 DragGesture(minimumDistance: 0)120 .onChanged { value in121 let position = GlassScrubberMath.fraction(at: value.location.x, width: width, isRightToLeft: layoutDirection == .rightToLeft)122 scrubTime = GlassScrubberMath.time(fraction: position, duration: duration)123 }124 .onEnded { _ in125 if let target = scrubTime { onSeek(target) }126 scrubTime = nil127 },128 isEnabled: isEnabled && duration > 0129 )130 }131 .frame(height: 28)132 .opacity(isEnabled ? 1 : 0.5)133 .accessibilityRepresentation {134 Slider(135 value: Binding(136 get: { displayedTime },137 set: { onSeek(GlassPlaybackMath.clampedSeek($0, duration: duration)) }138 ),139 in: 0...max(GlassPlaybackMath.sanitizedDuration(duration), 1),140 step: GlassScrubberMath.step(duration: duration)141 ) {142 Text(GlassL10n.string(.playbackPosition))143 }144 .accessibilityValue(Text(GlassPlaybackMath.spokenPosition(currentTime: displayedTime, duration: duration)))145 }146 }147}148