@@ -12,7 +12,7 @@ use uuid::Uuid;
1212use crate :: benchmark;
1313use crate :: file_system:: listing:: caching:: { CachedListing , LISTING_CACHE } ;
1414use crate :: file_system:: listing:: metadata:: FileEntry ;
15- use crate :: file_system:: listing:: sorting:: { SortColumn , SortOrder , sort_entries} ;
15+ use crate :: file_system:: listing:: sorting:: { DirectorySortMode , SortColumn , SortOrder , sort_entries} ;
1616use crate :: file_system:: watcher:: { start_watching, stop_watching} ;
1717
1818/// Returns true if the entry is not a hidden dotfile.
@@ -39,7 +39,14 @@ pub struct ListingStartResult {
3939/// Reads the directory once, caches it, and returns listing ID + total count.
4040/// Frontend then fetches visible ranges on demand via `get_file_range`.
4141pub fn list_directory_start ( path : & Path , include_hidden : bool ) -> Result < ListingStartResult , std:: io:: Error > {
42- list_directory_start_with_volume ( "root" , path, include_hidden, SortColumn :: Name , SortOrder :: Ascending )
42+ list_directory_start_with_volume (
43+ "root" ,
44+ path,
45+ include_hidden,
46+ SortColumn :: Name ,
47+ SortOrder :: Ascending ,
48+ DirectorySortMode :: LikeFiles ,
49+ )
4350}
4451
4552/// Starts a new directory listing using a specific volume.
@@ -51,6 +58,7 @@ pub fn list_directory_start_with_volume(
5158 include_hidden : bool ,
5259 sort_by : SortColumn ,
5360 sort_order : SortOrder ,
61+ dir_sort_mode : DirectorySortMode ,
5462) -> Result < ListingStartResult , std:: io:: Error > {
5563 // Reset benchmark epoch for this navigation
5664 benchmark:: reset_epoch ( ) ;
@@ -80,9 +88,13 @@ pub fn list_directory_start_with_volume(
8088 all_entries. iter ( ) . filter ( |e| is_visible ( e) ) . count ( )
8189 } ;
8290
83- // Sort the entries
91+ // Enrich directory entries with index data (recursive_size etc.) before sorting,
92+ // so that sort-by-size works correctly for directories.
8493 let mut all_entries = all_entries;
85- sort_entries ( & mut all_entries, sort_by, sort_order) ;
94+ crate :: indexing:: enrich_entries_with_index ( & mut all_entries) ;
95+
96+ // Sort the entries
97+ sort_entries ( & mut all_entries, sort_by, sort_order, dir_sort_mode) ;
8698
8799 // Cache the entries FIRST (watcher will read from here)
88100 if let Ok ( mut cache) = LISTING_CACHE . write ( ) {
@@ -94,6 +106,7 @@ pub fn list_directory_start_with_volume(
94106 entries : all_entries. clone ( ) ,
95107 sort_by,
96108 sort_order,
109+ directory_sort_mode : dir_sort_mode,
97110 } ,
98111 ) ;
99112 }
@@ -318,10 +331,15 @@ pub struct ResortResult {
318331/// Re-sorts an existing cached listing in-place.
319332///
320333/// More efficient than creating a new listing when you just want to change the sort order.
334+ #[ allow(
335+ clippy:: too_many_arguments,
336+ reason = "Resort requires sort params, cursor tracking, and selection state"
337+ ) ]
321338pub fn resort_listing (
322339 listing_id : & str ,
323340 sort_by : SortColumn ,
324341 sort_order : SortOrder ,
342+ dir_sort_mode : DirectorySortMode ,
325343 cursor_filename : Option < & str > ,
326344 include_hidden : bool ,
327345 selected_indices : Option < & [ usize ] > ,
@@ -351,9 +369,13 @@ pub fn resort_listing(
351369 } )
352370 } ;
353371
372+ // Refresh index data before re-sorting (cache entries may not have fresh sizes)
373+ crate :: indexing:: enrich_entries_with_index ( & mut listing. entries ) ;
374+
354375 // Re-sort the entries
355- sort_entries ( & mut listing. entries , sort_by, sort_order) ;
376+ sort_entries ( & mut listing. entries , sort_by, sort_order, dir_sort_mode ) ;
356377 listing. sort_by = sort_by;
378+ listing. directory_sort_mode = dir_sort_mode;
357379 listing. sort_order = sort_order;
358380
359381 // Find the new cursor position
@@ -417,7 +439,13 @@ pub(crate) fn update_listing_entries(listing_id: &str, entries: Vec<FileEntry>)
417439 && let Some ( listing) = cache. get_mut ( listing_id)
418440 {
419441 let mut entries = entries;
420- sort_entries ( & mut entries, listing. sort_by , listing. sort_order ) ;
442+ crate :: indexing:: enrich_entries_with_index ( & mut entries) ;
443+ sort_entries (
444+ & mut entries,
445+ listing. sort_by ,
446+ listing. sort_order ,
447+ listing. directory_sort_mode ,
448+ ) ;
421449 listing. entries = entries;
422450 }
423451}
0 commit comments