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.
This commit is contained in:
Andreas Kling 2021-04-10 13:38:00 +02:00
parent c128b3fd91
commit 48eb58230b

View file

@ -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<Node> directory_children;
NonnullOwnPtrVector<Node> 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: