Skip to content

Add Padding and TappablePadding #83

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

Merged
merged 1 commit into from
Apr 23, 2024
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
10 changes: 9 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ let openSwiftUITarget = Target.target(
],
swiftSettings: sharedSwiftSettings
)
let openSwiftUIExtensionTarget = Target.target(
name: "OpenSwiftUIExtension",
dependencies: [
"OpenSwiftUI",
],
swiftSettings: sharedSwiftSettings
)
let openSwiftUITestTarget = Target.testTarget(
name: "OpenSwiftUITests",
dependencies: [
Expand Down Expand Up @@ -80,7 +87,7 @@ let package = Package(
.visionOS(.v1),
],
products: [
.library(name: "OpenSwiftUI", targets: ["OpenSwiftUI"]),
.library(name: "OpenSwiftUI", targets: ["OpenSwiftUI", "OpenSwiftUIExtension"]),
],
targets: [
// TODO: Add SwiftGTK as an backend alternative for UIKit/AppKit on Linux and macOS
Expand All @@ -101,6 +108,7 @@ let package = Package(
),
.binaryTarget(name: "CoreServices", path: "PrivateFrameworks/CoreServices.xcframework"),
openSwiftUITarget,
openSwiftUIExtensionTarget,
]
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// PaddingLayout.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: WIP

import Foundation

@frozen
public struct _PaddingLayout: /* UnaryLayout, */ Animatable, PrimitiveViewModifier /* , MultiViewModifier */ {
public var edges: Edge.Set
public var insets: EdgeInsets?

@inlinable
public init(edges: Edge.Set = .all, insets: EdgeInsets?) {
self.edges = edges
self.insets = insets
}

public typealias AnimatableData = EmptyAnimatableData
public typealias Body = Never
}

extension View {
@inlinable
public func padding(_ insets: EdgeInsets) -> some View {
modifier(_PaddingLayout(insets: insets))
}

@inlinable
public func padding(_ edges: Edge.Set = .all, _ length: CGFloat? = nil) -> some View {
let insets = length.map { EdgeInsets(_all: $0) }
return modifier(_PaddingLayout(edges: edges, insets: insets))
}

@inlinable
public func padding(_ length: CGFloat) -> some View {
padding(.all, length)
}

public func _tightPadding() -> some View {
padding(8.0)
}
}
94 changes: 94 additions & 0 deletions Sources/OpenSwiftUIExtension/TappablePadding.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//
// TappablePadding.swift
// OpenSwiftUIExtension

import Foundation

// FIXME: import OpenSwiftUI
#if canImport(SwiftUI)
import SwiftUI

struct TappablePadding: ViewModifier {
let edges: Edge.Set
let insets: EdgeInsets?

let perform: () -> Void

init(edges: Edge.Set = .all, insets: EdgeInsets?, perform: @escaping () -> Void) {
self.edges = edges
self.insets = insets
self.perform = perform
}

private var insetsValue: EdgeInsets {
EdgeInsets(
top: edges.contains(.top) ? insets?.top ?? .zero : .zero,
leading: edges.contains(.leading) ? insets?.leading ?? .zero : .zero,
bottom: edges.contains(.bottom) ? insets?.bottom ?? .zero : .zero,
trailing: edges.contains(.trailing) ? insets?.trailing ?? .zero : .zero
)
}

func body(content: Content) -> some View {
content
.padding(insetsValue)
.contentShape(Rectangle())
.onTapGesture(perform: perform)
.padding(insetsValue.inverted)
}
}

extension EdgeInsets {
var inverted: EdgeInsets {
.init(top: -top, leading: -leading, bottom: -bottom, trailing: -trailing)
}

init(_all all: CGFloat) {
self.init(top: all, leading: all, bottom: all, trailing: all)
}
}

extension View {
public func tappablePadding(
_ insets: EdgeInsets,
perform: @escaping () -> Void
) -> some View {
modifier(TappablePadding(insets: insets, perform: perform))
}

public func tappablePadding(
_ edges: Edge.Set = .all,
_ length: CGFloat?,
perform: @escaping () -> Void
) -> some View {
let insets = length.map { EdgeInsets(_all: $0) }
return modifier(TappablePadding(edges: edges, insets: insets, perform: perform))
}

public func tappablePadding(
_ length: CGFloat,
perform: @escaping () -> Void
) -> some View {
tappablePadding(.all, length, perform: perform)
}
}

@available(iOS 15, macOS 12, *)
#Preview {
HStack(spacing: 20) {
Text("Test 1")
.background { Color.yellow }
.padding(EdgeInsets())
.tappablePadding(.all, 20.0) {
print("Test 1")
}
Text("Test 2")
.background { Color.red }
.onTapGesture {
print("Test 2")
}
}
.padding(20)
.background { Color.blue }
}
#endif
Loading