import SwiftUI /// Resolved once at the presentation root; never scale these values again. /// Separate from color palettes so light, dark and OLED have identical metrics. public struct HapiTypography: Equatable, Sendable { public var bodyScale: CGFloat public var codeScale: CGFloat public var captionScale: CGFloat public var boldText: Bool public init(bodyScale: CGFloat = 1, codeScale: CGFloat = 1, captionScale: CGFloat = 1, boldText: Bool = false) { self.bodyScale = bodyScale self.codeScale = codeScale self.captionScale = captionScale self.boldText = boldText } public var bodySize: CGFloat { 16 * bodyScale } public var codeSize: CGFloat { 14 * codeScale } public var captionSize: CGFloat { 12 * captionScale } public var inlineCodeSize: CGFloat { 15 * bodyScale } public var bodyLineSpacing: CGFloat { 3 * bodyScale } public var codeLineSpacing: CGFloat { 2 * codeScale } public var bodyFont: Font { .system(size: bodySize, weight: regularWeight) } public var codeFont: Font { .system(size: codeSize, weight: regularWeight, design: .monospaced) } /// Code/terminal text uses the user override when present; everything /// else keeps the Dynamic-Type-derived code size. public func codeFont(size: CGFloat?) -> Font { guard let size else { return codeFont } return .system(size: size, weight: regularWeight, design: .monospaced) } public var captionFont: Font { .system(size: captionSize, weight: regularWeight) } public var inlineCodeFont: Font { .system(size: inlineCodeSize, weight: regularWeight, design: .monospaced) } public var captionMonoFont: Font { .system(size: captionSize, weight: regularWeight, design: .monospaced) } private var regularWeight: Font.Weight { boldText ? .semibold : .regular } public func headingSize(_ level: Int) -> CGFloat { let base: CGFloat = switch level { case 1: 24 case 2: 20 case 3: 18 default: 17 } return base * bodyScale } public func headingFont(_ level: Int) -> Font { .system(size: headingSize(level), weight: boldText ? .bold : .semibold) } } private struct HapiTypographyKey: EnvironmentKey { static let defaultValue = HapiTypography() } /// Optional override for terminal-styled/code text only. Kept separate from /// `HapiTypography` so the app's existing local typography installs do not /// reset it and chat body text is unaffected. private struct HapiCodeFontSizeKey: EnvironmentKey { static let defaultValue: CGFloat? = nil } public extension EnvironmentValues { var hapiTypography: HapiTypography { get { self[HapiTypographyKey.self] } set { self[HapiTypographyKey.self] = newValue } } var hapiCodeFontSize: CGFloat? { get { self[HapiCodeFontSizeKey.self] } set { self[HapiCodeFontSizeKey.self] = newValue } } } private struct HapiTypographyModifier: ViewModifier { @ScaledMetric(relativeTo: .body) private var bodyScale: CGFloat = 1 @ScaledMetric(relativeTo: .footnote) private var codeScale: CGFloat = 1 @ScaledMetric(relativeTo: .caption) private var captionScale: CGFloat = 1 @Environment(\.legibilityWeight) private var legibilityWeight func body(content: Content) -> some View { let platform = Self.platformScale return content.environment(\.hapiTypography, HapiTypography( bodyScale: bodyScale * platform, codeScale: codeScale * platform, captionScale: captionScale * platform, boldText: legibilityWeight == .bold )) } /// The point sizes are tuned for touch screens. A Mac-idiom window renders /// them 1:1 next to 13pt system text, so reading text steps down to 14pt. private static var platformScale: CGFloat { #if targetEnvironment(macCatalyst) 0.875 #else 1 #endif } } public enum HapiReadingLayout { public static let horizontalInset: CGFloat = 16 public static let maximumWidth: CGFloat = 720 public static func contentWidth(in availableWidth: CGFloat) -> CGFloat { max(1, min(maximumWidth, availableWidth - 2 * horizontalInset)) } } public extension View { /// Install outside the chat list so hosted rows inherit resolved metrics. func hapiTypography() -> some View { modifier(HapiTypographyModifier()) } func hapiReadingColumn(horizontalInset: CGFloat = HapiReadingLayout.horizontalInset) -> some View { frame(maxWidth: HapiReadingLayout.maximumWidth, alignment: .leading) .frame(maxWidth: .infinity) .padding(.horizontal, horizontalInset) } }