Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

VoiceOver opens a row's menu from its checkbox, selectable or not

Țugui Dragoș889199fe65b6
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/RowCheckboxButton.swift b/Packages/PeelCore/Sources/PeelCore/Support/RowCheckboxButton.swift
new file mode 100644
index 00000000..42fa2cee
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCore/Support/RowCheckboxButton.swift
@@ -0,0 +1,32 @@
+public import AppKit
+
+/// AppKit's checkbox for a row of a list. When VoiceOver asks for its menu, it opens what a right click on it opens,
+/// the row's menu, even while the checkbox cannot be changed.
+public final class RowCheckboxButton: NSButton {
+ public override func accessibilityPerformShowMenu() -> Bool {
+ guard window != nil else { return false }
+ // From the run loop, as AppKit opens a view's menu, so VoiceOver has its answer before the menu holds the
+ // main thread.
+ RunLoop.main.perform { [weak self] in
+ MainActor.assumeIsolated { self?.rightClick() }
+ }
+ return true
+ }
+
+ private func rightClick() {
+ guard let window else { return }
+ let center = convert(NSPoint(x: bounds.midX, y: bounds.midY), to: nil)
+ guard let event = NSEvent.mouseEvent(
+ with: .rightMouseDown,
+ location: center,
+ modifierFlags: [],
+ timestamp: ProcessInfo.processInfo.systemUptime,
+ windowNumber: window.windowNumber,
+ context: nil,
+ eventNumber: 0,
+ clickCount: 1,
+ pressure: 1
+ ) else { return }
+ rightMouseDown(with: event)
+ }
+}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/RowCheckboxButtonTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/RowCheckboxButtonTests.swift
new file mode 100644
index 00000000..1d047b26
--- /dev/null
+++ b/Packages/PeelCore/Tests/PeelCoreTests/RowCheckboxButtonTests.swift
@@ -0,0 +1,39 @@
+import AppKit
+@testable import PeelCore
+import Testing
+
+@MainActor
+struct RowCheckboxButtonTests {
+ @Test func showMenuOpensWhatARightClickOnItOpensEvenWhileItCannotBeChanged() {
+ let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 300, height: 100), styleMask: [.titled], backing: .buffered, defer: false)
+ let row = RightClicks(frame: NSRect(x: 0, y: 0, width: 300, height: 100))
+ window.contentView = row
+ let checkbox = RowCheckboxButton(frame: NSRect(x: 20, y: 30, width: 16, height: 16))
+ checkbox.setButtonType(.switch)
+ checkbox.isEnabled = false
+ row.addSubview(checkbox)
+
+ #expect(checkbox.accessibilityPerformShowMenu())
+ #expect(row.clicks.isEmpty)
+ let deadline = Date(timeIntervalSinceNow: 2)
+ while row.clicks.isEmpty, Date() < deadline {
+ RunLoop.main.run(mode: .default, before: Date(timeIntervalSinceNow: 0.05))
+ }
+ #expect(row.clicks == [NSPoint(x: 28, y: 38)])
+ #expect(checkbox.state == .off)
+ }
+
+ @Test func showMenuOpensNothingOutsideAWindow() {
+ let checkbox = RowCheckboxButton(frame: NSRect(x: 0, y: 0, width: 16, height: 16))
+ checkbox.setButtonType(.switch)
+ #expect(!checkbox.accessibilityPerformShowMenu())
+ }
+}
+
+private final class RightClicks: NSView {
+ var clicks: [NSPoint] = []
+
+ override func rightMouseDown(with event: NSEvent) {
+ clicks.append(event.locationInWindow)
+ }
+}
diff --git a/Peel/Views/NativeCheckbox.swift b/Peel/Views/NativeCheckbox.swift
index e4ae4f80..d84c1c27 100644
--- a/Peel/Views/NativeCheckbox.swift
+++ b/Peel/Views/NativeCheckbox.swift
@@ -1,4 +1,5 @@
import AppKit
+import PeelCore
import SwiftUI
/// AppKit's checkbox, for the rows of a list. A checkbox `Toggle` is the same button, but SwiftUI asks it for its size
@@ -13,21 +14,38 @@ struct NativeCheckbox: NSViewRepresentable {
Coordinator(isOn: $isOn)
}
- func makeNSView(context: Context) -> NSButton {
- let button = Self.checkbox()
- button.target = context.coordinator
- button.action = #selector(Coordinator.toggle(_:))
- update(button, in: context)
- return button
+ func makeNSView(context: Context) -> Host {
+ let host = Host()
+ host.button.target = context.coordinator
+ host.button.action = #selector(Coordinator.toggle(_:))
+ update(host.button, in: context)
+ return host
}
- func updateNSView(_ button: NSButton, context: Context) {
+ func updateNSView(_ host: Host, context: Context) {
context.coordinator.isOn = $isOn
- update(button, in: context)
+ update(host.button, in: context)
+ }
+
+ func sizeThatFits(_ proposal: ProposedViewSize, nsView host: Host, context: Context) -> CGSize? {
+ Self.size(at: host.button.controlSize)
}
- func sizeThatFits(_ proposal: ProposedViewSize, nsView: NSButton, context: Context) -> CGSize? {
- Self.size(at: nsView.controlSize)
+ /// Holds the button one level below the view SwiftUI hosts, so the button answers VoiceOver's Show Menu itself.
+ /// SwiftUI answers it for the view it hosts and opens nothing while that view is disabled, which would take the
+ /// row's menu away from a row that cannot be selected.
+ final class Host: NSView {
+ let button = NativeCheckbox.checkbox()
+
+ init() {
+ super.init(frame: .zero)
+ button.autoresizingMask = [.width, .height]
+ addSubview(button)
+ }
+
+ required init?(coder: NSCoder) {
+ nil
+ }
}
private func update(_ button: NSButton, in context: Context) {
@@ -43,7 +61,7 @@ struct NativeCheckbox: NSViewRepresentable {
/// Not `init(checkboxWithTitle:target:action:)`, which sizes the button to its title twice on the way.
private static func checkbox() -> NSButton {
- let button = NSButton(frame: .zero)
+ let button = RowCheckboxButton(frame: .zero)
button.setButtonType(.switch)
button.title = ""
return button