Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
174 changes: 173 additions & 1 deletion Sources/OpenSwiftUICore/Data/Environment/EnvironmentAdditions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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

Expand Down
60 changes: 0 additions & 60 deletions Sources/OpenSwiftUICore/View/Extension/View_Font.swift

This file was deleted.

Loading