@@ -66,28 +66,17 @@ extension NSDocumentController {
66
66
}
67
67
68
68
// MARK: - Private shared implementation
69
- // ─────────────────────────────────────────────────────────────────────
70
69
private enum SaveMode { case file, folder }
71
70
72
- /// Internal helper that contains 100 % of the common logic.
73
- @MainActor
74
- private func _createDocument(
75
- mode: SaveMode ,
76
- configuration: DocumentSaveDialogConfiguration ,
77
- onDialogPresented: @escaping ( ) -> Void ,
78
- onCompletion: @escaping ( ) -> Void ,
79
- onCancel: @escaping ( ) -> Void
80
- ) {
81
-
82
- // 1 ────────────────────────────────────────────────────────────────
83
- // Configure the NSSavePanel
71
+ /// Configure a save panel for ``_createDocument``.
72
+ private func configureSavePanel( mode: SaveMode , configuration: DocumentSaveDialogConfiguration ) -> NSSavePanel {
84
73
let panel = NSSavePanel ( )
85
- panel. prompt = configuration. prompt
86
- panel. title = configuration. title
87
- panel. nameFieldLabel = configuration. nameFieldLabel
74
+ panel. prompt = configuration. prompt
75
+ panel. title = configuration. title
76
+ panel. nameFieldLabel = configuration. nameFieldLabel
88
77
panel. canCreateDirectories = true
89
- panel. directoryURL = configuration. directoryURL
90
- panel. level = . modalPanel
78
+ panel. directoryURL = configuration. directoryURL
79
+ panel. level = . modalPanel
91
80
panel. treatsFilePackagesAsDirectories = true
92
81
93
82
switch mode {
@@ -96,52 +85,65 @@ extension NSDocumentController {
96
85
panel. allowedContentTypes = configuration. allowedContentTypes
97
86
case . folder:
98
87
panel. nameFieldStringValue =
99
- URL ( fileURLWithPath: configuration. defaultFileName)
100
- . deletingPathExtension ( )
101
- . lastPathComponent
88
+ URL ( fileURLWithPath: configuration. defaultFileName)
89
+ . deletingPathExtension ( )
90
+ . lastPathComponent
102
91
panel. allowedContentTypes = [ ] // treat as plain folder
103
92
}
104
93
94
+ return panel
95
+ }
96
+
97
+ /// Internal helper that contains 100 % of the common logic.
98
+ @MainActor
99
+ private func _createDocument(
100
+ mode: SaveMode ,
101
+ configuration: DocumentSaveDialogConfiguration ,
102
+ onDialogPresented: @escaping ( ) -> Void ,
103
+ onCompletion: @escaping ( ) -> Void ,
104
+ onCancel: @escaping ( ) -> Void
105
+ ) {
106
+ // 1 ────────────────────────────────────────────────────────────────
107
+ // Configure the NSSavePanel
108
+ let panel = configureSavePanel ( mode: mode, configuration: configuration)
109
+
105
110
DispatchQueue . main. async { onDialogPresented ( ) }
106
111
107
112
guard panel. runModal ( ) == . OK,
108
- let baseURL = panel . url // e.g. …/ProjectName
109
- else {
113
+ // e.g. …/ProjectName
114
+ let baseURL = panel . url else {
110
115
onCancel ( )
111
116
return
112
117
}
113
118
114
- // 2 ────────────────────────────────────────────────────────────────
115
- // For a *folder* document, create the workspace directory up front.
116
- if mode == . folder {
117
- do {
119
+ do {
120
+ // 2 ────────────────────────────────────────────────────────────────
121
+ // For a * folder* document, create the workspace directory up front.
122
+ if mode == . folder {
118
123
try FileManager . default. createDirectory (
119
124
at: baseURL,
120
125
withIntermediateDirectories: true ,
121
126
attributes: nil
122
127
)
123
- } catch {
124
- NSAlert ( error: error) . runModal ( )
125
- onCancel ( )
126
- return
127
128
}
128
- }
129
129
130
- // 3 ────────────────────────────────────────────────────────────────
131
- // Derive the final URL of the actual NSDocument header file.
132
- // …/ProjectName/ProjectName.circuitproj (folder mode)
133
- // …/SomeFile.circuitproj (file mode)
134
- let ext = configuration
135
- . defaultFileType
136
- . preferredFilenameExtension ?? " file "
130
+ // 3 ────────────────────────────────────────────────────────────────
131
+ // Derive the final URL of the actual NSDocument header file.
132
+ // …/ProjectName/ProjectName.circuitproj (folder mode)
133
+ // …/SomeFile.circuitproj (file mode)
134
+ let ext = configuration
135
+ . defaultFileType
136
+ . preferredFilenameExtension ?? " file "
137
137
138
- let finalURL = ( mode == . folder)
139
- ? baseURL. appendingPathComponent ( " \( baseURL. lastPathComponent) . \( ext) " )
140
- : baseURL
138
+ let finalURL = if mode == . folder {
139
+ baseURL. appendingPathComponent ( " \( baseURL. lastPathComponent) . \( ext) " )
140
+ } else {
141
+ baseURL
142
+ }
143
+
144
+ // 4 ────────────────────────────────────────────────────────────────
145
+ // Create, write and open the document.
141
146
142
- // 4 ────────────────────────────────────────────────────────────────
143
- // Create, write and open the document.
144
- do {
145
147
let document = try makeUntitledDocument (
146
148
ofType: configuration. defaultFileType. identifier
147
149
)
@@ -154,14 +156,10 @@ extension NSDocumentController {
154
156
originalContentsURL: nil
155
157
)
156
158
157
- openDocument (
158
- at: finalURL,
159
- onCompletion: onCompletion,
160
- onError: { error in
161
- NSAlert ( error: error) . runModal ( )
162
- onCancel ( )
163
- }
164
- )
159
+ openDocument ( at: finalURL, onCompletion: onCompletion, onError: { error in
160
+ NSAlert ( error: error) . runModal ( )
161
+ onCancel ( )
162
+ } )
165
163
} catch {
166
164
NSAlert ( error: error) . runModal ( )
167
165
onCancel ( )
0 commit comments