-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97472f1
commit 598b0f3
Showing
3 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// | ||
// Copyright (c) Nathan Tannar | ||
// | ||
|
||
import SwiftUI | ||
|
||
/// A custom parameter attribute that constructs an array from closures. | ||
@frozen | ||
@resultBuilder | ||
public struct ArrayBuilder<Element> { | ||
|
||
@inlinable | ||
public static func buildBlock() -> [Optional<Text>] { | ||
[] | ||
} | ||
|
||
@inlinable | ||
public static func buildPartialBlock( | ||
first: [Optional<Text>] | ||
) -> [Optional<Text>] { | ||
first | ||
} | ||
|
||
@inlinable | ||
public static func buildPartialBlock( | ||
accumulated: [Optional<Text>], | ||
next: [Optional<Text>] | ||
) -> [Optional<Text>] { | ||
accumulated + next | ||
} | ||
|
||
@inlinable | ||
public static func buildExpression( | ||
_ expression: Text | ||
) -> [Optional<Text>] { | ||
[expression] | ||
} | ||
|
||
@inlinable | ||
public static func buildEither( | ||
first component: [Optional<Text>] | ||
) -> [Optional<Text>] { | ||
component | ||
} | ||
|
||
@inlinable | ||
public static func buildEither( | ||
second component: [Optional<Text>] | ||
) -> [Optional<Text>] { | ||
component | ||
} | ||
|
||
@inlinable | ||
public static func buildOptional( | ||
_ component: [Optional<Text>]? | ||
) -> [Optional<Text>] { | ||
component ?? [] | ||
} | ||
|
||
@inlinable | ||
public static func buildLimitedAvailability( | ||
_ component: [Optional<Text>] | ||
) -> [Optional<Text>] { | ||
component | ||
} | ||
|
||
@inlinable | ||
public static func buildArray( | ||
_ components: [Optional<Text>] | ||
) -> [Optional<Text>] { | ||
components | ||
} | ||
|
||
@inlinable | ||
public static func buildBlock( | ||
_ components: [Optional<Text>]... | ||
) -> [Optional<Text>] { | ||
components.flatMap { $0 } | ||
} | ||
|
||
public static func buildFinalResult( | ||
_ component: [Optional<Text>] | ||
) -> [Text] { | ||
component.compactMap { $0 } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Nathan Tannar on 2023-04-21. | ||
// | ||
|
||
import Foundation |
114 changes: 114 additions & 0 deletions
114
Sources/Turbocharger/Sources/ViewModifier/ScaledFrame.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// | ||
// Copyright (c) Nathan Tannar | ||
// | ||
|
||
import SwiftUI | ||
|
||
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *) | ||
public struct ScaledFrameModifier: ViewModifier { | ||
var width: CGFloat? | ||
var height: CGFloat? | ||
var alignment: Alignment | ||
@ScaledMetric var scale: CGFloat | ||
|
||
public init( | ||
width: CGFloat? = nil, | ||
height: CGFloat? = nil, | ||
alignment: Alignment, | ||
relativeTo textSyle: Font.TextStyle | ||
) { | ||
self.width = width | ||
self.height = height | ||
self.alignment = alignment | ||
self._scale = ScaledMetric(wrappedValue: 1, relativeTo: textSyle) | ||
} | ||
|
||
public func body(content: Content) -> some View { | ||
content.frame( | ||
width: width?.scaled(by: scale), | ||
height: height?.scaled(by: scale), | ||
alignment: alignment | ||
) | ||
} | ||
} | ||
|
||
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *) | ||
public struct ScaledBoundsModifier: ViewModifier { | ||
var minWidth: CGFloat? | ||
var maxWidth: CGFloat? | ||
var minHeight: CGFloat? | ||
var maxHeight: CGFloat? | ||
var alignment: Alignment | ||
@ScaledMetric var scale: CGFloat | ||
|
||
public init( | ||
minWidth: CGFloat? = nil, | ||
maxWidth: CGFloat? = nil, | ||
minHeight: CGFloat? = nil, | ||
maxHeight: CGFloat? = nil, | ||
alignment: Alignment, | ||
relativeTo textSyle: Font.TextStyle | ||
) { | ||
self.minWidth = minWidth | ||
self.maxWidth = maxWidth | ||
self.minHeight = minHeight | ||
self.maxHeight = maxHeight | ||
self.alignment = alignment | ||
self._scale = ScaledMetric(wrappedValue: 1, relativeTo: textSyle) | ||
} | ||
|
||
public func body(content: Content) -> some View { | ||
content.frame( | ||
minWidth: minWidth?.scaled(by: scale), | ||
maxWidth: maxWidth?.scaled(by: scale), | ||
minHeight: minHeight?.scaled(by: scale), | ||
maxHeight: maxHeight?.scaled(by: scale), | ||
alignment: alignment | ||
) | ||
} | ||
} | ||
|
||
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *) | ||
extension View { | ||
public func frame(_ size: CGFloat?, relativeTo textSyle: Font.TextStyle) -> some View { | ||
frame(width: size, height: size, alignment: .center, relativeTo: textSyle) | ||
} | ||
|
||
public func frame( | ||
width: CGFloat? = nil, | ||
height: CGFloat? = nil, | ||
alignment: Alignment = .center, | ||
relativeTo textSyle: Font.TextStyle | ||
) -> some View { | ||
modifier(ScaledFrameModifier(width: height, height: width, alignment: alignment, relativeTo: textSyle)) | ||
} | ||
|
||
public func frame( | ||
minWidth: CGFloat? = nil, | ||
maxWidth: CGFloat? = nil, | ||
minHeight: CGFloat? = nil, | ||
maxHeight: CGFloat? = nil, | ||
alignment: Alignment = .center, | ||
relativeTo textSyle: Font.TextStyle | ||
) -> some View { | ||
modifier(ScaledBoundsModifier(minWidth: minWidth, maxWidth: maxWidth, minHeight: minHeight, maxHeight: maxHeight, alignment: alignment, relativeTo: textSyle)) | ||
} | ||
} | ||
|
||
// MARK: - Previews | ||
|
||
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) | ||
struct ScaledFrame_Previews: PreviewProvider { | ||
static var previews: some View { | ||
VStack { | ||
Circle() | ||
.fill(Color.yellow) | ||
.frame(width: 40, height: 40, relativeTo: .body) | ||
|
||
Circle() | ||
.fill(Color.yellow) | ||
.frame(width: 40, height: 40, relativeTo: .body) | ||
.dynamicTypeSize(.accessibility1) | ||
} | ||
} | ||
} |