Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>

This commit is contained in:
Matt Bruce 2025-09-11 13:35:50 -05:00
parent 4e02cb1336
commit 4ea684cbda
4 changed files with 58 additions and 50 deletions

View File

@ -125,16 +125,14 @@ struct FontUtils {
design: Font.Design, design: Font.Design,
size: CGFloat size: CGFloat
) -> Font { ) -> Font {
let fontName = weightedFontName(
name: name.rawValue,
weight: weight,
design: design
)
print(fontName)
if name == .system { if name == .system {
return .system(size: size, weight: weight, design: design) return .system(size: size, weight: weight, design: design)
} else { } else {
return .custom(fontName, size: size) return .custom(weightedFontName(
name: name.rawValue,
weight: weight,
design: design
), size: size)
} }
} }
} }

View File

@ -20,6 +20,9 @@ struct DigitView: View {
@State var glowIntensity: Double @State var glowIntensity: Double
@Binding var fontSize: CGFloat @Binding var fontSize: CGFloat
@State private var lastCalculatedSize: CGSize = .zero
@State private var isCalculating: Bool = false
init(digit: String, init(digit: String,
fontName: FontFamily, fontName: FontFamily,
weight: Font.Weight = .regular, weight: Font.Weight = .regular,
@ -85,35 +88,41 @@ struct DigitView: View {
.padding(.horizontal, 0) .padding(.horizontal, 0)
.baselineOffset(0) .baselineOffset(0)
.onAppear { .onAppear {
let optimalSize = FontUtils.calculateOptimalFontSize(digit: digit, calculateOptimalFontSize(for: geometry.size)
fontName: fontName,
weight: weight,
design: design,
for: geometry.size)
if optimalSize != fontSize {
fontSize = optimalSize
}
} }
.onChange(of: geometry.size) { _, newSize in .onChange(of: geometry.size) { _, newSize in
let optimalSize = FontUtils.calculateOptimalFontSize(digit: digit, calculateOptimalFontSize(for: newSize)
fontName: fontName,
weight: weight,
design: design,
for: newSize)
if optimalSize != fontSize {
fontSize = optimalSize
}
} }
.onChange(of: sizeCategory) { _, _ in .onChange(of: sizeCategory) { _, _ in
calculateOptimalFontSize(for: geometry.size)
}
}
}
private func calculateOptimalFontSize(for size: CGSize) {
// Prevent multiple calculations for the same size
guard size != lastCalculatedSize && !isCalculating else { return }
// Prevent multiple updates per frame
guard !isCalculating else { return }
isCalculating = true
let optimalSize = FontUtils.calculateOptimalFontSize(digit: digit, let optimalSize = FontUtils.calculateOptimalFontSize(digit: digit,
fontName: fontName, fontName: fontName,
weight: weight, weight: weight,
design: design, design: design,
for: geometry.size) for: size)
if optimalSize != fontSize {
// Only update if the size is significantly different to prevent micro-adjustments
if abs(optimalSize - fontSize) > 0.1 {
fontSize = optimalSize fontSize = optimalSize
} }
}
lastCalculatedSize = size
// Reset calculation flag after a brief delay to allow for frame completion
DispatchQueue.main.asyncAfter(deadline: .now() + 0.016) { // ~60fps
isCalculating = false
} }
} }
} }

View File

@ -107,7 +107,7 @@ struct TimeDisplayView: View {
.minimumScaleFactor(0.1) .minimumScaleFactor(0.1)
.clipped() // Prevent overflow beyond bounds .clipped() // Prevent overflow beyond bounds
} }
.border(.yellow, width: 1) //.border(.yellow, width: 1)
.frame(maxWidth: .infinity, maxHeight: .infinity) .frame(maxWidth: .infinity, maxHeight: .infinity)
.onOrientationChange() // Force updates on orientation changes .onOrientationChange() // Force updates on orientation changes
} }
@ -135,5 +135,6 @@ struct TimeDisplayView: View {
fontWeight: style.fontWeight, fontWeight: style.fontWeight,
fontDesign: style.fontDesign, fontDesign: style.fontDesign,
forceHorizontalMode: style.forceHorizontalMode forceHorizontalMode: style.forceHorizontalMode
) .background(Color.black) )
.background(Color.black)
} }

View File

@ -32,10 +32,10 @@ struct TimeSegment: View {
glowIntensity: glowIntensity, glowIntensity: glowIntensity,
fontSize: $fontSize fontSize: $fontSize
) )
.border(.red, width: 1) //.border(.red, width: 1)
} }
} }
.border(Color.green, width: 1) //.border(Color.green, width: 1)
.frame(maxHeight: .infinity) .frame(maxHeight: .infinity)
} }