Skip to content

Instantly share code, notes, and snippets.

View davidbalbert's full-sized avatar

David Albert davidbalbert

View GitHub Profile
@davidbalbert
davidbalbert / watch.js
Created November 19, 2024 15:45
Simple JavaScript observation
// Returns [get, set]. Changes to the value can be observed with onChange.
function watch(value) {
let val = value;
function get() {
return val;
}
get.handlers = [];
function set(newValue) {
@davidbalbert
davidbalbert / affine.js
Created September 22, 2024 21:46
An affine transform for JavaScript. Saved for later.
// an affine transformation matrix
class AffineTransform {
// the matrix is represented as a 3x3 matrix
// | A C Tx |
// | B D Ty |
// | 0 0 1 |
// A, B, C, D are the coefficients of the linear transformation
// Tx, Ty are the translation components
constructor(A, C, Tx, B, D, Ty) {
@davidbalbert
davidbalbert / DropDestination.swift
Created August 7, 2024 18:05
A variant of the SwiftUI dropDestination view modifier that supports an isValid callback. SwiftUI drag and drop has some annoying hangs, which this also exhibits. See here for more: https://github.com/davidbalbert/DragAndDropPerformance
//
// Transferable+Extensions.swift
// ImageWriter
//
// Created by David Albert on 8/5/24.
//
import CoreTransferable
import os
import SwiftUI
//
// ContentView.swift
// UnicodeExplorer
//
// Created by David Albert on 5/1/24.
//
import SwiftUI
struct ContentView: View {
@davidbalbert
davidbalbert / AppKitAutoscroll.swift
Last active April 4, 2024 16:31
Classic AppKit autoscroll
import Cocoa
class MyAutoscrollingView: NSView {
override func mouseDown(with event: NSEvent) {
// mouseDown-specific logic here
var mouseEvent = event
var done = false
NSEvent.startPeriodicEvents(afterDelay: 0.1, withPeriod: 0.05)
@davidbalbert
davidbalbert / UTTypeGraph.swift
Last active February 4, 2024 17:54
UTType graphviz
import UniformTypeIdentifiers
// As of macOS 14.3
extension UTType {
static var allTypes: [UTType] {
[
.item,
.content,
.compositeContent,
.diskImage,
@davidbalbert
davidbalbert / Text+String.swift
Last active January 7, 2025 01:21
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)
//
// OnWindowClose.swift
//
// Created by David Albert on 12/16/23.
//
import SwiftUI
import AppKit
struct WindowReader: NSViewRepresentable {
@davidbalbert
davidbalbert / BTree.swift
Created December 11, 2023 17:47
Partially working BigIndexSet + LineCache
extension Range where Bound == Int {
init<Summary>(_ range: Range<BTreeNode<Summary>.Index>, in root: BTreeNode<Summary>) where Summary: BTreeSummary {
range.lowerBound.validate(for: root)
range.upperBound.validate(for: root)
self = range.lowerBound.position..<range.upperBound.position
}
}
@davidbalbert
davidbalbert / Chunk.swift
Created November 28, 2023 21:32
Original fixup(withPrevious: Chunk)
extension Chunk {
mutating func fixup(withPrevious prev: Chunk) -> Bool {
var i = string.startIndex
var first: String.Index?
var old = startBreakState
var new = prev.endBreakState
startBreakState = new