Add common directories to file dialog

This commit is contained in:
Nikita Lisitsa 2023-01-05 19:30:00 +03:00
parent 9a5206ebf9
commit 098c3e84b1
3 changed files with 79 additions and 0 deletions

View file

@ -12,6 +12,8 @@
#include <psemek/io/memory_stream.hpp>
#include <psemek/io/file_stream.hpp>
#include <psemek/util/unicode.hpp>
#include <psemek/util/common_directories.hpp>
#include <psemek/util/to_shared.hpp>
#include <psemek/ui/resources/folder_png.hpp>
#include <psemek/ui/resources/file_png.hpp>
@ -145,6 +147,17 @@ namespace psemek::ui
auto common_paths_label = options.element_factory.make_label("");
auto common_paths = util::to_shared(util::common_directories());
{
std::string str;
int index = 0;
for (auto const & dir : *common_paths)
{
str += util::to_string("[link:", index++, "]", escape(dir.first), "[/link]\n");
}
common_paths_label->set_tagged_text(str);
}
auto current_path_label = options.element_factory.make_label("");
current_path_label->set_valign(label::valignment::center);
@ -321,6 +334,10 @@ namespace psemek::ui
}
};
common_paths_label->on_link_click([common_paths, set_path_callback](std::string const & link){
int index = util::from_string<int>(link);
(*set_path_callback)((*common_paths)[index].second);
});
path_edit->on_text_changed([=](std::u32string_view const & text_u32){
std::string text = util::to_utf8(std::u32string(text_u32));

View file

@ -0,0 +1,11 @@
#pragma once
#include <filesystem>
#include <vector>
namespace psemek::util
{
std::vector<std::pair<std::string, std::filesystem::path>> common_directories();
}

View file

@ -0,0 +1,51 @@
#include <psemek/util/common_directories.hpp>
#include <cstdlib>
#ifdef _WIN32
#include <fileapi.h>
#endif
namespace psemek::util
{
std::vector<std::pair<std::string, std::filesystem::path>> common_directories()
{
std::vector<std::pair<std::string, std::filesystem::path>> result;
#ifdef __linux__
{
std::filesystem::path path = std::getenv("HOME");
if (!path.empty())
result.push_back({"Home", path});
}
result.push_back({"Root", "/"});
#endif
#ifdef _WIN32
{
std::filesystem::path path = std::getenv("USERPROFILE");
if (!path.empty())
result.push_back({"Home", path});
}
{
auto drives = GetLogicalDrives();
for (int i = 0; i < 26; ++i)
{
if ((drives & (1 << i)) == 0) continue;
char name[3] = "A:";
char path[4] = "A:\\";
name[0] = 'A' + i;
path[0] = name[0];
result.push_back({name, path});
}
}
#endif
return result;
}
}