Skip to content

Instantly share code, notes, and snippets.

@siracusa
siracusa / observer.swift
Last active June 17, 2024 16:47
Swift Concurrency Candidate
NSWorkspace.shared.notificationCenter.addObserver(
forName: NSWorkspace.didLaunchApplicationNotification,
object: nil, queue: nil, using: { [weak self] notification in
self?.doStuff()
})
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BreakpadProduct</key>
<string>VLC</string>
<key>BreakpadRequestComments</key>
<string>1</string>
<key>BreakpadURL</key>
<string>https://mac.crashes.videolan.org/reports</string>
@siracusa
siracusa / ObservableLazyWorkarounds.swift
Last active June 8, 2023 18:50
Possible workarounds for lazy properties in an @observable class
import Cocoa
import Observation
@Observable
class User {
@ObservationIgnored lazy var id : Int = {
return Int.random(in: 1...100)
}()
}
@siracusa
siracusa / ObservableLazy.swift
Created June 8, 2023 18:09
Lazy properties in an @observable class
import Cocoa
import Observation
//@Observable
class User {
// If @Observable is applied to this class, compilation
// of this property declaration fails with the error:
//
// 'lazy' cannot be used on a computed property
//
@siracusa
siracusa / SwiftUIIfModifier.swift
Created July 11, 2022 14:56
SwiftUI .if modifier
extension View {
@ViewBuilder
func `if`<Content: View>(_ conditional: Bool, @ViewBuilder content: (Self) -> Content) -> some View {
if conditional {
content(self)
}
else {
self
}
}

When creating and using a custom ButtonStyle for a SwiftUI button that changes its appearance based on the value of the configuration.isPressed property, that property sometimes get stuck with a "true" value, leaving the button to appear in its pressed state even when it is not being pressed. It stays stuck this way until the button is clicked again.

A sample project is attached. It's also in a public repo at https://github.com/siracusa/SwiftUIButtonIsPressedStateBug

To reproduce the bug:

  1. Build and launch the sample project. A window should appear showing a large button view with a green background and some white text that says "Hello".

  2. Click button view a few times and confirm that background color turns blue on mouse-down and switches back to green on mouse-up.

@siracusa
siracusa / Pro Display XDR at 120Hz bandwidth calculation.md
Last active November 1, 2024 07:51
Pro Display XDR at 120Hz bandwidth calculations

From Jonathan Dietz, Jr.:

Almost all new displays employ VESA Coordinated Video Timings using Reduced Blanking Timing Version 2 (CVT-RBv2) which adds a small amount of overhead to the video signal. This overhead includes 80 additional horizontal pixels and enough additional lines to meet the 460 µs minimum vertical blanking interval requirement. You can download an Excel spreadsheet from VESA that will do all the calculations for you, but the math is pretty simple:

460 / ((1000000 / [refresh rate in Hz] - 460) / [vertical resolution])

To get an integer number of lines, you round this result down and add 1. So for Pro Display XDR resolution at 120 Hz that would work out to:

460 / (1000000 / 120 - 460) / 3384) = 197.710 = 198 additional lines

Sampling process 2517 for 3 seconds with 1 millisecond of run time between samples
Sampling completed, processing symbols...
Analysis of sampling Safari (pid 2517) every 1 millisecond
Process: Safari [2517]
Path: /Applications/Safari.app/Contents/MacOS/Safari
Load Address: 0x10a637000
Identifier: com.apple.Safari
Version: 15.0 (15612.1.29.41.4)
Build Info: Safari-7612001029041004~7
Code Type: X86-64
Process: Safari [2517]
Path: /Applications/Safari.app/Contents/MacOS/Safari
Identifier: com.apple.Safari
Version: 15.0 (15612.1.29.41.4)
Build Info: Safari-7612001029041004~7 (612A69a)
Code Type: X86-64 (Native)
Parent Process: ??? [1]
Responsible: Safari [2517]
User ID: 67152294
579 => Blessed System File is /System/Volumes/Preboot/B24F40F6-A709-475D-8FE9-0BC32E6BA240/System/Library/CoreServices/boot.efi
366 => Blessed System Folder is /System/Volumes/Preboot/B24F40F6-A709-475D-8FE9-0BC32E6BA240/System/Library/CoreServices
The blessed volume in this APFS container is "/".
The blessed APFS snapshot for this volume is "3E14543D-BE61-487B-B49A-2AF55563C403".