Skip to content

Instantly share code, notes, and snippets.

@christianselig
christianselig / widgets.swift
Created September 18, 2024 19:23
Proper way to handle backwards compatibility of iOS widgets
import WidgetKit
import SwiftUI
@main
struct WidgetExtMain {
static func main() {
if #available(iOS 18.0, *) {
MyWidgets_18.main()
} else {
@christianselig
christianselig / hammerspoon.lua
Created August 28, 2024 17:08
Add middle-button panning support to Sketch to match Figma, Fusion 360, etc.
-- Make Sketch use the middle mouse button for dragging like Figma, Fusion 360, etc.
local middleMouseDown = false
-- Function to handle the middle mouse down event
local function middleMouseDownHandler(event)
if event:getType() == hs.eventtap.event.types.otherMouseDown then
if event:getProperty(hs.eventtap.event.properties['mouseEventButtonNumber']) == 2 then
middleMouseDown = true
-- Simulate holding down the space bar
hs.eventtap.event.newKeyEvent(hs.keycodes.map['space'], true):post()
@christianselig
christianselig / LongPressButton.swift
Last active July 10, 2024 23:44
How to accomplish a long-pressable button in SwiftUI with two different techniques. The first just uses SwiftUI but due to the simultaneous gesture requirement you also have to ensure both don't fire concurrently. The second uses UIKit and wraps UIButton and UILongPressGestureRecognizer which natively handles this behavior.
import SwiftUI
// PURE SwiftUI way with State tracking to prevent double events
struct ContentView: View {
@State private var ignoreTapEvent = false
var body: some View {
Button {
guard !ignoreTapEvent else {
import SwiftUI
import WebKit
@main
struct WebViewActingUpApp: App {
var body: some Scene {
WindowGroup(id: "Main") {
ContentView()
}
import SwiftUI
import WebKit
@main
struct WebViewActingUpApp: App {
var body: some Scene {
WindowGroup(id: "Main") {
ContentView()
}
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationStack {
Text("Welcome to my ice cream store")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItemGroup(placement: .topBarLeading) {
Button {
import SwiftUI
@main
struct WindowPresentationFunApp: App {
@State var appState = AppState()
var body: some Scene {
WindowGroup {
RootView(appState: appState)
.onAppear {
import SwiftUI
struct ContentView: View {
var body: some View {
IceCreamView(viewModel: ViewModel(iceCream: "banana"))
}
}
struct IceCreamView: View {
@State var viewModel: ViewModel
import SwiftUI
struct ContentView: View {
var body: some View {
IceCreamView(viewModel: ViewModel(iceCream: "banana"))
}
}
struct IceCreamView: View {
@State var viewModel: ViewModel
//
// ContentView.swift
// UnderstandSwiftUIChange
//
// Created by Christian Selig on 2024-04-04.
//
import SwiftUI
import WebKit
import UIKit