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

This commit is contained in:
Matt Bruce 2026-02-01 08:57:30 -06:00
parent 403eeb620f
commit 6c2846a072
2 changed files with 51 additions and 2 deletions

View File

@ -92,6 +92,9 @@ struct DigitView: View {
.baselineOffset(0) .baselineOffset(0)
.onAppear { .onAppear {
calculateOptimalFontSize(for: geometry.size) calculateOptimalFontSize(for: geometry.size)
DispatchQueue.main.async {
calculateOptimalFontSize(for: geometry.size)
}
} }
.onChange(of: geometry.size) { _, newSize in .onChange(of: geometry.size) { _, newSize in
calculateOptimalFontSize(for: newSize) calculateOptimalFontSize(for: newSize)
@ -124,7 +127,7 @@ struct DigitView: View {
weight: weight, weight: weight,
design: design, design: design,
for: size, for: size,
isDisplayMode: isDisplayMode) isDisplayMode: false)
// Only update if the size is significantly different to prevent micro-adjustments // Only update if the size is significantly different to prevent micro-adjustments
fontSize = optimalSize fontSize = optimalSize

View File

@ -27,6 +27,7 @@ struct TimeDisplayView: View {
let forceHorizontalMode: Bool let forceHorizontalMode: Bool
let isDisplayMode: Bool let isDisplayMode: Bool
@State var fontSize: CGFloat = 100 @State var fontSize: CGFloat = 100
@State private var lastCalculatedContainerSize: CGSize = .zero
// MARK: - Formatters // MARK: - Formatters
private static let hour24DF: DateFormatter = { private static let hour24DF: DateFormatter = {
@ -137,13 +138,58 @@ struct TimeDisplayView: View {
.animation(.smooth(duration: Design.Animation.standard), value: showSeconds) // Smooth animation for seconds toggle .animation(.smooth(duration: Design.Animation.standard), value: showSeconds) // Smooth animation for seconds toggle
.minimumScaleFactor(0.1) .minimumScaleFactor(0.1)
.clipped() // Prevent overflow beyond bounds .clipped() // Prevent overflow beyond bounds
.onAppear {
updateFontSize(containerSize: containerSize, portrait: portrait, showSeconds: showSeconds)
}
.onChange(of: containerSize) { _, newSize in
updateFontSize(containerSize: newSize, portrait: portrait, showSeconds: showSeconds)
}
.onChange(of: showSeconds) { _, _ in
updateFontSize(containerSize: containerSize, portrait: portrait, showSeconds: showSeconds)
}
.onChange(of: fontFamily) { _, _ in
updateFontSize(containerSize: containerSize, portrait: portrait, showSeconds: showSeconds)
}
.onChange(of: fontWeight) { _, _ in
updateFontSize(containerSize: containerSize, portrait: portrait, showSeconds: showSeconds)
}
.onChange(of: fontDesign) { _, _ in
updateFontSize(containerSize: containerSize, portrait: portrait, showSeconds: showSeconds)
}
} }
//.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
} }
// MARK: - Font Sizing
private func updateFontSize(containerSize: CGSize, portrait: Bool, showSeconds: Bool) {
guard containerSize != .zero else { return }
if containerSize == lastCalculatedContainerSize {
return
}
let rows = portrait ? (showSeconds ? 5.0 : 3.0) : 1.0
let digits = portrait ? 2.0 : (showSeconds ? 6.0 : 4.0)
let digitSize = CGSize(
width: max(1, containerSize.width / digits),
height: max(1, containerSize.height / rows)
)
let estimated = FontUtils.calculateOptimalFontSize(
digit: "8",
fontName: fontFamily,
weight: fontWeight,
design: fontDesign,
for: digitSize,
isDisplayMode: false
)
if abs(estimated - fontSize) > 1 {
fontSize = estimated
}
lastCalculatedContainerSize = containerSize
}
} }