From 4a7748ad6bc49ff27ca9fba3d026fe3f3adcba15 Mon Sep 17 00:00:00 2001 From: Carson Katri Date: Wed, 7 Jul 2021 18:09:13 -0400 Subject: [PATCH] Add `Material` to the HTML renderer (#418) --- .../Modifiers/StyleModifiers.swift | 34 ++++++- .../Shapes/ShapeStyles/BackgroundStyle.swift | 79 ++++++++++++++++ .../Shapes/ShapeStyles/ForegroundStyle.swift | 53 +++++++++++ .../Shapes/ShapeStyles/Material.swift | 57 ++++++++++++ .../Shapes/ShapeStyles/ShapeStyle.swift | 58 +----------- Sources/TokamakDOM/Core.swift | 3 + Sources/TokamakDemo/ShapeStyleDemo.swift | 85 ++++++++++++++---- Sources/TokamakStaticHTML/Core.swift | 4 +- .../Modifiers/ViewModifier.swift | 7 -- .../Modifiers/_BackgroundShapeModifier.swift | 24 +++++ .../Modifiers/_BackgroundStyleModifier.swift | 57 ++++++++++++ .../RenderingTests.swift | 23 +++++ .../RenderingTests/testMaterial.1.png | Bin 0 -> 8097 bytes 13 files changed, 400 insertions(+), 84 deletions(-) create mode 100644 Sources/TokamakCore/Shapes/ShapeStyles/BackgroundStyle.swift create mode 100644 Sources/TokamakCore/Shapes/ShapeStyles/Material.swift create mode 100644 Sources/TokamakStaticHTML/Modifiers/_BackgroundShapeModifier.swift create mode 100644 Sources/TokamakStaticHTML/Modifiers/_BackgroundStyleModifier.swift create mode 100644 Tests/TokamakStaticHTMLTests/__Snapshots__/RenderingTests/testMaterial.1.png 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