feat: Added context menu with right-clicking on file provider (#1084)

Co-authored-by: Nik <werwolv98@gmail.com>
This commit is contained in:
iTrooz 2023-05-21 13:21:53 +02:00 committed by GitHub
parent 047c39e2c7
commit 5666a5c5fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 107 additions and 0 deletions

View File

@ -23,6 +23,10 @@ namespace hex::fs {
void setFileBrowserErrorCallback(const std::function<void(const std::string&)> &callback);
bool openFileBrowser(DialogMode mode, const std::vector<nfdfilteritem_t> &validExtensions, const std::function<void(std::fs::path)> &callback, const std::string &defaultPath = {}, bool multiple = false);
void openFileExternal(const std::fs::path &filePath);
void openFolderExternal(const std::fs::path &dirPath);
void openFolderWithSelectionExternal(const std::fs::path &selectedFilePath);
enum class ImHexPath : u32 {
Patterns = 0,
PatternsInclude,

View File

@ -118,6 +118,10 @@ namespace hex::prv {
[[nodiscard]] virtual bool hasFilePicker() const;
virtual bool handleFilePicker();
virtual std::vector<std::pair<std::string, std::function<void()>>> getMenuEntries() {
return { };
};
[[nodiscard]] virtual bool hasLoadInterface() const;
[[nodiscard]] virtual bool hasInterface() const;
virtual bool drawLoadInterface();

View File

@ -3,6 +3,7 @@
#include <hex/api/content_registry.hpp>
#include <hex/api/project_file_manager.hpp>
#include <hex/helpers/logger.hpp>
#include <hex/helpers/fmt.hpp>
#include <xdg.hpp>
@ -27,6 +28,71 @@ namespace hex::fs {
s_fileBrowserErrorCallback = callback;
}
// With help from https://github.com/owncloud/client/blob/cba22aa34b3677406e0499aadd126ce1d94637a2/src/gui/openfilemanager.cpp
void openFileExternal(const std::fs::path &filePath) {
if (!wolv::io::fs::exists(filePath))
return;
#if defined(OS_WINDOWS)
hex::unused(
ShellExecute(nullptr, "open", wolv::util::toUTF8String(filePath).c_str(), nullptr, nullptr, SW_SHOWNORMAL)
);
#elif defined(OS_MACOS)
hex::unused(system(
hex::format("open {}", wolv::util::toUTF8String(filePath)).c_str()
));
#elif defined(OS_LINUX)
hex::unused(system(
hex::format("xdg-open {}", wolv::util::toUTF8String(filePath)).c_str()
));
#endif
}
void openFolderExternal(const std::fs::path &dirPath) {
if (!wolv::io::fs::exists(dirPath))
return;
#if defined(OS_WINDOWS)
hex::unused(system(
hex::format("explorer.exe {}", wolv::util::toUTF8String(dirPath)).c_str()
));
#elif defined(OS_MACOS)
hex::unused(system(
hex::format("open {}", wolv::util::toUTF8String(dirPath)).c_str()
));
#elif defined(OS_LINUX)
hex::unused(system(
hex::format("xdg-open {}", wolv::util::toUTF8String(dirPath)).c_str()
));
#endif
}
void openFolderWithSelectionExternal(const std::fs::path &selectedFilePath) {
if (!wolv::io::fs::exists(selectedFilePath))
return;
#if defined(OS_WINDOWS)
hex::unused(system(
hex::format(R"(explorer.exe /select,"{}")", wolv::util::toUTF8String(selectedFilePath)).c_str()
));
#elif defined(OS_MACOS)
hex::unused(system(
hex::format(
R"(osascript -e 'tell application "Finder" to reveal POSIX file "{}"')",
wolv::util::toUTF8String(selectedFilePath)
).c_str()
));
system(R"(osascript -e 'tell application "Finder" to activate')");
#elif defined(OS_LINUX)
// fallback to only opening the folder for now
// TODO actually select the file
hex::unused(system(
hex::format("xdg-open {}", wolv::util::toUTF8String(selectedFilePath.parent_path())).c_str()
));
#endif
}
bool openFileBrowser(DialogMode mode, const std::vector<nfdfilteritem_t> &validExtensions, const std::function<void(std::fs::path)> &callback, const std::string &defaultPath, bool multiple) {
NFD::ClearError();

View File

@ -42,6 +42,8 @@ namespace hex::plugin::builtin {
[[nodiscard]] bool hasFilePicker() const override { return true; }
[[nodiscard]] bool handleFilePicker() override;
std::vector<std::pair<std::string, std::function<void()>>> getMenuEntries() override;
void setPath(const std::fs::path &path);
[[nodiscard]] bool open() override;

View File

@ -352,6 +352,8 @@
"hex.builtin.provider.file.modification",
"hex.builtin.provider.file.path",
"hex.builtin.provider.file.size",
"hex.builtin.provider.file.menu.open_file",
"hex.builtin.provider.file.menu.open_folder",
"hex.builtin.provider.gdb",
"hex.builtin.provider.gdb.ip",
"hex.builtin.provider.gdb.name",

View File

@ -403,6 +403,8 @@
"hex.builtin.provider.file.modification": "Last modification time",
"hex.builtin.provider.file.path": "File path",
"hex.builtin.provider.file.size": "Size",
"hex.builtin.provider.file.menu.open_file": "Open file externally",
"hex.builtin.provider.file.menu.open_folder": "Open containing folder",
"hex.builtin.provider.gdb": "GDB Server Provider",
"hex.builtin.provider.gdb.ip": "IP Address",
"hex.builtin.provider.gdb.name": "GDB Server <{0}:{1}>",

View File

@ -185,6 +185,19 @@ namespace hex::plugin::builtin {
});
}
std::vector<std::pair<std::string, std::function<void()>>> FileProvider::getMenuEntries(){
return {
{"hex.builtin.provider.file.menu.open_folder"_lang, [path = this->m_path] {
fs::openFolderWithSelectionExternal(path);
}},
{"hex.builtin.provider.file.menu.open_file"_lang, [path = this->m_path] {
fs::openFileExternal(path);
}},
};
}
void FileProvider::setPath(const std::fs::path &path) {
this->m_path = path;
}

View File

@ -224,6 +224,20 @@ namespace hex::plugin::builtin {
ImHexApi::Provider::remove(providers[i]);
break;
}
std::string popupID = std::string("ProviderMenu.") + std::to_string(tabProvider->getID());
if (ImGui::IsMouseReleased(1) && ImGui::IsItemHovered()) {
ImGui::OpenPopup(popupID.c_str());
}
if (ImGui::BeginPopup(popupID.c_str())) {
for (auto p : tabProvider->getMenuEntries()) {
if (ImGui::MenuItem(p.first.c_str())) {
p.second();
}
}
ImGui::EndPopup();
}
}
ImGui::EndTabBar();
}