Skip to content

Commit 4609b0a

Browse files
authored
Revise ShapeStyle and add Gradients (#435)
1 parent 21c21cd commit 4609b0a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1323
-573
lines changed

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ let package = Package(
209209
condition: .when(platforms: [.macOS])
210210
),
211211
],
212-
exclude: ["__Snapshots__"]
212+
exclude: ["__Snapshots__", "RenderingTests/__Snapshots__"]
213213
),
214214
]
215215
)

Sources/TokamakCore/Shapes/Shape.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public extension Shape {
3434
}
3535

3636
public extension ShapeStyle where Self: View, Self.Body == _ShapeView<Rectangle, Self> {
37-
var body: some View {
37+
var body: Body {
3838
_ShapeView(shape: Rectangle(), style: self)
3939
}
4040
}

Sources/TokamakCore/Shapes/ShapeStyles/BackgroundStyle.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,12 @@ public extension View {
7575

7676
public func modifyEnvironment(_ values: inout EnvironmentValues) {
7777
values._backgroundStyle = .init(
78-
styles: (primary: style, secondary: style, tertiary: style),
78+
styles: (primary: style, secondary: style, tertiary: style, quaternary: style),
7979
environment: values
8080
)
8181
}
8282
}
83+
84+
public extension ShapeStyle where Self == BackgroundStyle {
85+
static var background: Self { .init() }
86+
}

Sources/TokamakCore/Shapes/ShapeStyles/ContentStyles.swift

-104
This file was deleted.

Sources/TokamakCore/Shapes/ShapeStyles/ForegroundStyle.swift

+11-3
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ public extension View {
7171
}
7272
}
7373

74-
@frozen public struct _ForegroundStyleModifier<Primary, Secondary, Tertiary>: ViewModifier,
75-
EnvironmentModifier
74+
@frozen public struct _ForegroundStyleModifier<
75+
Primary, Secondary, Tertiary
76+
>: ViewModifier, EnvironmentModifier
7677
where Primary: ShapeStyle, Secondary: ShapeStyle, Tertiary: ShapeStyle
7778
{
7879
public var primary: Primary
@@ -90,6 +91,13 @@ public extension View {
9091

9192
public typealias Body = Never
9293
public func modifyEnvironment(_ values: inout EnvironmentValues) {
93-
values._foregroundStyle = .init(styles: (primary, secondary, tertiary), environment: values)
94+
values._foregroundStyle = .init(
95+
styles: (primary, secondary, tertiary, tertiary),
96+
environment: values
97+
)
9498
}
9599
}
100+
101+
public extension ShapeStyle where Self == ForegroundStyle {
102+
static var foreground: Self { .init() }
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// Copyright 2020-2021 Tokamak contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
// Created by Carson Katri on 8/7/21.
16+
//
17+
18+
import Foundation
19+
20+
@frozen public struct AngularGradient: ShapeStyle, View {
21+
internal var gradient: Gradient
22+
internal var center: UnitPoint
23+
internal var startAngle: Angle
24+
internal var endAngle: Angle
25+
26+
public init(
27+
gradient: Gradient,
28+
center: UnitPoint,
29+
startAngle: Angle = .zero,
30+
endAngle: Angle = .zero
31+
) {
32+
self.gradient = gradient
33+
self.center = center
34+
self.startAngle = startAngle
35+
self.endAngle = endAngle
36+
}
37+
38+
public init(colors: [Color], center: UnitPoint, startAngle: Angle, endAngle: Angle) {
39+
self.init(
40+
gradient: Gradient(colors: colors),
41+
center: center,
42+
startAngle: startAngle,
43+
endAngle: endAngle
44+
)
45+
}
46+
47+
public init(stops: [Gradient.Stop], center: UnitPoint, startAngle: Angle, endAngle: Angle) {
48+
self.init(
49+
gradient: Gradient(stops: stops),
50+
center: center,
51+
startAngle: startAngle,
52+
endAngle: endAngle
53+
)
54+
}
55+
56+
public init(gradient: Gradient, center: UnitPoint, angle: Angle = .zero) {
57+
self.init(gradient: gradient, center: center, startAngle: angle, endAngle: angle)
58+
}
59+
60+
public init(colors: [Color], center: UnitPoint, angle: Angle = .zero) {
61+
self.init(
62+
gradient: Gradient(colors: colors),
63+
center: center,
64+
angle: angle
65+
)
66+
}
67+
68+
public init(stops: [Gradient.Stop], center: UnitPoint, angle: Angle = .zero) {
69+
self.init(
70+
gradient: Gradient(stops: stops),
71+
center: center,
72+
angle: angle
73+
)
74+
}
75+
76+
public typealias Body = _ShapeView<Rectangle, Self>
77+
78+
public func _apply(to shape: inout _ShapeStyle_Shape) {
79+
shape.result = .resolved(
80+
.gradient(
81+
gradient,
82+
style: .angular(center: center, startAngle: startAngle, endAngle: endAngle)
83+
)
84+
)
85+
}
86+
87+
public static func _apply(to type: inout _ShapeStyle_ShapeType) {}
88+
}
89+
90+
public extension ShapeStyle where Self == AngularGradient {
91+
static func angularGradient(
92+
_ gradient: Gradient,
93+
center: UnitPoint,
94+
startAngle: Angle,
95+
endAngle: Angle
96+
) -> AngularGradient {
97+
.init(
98+
gradient: gradient, center: center,
99+
startAngle: startAngle, endAngle: endAngle
100+
)
101+
}
102+
103+
static func angularGradient(
104+
colors: [Color],
105+
center: UnitPoint,
106+
startAngle: Angle,
107+
endAngle: Angle
108+
) -> AngularGradient {
109+
.init(
110+
colors: colors, center: center,
111+
startAngle: startAngle, endAngle: endAngle
112+
)
113+
}
114+
115+
static func angularGradient(
116+
stops: [Gradient.Stop],
117+
center: UnitPoint,
118+
startAngle: Angle,
119+
endAngle: Angle
120+
) -> AngularGradient {
121+
.init(
122+
stops: stops, center: center,
123+
startAngle: startAngle, endAngle: endAngle
124+
)
125+
}
126+
}
127+
128+
public extension ShapeStyle where Self == AngularGradient {
129+
static func conicGradient(
130+
_ gradient: Gradient,
131+
center: UnitPoint,
132+
angle: Angle = .zero
133+
) -> AngularGradient {
134+
.init(gradient: gradient, center: center, angle: angle)
135+
}
136+
137+
static func conicGradient(
138+
colors: [Color],
139+
center: UnitPoint,
140+
angle: Angle = .zero
141+
) -> AngularGradient {
142+
.init(colors: colors, center: center, angle: angle)
143+
}
144+
145+
static func conicGradient(
146+
stops: [Gradient.Stop],
147+
center: UnitPoint,
148+
angle: Angle = .zero
149+
) -> AngularGradient {
150+
.init(stops: stops, center: center, angle: angle)
151+
}
152+
}

0 commit comments

Comments
 (0)