Skip to content

Instantly share code, notes, and snippets.

View Amzd's full-sized avatar
🚀

Casper Zandbergen Amzd

🚀
View GitHub Profile
@Amzd
Amzd / AppendingClosures.swift
Last active May 16, 2023 19:50
Appending Closures, eg a shorthand to create one new closure that calls two closures.
// MARK: Appending closures `a + b`
/// Append closures, lhs first.
public func + <I>(lhs: @escaping (I) -> Void, rhs: @escaping (I) -> Void) -> ((I) -> Void) {
return {
lhs($0)
rhs($0)
}
}
@Amzd
Amzd / ColorPicker.swift
Last active January 25, 2025 08:49
What SwiftUI's ColorPicker should have been.
import SwiftUI
@available(iOS 14.0, *)
public struct ColorPickerWithoutLabel: UIViewRepresentable {
@Binding var selection: Color
var supportsAlpha: Bool = true
public init(selection: Binding<Color>, supportsAlpha: Bool = true) {
self._selection = selection
self.supportsAlpha = supportsAlpha
@Amzd
Amzd / Publisher+void.swift
Last active January 16, 2021 22:35
Void publisher result
extension Publisher {
func void() -> Publishers.Map<Self, Void> {
self.map { _ in () }
}
}
@Amzd
Amzd / AnyPublisher.swift
Created December 7, 2020 11:26
Empty and constant AnyPublisher extensions
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
extension AnyPublisher {
static var empty: Self {
Empty(completeImmediately: false).eraseToAnyPublisher()
}
static func constant(_ value: Output) -> Self where Failure == Never {
Just(value).eraseToAnyPublisher()
}
}
@Amzd
Amzd / HierarchyDescription.swift
Last active May 16, 2023 19:48
Description that can be used with any type that has recursive children
public protocol HierarchyDescription {
associatedtype Child: HierarchyDescription
var children: [Child] { get }
var description: String { get }
}
extension HierarchyDescription {
public var hierarchyDescription: String {
var description = self.description
for child in children {
@Amzd
Amzd / View+NSCursor.swift
Last active November 17, 2024 15:18
Set the cursor that is displayed when hovering a View. (macOS, SwiftUI)
import SwiftUI
extension View {
/// https://stackoverflow.com/a/61985678/3393964
public func cursor(_ cursor: NSCursor) -> some View {
self.onHover { inside in
if inside {
cursor.push()
} else {
NSCursor.pop()
@Amzd
Amzd / TextFieldFocusableArea.swift
Last active August 6, 2021 14:48
SwiftUI TextField has a very small tappable area and there is no simple way to make that area bigger. This is a solution using Introspect (https://github.com/siteline/SwiftUI-Introspect/) Stack Overflow question: https://stackoverflow.com/questions/56795712/swiftui-textfield-touchable-area/62089790#62089790
extension View {
public func textFieldFocusableArea() -> some View {
TextFieldButton { self.contentShape(Rectangle()) }
}
}
fileprivate struct TextFieldButton<Label: View>: View {
init(label: @escaping () -> Label) {
self.label = label
}
@Amzd
Amzd / NestedPublished.swift
Last active July 20, 2020 11:12
Moved to repo so you can use it in Swift Package Manager: https://github.com/Amzd/NestedPublished
/// Just like @Published this sends willSet events to the enclosing ObservableObject's ObjectWillChangePublisher
/// but unlike @Published it also sends the wrapped value's published changes on to the enclosing ObservableObject
@propertyWrapper @available(iOS 13.0, *)
public struct NestedPublished<Value: ObservableObject> where Value.ObjectWillChangePublisher == ObservableObjectPublisher {
public init(wrappedValue: Value) {
self.wrappedValue = wrappedValue
self.cancellable = nil
startListening(to: wrappedValue)
}
@Amzd
Amzd / PreferenceUIHostingController.swift
Last active November 14, 2024 10:01
PreferenceUIHostingController. Adds hiding home indicator and deferring system edge gestures to SwiftUI. (Don't work at the same time but I think that's normal?)
extension View {
/// Controls the application's preferred home indicator auto-hiding when this view is shown.
func prefersHomeIndicatorAutoHidden(_ value: Bool) -> some View {
preference(key: PreferenceUIHostingController.PrefersHomeIndicatorAutoHiddenPreferenceKey.self, value: value)
}
/// Controls the application's preferred screen edges deferring system gestures when this view is shown. Default is UIRectEdgeNone.
func edgesDeferringSystemGestures(_ edge: UIRectEdge) -> some View {
preference(key: PreferenceUIHostingController.PreferredScreenEdgesDeferringSystemGesturesPreferenceKey.self, value: edge)
}
extension Array where Element: Identifiable {
public subscript(id: Element.ID) -> Element? {
first { $0.id == id }
}
}
// let arrayOfIdentifiables = []
// let itemWithId = arrayOfIdentifiables[id]