Move compound-ios package into the project.

This commit is contained in:
Doug
2025-09-28 15:19:46 +01:00
committed by Doug
parent 9874d6fd84
commit ed73853c9b
80 changed files with 3658 additions and 25 deletions
@@ -0,0 +1,52 @@
//
// Copyright 2023, 2024 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
// Please see LICENSE files in the repository root for full details.
//
import SwiftUI
public extension Font {
/// The fonts used by Element as defined in Compound Design Tokens.
static let compound = CompoundFonts()
}
/// A manual mapping of the Compound font styles to iOS styles. This will be
/// generated directly from the style dictionary in the future.
public struct CompoundFonts {
public let bodyXS = Font.caption
public let bodyXSSemibold = Font.caption.weight(.semibold)
public let bodySM = Font.footnote
public let bodySMSemibold = Font.footnote.weight(.semibold)
public let bodyMD = Font.subheadline
public let bodyMDSemibold = Font.subheadline.weight(.semibold)
public let bodyLG = Font.body
public let bodyLGSemibold = Font.body.weight(.semibold)
public let headingSM = Font.title3
public let headingSMSemibold = Font.title3.weight(.semibold)
public let headingMD = Font.title2
public let headingMDBold = Font.title2.bold()
public let headingLG = Font.title
public let headingLGBold = Font.title.bold()
public let headingXL = Font.largeTitle
public let headingXLBold = Font.largeTitle.bold()
}
public extension Font.TextStyle {
/// The text styles used by Element as defined in Compound Design Tokens.
static let compound = CompoundTextStyles()
}
/// A manual mapping of the Compound font styles to iOS text styles. This is useful
/// for `@ScaledMetric` along with modifiers such as `scaledPadding` etc.
public struct CompoundTextStyles {
public let bodyXS = Font.TextStyle.caption
public let bodySM = Font.TextStyle.footnote
public let bodyMD = Font.TextStyle.subheadline
public let bodyLG = Font.TextStyle.body
public let headingSM = Font.TextStyle.title3
public let headingMD = Font.TextStyle.title2
public let headingLG = Font.TextStyle.title
public let headingXL = Font.TextStyle.largeTitle
}
@@ -0,0 +1,72 @@
import SwiftUI
/// The size of a SwiftUI font.
enum FontSize {
case custom(CGFloat, Font.TextStyle?)
case style(Font.TextStyle)
/// The raw value in points.
var value: CGFloat {
switch self {
case .custom(let size, _):
return size
case .style(let style):
switch style {
case .largeTitle:
return UIFont.preferredFont(forTextStyle: .largeTitle).pointSize
case .title:
return UIFont.preferredFont(forTextStyle: .title1).pointSize
case .title2:
return UIFont.preferredFont(forTextStyle: .title2).pointSize
case .title3:
return UIFont.preferredFont(forTextStyle: .title3).pointSize
case .body:
return UIFont.preferredFont(forTextStyle: .body).pointSize
case .headline:
return UIFont.preferredFont(forTextStyle: .headline).pointSize
case .callout:
return UIFont.preferredFont(forTextStyle: .callout).pointSize
case .subheadline:
return UIFont.preferredFont(forTextStyle: .subheadline).pointSize
case .footnote:
return UIFont.preferredFont(forTextStyle: .footnote).pointSize
case .caption:
return UIFont.preferredFont(forTextStyle: .caption1).pointSize
case .caption2:
return UIFont.preferredFont(forTextStyle: .caption2).pointSize
@unknown default:
return UIFont.preferredFont(forTextStyle: .body).pointSize
}
}
}
/// The text style of the font.
var style: Font.TextStyle {
switch self {
case .custom(_, let textStyle):
return textStyle ?? .body
case .style(let textStyle):
return textStyle
}
}
static func reflecting(_ font: Font) -> FontSize? {
let mirror = Mirror(reflecting: font)
guard let provider = mirror.descendant("provider", "base") else { return nil }
return resolveFontSize(provider)
}
private static func resolveFontSize(_ provider: Any) -> FontSize? {
let mirror = Mirror(reflecting: provider)
if let size = mirror.descendant("size") as? CGFloat {
return .custom(size, mirror.descendant("textStyle") as? Font.TextStyle)
} else if let textStyle = mirror.descendant("style") as? Font.TextStyle {
return .style(textStyle)
}
// recurse to handle modifiers.
guard let provider = mirror.descendant("base", "provider", "base") else { return nil }
return resolveFontSize(provider)
}
}