Skip to content

Commit

Permalink
Fix background/overlay layout in DOM/HTML renderers (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
carson-katri committed Jul 17, 2021
1 parent 12a6256 commit 6792dbb
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 10 deletions.
38 changes: 28 additions & 10 deletions Sources/TokamakCore/Modifiers/StyleModifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@

import Foundation

/// Override this View's body to provide a layout that fits the background to the content.
public struct _BackgroundLayout<Content, Background>: _PrimitiveView
where Content: View, Background: View
{
public let content: Content
public let background: Background
public let alignment: Alignment
}

public struct _BackgroundModifier<Background>: ViewModifier, EnvironmentReader
where Background: View
{
Expand All @@ -30,11 +39,11 @@ public struct _BackgroundModifier<Background>: ViewModifier, EnvironmentReader
}

public func body(content: Content) -> some View {
// FIXME: Clip to bounds of foreground.
ZStack(alignment: alignment) {
background
content
}
_BackgroundLayout(
content: content,
background: background,
alignment: alignment
)
}

mutating func setContent(from values: EnvironmentValues) {
Expand Down Expand Up @@ -113,6 +122,15 @@ public extension View {
}
}

/// Override this View's body to provide a layout that fits the background to the content.
public struct _OverlayLayout<Content, Overlay>: _PrimitiveView
where Content: View, Overlay: View
{
public let content: Content
public let overlay: Overlay
public let alignment: Alignment
}

public struct _OverlayModifier<Overlay>: ViewModifier, EnvironmentReader
where Overlay: View
{
Expand All @@ -126,11 +144,11 @@ public struct _OverlayModifier<Overlay>: ViewModifier, EnvironmentReader
}

public func body(content: Content) -> some View {
// FIXME: Clip to content shape.
ZStack(alignment: alignment) {
content
overlay
}
_OverlayLayout(
content: content,
overlay: overlay,
alignment: alignment
)
}

mutating func setContent(from values: EnvironmentValues) {
Expand Down
60 changes: 60 additions & 0 deletions Sources/TokamakStaticHTML/Modifiers/LayoutModifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,63 @@ extension _AspectRatioLayout: DOMViewModifier {
]
}
}

extension _BackgroundLayout: _HTMLPrimitive {
public var renderedBody: AnyView {
AnyView(
HTML(
"div",
["style": "display: inline-grid; grid-template-columns: auto auto;"]
) {
HTML(
"div",
["style": """
display: flex;
justify-content: \(alignment.horizontal.flexAlignment);
align-items: \(alignment.vertical.flexAlignment);
grid-area: a;
width: 0; min-width: 100%;
height: 0; min-height: 100%;
overflow: hidden;
"""]
) {
background
}
HTML("div", ["style": "grid-area: a;"]) {
content
}
}
)
}
}

extension _OverlayLayout: _HTMLPrimitive {
public var renderedBody: AnyView {
AnyView(
HTML(
"div",
["style": "display: inline-grid; grid-template-columns: auto auto;"]
) {
HTML("div", ["style": "grid-area: a;"]) {
content
}
HTML(
"div",
["style": """
display: flex;
justify-content: \(alignment.horizontal.flexAlignment);
align-items: \(alignment.vertical.flexAlignment);
grid-area: a;
width: 0; min-width: 100%;
height: 0; min-height: 100%;
overflow: hidden;
"""]
) {
overlay
}
}
)
}
}
53 changes: 53 additions & 0 deletions Tests/TokamakStaticHTMLTests/RenderingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,59 @@ final class RenderingTests: XCTestCase {
timeout: defaultSnapshotTimeout
)
}

func testBackground() {
assertSnapshot(
matching: Rectangle()
.fill(Color.blue)
.opacity(0.5)
.frame(width: 80, height: 80)
.background(
RoundedRectangle(cornerRadius: 10).fill(Color.red)
),
as: .image(size: .init(width: 100, height: 100))
)

assertSnapshot(
matching: Rectangle()
.fill(Color.blue)
.opacity(0.5)
.frame(width: 80, height: 80)
.background(
RoundedRectangle(cornerRadius: 10)
.fill(Color.red)
.frame(width: 40, height: 40),
alignment: .bottomTrailing
),
as: .image(size: .init(width: 100, height: 100))
)
}

func testOverlay() {
assertSnapshot(
matching: Rectangle()
.fill(Color.blue)
.frame(width: 80, height: 80)
.overlay(
RoundedRectangle(cornerRadius: 10)
.fill(Color.red.opacity(0.5))
),
as: .image(size: .init(width: 100, height: 100))
)

assertSnapshot(
matching: Rectangle()
.fill(Color.blue)
.frame(width: 80, height: 80)
.overlay(
RoundedRectangle(cornerRadius: 10)
.fill(Color.red)
.frame(width: 40, height: 40),
alignment: .bottomTrailing
),
as: .image(size: .init(width: 100, height: 100))
)
}
}

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

0 comments on commit 6792dbb

Please sign in to comment.