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
Command-Delete in a search field deletes text instead of asking to move files
Țugui Dragoș7d5d3764890d
diff --git a/Packages/PeelCore/Sources/PeelCore/Support/TextEditing.swift b/Packages/PeelCore/Sources/PeelCore/Support/TextEditing.swiftnew file mode 100644index 00000000..8300c756--- /dev/null+++ b/Packages/PeelCore/Sources/PeelCore/Support/TextEditing.swift@@ -0,0 +1,47 @@+public import AppKit+public import Observation++/// Whether text is being typed in the key window. A key equivalent reaches the menus before the text system, so a+/// command whose key also edits text, such as Command-Delete, is turned off meanwhile and the key reaches the text.+@MainActor+@Observable+public final class TextEditing {+ public private(set) var isEditing = false+ @ObservationIgnored private let keyWindow: @MainActor () -> NSWindow?+ @ObservationIgnored private var responder: NSKeyValueObservation?+ @ObservationIgnored private var observers: [any NSObjectProtocol] = []++ public init(keyWindow: @escaping @MainActor () -> NSWindow? = { NSApp.keyWindow }) {+ self.keyWindow = keyWindow+ }++ /// Follows whichever window is key from now on. The window a notification names is not asked: the list macOS+ /// shows to complete a word says it became key while the field's window stays the key one.+ public func start() {+ guard observers.isEmpty else { return }+ observers = [NSWindow.didBecomeKeyNotification, NSWindow.didResignKeyNotification].map { name in+ NotificationCenter.default.addObserver(forName: name, object: nil, queue: .main) { [weak self] _ in+ MainActor.assumeIsolated {+ guard let self else { return }+ self.follow(self.keyWindow())+ }+ }+ }+ follow(keyWindow())+ }++ func follow(_ window: NSWindow?) {+ // `firstResponder` is KVO compliant (`NSWindow.h`), and AppKit changes it on the main thread.+ responder = window?.observe(\.firstResponder, options: [.initial, .new]) { [weak self] window, _ in+ MainActor.assumeIsolated { self?.isEditing = Self.edits(window.firstResponder) }+ }+ if window == nil {+ isEditing = false+ }+ }++ /// A text field being typed in answers with its field editor, an `NSTextView`, as the first responder.+ static func edits(_ responder: NSResponder?) -> Bool {+ (responder as? NSText)?.isEditable == true+ }+}diff --git a/Packages/PeelCore/Tests/PeelCoreTests/TextEditingTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/TextEditingTests.swiftnew file mode 100644index 00000000..cd1a000f--- /dev/null+++ b/Packages/PeelCore/Tests/PeelCoreTests/TextEditingTests.swift@@ -0,0 +1,66 @@+import AppKit+@testable import PeelCore+import Testing++@MainActor+struct TextEditingTests {+ /// A text field being typed in makes its window's field editor the first responder, and while it is, a command+ /// whose key is also a text editing key leaves that key to the text.+ @Test func followsTheFieldEditorOfTheWindowItWatches() {+ let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 300, height: 100), styleMask: [.titled], backing: .buffered, defer: true)+ let field = NSTextField(frame: NSRect(x: 10, y: 10, width: 200, height: 24))+ let label = NSTextField(labelWithString: "Name")+ window.contentView?.addSubview(field)+ window.contentView?.addSubview(label)+ let editing = TextEditing()+ editing.follow(window)+ #expect(!editing.isEditing)++ window.makeFirstResponder(field)+ #expect(editing.isEditing)++ window.makeFirstResponder(nil)+ #expect(!editing.isEditing)++ window.makeFirstResponder(field)+ editing.follow(nil)+ #expect(!editing.isEditing)+ }++ /// A window that is not key can still say it became key, as the list macOS shows to complete a word does+ /// while a field is typed in. The field's window is still the key one, so the text is still being edited.+ @Test func asksWhichWindowIsKeyWheneverOneSaysItBecameKey() {+ let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: 300, height: 100), styleMask: [.titled], backing: .buffered, defer: true)+ let field = NSTextField(frame: NSRect(x: 10, y: 10, width: 200, height: 24))+ window.contentView?.addSubview(field)+ window.makeFirstResponder(field)+ let completions = NSPanel(contentRect: NSRect(x: 0, y: 0, width: 100, height: 100), styleMask: [], backing: .buffered, defer: true)+ var key: NSWindow? = window+ let editing = TextEditing(keyWindow: { key })+ editing.start()+ #expect(editing.isEditing)++ NotificationCenter.default.post(name: NSWindow.didBecomeKeyNotification, object: completions)+ #expect(editing.isEditing)++ key = completions+ NotificationCenter.default.post(name: NSWindow.didBecomeKeyNotification, object: completions)+ #expect(!editing.isEditing)++ key = nil+ NotificationCenter.default.post(name: NSWindow.didResignKeyNotification, object: completions)+ #expect(!editing.isEditing)+ }++ /// Only text that can be typed in counts: selecting the words of a label, or a list holding the focus, edits+ /// nothing.+ @Test func countsOnlyTextThatCanBeTypedIn() {+ let editable = NSTextView()+ let readOnly = NSTextView()+ readOnly.isEditable = false+ #expect(TextEditing.edits(editable))+ #expect(!TextEditing.edits(readOnly))+ #expect(!TextEditing.edits(NSTableView()))+ #expect(!TextEditing.edits(nil))+ }+}diff --git a/Peel/Navigation/PeelCommands.swift b/Peel/Navigation/PeelCommands.swiftindex 300ebcff..c0d6008a 100644--- a/Peel/Navigation/PeelCommands.swift+++ b/Peel/Navigation/PeelCommands.swift@@ -48,6 +48,7 @@ struct PeelCommands: Commands {/// What the Applications toolbar's two menus act on, so the menu bar can offer them as well.let library: AppLibrarylet homebrew: HomebrewLibrary+ let textEditing: TextEditing@Environment(\.openWindow) private var openWindow@FocusedValue(\.removalHistory) private var history@FocusedBinding(\.selectedTool) private var selectedTool@@ -88,7 +89,7 @@ struct PeelCommands: Commands {Divider()Button(String(localized: moveToTrash?.title ?? "Move to Trash")) { moveToTrash?.perform() }.keyboardShortcut(.delete, modifiers: .command)- .disabled(moveToTrash?.isEnabled != true)+ .disabled(moveToTrash?.isEnabled != true || textEditing.isEditing)Divider()Menu("Export List of Apps") {ForEach(Inventory.Format.allCases.filter { $0 != .brewfile || homebrew.isInstalled }, id: \.self) { format indiff --git a/Peel/PeelApp.swift b/Peel/PeelApp.swiftindex 5b98889a..39f3908d 100644--- a/Peel/PeelApp.swift+++ b/Peel/PeelApp.swift@@ -51,6 +51,7 @@ struct PeelApp: App {@State private var space: SpaceLibrary@State private var carrier: SelectionCarrier@State private var background = StandingWork()+ @State private var textEditing = TextEditing()@State private var hasLaunched = falseprivate let exclusions = ExclusionsStore.shared@@ -291,11 +292,12 @@ struct PeelApp: App {// Started once, and not as a child of this view's task, so the work goes on in the menu bar// after the window closes.background.start([followFolders, askWhenDue, followFindings, followActivations])+ textEditing.start()}}.defaultSize(width: 1120, height: 764).commands {- PeelCommands(library: library, homebrew: homebrew)+ PeelCommands(library: library, homebrew: homebrew, textEditing: textEditing)}.onChange(of: watchesTrash) { _, isWatching inif isWatching {