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
@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 April 23, 2024 20:29
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.
///
@shaps80
shaps80 / Print recursively
Created January 25, 2024 09:18
Recursively prints the entire SwiftUI View tree using Mirror
import SwiftUI
extension Mirror {
func printRecusively() {
for child in children {
let label = child.label ?? "<unknown>"
let value = child.value
print(type(of: value), label, value, separator: " | ")
Mirror(reflecting: value)
@shaps80
shaps80 / Interface.swift
Last active January 13, 2024 00:19
SwiftUI Interface (2023)
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.9.2 (swiftlang-5.9.2.2.11 clang-1500.1.0.2.2)
// swift-module-flags: -target arm64e-apple-macos14.2 -enable-objc-interop -enable-library-evolution -swift-version 5 -enforce-exclusivity=checked -Osize -library-level api -library-level api -module-name SwiftUI
// swift-module-flags-ignorable: -enable-bare-slash-regex -user-module-version 5.2.12
import Accessibility
import AppKit
import Combine
import CoreData
import CoreFoundation
@_exported import CoreGraphics
@shaps80
shaps80 / Mirror+Debug.swift
Created January 11, 2024 12:01
Mirror: Print recursively
extension Mirror {
func printRecusively() {
for child in children {
let label = child.label ?? "<unknown>"
let value = child.value
print(type(of: value), label, value, separator: " | ")
Mirror(reflecting: value)
.printRecusively()
}
@shaps80
shaps80 / Variadic Views.swift
Created December 22, 2023 10:02
Enables the creation of custom container views in SwiftUI
import SwiftUI
public extension View {
func variadic<R: View>(@ViewBuilder _ transform: @escaping (_VariadicView.Children) -> R) -> some View {
_VariadicView.Tree(Helper(transform: transform)) { self }
}
}
private struct Helper<R: View>: _VariadicView.MultiViewRoot {
var transform: (_VariadicView.Children) -> R
@shaps80
shaps80 / InlineButtonStyle.swift
Created December 22, 2023 10:01
Provides a button style that can be provided inline, useful during prototyping or for simply 'removing' any styling.
import SwiftUI
public extension View {
func buttonStyle(_ style: @escaping (InlineButtonStyle.Configuration) -> some View) -> some View {
buttonStyle(InlineButtonStyle(style: style))
}
}
public struct InlineButtonStyle: ButtonStyle {
@ViewBuilder let style: (Configuration) -> any View