99public import Foundation
1010package import OpenAttributeGraphShims
1111
12+ // MARK: - View + Font [WIP]
13+
14+ extension View {
15+
16+ /// Sets the default font for text in this view.
17+ ///
18+ /// Use `font(_:)` to apply a specific font to all of the text in a view.
19+ ///
20+ /// The example below shows the effects of applying fonts to individual
21+ /// views and to view hierarchies. Font information flows down the view
22+ /// hierarchy as part of the environment, and remains in effect unless
23+ /// overridden at the level of an individual view or view container.
24+ ///
25+ /// Here, the outermost ``VStack`` applies a 16-point system font as a
26+ /// default font to views contained in that ``VStack``. Inside that stack,
27+ /// the example applies a ``Font/largeTitle`` font to just the first text
28+ /// view; this explicitly overrides the default. The remaining stack and the
29+ /// views contained with it continue to use the 16-point system font set by
30+ /// their containing view:
31+ ///
32+ /// VStack {
33+ /// Text("Font applied to a text view.")
34+ /// .font(.largeTitle)
35+ ///
36+ /// VStack {
37+ /// Text("These 2 text views have the same font")
38+ /// Text("applied to their parent hierarchy")
39+ /// }
40+ /// }
41+ /// .font(.system(size: 16, weight: .light, design: .default))
42+ ///
43+ /// 
45+ ///
46+ /// - Parameter font: The default font to use in this view.
47+ ///
48+ /// - Returns: A view with the default font set to the value you supply.
49+ @inlinable
50+ nonisolated public func font( _ font: Font ? ) -> some View {
51+ environment ( \. font, font)
52+ }
53+
54+ @_spi ( UIFrameworks)
55+ @available ( OpenSwiftUI_v5_0, * )
56+ nonisolated public func defaultFont( _ font: Font ? ) -> some View {
57+ environment ( \. defaultFont, font)
58+ }
59+ }
60+
1261// MARK: - EnvironmentValues + Root
1362
1463extension EnvironmentValues {
@@ -25,7 +74,130 @@ extension EnvironmentValues {
2574 }
2675}
2776
28- // TODO: EnvironementValues + Font
77+ // MARK: - EnvironementValues + Font
78+
79+ private struct FontKey : EnvironmentKey {
80+ static var defaultValue : Font ? { nil }
81+ }
82+
83+ private struct SymbolFontKey : EnvironmentKey {
84+ static var defaultValue : Font ? { nil }
85+ }
86+
87+ private struct DefaultFontKey : EnvironmentKey {
88+ static var defaultValue : Font ? { nil }
89+ }
90+
91+ private struct DefaultSymbolFontKey : EnvironmentKey {
92+ static var defaultValue : Font ? { nil }
93+ }
94+
95+ private struct FallbackFontProviderKey : EnvironmentKey {
96+ static let defaultValue : any FallbackFontProvider = DefaultFallbackFontProvider ( )
97+ }
98+
99+ private struct InTouchBarKey : EnvironmentKey {
100+ static var defaultValue : Bool { false }
101+ }
102+
103+ @available ( OpenSwiftUI_v1_0, * )
104+ extension EnvironmentValues {
105+
106+ /// The default font of this environment.
107+ public var font : Font ? {
108+ get { self [ FontKey . self] }
109+ set { self [ FontKey . self] = newValue }
110+ }
111+
112+ private struct EffectiveFontKey : DerivedEnvironmentKey {
113+ typealias Value = Font
114+
115+ static func value( in environment: EnvironmentValues ) -> Font {
116+ environment. font
117+ ?? environment. defaultFont
118+ ?? environment. fallbackFont
119+ }
120+ }
121+
122+ package var effectiveFont : Font {
123+ self [ EffectiveFontKey . self]
124+ }
125+
126+ private struct EffectiveSymbolFontKey : DerivedEnvironmentKey {
127+ static func value( in environment: EnvironmentValues ) -> Font {
128+ environment. symbolFont
129+ ?? environment. font
130+ ?? environment. defaultSymbolFont
131+ ?? environment. effectiveFont
132+ }
133+ }
134+
135+ package var effectiveSymbolFont : Font {
136+ self [ EffectiveSymbolFontKey . self]
137+ }
138+
139+ private struct FallbackFontKey : DerivedEnvironmentKey {
140+ typealias Value = Font
141+
142+ static func value( in environment: EnvironmentValues ) -> Font {
143+ environment. fallbackFontProvider. makeFont ( in: environment)
144+ }
145+ }
146+
147+ package var fallbackFont : Font {
148+ self [ FallbackFontKey . self]
149+ }
150+
151+ package var fallbackFontProvider : any FallbackFontProvider {
152+ get { self [ FallbackFontProviderKey . self] }
153+ set { self [ FallbackFontProviderKey . self] = newValue }
154+ }
155+
156+ package var defaultFont : Font ? {
157+ get { self [ DefaultFontKey . self] }
158+ set { self [ DefaultFontKey . self] = newValue }
159+ }
160+
161+ package var defaultSymbolFont : Font ? {
162+ get { self [ DefaultSymbolFontKey . self] }
163+ set { self [ DefaultSymbolFontKey . self] = newValue }
164+ }
165+
166+ @_spi ( ForOpenSwiftUIOnly)
167+ @available ( OpenSwiftUI_v5_0, * )
168+ public var symbolFont : Font ? {
169+ get { self [ SymbolFontKey . self] }
170+ set { self [ SymbolFontKey . self] = newValue }
171+ }
172+
173+ // /// The image scale for this environment.
174+ // @available(macOS 11.0, *)
175+ // public var imageScale: Image.Scale {
176+ // get { _openSwiftUIUnimplementedFailure() }
177+ // set { _openSwiftUIUnimplementedFailure() }
178+ // }
179+
180+ package var isInTouchBar : Bool {
181+ get { self [ InTouchBarKey . self] }
182+ set { self [ InTouchBarKey . self] = newValue }
183+ }
184+ }
185+
186+ // MARK: - FallbackFontProvider
187+
188+ package protocol FallbackFontProvider {
189+ func makeFont( in env: EnvironmentValues ) -> Font
190+ }
191+
192+ package struct DefaultFallbackFontProvider : FallbackFontProvider {
193+ package init ( ) {
194+ _openSwiftUIEmptyStub ( )
195+ }
196+
197+ package func makeFont( in env: EnvironmentValues ) -> Font {
198+ Font . body
199+ }
200+ }
29201
30202// MARK: - EnvironmentValues + Display
31203
0 commit comments