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
59 changes: 36 additions & 23 deletions Sources/OpenSwiftUICore/Shape/ShapeStyle/BlendMode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// BlendMode.swift
// OpenSwiftUICore
//
// Audited for 6.0.87
// Status: WIP
// Audited for 6.5.4
// Status: Complete

// MARK: - BlendMode

Expand Down Expand Up @@ -33,7 +33,9 @@ public enum BlendMode: Sendable {
case plusLighter
}

@available(OpenSwiftUI_v3_0, *)
extension ShapeStyle {

/// Returns a new style based on `self` that applies the specified
/// blend mode when drawing.
@inlinable
Expand All @@ -42,7 +44,9 @@ extension ShapeStyle {
}
}

@available(OpenSwiftUI_v4_0, *)
extension ShapeStyle where Self == AnyShapeStyle {

/// Returns a new style based on the current style that uses
/// `mode` as its blend mode when drawing.
///
Expand All @@ -64,42 +68,51 @@ extension ShapeStyle where Self == AnyShapeStyle {
}
}

@available(OpenSwiftUI_v3_0, *)
@frozen
public struct _BlendModeShapeStyle<Style>: ShapeStyle, PrimitiveShapeStyle where Style: ShapeStyle {
public var style: Style

public var blendMode: BlendMode
@inlinable public init(style: Style, blendMode: BlendMode) {

@inlinable
public init(style: Style, blendMode: BlendMode) {
self.style = style
self.blendMode = blendMode
}

public func _apply(to shape: inout _ShapeStyle_Shape) {
switch shape.operation {
case .fallbackColor, .modifyBackground:
case .prepareText:
if blendMode == .normal {
style._apply(to: &shape)
case .prepareText:
if blendMode == .normal {
style._apply(to: &shape)
} else {
shape.result = .preparedText(.foregroundKeyColor)
}
default:
_openSwiftUIUnimplementedFailure()
// case resolveStyle(name: _ShapeStyle_Name, levels: Range<Int>):
//
//
// case .multiLevel:
// <#code#>
// case .copyStyle(let name):
// <#code#>
// case .primaryStyle:
// <#code#>
} else {
shape.result = .preparedText(.foregroundKeyColor)
}
case let .resolveStyle(name: name, levels: levels):
style._apply(to: &shape)
let blend = GraphicsBlendMode(blendMode)
shape.stylePack.modify(
name: name,
levels: levels
) { style in
style.applyBlend(blend)
}
case .fallbackColor, .modifyBackground, .multiLevel:
style._apply(to: &shape)
case let .copyStyle(name: name):
style.mapCopiedStyle(in: &shape) { style in
_BlendModeShapeStyle<AnyShapeStyle>(
style: style,
blendMode: blendMode
)
}
case .primaryStyle:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the .primaryStyle operation, _BlendModeShapeStyle currently does nothing, which can cause ShapeStyle.primaryStyle(in:) to return nil even if the wrapped style provides a primary style (other wrappers like OffsetShapeStyle forward something here).

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎

break
}
}

public static func _apply(to type: inout _ShapeStyle_ShapeType) {
Style._apply(to: &type)
}

public typealias Resolved = Never
}
53 changes: 42 additions & 11 deletions Sources/OpenSwiftUICore/Shape/ShapeStyle/ShapeStylePack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ package struct _ShapeStyle_Pack: Equatable {
package struct Style: Equatable, Sendable {
package var fill: _ShapeStyle_Pack.Fill
package var opacity: Float
package var _blend: GraphicsBlendMode?
private var _blend: GraphicsBlendMode?

package var blend: GraphicsBlendMode {
_blend ?? .normal
}
Expand Down Expand Up @@ -122,7 +122,11 @@ package struct _ShapeStyle_Pack: Equatable {
}
}
}


private func indices(of name: ShapeStyle.Name) -> Range<Int> {
_openSwiftUIUnimplementedFailure()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indices(of:) currently calls _openSwiftUIUnimplementedFailure(), but _ShapeStyle_Pack.modify(...) now depends on it (and BlendMode calls modify in the .resolveStyle path), so using blendMode(_:) during style resolution will still trap at runtime.

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎

}

package subscript(name: _ShapeStyle_Name) -> _ShapeStyle_Pack.Slice {
Slice(pack: self, name: name)
}
Expand All @@ -148,8 +152,20 @@ package struct _ShapeStyle_Pack: Equatable {
}
}

package mutating func modify(name: _ShapeStyle_Name, levels: Range<Int>, _ modifier: (inout _ShapeStyle_Pack.Style) -> Void) {
_openSwiftUIUnimplementedFailure()
package mutating func modify(
name: ShapeStyle.Name,
levels: Range<Int>,
_ modifier: (inout ShapeStyle.Pack.Style) -> Void
) {
let indices = indices(of: name)
guard !indices.isEmpty else { return }
var modifiedStyles = styles
for index in indices {
if levels.contains(modifiedStyles[index].key.level) {
modifier(&modifiedStyles[index].style)
}
}
styles = modifiedStyles
}

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

package mutating func applyBlend(_ blend: GraphicsBlendMode) {
_openSwiftUIUnimplementedFailure()
let shouldApply = !_SemanticFeature_v6.isEnabled
if shouldApply || _blend == nil {
_blend = blend
}
for index in effects.indices {
if shouldApply || effects[index]._blend == nil {
effects[index]._blend = blend
}
}
}

package var color: Color.Resolved? {
_openSwiftUIUnimplementedFailure()
}
Expand All @@ -213,17 +237,24 @@ extension _ShapeStyle_Pack.Style {
// MARK: - _ShapeStyle_Shape + _ShapeStyle_Pack

extension _ShapeStyle_Shape {
package var stylePack: _ShapeStyle_Pack {
package var stylePack: ShapeStyle.Pack {
get {
switch result {
case let .pack(pack): pack
default: .defaultValue
}
}
_modify {
var pack = stylePack
yield &pack
result = .pack(pack)
var styles: ShapeStyle.Pack
switch result {
case let .pack(pack):
styles = pack
result = .none
default:
styles = .defaultValue
}
yield &styles
result = .pack(styles)
}
}
}
Loading