Skip to content

Commit 76a7c84

Browse files
committed
Add Padding and TappablePadding
1 parent 4318efe commit 76a7c84

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

Package.swift

+9
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: [
@@ -81,6 +88,7 @@ let package = Package(
8188
],
8289
products: [
8390
.library(name: "OpenSwiftUI", targets: ["OpenSwiftUI"]),
91+
.library(name: "OpenSwiftUIExtension", targets: ["OpenSwiftUIExtension"]),
8492
],
8593
targets: [
8694
// TODO: Add SwiftGTK as an backend alternative for UIKit/AppKit on Linux and macOS
@@ -101,6 +109,7 @@ let package = Package(
101109
),
102110
.binaryTarget(name: "CoreServices", path: "PrivateFrameworks/CoreServices.xcframework"),
103111
openSwiftUITarget,
112+
openSwiftUIExtensionTarget,
104113
]
105114
)
106115

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

0 commit comments

Comments
 (0)