Skip to content

Commit bd514a0

Browse files
committed
Update BlendMode
1 parent 793b5ec commit bd514a0

File tree

2 files changed

+78
-34
lines changed

2 files changed

+78
-34
lines changed

Sources/OpenSwiftUICore/Shape/ShapeStyle/BlendMode.swift

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// BlendMode.swift
33
// OpenSwiftUICore
44
//
5-
// Audited for 6.0.87
6-
// Status: WIP
5+
// Audited for 6.5.4
6+
// Status: Complete
77

88
// MARK: - BlendMode
99

@@ -33,7 +33,9 @@ public enum BlendMode: Sendable {
3333
case plusLighter
3434
}
3535

36+
@available(OpenSwiftUI_v3_0, *)
3637
extension ShapeStyle {
38+
3739
/// Returns a new style based on `self` that applies the specified
3840
/// blend mode when drawing.
3941
@inlinable
@@ -42,7 +44,9 @@ extension ShapeStyle {
4244
}
4345
}
4446

47+
@available(OpenSwiftUI_v4_0, *)
4548
extension ShapeStyle where Self == AnyShapeStyle {
49+
4650
/// Returns a new style based on the current style that uses
4751
/// `mode` as its blend mode when drawing.
4852
///
@@ -64,42 +68,51 @@ extension ShapeStyle where Self == AnyShapeStyle {
6468
}
6569
}
6670

71+
@available(OpenSwiftUI_v3_0, *)
6772
@frozen
6873
public struct _BlendModeShapeStyle<Style>: ShapeStyle, PrimitiveShapeStyle where Style: ShapeStyle {
6974
public var style: Style
75+
7076
public var blendMode: BlendMode
71-
@inlinable public init(style: Style, blendMode: BlendMode) {
77+
78+
@inlinable
79+
public init(style: Style, blendMode: BlendMode) {
7280
self.style = style
7381
self.blendMode = blendMode
7482
}
7583

7684
public func _apply(to shape: inout _ShapeStyle_Shape) {
7785
switch shape.operation {
78-
case .fallbackColor, .modifyBackground:
86+
case .prepareText:
87+
if blendMode == .normal {
7988
style._apply(to: &shape)
80-
case .prepareText:
81-
if blendMode == .normal {
82-
style._apply(to: &shape)
83-
} else {
84-
shape.result = .preparedText(.foregroundKeyColor)
85-
}
86-
default:
87-
_openSwiftUIUnimplementedFailure()
88-
// case resolveStyle(name: _ShapeStyle_Name, levels: Range<Int>):
89-
//
90-
//
91-
// case .multiLevel:
92-
// <#code#>
93-
// case .copyStyle(let name):
94-
// <#code#>
95-
// case .primaryStyle:
96-
// <#code#>
89+
} else {
90+
shape.result = .preparedText(.foregroundKeyColor)
91+
}
92+
case let .resolveStyle(name: name, levels: levels):
93+
style._apply(to: &shape)
94+
let blend = GraphicsBlendMode(blendMode)
95+
shape.stylePack.modify(
96+
name: name,
97+
levels: levels
98+
) { style in
99+
style.applyBlend(blend)
100+
}
101+
case .fallbackColor, .modifyBackground, .multiLevel:
102+
style._apply(to: &shape)
103+
case let .copyStyle(name: name):
104+
style.mapCopiedStyle(in: &shape) { style in
105+
_BlendModeShapeStyle<AnyShapeStyle>(
106+
style: style,
107+
blendMode: blendMode
108+
)
109+
}
110+
case .primaryStyle:
111+
break
97112
}
98113
}
99114

100115
public static func _apply(to type: inout _ShapeStyle_ShapeType) {
101116
Style._apply(to: &type)
102117
}
103-
104-
public typealias Resolved = Never
105118
}

Sources/OpenSwiftUICore/Shape/ShapeStyle/ShapeStylePack.swift

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ package struct _ShapeStyle_Pack: Equatable {
1010
package struct Style: Equatable, Sendable {
1111
package var fill: _ShapeStyle_Pack.Fill
1212
package var opacity: Float
13-
package var _blend: GraphicsBlendMode?
14-
13+
private var _blend: GraphicsBlendMode?
14+
1515
package var blend: GraphicsBlendMode {
1616
_blend ?? .normal
1717
}
@@ -122,7 +122,11 @@ package struct _ShapeStyle_Pack: Equatable {
122122
}
123123
}
124124
}
125-
125+
126+
private func indices(of name: ShapeStyle.Name) -> Range<Int> {
127+
_openSwiftUIUnimplementedFailure()
128+
}
129+
126130
package subscript(name: _ShapeStyle_Name) -> _ShapeStyle_Pack.Slice {
127131
Slice(pack: self, name: name)
128132
}
@@ -148,8 +152,20 @@ package struct _ShapeStyle_Pack: Equatable {
148152
}
149153
}
150154

151-
package mutating func modify(name: _ShapeStyle_Name, levels: Range<Int>, _ modifier: (inout _ShapeStyle_Pack.Style) -> Void) {
152-
_openSwiftUIUnimplementedFailure()
155+
package mutating func modify(
156+
name: ShapeStyle.Name,
157+
levels: Range<Int>,
158+
_ modifier: (inout ShapeStyle.Pack.Style) -> Void
159+
) {
160+
let indices = indices(of: name)
161+
guard !indices.isEmpty else { return }
162+
var modifiedStyles = styles
163+
for index in indices {
164+
if levels.contains(modifiedStyles[index].key.level) {
165+
modifier(&modifiedStyles[index].style)
166+
}
167+
}
168+
styles = modifiedStyles
153169
}
154170

155171
package mutating func adjustLevelIndices(of name: _ShapeStyle_Name, by offset: Int) {
@@ -200,9 +216,17 @@ extension _ShapeStyle_Pack.Style {
200216
}
201217

202218
package mutating func applyBlend(_ blend: GraphicsBlendMode) {
203-
_openSwiftUIUnimplementedFailure()
219+
let shouldApply = !_SemanticFeature_v6.isEnabled
220+
if shouldApply || _blend == nil {
221+
_blend = blend
222+
}
223+
for index in effects.indices {
224+
if shouldApply || effects[index]._blend == nil {
225+
effects[index]._blend = blend
226+
}
227+
}
204228
}
205-
229+
206230
package var color: Color.Resolved? {
207231
_openSwiftUIUnimplementedFailure()
208232
}
@@ -213,17 +237,24 @@ extension _ShapeStyle_Pack.Style {
213237
// MARK: - _ShapeStyle_Shape + _ShapeStyle_Pack
214238

215239
extension _ShapeStyle_Shape {
216-
package var stylePack: _ShapeStyle_Pack {
240+
package var stylePack: ShapeStyle.Pack {
217241
get {
218242
switch result {
219243
case let .pack(pack): pack
220244
default: .defaultValue
221245
}
222246
}
223247
_modify {
224-
var pack = stylePack
225-
yield &pack
226-
result = .pack(pack)
248+
var styles: ShapeStyle.Pack
249+
switch result {
250+
case let .pack(pack):
251+
styles = pack
252+
result = .none
253+
default:
254+
styles = .defaultValue
255+
}
256+
yield &styles
257+
result = .pack(styles)
227258
}
228259
}
229260
}

0 commit comments

Comments
 (0)