mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-03-30 21:05:56 -05:00
impr: Display folder names of files in subfolders in file chooser popup
This commit is contained in:
@@ -13,14 +13,29 @@ namespace hex::plugin::builtin {
|
||||
|
||||
class PopupFileChooser : public Popup<PopupFileChooser> {
|
||||
public:
|
||||
PopupFileChooser(const std::vector<std::fs::path> &files, const std::vector<hex::fs::ItemFilter> &validExtensions, bool multiple, const std::function<void(std::fs::path)> &callback)
|
||||
PopupFileChooser(const std::vector<std::fs::path> &basePaths, const std::vector<std::fs::path> &files, const std::vector<hex::fs::ItemFilter> &validExtensions, bool multiple, const std::function<void(std::fs::path)> &callback)
|
||||
: hex::Popup<PopupFileChooser>("hex.builtin.common.choose_file"),
|
||||
m_indices({ }), m_files(files),
|
||||
m_indices({ }),
|
||||
m_openCallback(callback),
|
||||
m_validExtensions(validExtensions), m_multiple(multiple) {
|
||||
|
||||
std::sort(this->m_files.begin(), this->m_files.end(), [](const std::fs::path &a, const std::fs::path &b) {
|
||||
return a.filename() < b.filename();
|
||||
for (const auto &path : files) {
|
||||
std::fs::path adjustedPath;
|
||||
for (const auto &basePath : basePaths) {
|
||||
if (isSubpath(basePath, path)) {
|
||||
adjustedPath = std::fs::relative(path, basePath);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (adjustedPath.empty())
|
||||
adjustedPath = path.filename();
|
||||
|
||||
this->m_files.push_back({ path, adjustedPath });
|
||||
}
|
||||
|
||||
std::sort(this->m_files.begin(), this->m_files.end(), [](const auto &a, const auto &b) {
|
||||
return a.first < b.first;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -29,11 +44,11 @@ namespace hex::plugin::builtin {
|
||||
|
||||
if (ImGui::BeginListBox("##files", scaled(ImVec2(500, 400)))) {
|
||||
u32 index = 0;
|
||||
for (auto &path : this->m_files) {
|
||||
for (auto &[path, pathName] : this->m_files) {
|
||||
ImGui::PushID(index);
|
||||
|
||||
bool selected = this->m_indices.contains(index);
|
||||
if (ImGui::Selectable(wolv::util::toUTF8String(path.filename()).c_str(), selected, ImGuiSelectableFlags_DontClosePopups)) {
|
||||
if (ImGui::Selectable(wolv::util::toUTF8String(pathName).c_str(), selected, ImGuiSelectableFlags_DontClosePopups)) {
|
||||
if (!this->m_multiple) {
|
||||
this->m_indices.clear();
|
||||
this->m_indices.insert(index);
|
||||
@@ -60,7 +75,7 @@ namespace hex::plugin::builtin {
|
||||
|
||||
if (ImGui::Button("hex.builtin.common.open"_lang) || doubleClicked) {
|
||||
for (const auto &index : this->m_indices)
|
||||
this->m_openCallback(this->m_files[index]);
|
||||
this->m_openCallback(this->m_files[index].first);
|
||||
Popup::close();
|
||||
}
|
||||
|
||||
@@ -78,9 +93,16 @@ namespace hex::plugin::builtin {
|
||||
return ImGuiWindowFlags_AlwaysAutoResize;
|
||||
}
|
||||
|
||||
private:
|
||||
static bool isSubpath(const std::fs::path &basePath, const std::fs::path &path) {
|
||||
auto relativePath = std::fs::relative(path, basePath);
|
||||
|
||||
return !relativePath.empty() && relativePath.native()[0] != '.';
|
||||
}
|
||||
|
||||
private:
|
||||
std::set<u32> m_indices;
|
||||
std::vector<std::fs::path> m_files;
|
||||
std::vector<std::pair<std::fs::path, std::fs::path>> m_files;
|
||||
std::function<void(std::fs::path)> m_openCallback;
|
||||
std::vector<hex::fs::ItemFilter> m_validExtensions;
|
||||
bool m_multiple = false;
|
||||
|
||||
Reference in New Issue
Block a user