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 scaleEffect modifier #424

Merged
merged 5 commits into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
56 changes: 56 additions & 0 deletions Sources/TokamakCore/Modifiers/Effects/ScaleEffect.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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/9/21.
//

import Foundation

@frozen public struct _ScaleEffect: GeometryEffect, Equatable {
public var scale: CGSize
public var anchor: UnitPoint

@inlinable
public init(scale: CGSize, anchor: UnitPoint = .center) {
self.scale = scale
self.anchor = anchor
}

public func effectValue(size: CGSize) -> ProjectionTransform {
.init(.init(scaleX: scale.width, y: scale.height))
}

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

public extension View {
@inlinable
func scaleEffect(_ scale: CGSize, anchor: UnitPoint = .center) -> some View {
modifier(_ScaleEffect(scale: scale, anchor: anchor))
}

@inlinable
func scaleEffect(_ s: CGFloat, anchor: UnitPoint = .center) -> some View {
scaleEffect(CGSize(width: s, height: s), anchor: anchor)
}

@inlinable
func scaleEffect(x: CGFloat = 1.0, y: CGFloat = 1.0,
anchor: UnitPoint = .center) -> some View
{
scaleEffect(CGSize(width: x, height: y), anchor: anchor)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import TokamakCore

extension _RotationEffect: DOMViewModifier {
public var attributes: [HTMLAttribute: String] {
["style": "transform: rotate(\(angle.degrees)deg)"]
[
"style": """
transform: rotate(\(angle.degrees)deg);
transform-origin: \(anchor.cssValue);
""",
]
}
}
29 changes: 29 additions & 0 deletions Sources/TokamakStaticHTML/Modifiers/Effects/ScaleEffect.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2020 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/9/21.
//

import TokamakCore

extension _ScaleEffect: DOMViewModifier {
public var attributes: [HTMLAttribute: String] {
[
"style": """
transform: scale(\(scale.width), \(scale.height));
transform-origin: \(anchor.cssValue);
""",
]
}
}
6 changes: 6 additions & 0 deletions Sources/TokamakStaticHTML/Tokens/Tokens.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ extension GridItem: CustomStringConvertible {
}
}
}

extension UnitPoint {
var cssValue: String {
"\(x * 100)% \((1 - y) * 100)%"
}
}
48 changes: 48 additions & 0 deletions Tests/TokamakStaticHTMLTests/RenderingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,54 @@ final class RenderingTests: XCTestCase {
timeout: defaultSnapshotTimeout
)
}

func testScaleEffect() {
assertSnapshot(
matching: ZStack {
Circle()
.fill(Color.red)
.frame(width: 50, height: 50)
.scaleEffect(2)
.opacity(0.5)
Circle()
.fill(Color.blue)
.frame(width: 50, height: 50)
.opacity(0.5)
},
as: .image(size: .init(width: 100, height: 100)),
timeout: defaultSnapshotTimeout
)
}

func testAnchoredModifiers() {
assertSnapshot(
matching: ZStack {
Circle()
.fill(Color.red)
.frame(width: 50, height: 50)
.scaleEffect(2, anchor: .topLeading)
.opacity(0.5)
Circle()
.fill(Color.blue)
.frame(width: 50, height: 50)
.scaleEffect(2, anchor: .center)
.opacity(0.5)

Rectangle()
.fill(Color.red)
.frame(width: 50, height: 50)
.rotationEffect(.degrees(45), anchor: .topLeading)
.opacity(0.5)
Rectangle()
.fill(Color.blue)
.frame(width: 50, height: 50)
.rotationEffect(.degrees(45), anchor: .center)
.opacity(0.5)
},
as: .image(size: .init(width: 200, height: 200)),
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.