diff --git a/Sources/OpenSwiftUI/View/Control/Button/ButtonStyle/TODO/BorderlessButtonStyle.swift b/Sources/OpenSwiftUI/View/Control/Button/ButtonStyle/TODO/BorderlessButtonStyle.swift index f172a0744..f99f5814f 100644 --- a/Sources/OpenSwiftUI/View/Control/Button/ButtonStyle/TODO/BorderlessButtonStyle.swift +++ b/Sources/OpenSwiftUI/View/Control/Button/ButtonStyle/TODO/BorderlessButtonStyle.swift @@ -6,6 +6,8 @@ // Status: WIP // ID: 8946ABD13E6925C5D5FDD316D4A45F59 +@_spi(UIFrameworks) import OpenSwiftUICore + @available(tvOS, unavailable) public struct BorderlessButtonStyle: PrimitiveButtonStyle, ButtonStyleConvertible { /// Creates a borderless button style. diff --git a/Sources/OpenSwiftUICore/Data/Environment/EnvironmentAdditions.swift b/Sources/OpenSwiftUICore/Data/Environment/EnvironmentAdditions.swift index a171694cb..282a477d0 100644 --- a/Sources/OpenSwiftUICore/Data/Environment/EnvironmentAdditions.swift +++ b/Sources/OpenSwiftUICore/Data/Environment/EnvironmentAdditions.swift @@ -9,6 +9,55 @@ public import Foundation package import OpenAttributeGraphShims +// MARK: - View + Font [WIP] + +extension View { + + /// Sets the default font for text in this view. + /// + /// Use `font(_:)` to apply a specific font to all of the text in a view. + /// + /// The example below shows the effects of applying fonts to individual + /// views and to view hierarchies. Font information flows down the view + /// hierarchy as part of the environment, and remains in effect unless + /// overridden at the level of an individual view or view container. + /// + /// Here, the outermost ``VStack`` applies a 16-point system font as a + /// default font to views contained in that ``VStack``. Inside that stack, + /// the example applies a ``Font/largeTitle`` font to just the first text + /// view; this explicitly overrides the default. The remaining stack and the + /// views contained with it continue to use the 16-point system font set by + /// their containing view: + /// + /// VStack { + /// Text("Font applied to a text view.") + /// .font(.largeTitle) + /// + /// VStack { + /// Text("These 2 text views have the same font") + /// Text("applied to their parent hierarchy") + /// } + /// } + /// .font(.system(size: 16, weight: .light, design: .default)) + /// + /// ![A screenshot showing the application fonts to an individual text field + /// and view hierarchy.](OpenSwiftUI-view-font.png) + /// + /// - Parameter font: The default font to use in this view. + /// + /// - Returns: A view with the default font set to the value you supply. + @inlinable + nonisolated public func font(_ font: Font?) -> some View { + environment(\.font, font) + } + + @_spi(UIFrameworks) + @available(OpenSwiftUI_v5_0, *) + nonisolated public func defaultFont(_ font: Font?) -> some View { + environment(\.defaultFont, font) + } +} + // MARK: - EnvironmentValues + Root extension EnvironmentValues { @@ -25,7 +74,130 @@ extension EnvironmentValues { } } -// TODO: EnvironementValues + Font +// MARK: - EnvironementValues + Font + +private struct FontKey: EnvironmentKey { + static var defaultValue: Font? { nil } +} + +private struct SymbolFontKey: EnvironmentKey { + static var defaultValue: Font? { nil } +} + +private struct DefaultFontKey: EnvironmentKey { + static var defaultValue: Font? { nil } +} + +private struct DefaultSymbolFontKey: EnvironmentKey { + static var defaultValue: Font? { nil } +} + +private struct FallbackFontProviderKey: EnvironmentKey { + static let defaultValue: any FallbackFontProvider = DefaultFallbackFontProvider() +} + +private struct InTouchBarKey: EnvironmentKey { + static var defaultValue: Bool { false } +} + +@available(OpenSwiftUI_v1_0, *) +extension EnvironmentValues { + + /// The default font of this environment. + public var font: Font? { + get { self[FontKey.self] } + set { self[FontKey.self] = newValue } + } + + private struct EffectiveFontKey: DerivedEnvironmentKey { + typealias Value = Font + + static func value(in environment: EnvironmentValues) -> Font { + environment.font + ?? environment.defaultFont + ?? environment.fallbackFont + } + } + + package var effectiveFont: Font { + self[EffectiveFontKey.self] + } + + private struct EffectiveSymbolFontKey: DerivedEnvironmentKey { + static func value(in environment: EnvironmentValues) -> Font { + environment.symbolFont + ?? environment.font + ?? environment.defaultSymbolFont + ?? environment.effectiveFont + } + } + + package var effectiveSymbolFont: Font { + self[EffectiveSymbolFontKey.self] + } + + private struct FallbackFontKey: DerivedEnvironmentKey { + typealias Value = Font + + static func value(in environment: EnvironmentValues) -> Font { + environment.fallbackFontProvider.makeFont(in: environment) + } + } + + package var fallbackFont: Font { + self[FallbackFontKey.self] + } + + package var fallbackFontProvider: any FallbackFontProvider { + get { self[FallbackFontProviderKey.self] } + set { self[FallbackFontProviderKey.self] = newValue } + } + + package var defaultFont: Font? { + get { self[DefaultFontKey.self] } + set { self[DefaultFontKey.self] = newValue } + } + + package var defaultSymbolFont: Font? { + get { self[DefaultSymbolFontKey.self] } + set { self[DefaultSymbolFontKey.self] = newValue } + } + + @_spi(ForOpenSwiftUIOnly) + @available(OpenSwiftUI_v5_0, *) + public var symbolFont: Font? { + get { self[SymbolFontKey.self] } + set { self[SymbolFontKey.self] = newValue } + } + +// /// The image scale for this environment. +// @available(macOS 11.0, *) +// public var imageScale: Image.Scale { +// get { _openSwiftUIUnimplementedFailure() } +// set { _openSwiftUIUnimplementedFailure() } +// } + + package var isInTouchBar: Bool { + get { self[InTouchBarKey.self] } + set { self[InTouchBarKey.self] = newValue } + } +} + +// MARK: - FallbackFontProvider + +package protocol FallbackFontProvider { + func makeFont(in env: EnvironmentValues) -> Font +} + +package struct DefaultFallbackFontProvider: FallbackFontProvider { + package init() { + _openSwiftUIEmptyStub() + } + + package func makeFont(in env: EnvironmentValues) -> Font { + Font.body + } +} // MARK: - EnvironmentValues + Display diff --git a/Sources/OpenSwiftUICore/View/Extension/View_Font.swift b/Sources/OpenSwiftUICore/View/Extension/View_Font.swift deleted file mode 100644 index 041577ff0..000000000 --- a/Sources/OpenSwiftUICore/View/Extension/View_Font.swift +++ /dev/null @@ -1,60 +0,0 @@ -// -// View_Font.swift -// OpenSwiftUI -// -// Audited for 3.5.2 -// Status: TODO - -// MARK: - Font -private struct FontKey: EnvironmentKey { - static var defaultValue: Font? { nil } -} - -private struct DefaultFontKey: EnvironmentKey { - typealias Value = Font? - static var defaultValue: Font? { nil } -} - -private struct EffectiveFontKey: DerivedEnvironmentKey { - typealias Value = Font - - static func value(in environment: EnvironmentValues) -> Font { - environment.font ?? environment.defaultFont ?? .body - } -} - -// TODO -extension EnvironmentValues { - @inline(__always) - public var font: Font? { - get { self[FontKey.self] } - set { self[FontKey.self] = newValue } - } - - @inline(__always) - package var defaultFont: Font? { - get { self[DefaultFontKey.self] } - set { self[DefaultFontKey.self] = newValue } - } - - @inline(__always) - package var effectiveFont: Font { - EffectiveFontKey.value(in: self) - } -} - - -extension View { - @inlinable - @inline(__always) - public func font(_ font: Font?) -> some View { - environment(\.font, font) - } -} - -extension View { - @inline(__always) - package func defaultFont(_ font: Font?) -> some View { - environment(\.defaultFont, font) - } -}