Skip to content

Instantly share code, notes, and snippets.

@ole
ole / set-photos-keyboard-shortcuts.sh
Last active May 4, 2024 02:10
Assign a keyboard shortcut to the Export Unmodified Originals menu command in Photos.app on macOS
#!/bin/bash
# Assigns a keyboard shortcut to the Export Unmodified Originals
# menu command in Photos.app on macOS.
# @ = Command
# ^ = Control
# ~ = Option
# $ = Shift
shortcut='@~^e'
@ole
ole / FinderFileColors.swift
Created February 2, 2023 11:43
Assign colors (as shown in the macOS Finder) to files
import Foundation
enum FinderColor: Int {
case noColor = 0
case grey = 1
case green = 2
case purple = 3
case blue = 4
case yellow = 5
case red = 6
@ole
ole / CodableContainerVsEncoder.swift
Created January 1, 2023 15:43
Illustrating the differences between encoding through a encoding container (recommended) vs. calling encode(to:) directly on the encoder (generally not recommended).
import Foundation
struct ContainerEncoded: Encodable {
var date: Date = .now
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(date)
}
}
@ole
ole / NoLargerThanLayout.swift
Last active December 16, 2022 06:47
SwiftUI: Show a view with its ideal size, but no larger than a given maximum size. The view must be flexible, otherwise it may stretch out of the wrapping frame. Two variants: as a custom Layout (iOS 16+), or with manual measuring using GeometryReader.
import SwiftUI
extension View {
func noLargerThan(_ maxSize: CGSize) -> some View {
NoLargerThanLayout(maxSize: maxSize) {
self
}
}
}
@ole
ole / clipped-hit-testing.swift
Last active January 11, 2023 00:57
SwiftUI: .clipped() doesn’t limit hit testing to the visible area. This is the sample code for https://oleb.net/2022/clipped-hit-testing/
import SwiftUI
struct ContentView: View {
@State private var buttonTapCount: Int = 0
@State private var rectTapCount: Int = 0
@State private var isClippingDisabled: Bool = false
@State private var activateContentShape: Bool = false
var body: some View {
VStack(spacing: 40) {
@ole
ole / ListScrollPosition.swift
Created November 17, 2022 10:40
SwiftUI: control List scroll position on element insert
import SwiftUI
struct Item: Identifiable {
var id: UUID = .init()
var value: Int
}
let sampleItems: [Item] = (1...99).map { Item.init(value: $0) }
struct ContentView: View {
// SwiftUI question: How do you put a continuous background behind a GridRow?
//
// I would have thought:
//
// GridRow { … }
// .background { … }
//
// But this puts the background behind each cell individually (behaves like Group),
// which is not what I want.
@ole
ole / Stateful.swift
Last active December 5, 2022 20:58
A wrapper view that provides a mutable Binding to its content closure. Useful in Xcode Previews for interactive previews of views that take a Binding. https://twitter.com/olebegemann/status/1565707085849010176
import SwiftUI
/// A wrapper view that provides a mutable Binding to its content closure.
///
/// Useful in Xcode Previews for interactive previews of views that take a Binding.
struct Stateful<Value, Content: View>: View {
var content: (Binding<Value>) -> Content
@State private var state: Value
init(initialState: Value, @ViewBuilder content: @escaping (Binding<Value>) -> Content) {
@ole
ole / _StackLayoutCache.swift
Last active September 6, 2022 15:33
Structure of _StackLayoutCache and related types, used by SwiftUI as the Cache type for VStackLayout and HStackLayout
// Structure of _StackLayoutCache and related types.
// Used by SwiftUI as the Cache type for VStackLayout and HStackLayout.
//
// As of: iOS 16.0 simulator in Xcode 14.0b6
import SwiftUI
struct _StackLayoutCache {
var stack: StackLayout
}
@ole
ole / LayoutCallAsFunction.swift
Created June 12, 2022 15:19
SwiftUI: How any `Layout`-conforming type becomes a container view when it's used with a `@ViewBuilder` closure.
// Excerpt from `SwiftUI.framework/Modules/SwiftUI.swiftmodule/arm64-apple-ios.swiftinterface`
// in Xcode 14.0b1
//
// This is how any `Layout`-conforming type becomes a container view
// when it's used with a `@ViewBuilder` closure.
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
extension SwiftUI.Layout {
@_alwaysEmitIntoClient public func callAsFunction<V>(@SwiftUI.ViewBuilder _ content: () -> V) -> some SwiftUI.View where V : SwiftUI.View {
return _VariadicView.Tree(
root: _LayoutRoot(self), content: content())