Skip to content

Instantly share code, notes, and snippets.

View davidbalbert's full-sized avatar

David Albert davidbalbert

View GitHub Profile
@davidbalbert
davidbalbert / .gitignore
Last active October 2, 2026 15:18
cocotb dynamic_cast bug
*.log
control
repro
@davidbalbert
davidbalbert / .bazelrc
Last active September 28, 2026 23:19
hermetic-llvm tgmath.h failure
common --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1
build:verbose --copt=-v
@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 September 30, 2025 22:04
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 {