@@ -78,43 +78,41 @@ extension NSDocumentController {
7878 onCompletion: @escaping ( ) -> Void ,
7979 onCancel: @escaping ( ) -> Void
8080 ) {
81- // Configure NSSavePanel
81+
82+ // 1. Configure NSSavePanel ------------------------------------------------
8283 let panel = NSSavePanel ( )
83- panel. prompt = configuration. prompt
84- panel. title = configuration. title
85- panel. nameFieldLabel = configuration. nameFieldLabel
84+ panel. prompt = configuration. prompt
85+ panel. title = configuration. title
86+ panel. nameFieldLabel = configuration. nameFieldLabel
8687 panel. canCreateDirectories = true
87- panel. directoryURL = configuration. directoryURL
88- panel. level = . modalPanel
88+ panel. directoryURL = configuration. directoryURL
89+ panel. level = . modalPanel
8990 panel. treatsFilePackagesAsDirectories = true
9091
9192 switch mode {
9293 case . file:
9394 panel. nameFieldStringValue = configuration. defaultFileName
94- panel. allowedContentTypes = configuration. allowedContentTypes
95+ panel. allowedContentTypes = configuration. allowedContentTypes
9596 case . folder:
9697 panel. nameFieldStringValue =
97- URL ( fileURLWithPath: configuration. defaultFileName)
98- . deletingPathExtension ( ) . lastPathComponent
99- panel. allowedContentTypes = [ ] // treat as folder
98+ URL ( fileURLWithPath: configuration. defaultFileName)
99+ . deletingPathExtension ( ) . lastPathComponent
100+ panel. allowedContentTypes = [ ] // treat as folder
100101 }
101102
102103 DispatchQueue . main. async { onDialogPresented ( ) }
103104 guard panel. runModal ( ) == . OK,
104105 let baseURL = panel. url else { onCancel ( ) ; return }
105106
106- // Derive the final document file URL
107- let finalURL : URL
108- if mode == . file && configuration. includeExtension {
109- let ext = configuration. defaultFileType. preferredFilenameExtension ?? " file "
110- finalURL = baseURL. appendingPathExtension ( ext)
111- } else {
112- finalURL = baseURL
113- }
107+ // 2. Derive the final document file URL ----------------------------------
108+ let ext = configuration. defaultFileType. preferredFilenameExtension ?? " file "
109+ let finalURL = ( mode == . folder)
110+ ? baseURL. appendingPathComponent ( " \( baseURL. lastPathComponent) . \( ext) " )
111+ : baseURL
114112
115- // Create, write, and open the NSDocument
113+ // 3. Create, write, and open the NSDocument ------------------------------
116114 do {
117- let document = try makeUntitledDocument ( ofType: configuration. defaultFileType. identifier)
115+ let document = try makeUntitledDocument ( ofType: configuration. defaultFileType. identifier)
118116 document. fileURL = finalURL
119117
120118 try document. write (
@@ -147,7 +145,7 @@ extension NSDocumentController {
147145 /// - onCancel: Called if the user cancels or an error occurs.
148146 @MainActor
149147 public func openDocumentWithDialog(
150- configuration: DocumentOpenDialogConfiguration = . init ( ) ,
148+ configuration: DocumentOpenDialogConfiguration = DocumentOpenDialogConfiguration ( ) ,
151149 onDialogPresented: @escaping ( ) -> Void = { } ,
152150 onCompletion: @escaping ( ) -> Void = { } ,
153151 onCancel: @escaping ( ) -> Void = { }
0 commit comments