From 48eb58230bda6f96ed73d908cc1672b62b20f900 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 10 Apr 2021 13:38:00 +0200 Subject: [PATCH] LibGUI: List directories before files in FileSystemModel Instead of mixing directories and files, sorting a FileSystemModel by the Name column will now give you all the directories first, followed by all the files. --- Userland/Libraries/LibGUI/FileSystemModel.cpp | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibGUI/FileSystemModel.cpp b/Userland/Libraries/LibGUI/FileSystemModel.cpp index d99499b0c3..4b3ca0470b 100644 --- a/Userland/Libraries/LibGUI/FileSystemModel.cpp +++ b/Userland/Libraries/LibGUI/FileSystemModel.cpp @@ -123,20 +123,29 @@ void FileSystemModel::Node::traverse_if_needed() } quick_sort(child_names); - for (auto& name : child_names) { - String child_path = String::formatted("{}/{}", full_path, name); + NonnullOwnPtrVector directory_children; + NonnullOwnPtrVector file_children; + + for (auto& child_name : child_names) { + String child_path = String::formatted("{}/{}", full_path, child_name); auto child = adopt_own(*new Node(m_model)); bool ok = child->fetch_data(child_path, false); if (!ok) continue; if (m_model.m_mode == DirectoriesOnly && !S_ISDIR(child->mode)) continue; - child->name = name; + child->name = child_name; child->parent = this; total_size += child->size; - children.append(move(child)); + if (S_ISDIR(child->mode)) + directory_children.append(move(child)); + else + file_children.append(move(child)); } + children.append(move(directory_children)); + children.append(move(file_children)); + if (!m_file_watcher) { // We are not already watching this file, create a new watcher @@ -424,7 +433,9 @@ Variant FileSystemModel::data(const ModelIndex& index, ModelRole role) const case Column::Icon: return node.is_directory() ? 0 : 1; case Column::Name: - return node.name; + // NOTE: The children of a Node are grouped by directory-or-file and then sorted alphabetically. + // Hence, the sort value for the name column is simply the index row. :^) + return index.row(); case Column::Size: return (int)node.size; case Column::Owner: