From 0a64bd0cbb15a99b95b19013d26fd8fa8d6b283a Mon Sep 17 00:00:00 2001 From: lisyarus Date: Wed, 4 Jan 2023 01:44:37 +0300 Subject: [PATCH] Sort entries in file dialog --- libs/ui/source/file_dialog.cpp | 39 +++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/libs/ui/source/file_dialog.cpp b/libs/ui/source/file_dialog.cpp index 9d07c662..0b696cec 100644 --- a/libs/ui/source/file_dialog.cpp +++ b/libs/ui/source/file_dialog.cpp @@ -232,26 +232,49 @@ namespace psemek::ui { std::string contents_str; - int index = 0; - for (auto const & entry : std::filesystem::directory_iterator(new_path)) + + struct directory_entry { - if (std::filesystem::is_directory(entry.path())) + bool folder; + std::filesystem::path path; + }; + + std::vector entries; + + for (auto const & entry : std::filesystem::directory_iterator(new_path)) + entries.push_back({std::filesystem::is_directory(entry.path()), entry.path()}); + + std::sort(entries.begin(), entries.end(), [](directory_entry const & e1, directory_entry const & e2){ + if (e1.folder && !e2.folder) + return true; + + if (!e1.folder && e2.folder) + return false; + + return e1.path.filename().string() < e2.path.filename().string(); + }); + + int index = 0; + for (auto const & entry : entries) + { + if (entry.folder) contents_str += "[image:folder]"; else contents_str += "[image:file]"; contents_str += "[link:" + util::to_string(index++) + "]"; - contents_str += entry.path().filename().string() + "[/link]\n"; + contents_str += entry.path.filename().string() + "[/link]\n"; } directory_view->set_tagged_text(std::move(contents_str)); - directory_view->on_link_click([=](std::string_view const & value){ + directory_view->on_link_click([=, entries = std::move(entries)](std::string_view const & value){ int index = util::from_string(std::string{value}); - auto path = std::next(std::filesystem::directory_iterator(new_path), index)->path(); - if (std::filesystem::is_directory(path)) - (*set_path_callback)(path); + if (entries[index].folder) + (*set_path_callback)(entries[index].path); else { + auto path = entries[index].path; + try { if (type == file_dialog_type::load)