Skip to content

Instantly share code, notes, and snippets.

View tcldr's full-sized avatar

Tristan tcldr

View GitHub Profile
@tcldr
tcldr / ConcurrencyUtilities.swift
Last active April 23, 2023 12:48
Swift Concurrency Utilities for managing Transactional Resources
// A simple serial queue. Every operation sent through the queue will be executed
// in first-in first-out order. Every operation will complete its execution fully,
// even if it includes suspension points, prior to the next operation commencing.
//
// TIP: If calling from a global actor like @MainActor annotate your closure to
// get better ergonomics: `queue.send { @MainActor in /* ... */ }`
public final class AsyncSerialQueue: Sendable {
@tcldr
tcldr / gpg-agent.conf
Last active May 16, 2020 14:33
GitHub GPG verification set-up macOS
# ~/.gnupg/gpg-agent.conf
#
# Connects gpg-agent to the OSX keychain via the brew-installed
# pinentry program from GPGtools. This is the OSX 'magic sauce',
# allowing the gpg key's passphrase to be stored in the login
# keychain, enabling automatic key signing.
pinentry-program /usr/local/bin/pinentry-mac
@tcldr
tcldr / stack_answer.md
Created December 16, 2019 16:17
StackOverflow: Positioning View using anchor point

So, as far as I can tell, alignment guides can't be used in this way – yet. Hopefully this will be coming soon, but in the meantime we can do a little padding trickery to get the desired effect.

Caveats

  • You will need to have some way of retrieving the font metrics – I'm using CTFont to initialise my Font instances and retrieving metrics that way.
  • As far as I can tell, Playgrounds aren't always representative of how a SwiftUI layout will be laid out on the device, and certain inconsistencies arise. One that I've identified is that the displayScale environment value (and the derived pixelLength value) is not set correctly by default in playgrounds and even previews. Therefore, you have to set this manually in these environments if you want a representative layout (FB7280058).
  • Significantly, the default text view height appears to differ between macOS and iOS. I have a calculation that works with most fonts on iOS, and here I've found a calculation for macOS that seems to work – but ideal
//
// AppDelegate.swift
// Desync
//
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
@tcldr
tcldr / FloatingPoint+Clamped.swift
Last active November 10, 2018 17:16
Range based clamping for `FloatingPoint` types.
// FloatingPoint extension
extension FloatingPoint {
func clamped<T: ClampableRangeExpression>(to range: T) -> Self where T.Bound == Self {
return range.clamping(self)
}
}
// RangeExpression extension
@tcldr
tcldr / TimingFunction.swift
Last active March 21, 2023 07:24
For when you need the progress along an Apple Animation curve outside of CoreAnimation in a Swift 4.2 interface. Given a cubic Bezier timing curve defined by two control points, (or UIKit's `UICubicTimingParameters`), returns the progress along the curve at any given time. Includes a port of the WebKit implementation of UnitBezier which hopefull…
//
// TimingFunction.swift
//
// Created by tcldr on 04/11/2018.
// https://github.com/tcldr
// Copyright © 2018 tcldr.
//
// Permission is hereby granted, free of charge,
// to any person obtaining a copy of this software and
// associated documentation files (the "Software"), to