@@ -44,39 +44,37 @@ struct SelectFileButton<Label>: View where Label: View {
4444 let label : ( ) -> Label
4545 let model : Binding < String >
4646
47- @State private var openPanel = NSOpenPanel ( )
4847 @State private var duplicateFile : DuplicateFile ? = nil
4948
5049 var body : some View {
5150 HStack {
5251 Button {
5352 mkdirP ( directory. localPath ( ) )
54- // Only consider the first file, but allow multiple deletion.
55- openPanel. allowsMultipleSelection = true
56- openPanel. canChooseDirectories = false
57- openPanel. allowedContentTypes = allowedContentTypes
58- openPanel. directoryURL = directory
59- openPanel. begin { response in
60- if response == . OK {
61- guard let file = openPanel. urls. first else {
53+ let _ = selectFile (
54+ allowsMultipleSelection: false ,
55+ canChooseDirectories: false ,
56+ canChooseFiles: true ,
57+ allowedContentTypes: allowedContentTypes,
58+ directoryURL: directory
59+ ) { urls, _ in
60+ guard let file = urls. first else {
61+ return
62+ }
63+ var fileName = file. lastPathComponent
64+ if !directory. contains ( file) {
65+ let dst = directory. appendingPathComponent ( fileName)
66+ if dst. exists ( ) {
67+ duplicateFile = DuplicateFile ( url: file)
6268 return
6369 }
64- var fileName = file. lastPathComponent
65- if !directory. contains ( file) {
66- let dst = directory. appendingPathComponent ( fileName)
67- if dst. exists ( ) {
68- duplicateFile = DuplicateFile ( url: file)
69- return
70- }
71- if !copyFile( file, dst) {
72- return
73- }
74- } else {
75- // Need to consider subdirectory of www/img.
76- fileName = String ( file. localPath ( ) . dropFirst ( directory. localPath ( ) . count) )
70+ if !copyFile( file, dst) {
71+ return
7772 }
78- onFinish ( fileName)
73+ } else {
74+ // Need to consider subdirectory of www/img.
75+ fileName = String ( file. localPath ( ) . dropFirst ( directory. localPath ( ) . count) )
7976 }
77+ onFinish ( fileName)
8078 }
8179 } label: {
8280 label ( )
@@ -114,17 +112,16 @@ struct SelectFileButton<Label>: View where Label: View {
114112}
115113
116114@MainActor
117- func selectApplication( _ openPanel: NSOpenPanel , onFinish: @escaping ( String ) -> Void ) {
118- openPanel. allowsMultipleSelection = false
119- openPanel. canChooseDirectories = false
120- openPanel. allowedContentTypes = [ . application]
121- openPanel. directoryURL = URL ( fileURLWithPath: " /Applications " )
122- openPanel. begin { response in
123- if response == . OK {
124- let selectedApp = openPanel. urls. first
125- if let appURL = selectedApp {
126- onFinish ( appURL. localPath ( ) )
127- }
115+ func selectApplication( onFinish: @escaping ( String ) -> Void ) {
116+ let _ = selectFile (
117+ allowsMultipleSelection: false ,
118+ canChooseDirectories: false ,
119+ canChooseFiles: true ,
120+ allowedContentTypes: [ . application] ,
121+ directoryURL: URL ( fileURLWithPath: " /Applications " )
122+ ) { urls, _ in
123+ if let url = urls. first {
124+ onFinish ( url. localPath ( ) )
128125 }
129126 }
130127}
0 commit comments