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 / SwiftUI+ScaledFont.swift
Created October 27, 2023 11:27
Automatically scales the font size in respect to dynamic type size changes.
public extension View {
func font<T: BinaryFloatingPoint>(_ scaled: Scaled<T>) -> some View {
modifier(ScaledFontModifier(metric: scaled.value))
}
}
public struct Scaled<T: BinaryFloatingPoint> {
fileprivate let value: T
public static func scaled(size: T) -> Self {
.init(value: size)
@shaps80
shaps80 / reset.css
Last active October 29, 2023 22:43
/* Box sizing rules */
*,
*::before,
*::after {
box-sizing: border-box;
}
/* Remove default margin */
html,
body,
@shaps80
shaps80 / URL+Scoped.swift
Last active September 28, 2023 08:40
URL with scoped security access
import Foundation
struct ScopeError: LocalizedError {
let url: URL
var errorDescription: String? {
"Unabled to gain scoped access to the url: \(url)"
}
}
extension URL {
@shaps80
shaps80 / Environment+Dump.swift
Last active August 31, 2023 15:27
Dump SwiftUI Environment
import SwiftUI
extension EnvironmentValues {
var dump: String {
let keys = asTextualRepresentationWithNonReaptingKeys
return """
--- Environment Values - BEGIN ---
\(keys.map { $0 }.joined(separator: "\n"))
--- Environment Values - END ---
@shaps80
shaps80 / GeometryObserver.swift
Created July 28, 2023 22:10
Observe Geometry updates on any SwiftUI view
import SwifUI
public struct Geometry: Equatable {
fileprivate var proxy: GeometryProxy?
public var size: CGSize = .init(width: 10, height: 10)
public var safeAreaInsets: EdgeInsets = .init()
public func frame(in coordinateSpace: CoordinateSpaceProtocol) -> CGRect {
proxy?.frame(in: coordinateSpace) ?? .init(origin: .zero, size: size)
}
@shaps80
shaps80 / Inspect.swift
Last active March 2, 2024 16:25
Inspect UIKit/AppKit View’s and Controllers from SwiftUI
import SwiftUI
#if os(iOS) || os(macOS)
#if os(iOS)
typealias PlatformView = UIView
typealias PlatformViewController = UIViewController
#else
typealias PlatformView = NSView
typealias PlatformViewController = NSViewController
@shaps80
shaps80 / Shaps-Console.itermcolors
Last active May 3, 2023 10:08
Shaps Console Theme
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Ansi 0 Color</key>
<dict>
<key>Alpha Component</key>
<real>1</real>
<key>Blue Component</key>
<real>0.10980392247438431</real>
xcrun simctl spawn booted defaults write -g SBChamoisWindowingEnabled -bool true
@shaps80
shaps80 / Concurrency.swift
Last active April 11, 2024 20:38
Async/Await Swift extensions
import Foundation
extension Optional {
public func flatMap<U>(_ transform: (Wrapped) async throws -> U?) async rethrows -> U? {
switch self {
case .some(let wrapped):
return try await transform(wrapped)
case .none:
return nil
}
@shaps80
shaps80 / Binding+NonOptional.swift
Created March 10, 2023 14:46
Convert SwiftUI Optional Binding to Non-Optional Binding
extension Binding {
public init(wrappedValue: Binding<Value?>, defaultValue: Value) {
self = Binding<Value>(
get: { wrappedValue.wrappedValue ?? defaultValue },
set: { wrappedValue.wrappedValue = $0 }
)
}
public func defaultValue<T>(_ value: T) -> Binding<T> where Value == T? {
Binding<T>(