Sources/Orgstar/TextScale.swift
47 lines · 1467 bytes
1import AppKit
2import OrgPresentation
3import SwiftUI
4
5extension EnvironmentValues {
6 /// Settings ▸ Appearance ▸ Size over its default, never below 1: how much larger than the
7 /// system's sizes the agenda and sidebars show text. macOS has no Dynamic Type to do this.
8 @Entry var textScale: CGFloat = 1
9}
10
11extension Font {
12 /// `style` at the system's size for it times `scale`.
13 static func scaled(_ style: Font.TextStyle, _ scale: CGFloat) -> Font {
14 .system(size: NSFont.preferredFont(forTextStyle: style.appKit).pointSize * scale)
15 }
16}
17
18extension Font.TextStyle {
19 fileprivate var appKit: NSFont.TextStyle {
20 switch self {
21 case .largeTitle: .largeTitle
22 case .title: .title1
23 case .title2: .title2
24 case .title3: .title3
25 case .headline: .headline
26 case .subheadline: .subheadline
27 case .callout: .callout
28 case .footnote: .footnote
29 case .caption: .caption1
30 case .caption2: .caption2
31 default: .body
32 }
33 }
34}
35
36private struct TextScaled: ViewModifier {
37 @AppStorage("themeFontSize") private var size = ThemeSpec.default.fontSize
38
39 func body(content: Content) -> some View {
40 content.environment(\.textScale, max(1, CGFloat(size) / CGFloat(ThemeSpec.default.fontSize)))
41 }
42}
43
44extension View {
45 /// Sets `textScale` from Settings ▸ Appearance ▸ Size.
46 func textScaled() -> some View { modifier(TextScaled()) }
47}