Skip to content

Commit

Permalink
Add HTML implementation for opacity modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxDesiatov committed Jul 4, 2021
1 parent ff5335c commit a028f18
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 1 deletion.
34 changes: 34 additions & 0 deletions Sources/TokamakCore/Modifiers/Effects/OpacityEffect.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 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 1/20/21.
//

public struct _OpacityEffect: ViewModifier, Equatable {
public let opacity: Double

public init(opacity: Double) {
self.opacity = opacity
}

public func body(content: Content) -> some View {
content
}
}

public extension View {
func opacity(_ opacity: Double) -> some View {
modifier(_OpacityEffect(opacity: opacity))
}
}
24 changes: 24 additions & 0 deletions Sources/TokamakStaticHTML/Modifiers/Effects/OpacityEffect.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 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 1/20/21.
//

import TokamakCore

extension _OpacityEffect: DOMViewModifier {
public var attributes: [HTMLAttribute: String] {
["style": "opacity: \(opacity)"]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public extension Snapshotting where Value: View, Format == NSImage {
}
}

private let defaultSnapshotTimeout: TimeInterval = 10

struct Star: Shape {
func path(in rect: CGRect) -> Path {
Path { path in
Expand Down Expand Up @@ -107,7 +109,24 @@ struct Stacks: View {
}
}

private let defaultSnapshotTimeout: TimeInterval = 10
struct Opacity: View {
var body: some View {
ZStack {
Circle()
.fill(Color.red)
.opacity(0.5)
.frame(width: 25, height: 25)
Circle()
.fill(Color.green)
.opacity(0.5)
.frame(width: 50, height: 50)
Circle()
.fill(Color.blue)
.opacity(0.5)
.frame(width: 75, height: 75)
}
}
}

final class LayoutTests: XCTestCase {
func testPath() {
Expand Down Expand Up @@ -139,6 +158,14 @@ final class LayoutTests: XCTestCase {
timeout: defaultSnapshotTimeout
)
}

func testOpacity() {
assertSnapshot(
matching: Opacity().preferredColorScheme(.light),
as: .image(size: .init(width: 75, height: 75)),
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.

0 comments on commit a028f18

Please sign in to comment.