@@ -159,6 +159,7 @@ function open_file_navigation_menu(directory_path, handle_select, opts)
159159
160160 local files , directories = read_directory (directory .path , opts .allowed_types )
161161 local is_root = not directory .dirname
162+ local path_separator = path_separator (directory .path )
162163
163164 if not files or not directories then return end
164165
@@ -171,77 +172,72 @@ function open_file_navigation_menu(directory_path, handle_select, opts)
171172
172173 if is_root then
173174 if state .os == ' windows' then
174- items [# items + 1 ] = {
175- title = ' ..' , hint = ' 驱动器列表' , value = {is_drives = true , is_to_parent = true }, separator = true ,
176- }
175+ items [# items + 1 ] = {title = ' ..' , hint = ' 驱动器列表' , value = ' {drives}' , separator = true }
177176 end
178177 else
179- local serialized = serialize_path (directory .dirname )
180- serialized .is_directory = true
181- serialized .is_to_parent = true
182- items [# items + 1 ] = {title = ' ..' , hint = ' 上级目录' , value = serialized , separator = true }
178+ items [# items + 1 ] = {title = ' ..' , hint = ' 上级目录' , value = directory .dirname , separator = true }
183179 end
184180
185- local items_start_index = # items + 1
181+ local selected_index = # items + 1
186182
187- local path_separator = path_separator (directory .path )
188183 for _ , dir in ipairs (directories ) do
189- local serialized = serialize_path (utils .join_path (directory .path , dir ))
190- if serialized then
191- serialized .is_directory = true
192- items [# items + 1 ] = {title = serialized .basename , value = serialized , hint = path_separator }
193- end
184+ items [# items + 1 ] = {title = dir , value = join_path (directory .path , dir ), hint = path_separator }
194185 end
195186
196187 for _ , file in ipairs (files ) do
197- local serialized = serialize_path (utils .join_path (directory .path , file ))
198- if serialized then
199- serialized .is_file = true
200- items [# items + 1 ] = {title = serialized .basename , value = serialized }
201- end
188+ items [# items + 1 ] = {title = file , value = join_path (directory .path , file )}
202189 end
203190
204191 for index , item in ipairs (items ) do
205- if not item .value .is_to_parent then
206- if index == items_start_index then item .selected = true end
207-
208- if opts .active_path == item .value .path then
209- item .active = true
210- if not opts .selected_path then item .selected = true end
211- end
212-
213- if opts .selected_path == item .value .path then item .selected = true end
192+ if not item .value .is_to_parent and opts .active_path == item .value then
193+ item .active = true
194+ if not opts .selected_path then selected_index = index end
214195 end
196+
197+ if opts .selected_path == item .value then selected_index = index end
215198 end
216199
217200 local menu_data = {
218201 type = opts .type , title = opts .title or directory .basename .. path_separator , items = items ,
219- on_open = opts . on_open , on_close = opts . on_close ,
202+ selected_index = selected_index ,
220203 }
204+ local menu_options = {on_open = opts .on_open , on_close = opts .on_close }
221205
222206 return Menu :open (menu_data , function (path )
207+ local is_drives = path == ' {drives}'
208+ local is_to_parent = is_drives or # path < # directory_path
223209 local inheritable_options = {
224210 type = opts .type , title = opts .title , allowed_types = opts .allowed_types , active_path = opts .active_path ,
225211 }
226212
227- if path . is_drives then
213+ if is_drives then
228214 open_drives_menu (function (drive_path )
229215 open_file_navigation_menu (drive_path , handle_select , inheritable_options )
230- end , {type = inheritable_options .type , title = inheritable_options .title , selected_path = directory .path })
216+ end , {
217+ type = inheritable_options .type , title = inheritable_options .title , selected_path = directory .path ,
218+ on_open = opts .on_open , on_close = opts .on_close ,
219+ })
231220 return
232221 end
233222
234- if path .is_directory then
223+ local info , error = utils .file_info (path )
224+
225+ if not info then
226+ msg .error (' Can\' t retrieve path info for "' .. path .. ' ". Error: ' .. (error or ' ' ))
227+ return
228+ end
229+
230+ if info .is_dir then
235231 -- Preselect directory we are coming from
236- if path . is_to_parent then
232+ if is_to_parent then
237233 inheritable_options .selected_path = directory .path
238234 end
239235
240- open_file_navigation_menu (path . path , handle_select , inheritable_options )
236+ open_file_navigation_menu (path , handle_select , inheritable_options )
241237 else
242- handle_select (path . path )
238+ handle_select (path )
243239 end
244- end )
240+ end , menu_options )
245241end
246242
247243-- Opens a file navigation menu with Windows drives as items.
@@ -255,23 +251,25 @@ function open_drives_menu(handle_select, opts)
255251 playback_only = false ,
256252 args = {' wmic' , ' logicaldisk' , ' get' , ' name' , ' /value' },
257253 })
258- local items = {}
254+ local items , selected_index = {}, 1
259255
260256 if process .status == 0 then
261257 for _ , value in ipairs (split (process .stdout , ' \n ' )) do
262258 local drive = string.match (value , ' Name=([A-Z]:)' )
263259 if drive then
264260 local drive_path = normalize_path (drive )
265261 items [# items + 1 ] = {
266- title = drive , hint = ' 盘符' , value = drive_path ,
267- selected = opts .selected_path == drive_path ,
268- active = opts .active_path == drive_path ,
262+ title = drive , hint = ' 盘符' , value = drive_path , active = opts .active_path == drive_path ,
269263 }
264+ if opts .selected_path == drive_path then selected_index = # items end
270265 end
271266 end
272267 else
273268 msg .error (process .stderr )
274269 end
275270
276- return Menu :open ({type = opts .type , title = opts .title or ' 驱动器列表' , items = items }, handle_select )
271+ return Menu :open (
272+ {type = opts .type , title = opts .title or ' 驱动器列表' , items = items , selected_index = selected_index },
273+ handle_select
274+ )
277275end
0 commit comments