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
Undo and Redo take back typing in a text field
Țugui Dragoșa8c77474f990
diff --git a/Localization/Peel/Localizable.xcstrings b/Localization/Peel/Localizable.xcstringsindex bd458736..e6a27d1c 100644--- a/Localization/Peel/Localizable.xcstrings+++ b/Localization/Peel/Localizable.xcstrings@@ -97010,6 +97010,112 @@}}},+ "Redo" : {+ "localizations" : {+ "cs" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Opakovat akci"+ }+ },+ "de" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Wiederholen"+ }+ },+ "es" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Rehacer"+ }+ },+ "fr" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Rétablir"+ }+ },+ "it" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Ripristina"+ }+ },+ "ja" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "やり直す"+ }+ },+ "ko" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "실행 복귀"+ }+ },+ "nl" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Opnieuw"+ }+ },+ "pl" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Przywróć"+ }+ },+ "pt" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Refazer"+ }+ },+ "ro" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Refaceți"+ }+ },+ "ru" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Повторить"+ }+ },+ "sv" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Gör om"+ }+ },+ "tr" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Yinele"+ }+ },+ "uk" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "Повторити"+ }+ },+ "zh-Hans" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "重做"+ }+ },+ "zh-Hant" : {+ "stringUnit" : {+ "state" : "translated",+ "value" : "重做"+ }+ }+ }+ },"Release Notes" : {"localizations" : {"cs" : {diff --git a/Packages/PeelCore/Sources/PeelCore/Support/TextEditing.swift b/Packages/PeelCore/Sources/PeelCore/Support/TextEditing.swiftindex 8300c756..eaeceb62 100644--- a/Packages/PeelCore/Sources/PeelCore/Support/TextEditing.swift+++ b/Packages/PeelCore/Sources/PeelCore/Support/TextEditing.swift@@ -7,9 +7,22 @@ public import Observation@Observablepublic final class TextEditing {public private(set) var isEditing = false+ /// What Undo and Redo would do to the text being typed, or nil while nothing is.+ public private(set) var typing: Typing?@ObservationIgnored private let keyWindow: @MainActor () -> NSWindow?+ @ObservationIgnored private var window: NSWindow?@ObservationIgnored private var responder: NSKeyValueObservation?@ObservationIgnored private var observers: [any NSObjectProtocol] = []+ @ObservationIgnored private var followedUndoManager: UndoManager?+ @ObservationIgnored private var undoObservers: [any NSObjectProtocol] = []++ /// The items Undo and Redo show for the text being typed, titled by its undo manager as the standard items are.+ public struct Typing: Equatable, Sendable {+ public let undo: String+ public let redo: String+ public let canUndo: Bool+ public let canRedo: Bool+ }public init(keyWindow: @escaping @MainActor () -> NSWindow? = { NSApp.keyWindow }) {self.keyWindow = keyWindow@@ -31,12 +44,55 @@ public final class TextEditing {}func follow(_ window: NSWindow?) {+ self.window = window// `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) }+ responder = window?.observe(\.firstResponder, options: [.initial, .new]) { [weak self] _, _ in+ MainActor.assumeIsolated { self?.read() }}if window == nil {- isEditing = false+ read()+ }+ }++ public func undo() {+ editedUndoManager?.undo()+ }++ public func redo() {+ editedUndoManager?.redo()+ }++ private var editedUndoManager: UndoManager? {+ guard let responder = window?.firstResponder, Self.edits(responder) else { return nil }+ return responder.undoManager+ }++ private func read() {+ isEditing = Self.edits(window?.firstResponder)+ let undoManager = editedUndoManager+ typing = undoManager.map {+ Typing(undo: $0.undoMenuItemTitle, redo: $0.redoMenuItemTitle, canUndo: $0.canUndo, canRedo: $0.canRedo)+ }+ follow(undoManager)+ }++ /// Reads the titles again whenever the undo manager of the text being typed records, undoes or redoes+ /// something. That undo manager is its window's, which AppKit uses on the main thread.+ private func follow(_ undoManager: UndoManager?) {+ guard undoManager !== followedUndoManager else { return }+ undoObservers.forEach(NotificationCenter.default.removeObserver)+ followedUndoManager = undoManager+ guard let undoManager else {+ undoObservers = []+ return+ }+ let names: [Notification.Name] = [+ .NSUndoManagerCheckpoint, .NSUndoManagerDidCloseUndoGroup, .NSUndoManagerDidUndoChange, .NSUndoManagerDidRedoChange,+ ]+ undoObservers = names.map { name in+ NotificationCenter.default.addObserver(forName: name, object: undoManager, queue: nil) { [weak self] _ in+ MainActor.assumeIsolated { self?.read() }+ }}}diff --git a/Packages/PeelCore/Tests/PeelCoreTests/StringCatalogTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/StringCatalogTests.swiftindex 11e6f97c..f2ff21d9 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/StringCatalogTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/StringCatalogTests.swift@@ -266,7 +266,7 @@ struct CatalogChecker {static let swiftUILanguages = ["zh-Hans": "zh_CN", "zh-Hant": "zh_TW", "pt": "pt_BR"]/// The items Peel puts in menus SwiftUI builds, each with SwiftUI's key. Their words are SwiftUI's, whatever/// the English says: in Polish, About Peel reads "Peel…".- static let menuWords = ["About Peel": "About %@", "Quit Peel": "Quit %@", "Settings…": "Settings…", "Find": "Find", "Undo": "Undo", "Copy": "Copy", "Select All": "Select All"]+ static let menuWords = ["About Peel": "About %@", "Quit Peel": "Quit %@", "Settings…": "Settings…", "Find": "Find", "Undo": "Undo", "Redo": "Redo", "Copy": "Copy", "Select All": "Select All"]/// Names SwiftUI looks up in the app's own strings, so an app without the key shows them in English: here, the/// name of the tab bar a `TabView` puts in the toolbar, which VoiceOver reads and the overflow menu shows.static let swiftUIWords = ["Navigation Tab Bar": "Navigation Tab Bar"]diff --git a/Packages/PeelCore/Tests/PeelCoreTests/TextEditingTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/TextEditingTests.swiftindex 6b61c3c9..a5057fa9 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/TextEditingTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/TextEditingTests.swift@@ -52,6 +52,45 @@ struct TextEditingTests {#expect(!editing.isEditing)}+ /// Undo and Redo act on the typing in the field being edited, and say what they would take back in the+ /// words of the field's undo manager.+ @Test func undoesAndRedoesTheTypingInTheFieldBeingEdited() throws {+ 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)+ let editing = TextEditing()+ editing.follow(window)+ #expect(editing.typing == nil)++ window.makeFirstResponder(field)+ let editor = try #require(window.firstResponder as? NSTextView)+ let undoManager = try #require(editor.undoManager)+ let nothingTyped = TextEditing.Typing(+ undo: undoManager.undoMenuItemTitle, redo: undoManager.redoMenuItemTitle, canUndo: false, canRedo: false+ )+ #expect(editing.typing == nothingTyped)++ undoManager.beginUndoGrouping()+ editor.insertText("Xcode", replacementRange: editor.selectedRange())+ undoManager.endUndoGrouping()+ #expect(editor.string == "Xcode")+ #expect(editing.typing?.canUndo == true)+ #expect(editing.typing?.undo == undoManager.undoMenuItemTitle)++ editing.undo()+ #expect(editor.string.isEmpty)+ #expect(editing.typing?.canUndo == false)+ #expect(editing.typing?.canRedo == true)++ editing.redo()+ #expect(editor.string == "Xcode")+ #expect(editing.typing?.canUndo == true)+ #expect(editing.typing?.canRedo == false)++ window.makeFirstResponder(nil)+ #expect(editing.typing == nil)+ }+/// 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() {diff --git a/Peel/Navigation/PeelCommands.swift b/Peel/Navigation/PeelCommands.swiftindex c0d6008a..d72d46d6 100644--- a/Peel/Navigation/PeelCommands.swift+++ b/Peel/Navigation/PeelCommands.swift@@ -56,20 +56,32 @@ struct PeelCommands: Commands {@FocusedValue(\.moveToTrash) private var moveToTrashvar body: some Commands {- // In an app with no documents, `@Environment(\.undoManager)` is nil, so the standard Undo item stays- // disabled. This one undoes the last removal, as Finder's Undo does after a move to the Trash.+ // Undo takes back the typing while text is being edited, and otherwise the last removal, as Finder's+ // Undo does after a move to the Trash.CommandGroup(replacing: .undoRedo) {- Button {- Task { await history?.undoLastRemoval() }- } label: {- if let name = history?.undoName {- Text("Undo Remove \(name)")- } else {- Text("Undo")+ if let typing = textEditing.typing {+ Button(typing.undo) { textEditing.undo() }+ .keyboardShortcut("z", modifiers: .command)+ .disabled(!typing.canUndo)+ Button(typing.redo) { textEditing.redo() }+ .keyboardShortcut("z", modifiers: [.command, .shift])+ .disabled(!typing.canRedo)+ } else {+ Button {+ Task { await history?.undoLastRemoval() }+ } label: {+ if let name = history?.undoName {+ Text("Undo Remove \(name)")+ } else {+ Text("Undo")+ }}+ .keyboardShortcut("z", modifiers: .command)+ .disabled(history?.undoName == nil || history?.isRestoring == true)+ Button("Redo") {}+ .keyboardShortcut("z", modifiers: [.command, .shift])+ .disabled(true)}- .keyboardShortcut("z", modifiers: .command)- .disabled(history?.undoName == nil || history?.isRestoring == true)}CommandGroup(replacing: .appInfo) {