Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
Change a list checkbox's state once SwiftUI has finished updating
Țugui Dragoș3fc6456fe319
diff --git a/AGENTS.md b/AGENTS.mdindex 564bd98b..7d410cdd 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -404,6 +404,10 @@ Peel runs on macOS 26 and later, and the two versions are treated differently inthe page's body again, and a `List` measures every visible row again. Make such a binding inside a small view ofits own, as `AppRemovalOptions` and `BulkRemovalOptions` do. Prove a cost like this by counting how often a bodyruns, with a probe thrown away afterward: the time of one click changes with what the list shows.+- **A hosted AppKit view changes a value accessibility watches outside SwiftUI's update.** A button's state set in+ `updateNSView` posts a notification, and AppKit then reads the row's elements from the view graph being updated: with+ a client watching the app and the rows read once, SwiftUI goes round in a cycle, at times for good. Keep the change for+ the view's next layout, as `NativeCheckbox.Host` does, which comes before the next frame.- **A `Group` hands its modifiers to its children**, so a `.task` on a `Group` that stays empty until the task loadssomething never runs: a list whose being empty decides whether its view exists is loaded by a model, or by a viewthat always exists (`SavedSettings`).diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 3382f733..07e6675e 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -764,6 +764,8 @@ its changes the way [Keep a Changelog](https://keepachangelog.com/) suggests.#### Every page+- Peel no longer stops responding for good when a checkbox in a list changes on its own, as at the end of a Move to Trash+ that kept an item, while an app that reads other apps' windows through accessibility is running.- With Reduce Motion on, figures fade to their new value instead of rolling, on Home, in the menu bar panel, and in thebars at a page's foot.- Apps' icons follow the icon style chosen in System Settings > Appearance at once, as the Dock does: Peel kept the olddiff --git a/Peel/Views/NativeCheckbox.swift b/Peel/Views/NativeCheckbox.swiftindex c8785953..e606fcc8 100644--- a/Peel/Views/NativeCheckbox.swift+++ b/Peel/Views/NativeCheckbox.swift@@ -19,12 +19,14 @@ struct NativeCheckbox: NSViewRepresentable {let host = Host()host.button.target = context.coordinatorhost.button.action = #selector(Coordinator.toggle(_:))+ host.showNow(state)update(host.button, in: context)return host}func updateNSView(_ host: Host, context: Context) {context.coordinator.isOn = $isOn+ host.show(state)update(host.button, in: context)}@@ -37,6 +39,7 @@ struct NativeCheckbox: NSViewRepresentable {/// row's menu away from a row that cannot be selected.final class Host: NSView {let button = NativeCheckbox.checkbox()+ private var waitingState: NSControl.StateValue?init() {super.init(frame: .zero)@@ -47,12 +50,34 @@ struct NativeCheckbox: NSViewRepresentable {required init?(coder: NSCoder) {nil}++ /// Gives the button a state at the next layout, before the next frame and once SwiftUI has finished updating.+ /// As a button's state changes AppKit can ask SwiftUI for the row's accessibility elements, for a client that+ /// watches Peel, and SwiftUI can't answer that in the middle of an update: it goes round in a cycle for good.+ func show(_ state: NSControl.StateValue) {+ let isShown = button.state == state && button.allowsMixedState == (state == .mixed)+ waitingState = isShown ? nil : state+ if !isShown { needsLayout = true }+ }++ func showNow(_ state: NSControl.StateValue) {+ if button.allowsMixedState != (state == .mixed) { button.allowsMixedState = state == .mixed }+ if button.state != state { button.state = state }+ }++ override func layout() {+ super.layout()+ guard let waitingState else { return }+ self.waitingState = nil+ showNow(waitingState)+ }+ }++ private var state: NSControl.StateValue {+ isMixed ? .mixed : isOn ? .on : .off}private func update(_ button: NSButton, in context: Context) {- if button.allowsMixedState != isMixed { button.allowsMixedState = isMixed }- let state: NSControl.StateValue = isMixed ? .mixed : isOn ? .on : .off- if button.state != state { button.state = state }let size = NSControl.ControlSize(context.environment.controlSize) ?? .regularif button.controlSize != size { button.controlSize = size }if button.isEnabled != context.environment.isEnabled { button.isEnabled = context.environment.isEnabled }diff --git a/PeelTests/NativeCheckboxTests.swift b/PeelTests/NativeCheckboxTests.swiftnew file mode 100644index 00000000..3a9ace69--- /dev/null+++ b/PeelTests/NativeCheckboxTests.swift@@ -0,0 +1,109 @@+import AppKit+@testable import Peel+import SwiftUI+import Testing++@Observable+private final class Choice {+ var isOn = false+ var isMixed = false+}++@MainActor+struct NativeCheckboxTests {+ private struct Row: View {+ let choice: Choice++ var body: some View {+ NativeCheckbox(+ isOn: Binding(get: { choice.isOn }, set: { choice.isOn = $0 }),+ label: "An item",+ isMixed: choice.isMixed+ )+ }+ }++ private func button(in view: NSView) -> NSButton? {+ for subview in view.subviews {+ if let button = subview as? NSButton ?? button(in: subview) { return button }+ }+ return nil+ }++ private func shown(_ choice: Choice) throws -> (NSWindow, NSButton) {+ let window = NSWindow(+ contentRect: NSRect(x: 0, y: 0, width: 100, height: 100),+ styleMask: [.titled], backing: .buffered, defer: true+ )+ let host = NSHostingView(rootView: Row(choice: choice))+ window.contentView = host+ window.layoutIfNeeded()+ return (window, try #require(button(in: host)))+ }++ private func settle(_ window: NSWindow, until isDone: () -> Bool) async {+ for _ in 0..<200 where !isDone() {+ window.layoutIfNeeded()+ try? await Task.sleep(for: .milliseconds(5))+ }+ }++ @Test func theCheckmarkChangesOnceSwiftUIHasFinishedUpdating() async throws {+ let choice = Choice()+ let (window, button) = try shown(choice)+ var stacks: [[String]] = []+ let watch = try #require(button.cell).observe(\.state) { _, _ in+ MainActor.assumeIsolated { stacks.append(Thread.callStackSymbols) }+ }+ defer { watch.invalidate() }++ choice.isOn = true+ await settle(window) { button.state == .on }+ choice.isOn = false+ await settle(window) { button.state == .off }++ #expect(stacks.count == 2)+ #expect(!stacks.joined().contains { $0.contains("AttributeGraph") })+ }++ @Test func aStateWaitsForTheNextLayoutAndTheLastOneAskedForIsShown() {+ let host = NativeCheckbox.Host()+ host.show(.on)+ #expect(host.button.state == .off)+ host.show(.mixed)+ host.layout()+ #expect(host.button.state == .mixed)+ #expect(host.button.allowsMixedState)+ }++ @Test func aLayoutLeavesWhatAClickChangedAlone() {+ let host = NativeCheckbox.Host()+ host.show(.on)+ host.show(.off)+ host.button.state = .on+ host.layout()+ #expect(host.button.state == .on)+ }++ @Test func aNewCheckboxShowsItsStateFromTheStart() throws {+ let on = Choice()+ on.isOn = true+ #expect(try shown(on).1.state == .on)+ let mixed = Choice()+ mixed.isMixed = true+ let (_, button) = try shown(mixed)+ #expect(button.state == .mixed)+ #expect(button.allowsMixedState)+ }++ @Test func aMixedCheckboxLeavesTheMixedStateWhenItGoes() async throws {+ let choice = Choice()+ choice.isMixed = true+ let (window, button) = try shown(choice)+ choice.isMixed = false+ choice.isOn = true+ await settle(window) { button.state == .on }+ #expect(button.state == .on)+ #expect(!button.allowsMixedState)+ }+}