109 lines
5.2 KiB
Swift
109 lines
5.2 KiB
Swift
import AppKit
|
|
import SwiftUI
|
|
|
|
/// The bundled faces by PostScript name, as registered by `FontRegistry`.
|
|
public enum PlexFace: String, CaseIterable, Sendable {
|
|
case monoRegular = "IBMPlexMono-Regular"
|
|
case monoMedium = "IBMPlexMono-Medium"
|
|
case monoSemiBold = "IBMPlexMono-SemiBold"
|
|
case monoBold = "IBMPlexMono-Bold"
|
|
case sansRegular = "IBMPlexSans-Regular"
|
|
case sansMedium = "IBMPlexSans-Medium"
|
|
case sansSemiBold = "IBMPlexSans-SemiBold"
|
|
case sansBold = "IBMPlexSans-Bold"
|
|
case sansItalic = "IBMPlexSans-Italic"
|
|
|
|
public var isMono: Bool { rawValue.hasPrefix("IBMPlexMono") }
|
|
}
|
|
|
|
/// One typographic role: face, size, line box and tracking.
|
|
public struct TypeRole: Sendable {
|
|
public var face: PlexFace
|
|
public var size: CGFloat
|
|
/// Line box height in points; nil keeps the font's own.
|
|
public var lineHeight: CGFloat?
|
|
/// Tracking in points (the design's em values times the size).
|
|
public var tracking: CGFloat
|
|
public var uppercase: Bool
|
|
|
|
public init(_ face: PlexFace, _ size: CGFloat, lineHeight: CGFloat? = nil, trackingEm: CGFloat = 0, uppercase: Bool = false) {
|
|
self.face = face
|
|
self.size = size
|
|
self.lineHeight = lineHeight
|
|
self.tracking = trackingEm * size
|
|
self.uppercase = uppercase
|
|
}
|
|
|
|
/// Falls back to the system monospaced or UI font when the bundled face did not register.
|
|
public var nsFont: NSFont {
|
|
if let font = NSFont(name: face.rawValue, size: size) { return font }
|
|
let weight: NSFont.Weight = switch face {
|
|
case .monoMedium, .sansMedium: .medium
|
|
case .monoSemiBold, .sansSemiBold: .semibold
|
|
case .monoBold, .sansBold: .bold
|
|
default: .regular
|
|
}
|
|
return face.isMono ? .monospacedSystemFont(ofSize: size, weight: weight) : .systemFont(ofSize: size, weight: weight)
|
|
}
|
|
|
|
public var font: Font { Font(nsFont) }
|
|
}
|
|
|
|
public enum Typography {
|
|
public static let sectionTitle = TypeRole(.monoBold, 20, lineHeight: 24, trackingEm: -0.02)
|
|
public static let dialogTitle = TypeRole(.monoBold, 14, lineHeight: 14)
|
|
/// Section and panel labels: 10px mono, uppercase, text-4.
|
|
public static let panelLabel = TypeRole(.monoBold, 10, trackingEm: 0.14, uppercase: true)
|
|
public static let wordmark = TypeRole(.monoBold, 13, trackingEm: 0.02)
|
|
public static let launcherWordmark = TypeRole(.monoBold, 14, trackingEm: 0.02)
|
|
/// The last breadcrumb segment in the title bar.
|
|
public static let crumbLeaf = TypeRole(.monoSemiBold, 12)
|
|
/// The `esc`/`↵` chip and overlay footers.
|
|
public static let chip = TypeRole(.monoRegular, 11)
|
|
public static let keycapSmall = TypeRole(.monoSemiBold, 11)
|
|
public static let toastTitle = TypeRole(.sansRegular, 13)
|
|
public static let toastReference = TypeRole(.monoRegular, 12)
|
|
public static let focusFlash = TypeRole(.monoRegular, 19)
|
|
public static let focusFlashName = TypeRole(.monoBold, 19, trackingEm: -0.01)
|
|
public static let code = TypeRole(.monoRegular, 13, lineHeight: 20)
|
|
public static let searchHit = TypeRole(.monoRegular, 12, lineHeight: 24)
|
|
public static let path = TypeRole(.monoRegular, 12)
|
|
public static let meta = TypeRole(.monoRegular, 11)
|
|
public static let branch = TypeRole(.monoMedium, 12)
|
|
public static let keycap = TypeRole(.monoSemiBold, 12)
|
|
public static let statusLetter = TypeRole(.monoSemiBold, 10)
|
|
public static let badge = TypeRole(.monoSemiBold, 8)
|
|
public static let searchQuery = TypeRole(.monoMedium, 16)
|
|
public static let terminal = TypeRole(.monoRegular, 12, lineHeight: 20.4)
|
|
public static let row = TypeRole(.sansRegular, 13)
|
|
public static let rowSelected = TypeRole(.sansMedium, 13)
|
|
public static let sourceControlRow = TypeRole(.sansRegular, 12)
|
|
public static let body = TypeRole(.sansRegular, 14, lineHeight: 23.8)
|
|
public static let tooltip = TypeRole(.sansRegular, 12, lineHeight: 18)
|
|
public static let button = TypeRole(.sansMedium, 12)
|
|
public static let commitButton = TypeRole(.sansSemiBold, 12)
|
|
/// The editor bar's Preview and Diff buttons.
|
|
public static let segment = TypeRole(.sansRegular, 11)
|
|
public static let segmentOn = TypeRole(.sansMedium, 11)
|
|
public static let toolbarLabel = TypeRole(.sansRegular, 12)
|
|
public static let toolbarLabelOn = TypeRole(.sansMedium, 12)
|
|
public static let menuItem = TypeRole(.sansRegular, 13)
|
|
public static let menuItemHighlighted = TypeRole(.sansMedium, 13)
|
|
public static let note = TypeRole(.monoRegular, 14, lineHeight: 23.8)
|
|
public static let markdownH2 = TypeRole(.monoBold, 24, lineHeight: 26.4, trackingEm: -0.03)
|
|
public static let markdownH3 = TypeRole(.monoBold, 17, lineHeight: 20.4)
|
|
public static let markdownQuote = TypeRole(.sansItalic, 15)
|
|
}
|
|
|
|
extension View {
|
|
/// Applies a type role: font, tracking, line spacing and case.
|
|
public func typeRole(_ role: TypeRole) -> some View {
|
|
let natural = role.nsFont.ascender - role.nsFont.descender + role.nsFont.leading
|
|
return self
|
|
.font(role.font)
|
|
.tracking(role.tracking)
|
|
.lineSpacing(max(0, (role.lineHeight ?? natural) - natural))
|
|
.textCase(role.uppercase ? .uppercase : nil)
|
|
}
|
|
}
|