2023-11-10 20:47:08 +01:00
|
|
|
#pragma once
|
|
|
|
|
2023-04-08 00:58:53 +02:00
|
|
|
#include <hex/ui/popup.hpp>
|
|
|
|
|
|
|
|
#include <hex/api/localization.hpp>
|
|
|
|
|
2023-07-09 12:53:31 +02:00
|
|
|
#include <wolv/utils/string.hpp>
|
|
|
|
|
2023-04-08 00:58:53 +02:00
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace hex::plugin::builtin {
|
|
|
|
|
|
|
|
class PopupFileChooser : public Popup<PopupFileChooser> {
|
|
|
|
public:
|
2023-11-14 00:37:07 +01:00
|
|
|
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)
|
2023-04-17 16:18:48 +02:00
|
|
|
: hex::Popup<PopupFileChooser>("hex.builtin.common.choose_file"),
|
2023-11-14 00:37:07 +01:00
|
|
|
m_indices({ }),
|
2023-04-08 00:58:53 +02:00
|
|
|
m_openCallback(callback),
|
2023-05-21 18:41:43 +02:00
|
|
|
m_validExtensions(validExtensions), m_multiple(multiple) {
|
|
|
|
|
2023-11-14 00:37:07 +01:00
|
|
|
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;
|
2023-05-21 18:41:43 +02:00
|
|
|
});
|
|
|
|
}
|
2023-04-08 00:58:53 +02:00
|
|
|
|
|
|
|
void drawContent() override {
|
|
|
|
bool doubleClicked = false;
|
|
|
|
|
|
|
|
if (ImGui::BeginListBox("##files", scaled(ImVec2(500, 400)))) {
|
|
|
|
u32 index = 0;
|
2023-11-14 00:37:07 +01:00
|
|
|
for (auto &[path, pathName] : this->m_files) {
|
2023-04-08 00:58:53 +02:00
|
|
|
ImGui::PushID(index);
|
|
|
|
|
|
|
|
bool selected = this->m_indices.contains(index);
|
2023-11-14 00:37:07 +01:00
|
|
|
if (ImGui::Selectable(wolv::util::toUTF8String(pathName).c_str(), selected, ImGuiSelectableFlags_DontClosePopups)) {
|
2023-04-08 00:58:53 +02:00
|
|
|
if (!this->m_multiple) {
|
|
|
|
this->m_indices.clear();
|
|
|
|
this->m_indices.insert(index);
|
|
|
|
} else {
|
|
|
|
if (selected) {
|
|
|
|
this->m_indices.erase(index);
|
|
|
|
} else {
|
|
|
|
this->m_indices.insert(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(0))
|
|
|
|
doubleClicked = true;
|
|
|
|
|
|
|
|
ImGui::InfoTooltip(wolv::util::toUTF8String(path).c_str());
|
|
|
|
|
|
|
|
ImGui::PopID();
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndListBox();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::Button("hex.builtin.common.open"_lang) || doubleClicked) {
|
|
|
|
for (const auto &index : this->m_indices)
|
2023-11-14 00:37:07 +01:00
|
|
|
this->m_openCallback(this->m_files[index].first);
|
2023-04-08 23:34:46 +02:00
|
|
|
Popup::close();
|
2023-04-08 00:58:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::SameLine();
|
|
|
|
|
|
|
|
if (ImGui::Button("hex.builtin.common.browse"_lang)) {
|
|
|
|
fs::openFileBrowser(fs::DialogMode::Open, this->m_validExtensions, [this](const auto &path) {
|
|
|
|
this->m_openCallback(path);
|
2023-04-08 23:34:46 +02:00
|
|
|
Popup::close();
|
2023-04-08 00:58:53 +02:00
|
|
|
}, {}, this->m_multiple);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] ImGuiWindowFlags getFlags() const override {
|
|
|
|
return ImGuiWindowFlags_AlwaysAutoResize;
|
|
|
|
}
|
|
|
|
|
2023-11-14 00:37:07 +01:00
|
|
|
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] != '.';
|
|
|
|
}
|
|
|
|
|
2023-04-08 00:58:53 +02:00
|
|
|
private:
|
|
|
|
std::set<u32> m_indices;
|
2023-11-14 00:37:07 +01:00
|
|
|
std::vector<std::pair<std::fs::path, std::fs::path>> m_files;
|
2023-04-08 00:58:53 +02:00
|
|
|
std::function<void(std::fs::path)> m_openCallback;
|
2023-10-04 12:00:32 +02:00
|
|
|
std::vector<hex::fs::ItemFilter> m_validExtensions;
|
2023-04-08 00:58:53 +02:00
|
|
|
bool m_multiple = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|