Skip to content

Commit 0bf478e

Browse files
- reverting back some changes
1 parent 8cb4ce5 commit 0bf478e

3 files changed

Lines changed: 20 additions & 27 deletions

File tree

Example/WelcomeWindowExample/WelcomeWindowExampleApp.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct WelcomeWindowExampleApp: App {
3636
action: {
3737
NSDocumentController.shared.openDocumentWithDialog(
3838
configuration: .init(canChooseDirectories: true),
39-
onDialogPresented: { dismiss() },
39+
onCompletion: { dismiss() },
4040
onCancel: { openWindow(id: "welcome") }
4141
)
4242
}

Sources/WelcomeWindow/Model/DocumentSaveDialogConfiguration.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ public struct DocumentSaveDialogConfiguration {
3737
/// The initial directory shown when the dialog appears.
3838
public var directoryURL: URL?
3939

40-
/// Should file be created with or without an extension.
41-
public var includeExtension: Bool
42-
4340
/// Creates a new `DocumentSaveDialogConfiguration` with the given parameters.
4441
///
4542
/// - Parameters:
@@ -50,7 +47,6 @@ public struct DocumentSaveDialogConfiguration {
5047
/// - defaultFileType: The content type that will be used to create the document. Defaults to `.plainText`.
5148
/// - title: The title of the save dialog window. Default is `"Create a New Document"`.
5249
/// - directoryURL: The default directory URL. Default is the user’s Documents folder.
53-
/// - includeExtension: Should include file extension.
5450
public init(
5551
prompt: String = "Create Document",
5652
nameFieldLabel: String = "File Name:",
@@ -68,6 +64,5 @@ public struct DocumentSaveDialogConfiguration {
6864
self.defaultFileType = defaultFileType
6965
self.title = title
7066
self.directoryURL = directoryURL
71-
self.includeExtension = includeExtension
7267
}
7368
}

Sources/WelcomeWindow/Model/NSDocumentController+Extensions.swift

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)