Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Primary/Secondary/Tertiary/QuaternaryContentStyle #419

Merged
merged 3 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
104 changes: 104 additions & 0 deletions Sources/TokamakCore/Shapes/ShapeStyles/ContentStyles.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// 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/7/21.
//

@frozen public struct PrimaryContentStyle {
@inlinable
public init() {}
}

extension PrimaryContentStyle: ShapeStyle {
public func _apply(to shape: inout _ShapeStyle_Shape) {
if !shape.inRecursiveStyle,
let foregroundStyle = shape.environment._foregroundStyle
{
if foregroundStyle.styles[0] is Self {
Copy link
Collaborator

@MaxDesiatov MaxDesiatov Jul 7, 2021

Choose a reason for hiding this comment

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

Do we have a certainty that foregroundStyle.styles is always non-empty? If so, maybe this needs a comment in code to explain that?

Also, this PR needs conflicts to be resolved 🙂

Copy link
Member Author

Choose a reason for hiding this comment

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

I replaced the array with a tuple to guarantee its non-empty.

shape.inRecursiveStyle = true
}
foregroundStyle.styles[0]._apply(to: &shape)
} else {
shape.result = .color(shape.environment.foregroundColor ?? .primary)
}
}

public static func _apply(to shape: inout _ShapeStyle_ShapeType) {}
}

@frozen public struct SecondaryContentStyle {
@inlinable
public init() {}
}

extension SecondaryContentStyle: ShapeStyle {
public func _apply(to shape: inout _ShapeStyle_Shape) {
if !shape.inRecursiveStyle,
let foregroundStyle = shape.environment._foregroundStyle
{
if foregroundStyle.styles[1] is Self {
shape.inRecursiveStyle = true
}
foregroundStyle.styles[1]._apply(to: &shape)
} else {
shape.result = .color((shape.environment.foregroundColor ?? .primary).opacity(0.5))
}
}

public static func _apply(to shape: inout _ShapeStyle_ShapeType) {}
}

@frozen public struct TertiaryContentStyle {
@inlinable
public init() {}
}

extension TertiaryContentStyle: ShapeStyle {
public func _apply(to shape: inout _ShapeStyle_Shape) {
if !shape.inRecursiveStyle,
let foregroundStyle = shape.environment._foregroundStyle
{
if foregroundStyle.styles[2] is Self {
shape.inRecursiveStyle = true
}
foregroundStyle.styles[2]._apply(to: &shape)
} else {
shape.result = .color((shape.environment.foregroundColor ?? .primary).opacity(0.3))
}
}

public static func _apply(to shape: inout _ShapeStyle_ShapeType) {}
}

@frozen public struct QuaternaryContentStyle {
@inlinable
public init() {}
}

extension QuaternaryContentStyle: ShapeStyle {
public func _apply(to shape: inout _ShapeStyle_Shape) {
if !shape.inRecursiveStyle,
let foregroundStyle = shape.environment._foregroundStyle
{
if foregroundStyle.styles[2] is Self {
shape.inRecursiveStyle = true
}
foregroundStyle.styles[2]._apply(to: &shape)
} else {
shape.result = .color((shape.environment.foregroundColor ?? .primary).opacity(0.2))
}
}

public static func _apply(to shape: inout _ShapeStyle_ShapeType) {}
}
18 changes: 18 additions & 0 deletions Sources/TokamakCore/Shapes/ShapeStyles/ShapeStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,25 @@ public extension View {
}

public typealias Body = Never

public func modifyEnvironment(_ values: inout EnvironmentValues) {
var styles = self.styles
// Passthrough inert styles.
if styles[0] is PrimaryContentStyle {
styles[0] = values._foregroundStyle?.styles[0] ?? styles[0]
}
if styles[1] is SecondaryContentStyle {
styles[1] = values._foregroundStyle?.styles[1] ?? styles[1]
}
if styles[2] is TertiaryContentStyle {
styles[2] = values._foregroundStyle?.styles[2] ?? styles[2]
}
if styles[0] is QuaternaryContentStyle,
styles[1] is QuaternaryContentStyle,
styles[2] is QuaternaryContentStyle
{
styles[2] = values._foregroundStyle?.styles[2] ?? styles[2]
}
values._foregroundStyle = .init(styles: styles, environment: values)
}
}
7 changes: 7 additions & 0 deletions Sources/TokamakDOM/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ public typealias Rectangle = TokamakCore.Rectangle
public typealias RoundedRectangle = TokamakCore.RoundedRectangle
public typealias ContainerRelativeShape = TokamakCore.ContainerRelativeShape

// MARK: Shape Styles

public typealias PrimaryContentStyle = TokamakCore.PrimaryContentStyle
public typealias SecondaryContentStyle = TokamakCore.SecondaryContentStyle
public typealias TertiaryContentStyle = TokamakCore.TertiaryContentStyle
public typealias QuaternaryContentStyle = TokamakCore.QuaternaryContentStyle

// MARK: Primitive values

public typealias Color = TokamakCore.Color
Expand Down
12 changes: 12 additions & 0 deletions Sources/TokamakDemo/ShapeStyleDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ struct ShapeStyleDemo: View {
}
.foregroundStyle(Color.blue)
}
HStack {
VStack {
Text("Primary")
.foregroundStyle(PrimaryContentStyle())
Text("Secondary")
.foregroundStyle(SecondaryContentStyle())
Text("Tertiary")
.foregroundStyle(TertiaryContentStyle())
Text("Quaternary")
.foregroundStyle(QuaternaryContentStyle())
}
}
#endif
}
}
7 changes: 7 additions & 0 deletions Sources/TokamakStaticHTML/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ public typealias RoundedRectangle = TokamakCore.RoundedRectangle

public typealias ContainerRelativeShape = TokamakCore.ContainerRelativeShape

// MARK: Shape Styles

public typealias PrimaryContentStyle = TokamakCore.PrimaryContentStyle
public typealias SecondaryContentStyle = TokamakCore.SecondaryContentStyle
public typealias TertiaryContentStyle = TokamakCore.TertiaryContentStyle
public typealias QuaternaryContentStyle = TokamakCore.QuaternaryContentStyle

// MARK: Primitive values

public typealias Color = TokamakCore.Color
Expand Down
22 changes: 22 additions & 0 deletions Tests/TokamakStaticHTMLTests/RenderingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,28 @@ final class RenderingTests: XCTestCase {
timeout: defaultSnapshotTimeout
)
}

func testContentStyles() {
assertSnapshot(
matching: HStack {
Rectangle()
.frame(width: 50, height: 50)
.foregroundStyle(PrimaryContentStyle())
Rectangle()
.frame(width: 50, height: 50)
.foregroundStyle(SecondaryContentStyle())
Rectangle()
.frame(width: 50, height: 50)
.foregroundStyle(TertiaryContentStyle())
Rectangle()
.frame(width: 50, height: 50)
.foregroundStyle(QuaternaryContentStyle())
}
.foregroundColor(.blue),
as: .image(size: .init(width: 275, height: 100)),
timeout: defaultSnapshotTimeout
)
}
}

#endif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.