ui: Fixed automatic pattern loading, added better pattern browse popup

This commit is contained in:
WerWolv 2021-09-26 21:18:25 +02:00
parent e46807c600
commit 5db608c3fc
13 changed files with 102 additions and 29 deletions

View File

@ -26,12 +26,14 @@ namespace hex {
private:
pl::PatternLanguage *m_patternLanguageRuntime;
std::vector<std::string> m_possiblePatternFiles;
int m_selectedPatternFile = 0;
std::vector<std::filesystem::path> m_possiblePatternFiles;
u32 m_selectedPatternFile = 0;
bool m_runAutomatically = false;
bool m_evaluatorRunning = false;
bool m_hasUnevaluatedChanges = false;
bool m_acceptPatternWindowOpen = false;
TextEditor m_textEditor;
std::vector<std::pair<pl::LogConsole::Level, std::string>> m_console;

View File

@ -3,6 +3,7 @@
#include <filesystem>
#include <memory>
#include <string>
#include <list>
#include <vector>
#include <hex/views/view.hpp>
@ -64,6 +65,8 @@ namespace hex {
ImGui::Texture m_logoTexture;
std::filesystem::path m_safetyBackupPath;
std::list<std::string> m_popupsToOpen;
};
}

View File

@ -79,6 +79,8 @@ namespace hex::plugin::builtin {
{ "hex.common.dont_show_again", "Nicht mehr anzeigen" },
{ "hex.common.link", "Link" },
{ "hex.common.file", "Datei" },
{ "hex.common.open", "Öffnen" },
{ "hex.common.browse", "Druchsuchen..." },
{ "hex.view.bookmarks.name", "Lesezeichen" },
{ "hex.view.bookmarks.default_title", "Lesezeichen [0x{0:X} - 0x{1:X}]" },

View File

@ -79,6 +79,8 @@ namespace hex::plugin::builtin {
{ "hex.common.dont_show_again", "Don't show again" },
{ "hex.common.link", "Link" },
{ "hex.common.file", "File" },
{ "hex.common.open", "Open" },
{ "hex.common.browse", "Browse..." },
{ "hex.view.bookmarks.name", "Bookmarks" },
{ "hex.view.bookmarks.default_title", "Bookmark [0x{0:X} - 0x{1:X}]" },

View File

@ -78,6 +78,8 @@ namespace hex::plugin::builtin {
{ "hex.common.dont_show_again", "Non mostrare di nuovo" },
{ "hex.common.link", "Link" },
{ "hex.common.file", "File" },
//{ "hex.common.open", "Open" },
//{ "hex.common.browse", "Browse..." },
{ "hex.view.bookmarks.name", "Segnalibri" },
{ "hex.view.bookmarks.default_title", "Segnalibro [0x{0:X} - 0x{1:X}]" },

View File

@ -79,6 +79,8 @@ namespace hex::plugin::builtin {
{ "hex.common.dont_show_again", "不要再次显示" },
{ "hex.common.link", "链接" },
{ "hex.common.file", "文件" },
//{ "hex.common.open", "Open" },
//{ "hex.common.browse", "Browse..." },
{ "hex.view.bookmarks.name", "书签" },
{ "hex.view.bookmarks.default_title", "书签 [0x{0:X} - 0x{1:X}]" },

View File

@ -111,11 +111,12 @@ namespace hex {
EVENT_DEF(RequestOpenWindow, std::string);
EVENT_DEF(RequestSelectionChange, Region);
EVENT_DEF(RequestAddBookmark, ImHexApi::Bookmarks::Entry);
EVENT_DEF(RequestAppendPatternLanguageCode, std::string);
EVENT_DEF(RequestSetPatternLanguageCode, std::string);
EVENT_DEF(RequestChangeWindowTitle, std::string);
EVENT_DEF(RequestCloseImHex, bool);
EVENT_DEF(RequestOpenFile, std::string);
EVENT_DEF(RequestChangeTheme, u32);
EVENT_DEF(RequestOpenPopup, std::string);
EVENT_DEF(QuerySelection, Region&);

View File

@ -179,7 +179,7 @@ namespace hex {
Folder
};
void openFileBrowser(const std::string &title, DialogMode mode, const std::vector<nfdfilteritem_t> &validExtensions, const std::function<void(std::string)> &callback);
void openFileBrowser(const std::string &title, DialogMode mode, const std::vector<nfdfilteritem_t> &validExtensions, const std::function<void(std::string)> &callback, const std::string &defaultPath = {});
float float16ToFloat32(u16 float16);

View File

@ -74,7 +74,7 @@ namespace hex::magic {
auto magicFiles = getMagicFiles();
if (magicFiles.has_value()) {
magic_t ctx = magic_open(MAGIC_MIME);
magic_t ctx = magic_open(MAGIC_MIME_TYPE);
ON_SCOPE_EXIT { magic_close(ctx); };
if (magic_load(ctx, magicFiles->c_str()) == 0)

View File

@ -184,20 +184,20 @@ namespace hex {
}
void openFileBrowser(const std::string &title, DialogMode mode, const std::vector<nfdfilteritem_t> &validExtensions, const std::function<void(std::string)> &callback) {
void openFileBrowser(const std::string &title, DialogMode mode, const std::vector<nfdfilteritem_t> &validExtensions, const std::function<void(std::string)> &callback, const std::string &defaultPath) {
NFD::Init();
nfdchar_t *outPath;
nfdresult_t result;
switch (mode) {
case DialogMode::Open:
result = NFD::OpenDialog(outPath, validExtensions.data(), validExtensions.size(), nullptr);
result = NFD::OpenDialog(outPath, validExtensions.data(), validExtensions.size(), defaultPath.c_str());
break;
case DialogMode::Save:
result = NFD::SaveDialog(outPath, validExtensions.data(), validExtensions.size(), nullptr);
result = NFD::SaveDialog(outPath, validExtensions.data(), validExtensions.size(), defaultPath.c_str());
break;
case DialogMode::Folder:
result = NFD::PickFolder(outPath, nullptr);
result = NFD::PickFolder(outPath, defaultPath.c_str());
break;
default: __builtin_unreachable();
}

View File

@ -165,7 +165,7 @@ namespace hex {
code += "};\n";
EventManager::post<RequestAppendPatternLanguageCode>(code);
EventManager::post<RequestSetPatternLanguageCode>(code);
Py_RETURN_NONE;
}

View File

@ -17,6 +17,7 @@
namespace hex {
using namespace hex::literals;
namespace fs = std::filesystem;
static const TextEditor::LanguageDefinition& PatternLanguage() {
static bool initialized = false;
@ -96,18 +97,16 @@ namespace hex {
this->parsePattern(this->m_textEditor.GetText().data());
});
EventManager::subscribe<RequestAppendPatternLanguageCode>(this, [this](std::string code) {
this->m_textEditor.InsertText("\n");
EventManager::subscribe<RequestSetPatternLanguageCode>(this, [this](std::string code) {
this->m_textEditor.SelectAll();
this->m_textEditor.Delete();
this->m_textEditor.InsertText(code);
});
EventManager::subscribe<EventFileLoaded>(this, [this](const std::string &path) {
if (this->m_textEditor.GetText().find_first_not_of(" \f\n\r\t\v") != std::string::npos)
return;
pl::Preprocessor preprocessor;
if (ImHexApi::Provider::isValid())
if (!ImHexApi::Provider::isValid())
return;
std::string mimeType = magic::getMIMEType(ImHexApi::Provider::get());
@ -127,6 +126,7 @@ namespace hex {
std::error_code errorCode;
for (const auto &dir : hex::getPath(ImHexPath::Patterns)) {
for (auto &entry : std::filesystem::directory_iterator(dir, errorCode)) {
foundCorrectType = false;
if (!entry.is_regular_file())
continue;
@ -137,14 +137,15 @@ namespace hex {
preprocessor.preprocess(file.readString());
if (foundCorrectType)
this->m_possiblePatternFiles.push_back(entry.path().string());
this->m_possiblePatternFiles.push_back(entry.path());
}
}
if (!this->m_possiblePatternFiles.empty()) {
this->m_selectedPatternFile = 0;
View::doLater([] { ImGui::OpenPopup("hex.view.pattern.accept_pattern"_lang); });
EventManager::post<RequestOpenPopup>("hex.view.pattern.accept_pattern"_lang);
this->m_acceptPatternWindowOpen = true;
}
});
@ -174,7 +175,7 @@ namespace hex {
EventManager::unsubscribe<EventProjectFileStore>(this);
EventManager::unsubscribe<EventProjectFileLoad>(this);
EventManager::unsubscribe<RequestAppendPatternLanguageCode>(this);
EventManager::unsubscribe<RequestSetPatternLanguageCode>(this);
EventManager::unsubscribe<EventFileLoaded>(this);
EventManager::unsubscribe<RequestChangeTheme>(this);
}
@ -182,9 +183,19 @@ namespace hex {
void ViewPatternEditor::drawMenu() {
if (ImGui::BeginMenu("hex.menu.file"_lang)) {
if (ImGui::MenuItem("hex.view.pattern.menu.file.load_pattern"_lang)) {
hex::openFileBrowser("hex.view.pattern.open_pattern"_lang, DialogMode::Open, { { "Pattern File", "hexpat" } }, [this](auto path) {
this->loadPatternFile(path);
});
this->m_selectedPatternFile = 0;
this->m_possiblePatternFiles.clear();
for (auto &imhexPath : hex::getPath(ImHexPath::Patterns)) {
for (auto &entry: fs::recursive_directory_iterator(imhexPath)) {
if (entry.is_regular_file() && entry.path().extension() == ".hexpat") {
this->m_possiblePatternFiles.push_back(entry.path());
}
}
}
View::doLater([]{ ImGui::OpenPopup("hex.view.pattern.menu.file.load_pattern"_lang); });
}
ImGui::EndMenu();
}
@ -270,7 +281,7 @@ namespace hex {
}
void ViewPatternEditor::drawAlwaysVisible() {
if (ImGui::BeginPopupModal("hex.view.pattern.accept_pattern"_lang, nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
if (ImGui::BeginPopupModal("hex.view.pattern.accept_pattern"_lang, &this->m_acceptPatternWindowOpen, ImGuiWindowFlags_AlwaysAutoResize)) {
ImGui::TextWrapped("%s", static_cast<const char *>("hex.view.pattern.accept_pattern.desc"_lang));
std::vector<std::string> entries;
@ -280,19 +291,23 @@ namespace hex {
entries[i] = std::filesystem::path(this->m_possiblePatternFiles[i]).filename().string();
}
ImGui::ListBox("hex.view.pattern.accept_pattern.pattern_language"_lang, &this->m_selectedPatternFile, [](void *data, int id, const char** outText) -> bool {
auto &entries = *static_cast<std::vector<std::string>*>(data);
if (ImGui::BeginListBox("##patterns_accept", ImVec2(-FLT_MIN, 0))) {
*outText = entries[id].c_str();
u32 index = 0;
for (auto &path : this->m_possiblePatternFiles) {
if (ImGui::Selectable(path.filename().string().c_str(), index == this->m_selectedPatternFile))
this->m_selectedPatternFile = index;
index++;
}
return true;
}, &entries, entries.size(), 4);
ImGui::EndListBox();
}
ImGui::NewLine();
ImGui::Text("%s", static_cast<const char *>("hex.view.pattern.accept_pattern.question"_lang));
confirmButtons("hex.common.yes"_lang, "hex.common.no"_lang, [this]{
this->loadPatternFile(this->m_possiblePatternFiles[this->m_selectedPatternFile]);
this->loadPatternFile(this->m_possiblePatternFiles[this->m_selectedPatternFile].string());
ImGui::CloseCurrentPopup();
}, []{
ImGui::CloseCurrentPopup();
@ -303,6 +318,37 @@ namespace hex {
ImGui::EndPopup();
}
if (ImGui::BeginPopupModal("hex.view.pattern.menu.file.load_pattern"_lang, nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
if (ImGui::BeginListBox("##patterns", ImVec2(-FLT_MIN, 0))) {
u32 index = 0;
for (auto &path : this->m_possiblePatternFiles) {
if (ImGui::Selectable(path.filename().string().c_str(), index == this->m_selectedPatternFile))
this->m_selectedPatternFile = index;
index++;
}
ImGui::EndListBox();
}
if (ImGui::Button("hex.common.open"_lang)) {
this->loadPatternFile(this->m_possiblePatternFiles[this->m_selectedPatternFile].string());
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
if (ImGui::Button("hex.common.browse"_lang)) {
hex::openFileBrowser("hex.view.pattern.open_pattern"_lang, DialogMode::Open, { { "Pattern File", "hexpat" } }, [this](auto path) {
this->loadPatternFile(path);
ImGui::CloseCurrentPopup();
});
}
ImGui::EndPopup();
}
}

View File

@ -6,6 +6,7 @@
#include <hex/helpers/utils.hpp>
#include <hex/helpers/paths.hpp>
#include <hex/helpers/logger.hpp>
#include <hex/helpers/file.hpp>
#include <chrono>
#include <csignal>
@ -222,6 +223,10 @@ namespace hex {
}
});
EventManager::subscribe<RequestOpenPopup>(this, [this](auto name){
this->m_popupsToOpen.push_back(name);
});
for (const auto &path : hex::getPath(ImHexPath::Config)) {
if (auto filePath = std::filesystem::path(path) / CrashBackupFileName; std::filesystem::exists(filePath)) {
this->m_safetyBackupPath = filePath;
@ -263,6 +268,7 @@ namespace hex {
EventManager::unsubscribe<RequestChangeWindowTitle>(this);
EventManager::unsubscribe<EventAbnormalTermination>(this);
EventManager::unsubscribe<RequestChangeTheme>(this);
EventManager::unsubscribe<RequestOpenPopup>(this);
ImGui::UnloadImage(this->m_bannerTexture);
ImGui::UnloadImage(this->m_logoTexture);
@ -452,6 +458,13 @@ namespace hex {
ImGui::EndPopup();
}
for (auto it = this->m_popupsToOpen.begin(); it != this->m_popupsToOpen.end(); it++) {
if (ImGui::IsPopupOpen(it->c_str()))
this->m_popupsToOpen.erase(it);
else
ImGui::OpenPopup(it->c_str());
}
}
void Window::frame() {