Skip to content

Commit 929cffb

Browse files
committed
Add ForegroundColor API
1 parent 21fd831 commit 929cffb

File tree

2 files changed

+132
-1
lines changed

2 files changed

+132
-1
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
//
2+
// ForegroundColor.swift
3+
// OpenSwiftUICore
4+
//
5+
// Audited for 6.5.4
6+
// Status: Complete
7+
// ID: C468F93725C18A8D97E53626AB610118 (SwiftUICore)
8+
9+
import OpenAttributeGraphShims
10+
11+
// MARK: - View + foregroundColor
12+
13+
@available(OpenSwiftUI_v1_0, *)
14+
extension View {
15+
package func defaultForegroundColor(_ color: Color?) -> some View {
16+
environment(\.defaultForegroundColor, color)
17+
}
18+
19+
/// Sets the color of the foreground elements displayed by this view.
20+
///
21+
/// - Parameter color: The foreground color to use when displaying this
22+
/// view. Pass `nil` to remove any custom foreground color and to allow
23+
/// the system or the container to provide its own foreground color.
24+
/// If a container-specific override doesn't exist, the system uses
25+
/// the primary color.
26+
///
27+
/// - Returns: A view that uses the foreground color you supply.
28+
@available(OpenSwiftUI_v1_0, *)
29+
@available(*, deprecated, renamed: "foregroundStyle(_:)")
30+
@inlinable
31+
nonisolated public func foregroundColor(_ color: Color?) -> some View {
32+
environment(\.foregroundColor, color)
33+
}
34+
}
35+
36+
@available(OpenSwiftUI_v1_0, *)
37+
extension EnvironmentValues {
38+
package var defaultForegroundColor: Color? {
39+
get {
40+
guard let style = defaultForegroundStyle else { return nil }
41+
return style.fallbackColor(in: self)
42+
}
43+
set {
44+
guard let newValue else { return }
45+
defaultForegroundStyle = .init(newValue)
46+
}
47+
}
48+
49+
@usableFromInline
50+
package var foregroundColor: Color? {
51+
get {
52+
guard let style = foregroundStyle else { return nil }
53+
return style.fallbackColor(in: self)
54+
}
55+
set {
56+
guard let newValue else { return }
57+
foregroundStyle = .init(newValue)
58+
}
59+
}
60+
}
61+
62+
@available(OpenSwiftUI_v3_0, *)
63+
extension EnvironmentValues {
64+
package var _effectiveForegroundColor: Color? {
65+
let style = _effectiveForegroundStyle
66+
return style.fallbackColor(in: self)
67+
}
68+
}
69+
70+
// MARK: - _ForegroundColorModifier
71+
72+
@available(OpenSwiftUI_v3_0, *)
73+
@available(*, deprecated, message: "will be removed")
74+
@frozen
75+
public struct _ForegroundColorModifier: ViewInputsModifier, PrimitiveViewModifier {
76+
public var color: Color?
77+
78+
@inlinable
79+
public init(color: Color?) {
80+
self.color = color
81+
}
82+
83+
nonisolated public static func _makeViewInputs(
84+
modifier: _GraphValue<Self>,
85+
inputs: inout _ViewInputs
86+
) {
87+
inputs.environment = Attribute(
88+
ChildEnvironment(
89+
modifier: modifier.value,
90+
environment: inputs.environment
91+
)
92+
)
93+
}
94+
95+
private struct ChildEnvironment: Rule {
96+
@Attribute var modifier: _ForegroundColorModifier
97+
@Attribute var environment: EnvironmentValues
98+
99+
var value: EnvironmentValues {
100+
var environment = environment
101+
environment.foregroundColor = modifier.color.flatMap {
102+
Color.foreground != $0 ? $0 : nil
103+
}
104+
return environment
105+
}
106+
}
107+
}
108+
109+
@available(*, unavailable)
110+
extension _ForegroundColorModifier: Sendable {}
111+
112+
// MARK: - Color + foreground
113+
114+
extension Color {
115+
static let foreground = Color(provider: ForegroundColorProvider())
116+
117+
private struct ForegroundColorProvider: ColorProvider {
118+
func resolve(in environment: EnvironmentValues) -> Color.Resolved {
119+
let color = environment._effectiveForegroundStyle.fallbackColor(in: environment)
120+
return (color ?? .primary).resolve(in: environment)
121+
}
122+
}
123+
}

Sources/OpenSwiftUICore/Shape/ShapeStyle/AnyShapeStyle.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,15 @@ public struct AnyShapeStyle: ShapeStyle {
3232

3333
/// Create an instance from `style`.
3434
public init<S>(_ style: S) where S: ShapeStyle {
35-
storage = .init(box: ShapeStyleBox(style))
35+
if let style = style as? AnyShapeStyle {
36+
storage = style.storage
37+
} else if let color = style as? Color {
38+
storage = .init(box: color.provider)
39+
// } else if let gradient = style as? AnyGradient {
40+
// storage = .init(box: gradient.provider)
41+
} else {
42+
storage = .init(box: ShapeStyleBox(style))
43+
}
3644
}
3745

3846
public func _apply(to shape: inout _ShapeStyle_Shape) {

0 commit comments

Comments
 (0)