Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View IanKeen's full-sized avatar
🏂

Ian Keen IanKeen

🏂
View GitHub Profile
@ollieatkinson
ollieatkinson / Tuple.swift
Last active April 22, 2024 15:13
Implementation of `Tuple` type using Swift's new parameter packs
public struct Tuple<each T> {
public private(set) var value: (repeat each T)
public init(_ value: repeat each T) { self.value = (repeat each value) }
}
extension Tuple {
public func map<each U>(
_ transform: (repeat each T) throws -> (repeat each U)
) rethrows -> (repeat each U) {
@davidbalbert
davidbalbert / Text+String.swift
Last active April 21, 2024 03:12
Hack to extract a String from SwiftUI.Text
// Cursed! Do not use!
// Supports most, but not all Text initializers.
import SwiftUI
extension FormatStyle {
func format(any value: Any) -> FormatOutput? {
if let v = value as? FormatInput {
return format(v)
@HarshilShah
HarshilShah / CycledAnimationTimelineView.swift
Created April 29, 2023 05:17
A SwiftUI view that performs a looping animation in whole cycles. When the animation is disabled, it drives the current loop to completion, and pauses only then
struct CycledAnimationTimelineView<Content: View>: View {
var duration: Double
var isAnimating: Bool
@ViewBuilder var content: (Double) -> Content
@State private var isActuallyAnimating = false
@State private var startDate: Date?
var body: some View {
TimelineView(.animation(paused: !isActuallyAnimating)) { context in
@ole
ole / RelativeSizeLayout.swift
Last active March 24, 2024 22:24
A SwiftUI layout and modifier for working with relative sizes ("50 % of your container"). https://oleb.net/2023/swiftui-relative-size/
import SwiftUI
extension View {
/// Proposes a percentage of its received proposed size to `self`.
///
/// This modifier multiplies the proposed size it receives from its parent
/// with the given factors for width and height.
///
/// If the parent proposes `nil` or `.infinity` to us in any dimension,
/// we’ll forward these values to our child view unchanged.
import SwiftUI
public struct MouseTrackingView: View {
public var callbacks: NSMouseTrackingView.Callbacks
public var isEnabled: Bool
public var options: NSTrackingArea.Options
public var hitEnabled: Bool
public var flipYAxis: Bool
@schwa
schwa / flick.swift
Last active October 15, 2022 00:18
Flick Gesture
import PlaygroundSupport
import SwiftUI
PlaygroundPage.current.setLiveView(ContentView())
struct ContentView: View {
var body: some View {
Color.white.frame(width: 600, height: 800).touchVisualizer()
}
}
// TaskMeeting
// (c) 2022, Nikolai Ruhe
/// A type that synchronizes progress of two tasks.
public final actor TaskMeeting: Sendable {
private var completion: CheckedContinuation<Void, Error>? = nil
/// This method synchronizes two tasks so that both perform the closure at
/// the same time. Both tasks need to call `rendezvous`.
///
@DougGregor
DougGregor / parallel_map.swift
Created December 24, 2020 01:10
Swift async/await implementation of a parallel map
extension Collection {
func parallelMap<T>(
parallelism requestedParallelism: Int? = nil,
_ transform: @escaping (Element) async throws -> T
) async throws -> [T] {
let defaultParallelism = 2
let parallelism = requestedParallelism ?? defaultParallelism
let n = self.count
if n == 0 {
#!/bin/sh
# make sure you have imagemagick installed: brew install imagemagick
# your app_icons.sh file should have the correct permissions: run `chmod 775 app_icons.sh` in your terminal from where you put this file
# put your `my_icon.png` next to this file and run ./app_icons.sh to export your app icons
x=my_icon.png
y=${x%.*}
# delete the export directory so we start clean
@ollieatkinson
ollieatkinson / FileSystemEventStream.swift
Created October 22, 2020 10:27
sane `fsevents` in Swift
import Foundation
import Combine
public struct Event: Identifiable {
public let id: FSEventStreamEventId
public let path: String
public let flags: Flags
}
public class FileSystemEventStream {