Skip to content

Commit ec4ccd0

Browse files
authored
Add Padding and TappablePadding (#83)
1 parent 4318efe commit ec4ccd0

File tree

3 files changed

+148
-1
lines changed

3 files changed

+148
-1
lines changed

Package.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ let openSwiftUITarget = Target.target(
4242
],
4343
swiftSettings: sharedSwiftSettings
4444
)
45+
let openSwiftUIExtensionTarget = Target.target(
46+
name: "OpenSwiftUIExtension",
47+
dependencies: [
48+
"OpenSwiftUI",
49+
],
50+
swiftSettings: sharedSwiftSettings
51+
)
4552
let openSwiftUITestTarget = Target.testTarget(
4653
name: "OpenSwiftUITests",
4754
dependencies: [
@@ -80,7 +87,7 @@ let package = Package(
8087
.visionOS(.v1),
8188
],
8289
products: [
83-
.library(name: "OpenSwiftUI", targets: ["OpenSwiftUI"]),
90+
.library(name: "OpenSwiftUI", targets: ["OpenSwiftUI", "OpenSwiftUIExtension"]),
8491
],
8592
targets: [
8693
// TODO: Add SwiftGTK as an backend alternative for UIKit/AppKit on Linux and macOS
@@ -101,6 +108,7 @@ let package = Package(
101108
),
102109
.binaryTarget(name: "CoreServices", path: "PrivateFrameworks/CoreServices.xcframework"),
103110
openSwiftUITarget,
111+
openSwiftUIExtensionTarget,
104112
]
105113
)
106114

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// PaddingLayout.swift
3+
// OpenSwiftUI
4+
//
5+
// Audited for RELEASE_2021
6+
// Status: WIP
7+
8+
import Foundation
9+
10+
@frozen
11+
public struct _PaddingLayout: /* UnaryLayout, */ Animatable, PrimitiveViewModifier /* , MultiViewModifier */ {
12+
public var edges: Edge.Set
13+
public var insets: EdgeInsets?
14+
15+
@inlinable
16+
public init(edges: Edge.Set = .all, insets: EdgeInsets?) {
17+
self.edges = edges
18+
self.insets = insets
19+
}
20+
21+
public typealias AnimatableData = EmptyAnimatableData
22+
public typealias Body = Never
23+
}
24+
25+
extension View {
26+
@inlinable
27+
public func padding(_ insets: EdgeInsets) -> some View {
28+
modifier(_PaddingLayout(insets: insets))
29+
}
30+
31+
@inlinable
32+
public func padding(_ edges: Edge.Set = .all, _ length: CGFloat? = nil) -> some View {
33+
let insets = length.map { EdgeInsets(_all: $0) }
34+
return modifier(_PaddingLayout(edges: edges, insets: insets))
35+
}
36+
37+
@inlinable
38+
public func padding(_ length: CGFloat) -> some View {
39+
padding(.all, length)
40+
}
41+
42+
public func _tightPadding() -> some View {
43+
padding(8.0)
44+
}
45+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//
2+
// TappablePadding.swift
3+
// OpenSwiftUIExtension
4+
5+
import Foundation
6+
7+
// FIXME: import OpenSwiftUI
8+
#if canImport(SwiftUI)
9+
import SwiftUI
10+
11+
struct TappablePadding: ViewModifier {
12+
let edges: Edge.Set
13+
let insets: EdgeInsets?
14+
15+
let perform: () -> Void
16+
17+
init(edges: Edge.Set = .all, insets: EdgeInsets?, perform: @escaping () -> Void) {
18+
self.edges = edges
19+
self.insets = insets
20+
self.perform = perform
21+
}
22+
23+
private var insetsValue: EdgeInsets {
24+
EdgeInsets(
25+
top: edges.contains(.top) ? insets?.top ?? .zero : .zero,
26+
leading: edges.contains(.leading) ? insets?.leading ?? .zero : .zero,
27+
bottom: edges.contains(.bottom) ? insets?.bottom ?? .zero : .zero,
28+
trailing: edges.contains(.trailing) ? insets?.trailing ?? .zero : .zero
29+
)
30+
}
31+
32+
func body(content: Content) -> some View {
33+
content
34+
.padding(insetsValue)
35+
.contentShape(Rectangle())
36+
.onTapGesture(perform: perform)
37+
.padding(insetsValue.inverted)
38+
}
39+
}
40+
41+
extension EdgeInsets {
42+
var inverted: EdgeInsets {
43+
.init(top: -top, leading: -leading, bottom: -bottom, trailing: -trailing)
44+
}
45+
46+
init(_all all: CGFloat) {
47+
self.init(top: all, leading: all, bottom: all, trailing: all)
48+
}
49+
}
50+
51+
extension View {
52+
public func tappablePadding(
53+
_ insets: EdgeInsets,
54+
perform: @escaping () -> Void
55+
) -> some View {
56+
modifier(TappablePadding(insets: insets, perform: perform))
57+
}
58+
59+
public func tappablePadding(
60+
_ edges: Edge.Set = .all,
61+
_ length: CGFloat?,
62+
perform: @escaping () -> Void
63+
) -> some View {
64+
let insets = length.map { EdgeInsets(_all: $0) }
65+
return modifier(TappablePadding(edges: edges, insets: insets, perform: perform))
66+
}
67+
68+
public func tappablePadding(
69+
_ length: CGFloat,
70+
perform: @escaping () -> Void
71+
) -> some View {
72+
tappablePadding(.all, length, perform: perform)
73+
}
74+
}
75+
76+
@available(iOS 15, macOS 12, *)
77+
#Preview {
78+
HStack(spacing: 20) {
79+
Text("Test 1")
80+
.background { Color.yellow }
81+
.padding(EdgeInsets())
82+
.tappablePadding(.all, 20.0) {
83+
print("Test 1")
84+
}
85+
Text("Test 2")
86+
.background { Color.red }
87+
.onTapGesture {
88+
print("Test 2")
89+
}
90+
}
91+
.padding(20)
92+
.background { Color.blue }
93+
}
94+
#endif

0 commit comments

Comments
 (0)