Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

macOS: sort each group by the clicked column in a grouped list

Jaime Riffo712baa810199
diff --git a/crates/rf-core/src/sort.rs b/crates/rf-core/src/sort.rs
index 598fa536..48d6f939 100644
--- a/crates/rf-core/src/sort.rs
+++ b/crates/rf-core/src/sort.rs
@@ -103,6 +103,24 @@ impl SortSpec {
self.directed(primary.then_with(|| natural_cmp(a.shown(), b.shown())))
}
+ /// [`compare`](Self::compare) by the key alone, without breaking ties by name: entries the
+ /// key cannot tell apart compare equal, so a second sort can order them (the items inside
+ /// one group of a grouped list, sorted by the column the user picked).
+ pub fn compare_key(&self, a: &Entry, b: &Entry) -> Ordering {
+ if let Some(o) = self.folder_order(a, b) {
+ return o;
+ }
+ let primary = match self.key {
+ SortKey::Name | SortKey::Tag | SortKey::Path => natural_cmp(a.shown(), b.shown()),
+ SortKey::Modified => a.modified_ms.cmp(&b.modified_ms),
+ SortKey::Created => a.created_ms.cmp(&b.created_ms),
+ SortKey::Size => a.size.cmp(&b.size),
+ SortKey::Type => a.ext.cmp(&b.ext),
+ SortKey::Sync => crate::sortkeys::sync_rank(a).cmp(&crate::sortkeys::sync_rank(b)),
+ };
+ self.directed(primary)
+ }
+
/// [`compare`](Self::compare) for the keys that sort by a rank made beforehand (one `u32` per
/// entry: `ra`, `rb`). A tag sort keeps the entries without a tag ([`crate::sortkeys::NO_RANK`])
/// last in both directions, the way folders stay first; ties are broken by name.
diff --git a/crates/rf-mac/src/archive.rs b/crates/rf-mac/src/archive.rs
index 72fdad0f..25c90c47 100644
--- a/crates/rf-mac/src/archive.rs
+++ b/crates/rf-mac/src/archive.rs
@@ -107,7 +107,7 @@ impl Archive {
let entries = self.tree.get(&inner)?.clone();
let path = if inner.is_empty() { self.path.clone() } else { self.path.join(&inner) };
let folder = Folder::from_entries(path, entries);
- folder.arrange(column, ascending, folders_first, show_hidden, String::new());
+ folder.arrange(column, ascending, folders_first, show_hidden, String::new(), None, true);
Some(Arc::new(folder))
}
diff --git a/crates/rf-mac/src/lib.rs b/crates/rf-mac/src/lib.rs
index f0cb9355..819e4af7 100644
--- a/crates/rf-mac/src/lib.rs
+++ b/crates/rf-mac/src/lib.rs
@@ -135,7 +135,7 @@ impl Folder {
let path = PathBuf::from(path);
let entries = list(&path)?;
let folder = Folder { path, entries, order: RwLock::new(Vec::new()), homes: Vec::new() };
- folder.arrange(column, ascending, folders_first, show_hidden, String::new());
+ folder.arrange(column, ascending, folders_first, show_hidden, String::new(), None, true);
Ok(Arc::new(folder))
}
@@ -156,7 +156,7 @@ impl Folder {
.collect();
let (homes, entries): (Vec<PathBuf>, Vec<Entry>) = found.into_iter().unzip();
let folder = Folder { path: PathBuf::from(label), entries, order: RwLock::new(Vec::new()), homes };
- folder.arrange(column, ascending, folders_first, show_hidden, String::new());
+ folder.arrange(column, ascending, folders_first, show_hidden, String::new(), None, true);
Arc::new(folder)
}
@@ -204,13 +204,24 @@ impl Folder {
/// Re-sorts and re-filters in memory; the disk is not read again. `filter` keeps the items
/// whose name contains it, ignoring case (Turkish-aware: "I" matches "ı"); "" keeps all.
- pub fn arrange(&self, column: SortColumn, ascending: bool, folders_first: bool, show_hidden: bool, filter: String) {
+ /// `then_column` (a grouped list): the items `column` cannot tell apart (one group's) are
+ /// sorted by it, `then_ascending` giving its direction.
+ pub fn arrange(
+ &self, column: SortColumn, ascending: bool, folders_first: bool, show_hidden: bool, filter: String,
+ then_column: Option<SortColumn>, then_ascending: bool,
+ ) {
let spec = SortSpec {
key: column.into(),
dir: if ascending { SortDir::Asc } else { SortDir::Desc },
dirs_first: folders_first,
files_first: false,
};
+ let then = then_column.map(|c| SortSpec {
+ key: c.into(),
+ dir: if then_ascending { SortDir::Asc } else { SortDir::Desc },
+ dirs_first: false,
+ files_first: false,
+ });
// a part of the name, a * / ? pattern, ext:pdf or kind:video (see namequery)
let query = crate::namequery::NameQuery::parse(&filter);
let mut idx: Vec<u32> = (0..self.entries.len() as u32)
@@ -222,7 +233,13 @@ impl Folder {
&& (query.is_empty() || query.matches(e.shown()))
})
.collect();
- idx.par_sort_unstable_by(|&a, &b| spec.compare(&self.entries[a as usize], &self.entries[b as usize]));
+ idx.par_sort_unstable_by(|&a, &b| {
+ let (ea, eb) = (&self.entries[a as usize], &self.entries[b as usize]);
+ match &then {
+ Some(t) => spec.compare_key(ea, eb).then_with(|| t.compare(ea, eb)),
+ None => spec.compare(ea, eb),
+ }
+ });
*self.order.write().unwrap() = idx;
}
}
@@ -511,11 +528,11 @@ mod tests {
let names: Vec<String> = (0..f.count()).map(|r| f.item(r).unwrap().name).collect();
assert_eq!(names, ["b.txt", "dir2", "dir10"]);
assert_eq!(f.total(), 4);
- f.arrange(SortColumn::Name, true, false, false, "DIR".into());
+ f.arrange(SortColumn::Name, true, false, false, "DIR".into(), None, true);
assert_eq!(f.count(), 2);
- f.arrange(SortColumn::Name, true, true, false, String::new());
+ f.arrange(SortColumn::Name, true, true, false, String::new(), None, true);
assert_eq!(f.item(0).unwrap().name, "dir2", "folders first");
- f.arrange(SortColumn::Size, false, false, true, String::new());
+ f.arrange(SortColumn::Size, false, false, true, String::new(), None, true);
assert_eq!(f.item(0).unwrap().name, "b.txt");
assert_eq!(f.item(0).unwrap().size, 5);
assert!(f.row_of(".secret".into()).is_some());
@@ -524,6 +541,30 @@ mod tests {
fs::remove_dir_all(&d).unwrap();
}
+ #[test]
+ fn a_grouped_list_sorts_each_group_by_the_column_picked() {
+ let d = tempfile_dir("grouped");
+ let at = |n: &str, secs: u64| {
+ let p = d.join(n);
+ fs::write(&p, b"x").unwrap();
+ let t = std::time::UNIX_EPOCH + std::time::Duration::from_secs(secs);
+ fs::File::options().write(true).open(&p).unwrap().set_modified(t).unwrap();
+ };
+ at("a.dmg", 1_000);
+ at("b.dmg", 3_000);
+ at("c.docx", 2_000);
+ at("d.docx", 4_000);
+ let f = Folder::open(d.display().to_string(), SortColumn::Name, true, false, false).unwrap();
+ // grouped by type (ascending), newest first inside each type
+ f.arrange(SortColumn::Kind, true, false, false, String::new(), Some(SortColumn::Modified), false);
+ let names: Vec<String> = (0..f.count()).map(|r| f.item(r).unwrap().name).collect();
+ assert_eq!(names, ["b.dmg", "a.dmg", "d.docx", "c.docx"]);
+ f.arrange(SortColumn::Kind, true, false, false, String::new(), Some(SortColumn::Modified), true);
+ let names: Vec<String> = (0..f.count()).map(|r| f.item(r).unwrap().name).collect();
+ assert_eq!(names, ["a.dmg", "b.dmg", "c.docx", "d.docx"]);
+ let _ = fs::remove_dir_all(&d);
+ }
+
#[test]
fn a_gathered_listing_keeps_each_items_own_folder() {
let a = tempfile_dir("gathered-a");
diff --git a/macos/Sources/RustyFiles/FileListController.swift b/macos/Sources/RustyFiles/FileListController.swift
index 5ac8bc65..74d28cf7 100644
--- a/macos/Sources/RustyFiles/FileListController.swift
+++ b/macos/Sources/RustyFiles/FileListController.swift
@@ -2637,16 +2637,19 @@ extension FileListController {
}
/// Sorts the folder; a grouping sorts by what it groups by (folders stay first only where
- /// that keeps the groups whole).
+ /// that keeps the groups whole), and inside each group by the column the user picked.
func arrange(_ folder: Folder) {
let column = groupBy.column?.sort ?? sortKey.sort
let first = [.date, .name].contains(groupBy) ? false : foldersFirst
let asc = groupBy == .date ? false : (groupBy == .none ? ascending : (groupBy.column == sortKey ? ascending : groupBy.column?.startsAscending ?? true))
- folder.arrange(column: column, ascending: asc, foldersFirst: first, showHidden: showHidden, filter: filter)
+ // a grouped list sorts each group's items by the column picked, when it is another one
+ let within = groupBy.column.flatMap { $0 == sortKey ? nil : sortKey.sort }
+ folder.arrange(column: column, ascending: asc, foldersFirst: first, showHidden: showHidden, filter: filter,
+ thenColumn: within, thenAscending: ascending)
foldGroups(folder)
- // the header marks the column the list is really sorted by
- header.sortKey = groupBy.column ?? sortKey
- header.ascending = asc
+ // the header marks the column picked (the one sorting inside the groups, when grouped)
+ header.sortKey = within == nil ? (groupBy.column ?? sortKey) : sortKey
+ header.ascending = within == nil ? asc : ascending
}
func computeGroups() {