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 / 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 / AVPlayer+Scrubbing.swift
Last active April 18, 2024 22:31
Enables smooth frame-by-frame scrubbing (in both directions) – similar to Apple's applications.
public enum Direction {
case forward
case backward
}
internal var player: AVPlayer?
private var isSeekInProgress = false
private var chaseTime = kCMTimeZero
private var preferredFrameRate: Float = 23.98
@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 / Font.swift
Last active April 10, 2024 20:01
A set of UIFont/NSFont helpers that matches the equivalent SwiftUI Font API. (Supports iOS 13+ and macOS 10.15+)
import SwiftUI
#if os(macOS)
public typealias Font = NSFont
public typealias FontDescriptor = NSFontDescriptor
#else
public typealias Font = UIFont
public typealias FontDescriptor = UIFontDescriptor
#endif
@shaps80
shaps80 / UIMenu+ResultBuilder.swift
Created June 9, 2021 20:52
Adds ResultBuilder support to UIMenu to simplify creation
import SwiftUI
typealias Menu = UIMenu
typealias Action = UIAction
@resultBuilder
struct MenuElementBuilder {
static func buildBlock(_ components: UIMenuElement...) -> [UIMenuElement] { components }
}
@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 / CGPoint+Target.swift
Last active March 21, 2024 18:44
Distance travelled after decelerating to zero velocity at a constant rate. The included Playground file shows how you can use it with a pan gesture as an example.
public extension CGPoint {
// The target points after decelerating to 0 velocity at a constant rate
func target(initialVelocity: CGPoint, decelerationRate: CGFloat = UIScrollView.DecelerationRate.normal.rawValue) -> CGPoint {
let x = self.x + self.x.target(initialVelocity: initialVelocity.x, decelerationRate: decelerationRate)
let y = self.y + self.y.target(initialVelocity: initialVelocity.y, decelerationRate: decelerationRate)
return CGPoint(x: x, y: y)
}
}
@shaps80
shaps80 / FontCacheDescriptor+Graphik.swift
Last active March 19, 2024 14:54
Better font loading in iOS with Swift
extension UIFont {
// The `rawValue` MUST match the filename (without extension)
public enum Graphik: String, FontCacheDescriptor {
case regular = "GraphikAltWeb-Regular"
case medium = "GraphikAltWeb-Medium"
case regularItalic = "GraphikAltWeb-RegularItalic"
case mediumItalic = "GraphikAltWeb-MediumItalic"
}
@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