diff --git a/Sources/TokamakCore/Modifiers/StyleModifiers.swift b/Sources/TokamakCore/Modifiers/StyleModifiers.swift index d2271a887..7d691a1b5 100644 --- a/Sources/TokamakCore/Modifiers/StyleModifiers.swift +++ b/Sources/TokamakCore/Modifiers/StyleModifiers.swift @@ -66,16 +66,42 @@ public extension View { ) -> some View where V: View { background(content(), alignment: alignment) } +} + +@frozen public struct _BackgroundShapeModifier: ViewModifier, EnvironmentReader + where Style: ShapeStyle, Bounds: Shape +{ + public var environment: EnvironmentValues! + + public var style: Style + public var shape: Bounds + public var fillStyle: FillStyle + @inlinable + public init(style: Style, shape: Bounds, fillStyle: FillStyle) { + self.style = style + self.shape = shape + self.fillStyle = fillStyle + } + + public func body(content: Content) -> some View { + content + .background(shape.fill(style, style: fillStyle)) + } + + public mutating func setContent(from values: EnvironmentValues) { + environment = values + } +} + +public extension View { @inlinable func background( _ style: S, in shape: T, fillStyle: FillStyle = FillStyle() ) -> some View where S: ShapeStyle, T: Shape { - background { - shape.fill(style, style: fillStyle) - } + modifier(_BackgroundShapeModifier(style: style, shape: shape, fillStyle: fillStyle)) } @inlinable @@ -83,7 +109,7 @@ public extension View { in shape: S, fillStyle: FillStyle = FillStyle() ) -> some View where S: Shape { - background(ForegroundStyle(), in: shape, fillStyle: fillStyle) + background(BackgroundStyle(), in: shape, fillStyle: fillStyle) } } diff --git a/Sources/TokamakCore/Shapes/ShapeStyles/BackgroundStyle.swift b/Sources/TokamakCore/Shapes/ShapeStyles/BackgroundStyle.swift new file mode 100644 index 000000000..c2dd4fe7a --- /dev/null +++ b/Sources/TokamakCore/Shapes/ShapeStyles/BackgroundStyle.swift @@ -0,0 +1,79 @@ +// Copyright 2020-2021 Tokamak contributors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// Created by Carson Katri on 7/6/21. +// + +public struct BackgroundStyle: ShapeStyle { + public init() {} + + public func _apply(to shape: inout _ShapeStyle_Shape) { + if let backgroundStyle = shape.environment._backgroundStyle { + backgroundStyle._apply(to: &shape) + } else { + shape.result = .none + } + } + + public static func _apply(to shape: inout _ShapeStyle_ShapeType) {} +} + +extension EnvironmentValues { + private struct BackgroundStyleKey: EnvironmentKey { + static let defaultValue: AnyShapeStyle? = nil + } + + public var _backgroundStyle: AnyShapeStyle? { + get { + self[BackgroundStyleKey.self] + } + set { + self[BackgroundStyleKey.self] = newValue + } + } +} + +public extension View { + @inlinable + func background() -> some View { + modifier(_BackgroundStyleModifier(style: BackgroundStyle())) + } + + @inlinable + func background(_ style: S) -> some View where S: ShapeStyle { + modifier(_BackgroundStyleModifier(style: style)) + } +} + +@frozen public struct _BackgroundStyleModifier