2023-05-27 16:59:30 +02:00
|
|
|
#include <imgui.h>
|
2024-01-08 21:51:48 +01:00
|
|
|
#include <imgui_internal.h>
|
2023-05-27 16:59:30 +02:00
|
|
|
|
2023-11-18 14:50:43 +01:00
|
|
|
#include <hex/api/event_manager.hpp>
|
2023-05-27 16:59:30 +02:00
|
|
|
#include <hex/api/content_registry.hpp>
|
|
|
|
#include <hex/api/project_file_manager.hpp>
|
2023-11-18 14:50:43 +01:00
|
|
|
#include <hex/api/task_manager.hpp>
|
2023-05-27 16:59:30 +02:00
|
|
|
#include <hex/providers/provider.hpp>
|
2024-06-22 10:44:55 +02:00
|
|
|
#include <hex/helpers/default_paths.hpp>
|
2023-05-27 16:59:30 +02:00
|
|
|
|
2023-11-30 14:40:07 +01:00
|
|
|
#include <hex/helpers/fmt.hpp>
|
|
|
|
#include <fmt/chrono.h>
|
|
|
|
|
2023-05-27 16:59:30 +02:00
|
|
|
#include <wolv/utils/guards.hpp>
|
2023-07-09 12:53:31 +02:00
|
|
|
#include <wolv/utils/string.hpp>
|
2023-05-27 16:59:30 +02:00
|
|
|
|
|
|
|
#include <content/recent.hpp>
|
2023-12-26 00:22:47 +01:00
|
|
|
#include <toasts/toast_notification.hpp>
|
2023-05-27 16:59:30 +02:00
|
|
|
#include <fonts/codicons_font.h>
|
|
|
|
|
2024-01-20 10:35:37 +01:00
|
|
|
#include <ranges>
|
|
|
|
#include <unordered_set>
|
|
|
|
|
2023-05-27 16:59:30 +02:00
|
|
|
namespace hex::plugin::builtin::recent {
|
|
|
|
|
|
|
|
constexpr static auto MaxRecentEntries = 5;
|
2023-06-02 11:03:44 +02:00
|
|
|
constexpr static auto BackupFileName = "crash_backup.hexproj";
|
2023-05-27 16:59:30 +02:00
|
|
|
|
2023-12-11 11:42:33 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
std::atomic_bool s_recentEntriesUpdating = false;
|
|
|
|
std::list<RecentEntry> s_recentEntries;
|
|
|
|
std::atomic_bool s_autoBackupsFound = false;
|
|
|
|
|
|
|
|
|
|
|
|
class PopupAutoBackups : public Popup<PopupAutoBackups> {
|
2023-12-11 21:17:40 +01:00
|
|
|
private:
|
|
|
|
struct BackupEntry {
|
|
|
|
std::string displayName;
|
|
|
|
std::fs::path path;
|
|
|
|
};
|
2023-12-11 11:42:33 +01:00
|
|
|
public:
|
2023-12-19 12:22:28 +01:00
|
|
|
PopupAutoBackups() : Popup("hex.builtin.welcome.start.recent.auto_backups", true, true) {
|
2024-06-22 10:44:55 +02:00
|
|
|
for (const auto &backupPath : paths::Backups.read()) {
|
2023-12-11 21:17:40 +01:00
|
|
|
for (const auto &entry : std::fs::directory_iterator(backupPath)) {
|
|
|
|
if (entry.is_regular_file() && entry.path().extension() == ".hexproj") {
|
|
|
|
wolv::io::File backupFile(entry.path(), wolv::io::File::Mode::Read);
|
|
|
|
|
2023-12-19 13:10:25 +01:00
|
|
|
m_backups.emplace_back(
|
2023-12-11 21:17:40 +01:00
|
|
|
hex::format("hex.builtin.welcome.start.recent.auto_backups.backup"_lang, fmt::gmtime(backupFile.getFileInfo()->st_ctime)),
|
|
|
|
entry.path()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-11 11:42:33 +01:00
|
|
|
void drawContent() override {
|
|
|
|
if (ImGui::BeginTable("AutoBackups", 1, ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersInnerV, ImVec2(0, ImGui::GetTextLineHeightWithSpacing() * 5))) {
|
2023-12-19 13:10:25 +01:00
|
|
|
for (const auto &backup : m_backups | std::views::reverse | std::views::take(10)) {
|
2023-12-11 21:17:40 +01:00
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
|
|
|
if (ImGui::Selectable(backup.displayName.c_str())) {
|
|
|
|
ProjectFile::load(backup.path);
|
|
|
|
Popup::close();
|
2023-12-11 11:42:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndTable();
|
|
|
|
}
|
2023-12-13 15:08:27 +01:00
|
|
|
|
2024-04-12 22:56:10 +02:00
|
|
|
if (ImGui::IsKeyPressed(ImGuiKey_Escape))
|
2023-12-13 15:08:27 +01:00
|
|
|
this->close();
|
2023-12-11 11:42:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] ImGuiWindowFlags getFlags() const override {
|
|
|
|
return ImGuiWindowFlags_AlwaysAutoResize;
|
|
|
|
}
|
2023-12-11 21:17:40 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<BackupEntry> m_backups;
|
2023-12-11 11:42:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-05-27 16:59:30 +02:00
|
|
|
|
|
|
|
void registerEventHandlers() {
|
|
|
|
// Save every opened provider as a "recent" shortcut
|
2023-12-08 10:29:44 +01:00
|
|
|
(void)EventProviderOpened::subscribe([](const prv::Provider *provider) {
|
2024-02-03 12:16:36 +01:00
|
|
|
if (ContentRegistry::Settings::read<bool>("hex.builtin.setting.general", "hex.builtin.setting.general.save_recent_providers", true)) {
|
2023-05-27 16:59:30 +02:00
|
|
|
auto fileName = hex::format("{:%y%m%d_%H%M%S}.json", fmt::gmtime(std::chrono::system_clock::now()));
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
// Do not save to recents if the provider is part of a project
|
2023-11-08 11:54:21 +01:00
|
|
|
if (ProjectFile::hasPath())
|
|
|
|
return;
|
2023-05-27 16:59:30 +02:00
|
|
|
|
2023-11-08 11:54:21 +01:00
|
|
|
// Do not save to recents if the provider doesn't want it
|
|
|
|
if (!provider->isSavableAsRecent())
|
|
|
|
return;
|
2023-08-25 15:35:15 +02:00
|
|
|
|
2023-05-27 16:59:30 +02:00
|
|
|
// The recent provider is saved to every "recent" directory
|
2024-06-22 10:44:55 +02:00
|
|
|
for (const auto &recentPath : paths::Recent.write()) {
|
2023-05-27 16:59:30 +02:00
|
|
|
wolv::io::File recentFile(recentPath / fileName, wolv::io::File::Mode::Create);
|
|
|
|
if (!recentFile.isValid())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
{
|
|
|
|
auto path = ProjectFile::getPath();
|
|
|
|
ProjectFile::clearPath();
|
|
|
|
|
2023-11-30 14:40:07 +01:00
|
|
|
if (auto settings = provider->storeSettings({}); !settings.is_null())
|
2023-05-27 16:59:30 +02:00
|
|
|
recentFile.writeString(settings.dump(4));
|
|
|
|
|
|
|
|
ProjectFile::setPath(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateRecentEntries();
|
|
|
|
});
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
// Save opened projects as a "recent" shortcut
|
2023-12-08 10:29:44 +01:00
|
|
|
(void)EventProjectOpened::subscribe([] {
|
2024-02-03 12:16:36 +01:00
|
|
|
if (ContentRegistry::Settings::read<bool>("hex.builtin.setting.general", "hex.builtin.setting.general.save_recent_providers", true)) {
|
2023-05-27 16:59:30 +02:00
|
|
|
auto fileName = hex::format("{:%y%m%d_%H%M%S}.json", fmt::gmtime(std::chrono::system_clock::now()));
|
|
|
|
|
2023-06-02 11:03:44 +02:00
|
|
|
auto projectFileName = ProjectFile::getPath().filename();
|
|
|
|
if (projectFileName == BackupFileName)
|
|
|
|
return;
|
|
|
|
|
2023-05-27 16:59:30 +02:00
|
|
|
// The recent provider is saved to every "recent" directory
|
2024-06-22 10:44:55 +02:00
|
|
|
for (const auto &recentPath : paths::Recent.write()) {
|
2023-05-27 16:59:30 +02:00
|
|
|
wolv::io::File recentFile(recentPath / fileName, wolv::io::File::Mode::Create);
|
|
|
|
if (!recentFile.isValid())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
nlohmann::json recentEntry {
|
2024-05-19 10:21:30 +02:00
|
|
|
{ "type", "project" },
|
|
|
|
{ "displayName", wolv::util::toUTF8String(projectFileName) },
|
|
|
|
{ "path", wolv::util::toUTF8String(ProjectFile::getPath()) }
|
2023-05-27 16:59:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
recentFile.writeString(recentEntry.dump(4));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateRecentEntries();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateRecentEntries() {
|
2024-07-27 14:09:52 +02:00
|
|
|
TaskManager::createBackgroundTask("Updating recent files"_lang, [](auto&) {
|
2023-05-27 16:59:30 +02:00
|
|
|
if (s_recentEntriesUpdating)
|
|
|
|
return;
|
|
|
|
|
|
|
|
s_recentEntriesUpdating = true;
|
|
|
|
ON_SCOPE_EXIT { s_recentEntriesUpdating = false; };
|
|
|
|
|
|
|
|
s_recentEntries.clear();
|
|
|
|
|
|
|
|
// Query all recent providers
|
|
|
|
std::vector<std::fs::path> recentFilePaths;
|
2024-06-22 10:44:55 +02:00
|
|
|
for (const auto &folder : paths::Recent.read()) {
|
2023-05-27 16:59:30 +02:00
|
|
|
for (const auto &entry : std::fs::directory_iterator(folder)) {
|
|
|
|
if (entry.is_regular_file())
|
|
|
|
recentFilePaths.push_back(entry.path());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort recent provider files by last modified time
|
|
|
|
std::sort(recentFilePaths.begin(), recentFilePaths.end(), [](const auto &a, const auto &b) {
|
|
|
|
return std::fs::last_write_time(a) > std::fs::last_write_time(b);
|
|
|
|
});
|
|
|
|
|
|
|
|
std::unordered_set<RecentEntry, RecentEntry::HashFunction> uniqueProviders;
|
2024-06-29 18:49:23 +02:00
|
|
|
for (const auto &path : recentFilePaths) {
|
|
|
|
if (uniqueProviders.size() >= MaxRecentEntries)
|
|
|
|
break;
|
|
|
|
|
2023-05-27 16:59:30 +02:00
|
|
|
try {
|
2024-06-29 18:49:23 +02:00
|
|
|
wolv::io::File recentFile(path, wolv::io::File::Mode::Read);
|
|
|
|
if (!recentFile.isValid()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto content = recentFile.readString();
|
|
|
|
if (content.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto jsonData = nlohmann::json::parse(content);
|
2023-05-27 16:59:30 +02:00
|
|
|
uniqueProviders.insert(RecentEntry {
|
|
|
|
.displayName = jsonData.at("displayName"),
|
|
|
|
.type = jsonData.at("type"),
|
2023-12-17 23:15:52 +01:00
|
|
|
.entryFilePath = path,
|
2023-05-27 16:59:30 +02:00
|
|
|
.data = jsonData
|
|
|
|
});
|
2024-06-27 22:40:19 +02:00
|
|
|
} catch (const std::exception &e) {
|
|
|
|
log::error("Failed to parse recent file: {}", e.what());
|
|
|
|
}
|
2023-05-27 16:59:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all recent provider files that are not in the list
|
|
|
|
for (const auto &path : recentFilePaths) {
|
|
|
|
bool found = false;
|
|
|
|
for (const auto &provider : uniqueProviders) {
|
|
|
|
if (path == provider.entryFilePath) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
wolv::io::fs::remove(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::copy(uniqueProviders.begin(), uniqueProviders.end(), std::front_inserter(s_recentEntries));
|
2023-12-11 11:42:33 +01:00
|
|
|
|
|
|
|
s_autoBackupsFound = false;
|
2024-06-22 10:44:55 +02:00
|
|
|
for (const auto &backupPath : paths::Backups.read()) {
|
2023-12-11 11:42:33 +01:00
|
|
|
for (const auto &entry : std::fs::directory_iterator(backupPath)) {
|
|
|
|
if (entry.is_regular_file() && entry.path().extension() == ".hexproj") {
|
|
|
|
s_autoBackupsFound = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-27 16:59:30 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void loadRecentEntry(const RecentEntry &recentEntry) {
|
|
|
|
if (recentEntry.type == "project") {
|
|
|
|
std::fs::path projectPath = recentEntry.data["path"].get<std::string>();
|
2024-02-26 21:48:56 +01:00
|
|
|
if (!ProjectFile::load(projectPath)) {
|
|
|
|
ui::ToastError::open(hex::format("hex.builtin.popup.error.project.load"_lang, wolv::util::toUTF8String(projectPath)));
|
|
|
|
}
|
2023-05-27 16:59:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto *provider = ImHexApi::Provider::createProvider(recentEntry.type, true);
|
|
|
|
if (provider != nullptr) {
|
|
|
|
provider->loadSettings(recentEntry.data);
|
|
|
|
|
|
|
|
if (!provider->open() || !provider->isAvailable()) {
|
2023-12-26 00:22:47 +01:00
|
|
|
ui::ToastError::open(hex::format("hex.builtin.provider.error.open"_lang, provider->getErrorMessage()));
|
2023-05-27 16:59:30 +02:00
|
|
|
TaskManager::doLater([provider] { ImHexApi::Provider::remove(provider); });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
EventProviderOpened::post(provider);
|
2023-05-27 16:59:30 +02:00
|
|
|
|
|
|
|
updateRecentEntries();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void draw() {
|
2023-12-11 11:42:33 +01:00
|
|
|
if (s_recentEntries.empty() && !s_autoBackupsFound)
|
2023-11-18 15:11:12 +01:00
|
|
|
return;
|
|
|
|
|
2024-06-25 13:54:29 +02:00
|
|
|
static bool collapsed = false;
|
|
|
|
if (ImGuiExt::BeginSubWindow("hex.builtin.welcome.start.recent"_lang, &collapsed, ImVec2(), ImGuiChildFlags_AutoResizeX)) {
|
2023-05-27 16:59:30 +02:00
|
|
|
if (!s_recentEntriesUpdating) {
|
2023-11-16 09:32:24 +01:00
|
|
|
for (auto it = s_recentEntries.begin(); it != s_recentEntries.end();) {
|
2023-05-27 16:59:30 +02:00
|
|
|
const auto &recentEntry = *it;
|
|
|
|
bool shouldRemove = false;
|
|
|
|
|
2023-11-16 13:23:28 +01:00
|
|
|
const bool isProject = recentEntry.type == "project";
|
|
|
|
|
2023-05-27 16:59:30 +02:00
|
|
|
ImGui::PushID(&recentEntry);
|
|
|
|
ON_SCOPE_EXIT { ImGui::PopID(); };
|
|
|
|
|
|
|
|
const char* icon;
|
2023-11-16 13:23:28 +01:00
|
|
|
if (isProject) {
|
2023-05-27 16:59:30 +02:00
|
|
|
icon = ICON_VS_PROJECT;
|
|
|
|
} else {
|
|
|
|
icon = ICON_VS_FILE_BINARY;
|
|
|
|
}
|
2023-11-16 22:24:06 +01:00
|
|
|
|
|
|
|
if (ImGuiExt::Hyperlink(hex::format("{} {}", icon, hex::limitStringLength(recentEntry.displayName, 32)).c_str())) {
|
2023-05-27 16:59:30 +02:00
|
|
|
loadRecentEntry(recentEntry);
|
|
|
|
break;
|
|
|
|
}
|
2023-12-11 11:42:33 +01:00
|
|
|
|
|
|
|
if (ImGui::IsItemHovered() && ImGui::GetIO().KeyShift) {
|
|
|
|
if (ImGui::BeginTooltip()) {
|
|
|
|
if (ImGui::BeginTable("##RecentEntryTooltip", 2, ImGuiTableFlags_RowBg)) {
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
2023-12-23 21:09:41 +01:00
|
|
|
ImGui::TextUnformatted("hex.ui.common.name"_lang);
|
2023-12-11 11:42:33 +01:00
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TextUnformatted(recentEntry.displayName.c_str());
|
|
|
|
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
2023-12-23 21:09:41 +01:00
|
|
|
ImGui::TextUnformatted("hex.ui.common.type"_lang);
|
2023-12-11 11:42:33 +01:00
|
|
|
ImGui::TableNextColumn();
|
|
|
|
|
|
|
|
if (isProject) {
|
2023-12-23 21:09:41 +01:00
|
|
|
ImGui::TextUnformatted("hex.ui.common.project"_lang);
|
2023-12-11 11:42:33 +01:00
|
|
|
|
|
|
|
ImGui::TableNextRow();
|
|
|
|
ImGui::TableNextColumn();
|
2023-12-23 21:09:41 +01:00
|
|
|
ImGui::TextUnformatted("hex.ui.common.path"_lang);
|
2023-12-11 11:42:33 +01:00
|
|
|
ImGui::TableNextColumn();
|
|
|
|
ImGui::TextUnformatted(recentEntry.data["path"].get<std::string>().c_str());
|
|
|
|
} else {
|
|
|
|
ImGui::TextUnformatted(Lang(recentEntry.type));
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndTable();
|
|
|
|
}
|
|
|
|
ImGui::EndTooltip();
|
|
|
|
}
|
|
|
|
}
|
2023-05-27 16:59:30 +02:00
|
|
|
|
|
|
|
// Detect right click on recent provider
|
2023-11-16 09:32:24 +01:00
|
|
|
std::string popupID = hex::format("RecentEntryMenu.{}", recentEntry.getHash());
|
2023-05-27 16:59:30 +02:00
|
|
|
if (ImGui::IsMouseReleased(1) && ImGui::IsItemHovered()) {
|
|
|
|
ImGui::OpenPopup(popupID.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::BeginPopup(popupID.c_str())) {
|
2023-12-23 21:09:41 +01:00
|
|
|
if (ImGui::MenuItem("hex.ui.common.remove"_lang)) {
|
2023-05-27 16:59:30 +02:00
|
|
|
shouldRemove = true;
|
|
|
|
}
|
|
|
|
ImGui::EndPopup();
|
|
|
|
}
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
// Handle deletion from vector and on disk
|
2023-05-27 16:59:30 +02:00
|
|
|
if (shouldRemove) {
|
|
|
|
wolv::io::fs::remove(recentEntry.entryFilePath);
|
|
|
|
it = s_recentEntries.erase(it);
|
|
|
|
} else {
|
2023-11-10 20:47:08 +01:00
|
|
|
++it;
|
2023-05-27 16:59:30 +02:00
|
|
|
}
|
|
|
|
}
|
2023-12-11 11:42:33 +01:00
|
|
|
|
|
|
|
if (s_autoBackupsFound) {
|
|
|
|
ImGui::Separator();
|
|
|
|
if (ImGuiExt::Hyperlink(hex::format("{} {}", ICON_VS_ARCHIVE, "hex.builtin.welcome.start.recent.auto_backups"_lang).c_str()))
|
|
|
|
PopupAutoBackups::open();
|
|
|
|
}
|
2023-05-27 16:59:30 +02:00
|
|
|
}
|
2024-06-25 13:54:29 +02:00
|
|
|
|
2023-05-27 16:59:30 +02:00
|
|
|
}
|
2024-06-26 19:11:31 +02:00
|
|
|
ImGuiExt::EndSubWindow();
|
2023-05-27 16:59:30 +02:00
|
|
|
}
|
|
|
|
|
2023-11-17 15:54:38 +01:00
|
|
|
void addMenuItems() {
|
2023-05-27 16:59:30 +02:00
|
|
|
ContentRegistry::Interface::addMenuItemSubMenu({ "hex.builtin.menu.file" }, 1200, [] {
|
2024-01-08 21:51:48 +01:00
|
|
|
if (ImGui::BeginMenuEx("hex.builtin.menu.file.open_recent"_lang, ICON_VS_ARCHIVE, !recent::s_recentEntriesUpdating && !s_recentEntries.empty())) {
|
2023-05-27 16:59:30 +02:00
|
|
|
// Copy to avoid changing list while iteration
|
|
|
|
auto recentEntries = s_recentEntries;
|
|
|
|
for (auto &recentEntry : recentEntries) {
|
|
|
|
if (ImGui::MenuItem(recentEntry.displayName.c_str())) {
|
|
|
|
loadRecentEntry(recentEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::Separator();
|
|
|
|
if (ImGui::MenuItem("hex.builtin.menu.file.clear_recent"_lang)) {
|
|
|
|
s_recentEntries.clear();
|
|
|
|
|
|
|
|
// Remove all recent files
|
2024-06-22 10:44:55 +02:00
|
|
|
for (const auto &recentPath : paths::Recent.write()) {
|
2023-05-27 16:59:30 +02:00
|
|
|
for (const auto &entry : std::fs::directory_iterator(recentPath))
|
|
|
|
std::fs::remove(entry.path());
|
2023-12-27 16:33:49 +01:00
|
|
|
}
|
2023-05-27 16:59:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndMenu();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|