Skip to content

Instantly share code, notes, and snippets.

@dagronf
dagronf / Collection+chunk.swift
Created April 30, 2024 00:43
Swift routines for chunking a collection (eg. Data, Array) into an array of chunks
extension Collection {
/// Perform a block on each chunk of `size` elements in this collection
/// - Parameters:
/// - size: The maximum size of the chunk
/// - block: The block to call with the chunk. Set `stop = true` to stop processing
func chunking(into size: Int, _ block: (SubSequence, inout Bool) -> Void) {
var stop = false
var index = 0
while index < self.count {
let offset = index + Swift.min(size, count - index)
@dagronf
dagronf / CGPath+squircle.swift
Last active March 8, 2024 00:37
A CGPath extension for generating squircle (superellipse) paths. Adapted from the PaintCode blog (https://www.paintcodeapp.com/news/code-for-ios-7-rounded-rectangles)
//
// Copyright © 2024 Darren Ford. All rights reserved.
//
// Adapted from the PaintCode blog: https://www.paintcodeapp.com/news/code-for-ios-7-rounded-rectangles
//
// MIT license
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
@dagronf
dagronf / Sequence+unique.swift
Created August 23, 2023 04:12
Return the unique elements of a sequence while maintaining the order of the input sequence
public extension Sequence where Element: Equatable {
/// Return the unique elements in the array using Equatable as the predicate
var unique: [Element] {
return self.reduce(into: []) { uniqueElements, element in
if !uniqueElements.contains(element) {
uniqueElements.append(element)
}
}
}
}
@dagronf
dagronf / Sequence+zipPadded.swift
Created August 23, 2023 04:08
Zip two sequences together, padding the shorter sequences with nils
/// Zip two sequences together, padding the shorter sequence with nil
func zipPadded<Value1, Value2>(_ z1: any Sequence<Value1>, _ z2: any Sequence<Value2>) -> [(Value1?, Value2?)] {
var v1 = z1.makeIterator() as any IteratorProtocol<Value1>
var v2 = z2.makeIterator() as any IteratorProtocol<Value2>
var vv1 = v1.next()
var vv2 = v2.next()
var result = [(Value1?, Value2?)]()
@dagronf
dagronf / find-focus-stealer.swift
Last active September 24, 2024 09:17
Swift command-line tool to find which application is stealing your Mac's focus [macOS, script, steal, focus]
#!/usr/bin/swift
import Foundation
import AppKit.NSWorkspace
// Returns the name of the frontmost app, or <none> if no app is frontmost
func currentFocusApp() -> String {
NSWorkspace.shared.frontmostApplication?.localizedName ?? "<none>"
}
@dagronf
dagronf / Notification+Visibility.swift
Last active April 13, 2024 12:50
Observe all notifications sent through a NotificationCenter or DistributedNotificationCenter
// Observe all notifications generated by the default NotificationCenter
NotificationCenter.default.addObserver(
forName: nil, object: nil, queue: nil) { notification in
Swift.print("Notification: \(notification.name.rawValue), Object: \(notification.object)")
}
// Observe all notifications generated by the default DistributedNotificationCenter
DistributedNotificationCenter.default().addObserver(
forName: nil, object: nil, queue: nil) { notification in
Swift.print("Notification: \(notification.name.rawValue), Object: \(notification.object)")
@dagronf
dagronf / NSColor+animations.swift
Last active July 29, 2021 01:18
Animate between two nscolors
//
// NSColor+animations.swift
//
// Created by Darren Ford on 27/7/21
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
@dagronf
dagronf / DSFDelayedRepeatingButton.swift
Created January 15, 2021 21:28
An NSButton that repeats (after a delay) while holding the mouse down
//
// DSFDelayedRepeatingButton.swift
//
// Created by Darren Ford on 16/1/21.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
@dagronf
dagronf / IsEmptyStringValueTransformer.swift
Last active August 24, 2020 03:44
Swift Value transformer that returns true if the value is a non empty string or false otherwise
import Foundation
/// Value transformer that returns NSNumber(true) if the value is a non empty string or NSNumber(false) otherwise
@objc public class IsEmptyStringValueTransformer: ValueTransformer {
// Public name to use in interface builder
private static let name = NSValueTransformerName(rawValue: "IsEmptyStringValueTransformer")
// Shared value transformer instance
@objc static public var shared = ValueTransformer(forName: IsEmptyStringValueTransformer.name)
@dagronf
dagronf / CIImageToNSImageTransformer.swift
Created July 20, 2020 02:41
Swift-based ValueTransformer that converts CIImage to NSImage
@objc class CIImageToNSImageTransformer: ValueTransformer {
@objc public static func RegisterTransformer() {
ValueTransformer.setValueTransformer(
CIImageToNSImageTransformer(),
forName: NSValueTransformerName("CIImageToNSImageTransformer")
)
}
override class func transformedValueClass() -> AnyClass {
return NSImage.self