Skip to content

Instantly share code, notes, and snippets.

@schwa
schwa / ScreenSaver.swift
Created March 20, 2024 19:52
Silly Swift Screen Saver Sample Source
import SwiftUI
struct Point {
var position: CGPoint
var velocity: CGPoint
}
struct ContentView: View {
@State
var lines: [[Point]] = []
@schwa
schwa / SwiftUI for macOS Missing Features.md
Last active January 31, 2024 02:45
SwiftUI for macOS Missing Features

https://mastodon.social/@schwa/111801520286334341

My list:

  • Tap location for contextual menus. A lot of apps need to know exactly where a context menu is being triggered from (e.g. canvas style editors etc).
  • Modifier key observation during button presses/gestures etc.
  • Cursor support
  • Native Undo Manager support (SwiftUI has Environment.undoManager but it just exposes Cocoa's UndoManager which doesn't feel very SwiftUI like).
  • Expanded File Dialog options (e.g. adding buttons to the dialog).
  • The outline variant of List (List(… children: …)) is missing a lot of the flexibility of NSOutlineView (TODO: flesh this out with concrete example).
@schwa
schwa / SharedWindow.swift
Created December 15, 2023 19:28
SwiftUI example showing how to make a Shared Content Window…
import SwiftUI
import Observation
import os
/// A trick in SwiftUI to have a singls shared global window.
/// We want the user to be able to show and hide this window, and change the content in it. Hitting show multiply times should NOT open multiple windows but instead should bring the shared window forward.
/// The trick is to use a dummy Singleton type (here we embed it with the shared content) that we can pass to open/dismissWindow and the WindowGroup. The dummy SIngleton type must contain no properties and merely exists to trick SwiftUI into only ever showing one window for the type at a time.
@Observable
class SharedContent {
@schwa
schwa / ImmersiveSpaceHelper.swift
Created October 16, 2023 18:05
SwiftUI helpers to make dealing with ImmsersiveSpaces less annoying.
/// Create an ``ImmersiveSpaceState`` in your view, wrap it with `@State` and modify your view content with ``immersiveSpaceHelper()``.
/// Example:
/// ```swift
/// struct MyView: View {
/// @State
/// var immersiveSpaceState = ImmersiveSpaceState(id: "my-immersive-space-id")
///
/// var body: some View {
/// Toggle("Show/Hide Immersive Space", isOn: $immersiveSpaceState.show)
/// .immersiveSpaceHelper($immersiveSpaceState)
@schwa
schwa / metal_frameworks.md
Last active October 3, 2023 16:20
Metal Frameworks
@schwa
schwa / Devices.md
Created September 15, 2023 03:22
Metal 3 Availablity & GPU Family Table

Metal 3 Availablity & GPU Family Table

Device OS SOC Metal GPU Family
iPhone 6S & 6S Plus iOS 15 A8 2 Apple2
iPhone SE (1st generation) iOS 15 A9 2 Apple3
iPhone 7 & 7 Plus iOS 15 A10 Fusion 2 Apple3
iPod Touch (7th generation) iOS 15 A10 Fusion 2 Apple3
iPhone 8 & 8 Plus iOS 15, 16 A11 Bionic 2 Apple4
iPhone X iOS 15, 16 A11 Bionic 2 Apple4
@schwa
schwa / libfuzzer.md
Last active February 27, 2024 18:45
Using LLVM's libfuzzer with Swift

Using LLVM's libfuzzer with Swift

Background

Of the many tools available for fuzzing, I've found that LLVM's libfuzzer is the easiest to use for Swift on macOS. Other tools seem to have a list of requirements taht are difficult to meet. libfuzzer on the other hand is built into LLVM and is available on macOS in the custom Swift toolchains: https://www.swift.org/download/

In this document I'll describe how to use libfuzzer with Swift and Swift Packages.

I used this setup to fuzz an SVG Renderer package that I am building. I was able to find and fix a number of bugs in my SVG parsing code using libfuzzer in basically no time at all.

@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()
}
}
@schwa
schwa / spm-init.fish
Created July 29, 2022 20:20
CLI tool using gum (https://github.com/charmbracelet/gum) to streamline Swift Package creation…
#!/usr/bin/env fish
# brew tap charmbracelet/tap && brew install charmbracelet/tap/gum
function spm-init
gum style --foreground 11 "Package Type:"
set TYPE (gum choose "library" "executable" "empty" "system module" "manifest")
gum style --foreground 10 $TYPE
@schwa
schwa / GunHeatmaps.py
Created June 3, 2022 05:45
GunHeatmaps.py
# %%
import pandas as pd
import numpy as np
import datetime as dt
from datetime import datetime
import calmap
# Data from https://www.gunviolencearchive.org/
#events = pd.read_csv('ChildrenKilled.csv', parse_dates=['Incident Date'])
events = pd.read_csv('MassShootings.csv', parse_dates=['Incident Date'])