Skip to content

Commit

Permalink
switch implementation from fileImporter to NSOpenPanel
Browse files Browse the repository at this point in the history
  • Loading branch information
drewvolz authored Jun 12, 2024
1 parent e0bdb4a commit c8e2ea5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
40 changes: 27 additions & 13 deletions ClassDumper/Views/DatabaseButtons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,45 @@ struct CreateFileButton: View {
@Environment(\.fileRepository) private var fileRepository
@EnvironmentObject var alertController: AlertController

@State private var importing = false
private var titleKey: LocalizedStringKey

init(_ titleKey: LocalizedStringKey = "Open…") {
self.titleKey = titleKey
}

private var panel: NSOpenPanel = {
let panel = NSOpenPanel()
panel.allowedContentTypes = [.application, .executable, .symbolicLink, .aliasFile]
panel.allowsMultipleSelection = false
panel.canChooseDirectories = true
panel.treatsFilePackagesAsDirectories = true
return panel
}()

private func openPanel() {
switch panel.runModal() {
case .OK:
guard let url = panel.url else {
print("Unable to read file url path from NSOpenPanel.")
return
}
success(with: url)
case .cancel:
break // noop, need something on this line with breaks being implicit
case .abort, .stop:
print("Abort or stop result from NSOpenPanel. No filepath to pass.")
default:
print("Something went really wrong with NSOpenPanel. Unable to read file contents")
}
}

var body: some View {
Button {
importing = true
openPanel()
} label: {
Label(titleKey, systemImage: "folder.badge.plus")
}
.keyboardShortcut("o", modifiers: [.command])
.fileImporter(
isPresented: $importing,
allowedContentTypes: [.application, .executable, .symbolicLink, .aliasFile]
) { result in
switch result {
case .success(let file):
success(with: file)
case .failure(let error):
print("Unable to read file contents: \(error.localizedDescription)")
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion ClassDumperUITests/ImportFlow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ struct ImportFlow: Screen {
func openApp(named appName: String) -> Self {
open(.filepicker)

app.sheets.firstMatch.outlineRows.staticTexts["Applications"].tap()
app.dialogs.firstMatch.outlineRows.staticTexts["Applications"].tap()

// taking advantage of finder directing keyboard input towards the middle column
app.typeText(appName)
Expand Down

0 comments on commit c8e2ea5

Please sign in to comment.