@@ -70,8 +70,14 @@ struct FolderContextMenu: View {
7070 Button {
7171 withAnimation {
7272 let newFile = FileOrFolder < File < Payload > , Payload > ( file: File ( contents: Payload ( text: " " ) ) )
73- viewContext. add ( item: newFile, $to: $folder, withPreferredName: " Text.txt " )
74- viewContext. viewState. selection = newFile. id
73+ if let newName = viewContext. add ( item: newFile, $to: $folder, withPreferredName: " Text.txt " ) {
74+
75+ viewContext. viewState. selection = newFile. id
76+ Task { @MainActor in
77+ viewContext. viewState. editedLabel = FileNavigatorViewState . EditedLabel ( id: newFile. id, text: newName)
78+ }
79+
80+ }
7581 }
7682 } label: {
7783 Label ( " New file " , systemImage: " doc.badge.plus " )
@@ -80,8 +86,14 @@ struct FolderContextMenu: View {
8086 Button {
8187 withAnimation {
8288 let newFolder = FileOrFolder < File < Payload > , Payload > ( folder: Folder ( children: [ : ] ) )
83- viewContext. add ( item: newFolder, $to: $folder, withPreferredName: " Folder " )
84- viewContext. viewState. selection = newFolder. id
89+ if let newName = viewContext. add ( item: newFolder, $to: $folder, withPreferredName: " Folder " ) {
90+
91+ viewContext. viewState. selection = newFolder. id
92+ Task { @MainActor in
93+ viewContext. viewState. editedLabel = FileNavigatorViewState . EditedLabel ( id: newFolder. id, text: newName)
94+ }
95+
96+ }
8597 }
8698 } label: {
8799 Label ( " New folder " , systemImage: " folder.badge.plus " )
@@ -267,10 +279,10 @@ struct Navigator: View {
267279 // environment. Hence, the SwiftUI document system is not informed of changes to the text by the
268280 // `TextEditor`. As a consequence, (auto)saving does not work.
269281 //
270- // The following codes works around this issues by explicitly registering text changes with the undo
271- // manager provided by the SwiftUI document system. This is only a work around and not a proper solution
272- // as it will also undo changes made to the text outside of the `TextEditor`. (This is not a problem
273- // in this little demo app.)
282+ // The following code works around this issues by explicitly registering text changes with the undo
283+ // manager provided by the SwiftUI document system. This is only a work around and not a proper
284+ // solution as it will also undo changes made to the text outside of the `TextEditor`. (This is not a
285+ // problem in this little demo app, but generally not desirable .)
274286 undoManager? . registerUndo ( withTarget: model) { [ weak undoManager] _ in
275287 $text. wrappedValue = oldValue
276288 changeByUndoManager = true
0 commit comments