From cbfa363b2bf5b902edb965134146ff2f19cc0d18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Harald=20J=C3=B8rgensen?= <58829763+adamjoer@users.noreply.github.com> Date: Mon, 16 Oct 2023 11:23:02 +0200 Subject: [PATCH] LibGUI: Preserve correct child order in FileSystemModel When handling child creation in FileSystemModel, the new child should be inserted such that the children still follow the correct order of being grouped by directory-and-file and then sorted alphabetically, instead of just being appended at the end. --- Userland/Libraries/LibGUI/FileSystemModel.cpp | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibGUI/FileSystemModel.cpp b/Userland/Libraries/LibGUI/FileSystemModel.cpp index 6b7266ecbd..b6dc9a4945 100644 --- a/Userland/Libraries/LibGUI/FileSystemModel.cpp +++ b/Userland/Libraries/LibGUI/FileSystemModel.cpp @@ -415,18 +415,33 @@ void FileSystemModel::handle_file_event(Core::FileWatcherEvent const& event) break; } - int child_count = parent->m_children.size(); - auto& mutable_parent = const_cast(*parent); auto maybe_child = mutable_parent.create_child(child_name); if (!maybe_child) break; - begin_insert_rows(parent->index(0), child_count, child_count); - auto child = maybe_child.release_nonnull(); + + bool const is_new_child_dir = S_ISDIR(child->mode); + int insert_index = 0; + for (auto const& other_child : mutable_parent.m_children) { + bool const is_other_child_dir = S_ISDIR(other_child->mode); + if (!is_new_child_dir && is_other_child_dir) { + ++insert_index; + continue; + } + if (is_new_child_dir && !is_other_child_dir) + break; + + if (other_child->name.view() > child_name) + break; + ++insert_index; + } + + begin_insert_rows(parent->index(0), insert_index, insert_index); + mutable_parent.total_size += child->size; - mutable_parent.m_children.append(move(child)); + mutable_parent.m_children.insert(insert_index, move(child)); end_insert_rows(); break;