Sort entries in file dialog

This commit is contained in:
Nikita Lisitsa 2023-01-04 01:44:37 +03:00
parent b1063f83d9
commit 0a64bd0cbb

View file

@ -232,26 +232,49 @@ namespace psemek::ui
{ {
std::string contents_str; 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<directory_entry> 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]"; contents_str += "[image:folder]";
else else
contents_str += "[image:file]"; contents_str += "[image:file]";
contents_str += "[link:" + util::to_string(index++) + "]"; 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->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<int>(std::string{value}); int index = util::from_string<int>(std::string{value});
auto path = std::next(std::filesystem::directory_iterator(new_path), index)->path(); if (entries[index].folder)
if (std::filesystem::is_directory(path)) (*set_path_callback)(entries[index].path);
(*set_path_callback)(path);
else else
{ {
auto path = entries[index].path;
try try
{ {
if (type == file_dialog_type::load) if (type == file_dialog_type::load)