Skip to content

Instantly share code, notes, and snippets.

View shaps80's full-sized avatar
🏠
Working from home

Shaps shaps80

🏠
Working from home
View GitHub Profile
import SwiftUI
public extension View {
func onGeometryChange<T>(
for type: T.Type,
of transform: @escaping (GeometryProxy) -> T,
action: @escaping (_ newValue: T) -> Void
) -> some View where T: Equatable {
modifier(GeometryModifier(transform: transform, action: action))
}
import SwiftUI
extension Backport<Any> {
@MainActor
public struct Group<Content: View>: View, Sendable {
private var content: Content
public init<Base, Result>(
subviewsOf view: Base,
@ViewBuilder transform: @escaping (SubviewsCollection) -> Result
import SwiftUI
public extension Picker where SelectionValue: CaseIterable, SelectionValue.AllCases: RandomAccessCollection, SelectionValue: RawRepresentable, SelectionValue.RawValue == String {
init(_ title: some StringProtocol, selection: Binding<SelectionValue>) where Label == Text, Content == ForEach<SelectionValue.AllCases, SelectionValue, Text> {
self.init(title, selection: selection) {
ForEach(SelectionValue.allCases, id: \.self) { element in
Text(element.rawValue)
}
}
}
@shaps80
shaps80 / Binding+OptionSet.swift
Created September 11, 2024 15:29
Simplifies binding option set values to a SwiftUI View.
import SwiftUI
public extension Binding where Value: OptionSet {
func toggling(_ value: Value.Element) -> Binding<Bool> {
.init(
get: {
wrappedValue.contains(value)
},
set: {
if $0 {
@shaps80
shaps80 / SwiftUI-Interface.swift
Created July 20, 2024 12:56
SwiftUI 5.10 Interface
This file has been truncated, but you can view the full file.
// swift-interface-format-version: 1.0
// swift-compiler-version: Apple Swift version 5.10 (swiftlang-5.10.0.12.5 clang-1500.3.9.1.1)
// swift-module-flags: -target arm64e-apple-macos14.4 -enable-objc-interop -enable-library-evolution -swift-version 5 -enforce-exclusivity=checked -Osize -library-level api -library-level api -enable-experimental-feature Macros -enable-experimental-feature ExtensionMacros -package-name SwiftUI -enable-bare-slash-regex -user-module-version 5.4.38.401 -module-name SwiftUI
import Accessibility
import AppKit
import Combine
import CoreData
import CoreFoundation
@_exported import CoreGraphics
import CoreText
@shaps80
shaps80 / Named Expansions.swift
Created June 19, 2024 10:08
Provides a simple subscript focused API for easily binding DisclosureGroup or Section expansions including persistence across app launches.
import SwiftUI
struct NamedExpansions: RawRepresentable {
@Observable
final class Wrapper {
var sections: Set<String>
init(sections: Set<String> = []) {
self.sections = sections
}
}
import SwiftUI
public extension View {
/// Automatically sizes the view to match its content size
/// - Parameters:
/// - width: An optional binding to a width property. Pass nil to opt-out of auto-sizing
/// - height: A optional binding to a height property. Pass nil to opt-out of auto-sizing
/// - alignment: The alignment of this view inside the resulting frame. Note that most alignment values have no apparent effect when the size of the frame happens to match that of this view.
func frame(width: Binding<CGFloat>? = nil, height: Binding<CGFloat>? = nil, alignment: Alignment = .center) -> some View {
onSizeChange {
@shaps80
shaps80 / Color.swift
Created April 26, 2024 09:38
Includes RGB to HSV/HSB conversion.
import Swift
public struct Color: Sendable {
/// The red component of the color.
///
/// This property is not part of the public interface of the testing
/// library as we may wish to support non-RGB color spaces in the future.
internal var redComponent: UInt8
/// The green component of the color.
@shaps80
shaps80 / Pinned.swift
Last active August 17, 2024 10:59
pinned modified now allows any hashable value
import SwiftUI
public extension View {
func pinned<T: Hashable>(id: Binding<T?>) -> some View {
modifier(Pinned(pinnedId: .init(
get: { id.wrappedValue },
set: { id.wrappedValue = $0 as? T }
)))
}
}
@shaps80
shaps80 / Sequence+CountWhere.swift
Last active March 27, 2024 12:07
Returns the number of elements matching the closure predicate.
import Swift
extension Sequence {
/// Returns the number of elements matching the closure predicate.
///
/// The `isIncluded` closure is called sequentially comparing each
/// element to determine the number of matches found.
/// This example shows how to find the number of elements matching
/// the given predicate.
///