2023-11-10 20:47:08 +01:00
|
|
|
#pragma once
|
|
|
|
|
2023-04-08 00:58:53 +02:00
|
|
|
#include <hex/ui/popup.hpp>
|
|
|
|
|
2023-11-21 14:38:01 +01:00
|
|
|
#include <hex/api/localization_manager.hpp>
|
2023-04-08 00:58:53 +02:00
|
|
|
|
2023-07-09 12:53:31 +02:00
|
|
|
#include <wolv/utils/string.hpp>
|
2024-02-23 17:52:42 +01:00
|
|
|
#include <fonts/codicons_font.h>
|
2023-07-09 12:53:31 +02:00
|
|
|
|
2023-04-08 00:58:53 +02:00
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
|
|
|
|
2023-12-23 21:09:41 +01:00
|
|
|
namespace hex::ui {
|
2023-04-08 00:58:53 +02:00
|
|
|
|
2024-02-23 17:52:42 +01:00
|
|
|
template<typename T>
|
|
|
|
class PopupNamedFileChooserBase : public Popup<T> {
|
2023-04-08 00:58:53 +02:00
|
|
|
public:
|
2024-02-23 17:52:42 +01:00
|
|
|
PopupNamedFileChooserBase(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<std::string(const std::fs::path &)> &nameCallback, const std::function<void(std::fs::path)> &callback)
|
|
|
|
: hex::Popup<T>("hex.ui.common.choose_file"),
|
|
|
|
m_selectedFiles({ }),
|
|
|
|
m_nameCallback(nameCallback),
|
2023-04-08 00:58:53 +02:00
|
|
|
m_openCallback(callback),
|
2024-02-23 17:52:42 +01:00
|
|
|
m_validExtensions(validExtensions),
|
|
|
|
m_multiple(multiple) {
|
2023-05-21 18:41:43 +02:00
|
|
|
|
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();
|
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
m_files.push_back({ path, adjustedPath });
|
2023-11-14 00:37:07 +01:00
|
|
|
}
|
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
std::sort(m_files.begin(), m_files.end(), [](const auto &a, const auto &b) {
|
2023-11-14 00:37:07 +01:00
|
|
|
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;
|
|
|
|
|
2024-02-23 17:52:42 +01:00
|
|
|
ImGui::PushItemWidth(-1);
|
|
|
|
ImGuiExt::InputTextIcon("##search", ICON_VS_FILTER, m_filter);
|
|
|
|
ImGui::PopItemWidth();
|
|
|
|
|
2023-04-08 00:58:53 +02:00
|
|
|
if (ImGui::BeginListBox("##files", scaled(ImVec2(500, 400)))) {
|
2024-02-23 17:52:42 +01:00
|
|
|
for (auto fileIt = m_files.begin(); fileIt != m_files.end(); ++fileIt) {
|
|
|
|
const auto &[path, pathName] = *fileIt;
|
|
|
|
|
|
|
|
const auto &pathNameString = m_nameCallback(pathName);
|
|
|
|
if (!m_filter.empty() && !pathNameString.contains(m_filter))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ImGui::PushID(&*fileIt);
|
2023-04-08 00:58:53 +02:00
|
|
|
|
2024-02-23 17:52:42 +01:00
|
|
|
bool selected = m_selectedFiles.contains(fileIt);
|
|
|
|
if (ImGui::Selectable(pathNameString.c_str(), selected, ImGuiSelectableFlags_DontClosePopups)) {
|
2023-12-19 13:10:25 +01:00
|
|
|
if (!m_multiple) {
|
2024-02-23 17:52:42 +01:00
|
|
|
m_selectedFiles.clear();
|
|
|
|
m_selectedFiles.insert(fileIt);
|
2023-04-08 00:58:53 +02:00
|
|
|
} else {
|
|
|
|
if (selected) {
|
2024-02-23 17:52:42 +01:00
|
|
|
m_selectedFiles.erase(fileIt);
|
2023-04-08 00:58:53 +02:00
|
|
|
} else {
|
2024-02-23 17:52:42 +01:00
|
|
|
m_selectedFiles.insert(fileIt);
|
2023-04-08 00:58:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(0))
|
|
|
|
doubleClicked = true;
|
|
|
|
|
2023-11-16 22:24:06 +01:00
|
|
|
ImGuiExt::InfoTooltip(wolv::util::toUTF8String(path).c_str());
|
2023-04-08 00:58:53 +02:00
|
|
|
|
|
|
|
ImGui::PopID();
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndListBox();
|
|
|
|
}
|
|
|
|
|
2023-12-23 21:09:41 +01:00
|
|
|
if (ImGui::Button("hex.ui.common.open"_lang) || doubleClicked) {
|
2024-02-23 17:52:42 +01:00
|
|
|
for (const auto &it : m_selectedFiles)
|
|
|
|
m_openCallback(it->first);
|
|
|
|
Popup<T>::close();
|
2023-04-08 00:58:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::SameLine();
|
|
|
|
|
2023-12-23 21:09:41 +01:00
|
|
|
if (ImGui::Button("hex.ui.common.browse"_lang)) {
|
2023-12-19 13:10:25 +01:00
|
|
|
fs::openFileBrowser(fs::DialogMode::Open, m_validExtensions, [this](const auto &path) {
|
|
|
|
m_openCallback(path);
|
2024-02-23 17:52:42 +01:00
|
|
|
Popup<T>::close();
|
2023-12-19 13:10:25 +01:00
|
|
|
}, {}, m_multiple);
|
2023-04-08 00:58:53 +02:00
|
|
|
}
|
2023-12-13 15:08:27 +01:00
|
|
|
|
|
|
|
if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Escape)))
|
|
|
|
this->close();
|
2023-04-08 00:58:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[[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:
|
2024-02-23 17:52:42 +01:00
|
|
|
std::string m_filter;
|
2023-11-14 00:37:07 +01:00
|
|
|
std::vector<std::pair<std::fs::path, std::fs::path>> m_files;
|
2024-02-23 17:52:42 +01:00
|
|
|
std::set<std::vector<std::pair<std::fs::path, std::fs::path>>::const_iterator> m_selectedFiles;
|
|
|
|
std::function<std::string(const std::fs::path &)> m_nameCallback;
|
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;
|
|
|
|
};
|
|
|
|
|
2024-02-23 17:52:42 +01:00
|
|
|
class PopupNamedFileChooser : public PopupNamedFileChooserBase<PopupNamedFileChooser> {
|
|
|
|
public:
|
|
|
|
PopupNamedFileChooser(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<std::string(const std::fs::path &)> &nameCallback, const std::function<void(std::fs::path)> &callback)
|
|
|
|
: PopupNamedFileChooserBase(basePaths, files, validExtensions, multiple, nameCallback, callback) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
class PopupFileChooser : public PopupNamedFileChooserBase<PopupFileChooser> {
|
|
|
|
public:
|
|
|
|
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)
|
|
|
|
: PopupNamedFileChooserBase(basePaths, files, validExtensions, multiple, nameCallback, callback) { }
|
|
|
|
|
|
|
|
static std::string nameCallback(const std::fs::path &path) {
|
|
|
|
return wolv::util::toUTF8String(path);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-04-08 00:58:53 +02:00
|
|
|
}
|