1
0
mirror of synced 2024-11-28 09:30:51 +01:00

impr: Handle and show NFD errors (#995)

This PR handles errors that NFD might encounter (both in Init() and the
other method to open the dialog), and log them in the logs and in the
GUI

This (among other) fix the crash I had running ImHex as root and opening
a file
This commit is contained in:
Thomas 2023-03-26 11:02:51 +02:00 committed by GitHub
parent 725e32250b
commit fabb1596e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 11 deletions

@ -1 +1 @@
Subproject commit 6efc824070c79afd42a3a9e08da8f867d0ca2a97
Subproject commit d1b80e3a60fcf30d0c886652790796630d09c90e

View File

@ -20,7 +20,7 @@ namespace hex::fs {
Folder
};
void setFileBrowserErrorCallback(const std::function<void()> &callback);
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);
enum class ImHexPath : u32 {

View File

@ -2,6 +2,7 @@
#include <hex/api/content_registry.hpp>
#include <hex/api/project_file_manager.hpp>
#include <hex/helpers/logger.hpp>
#include <xdg.hpp>
@ -21,13 +22,20 @@
namespace hex::fs {
static std::function<void()> s_fileBrowserErrorCallback;
void setFileBrowserErrorCallback(const std::function<void()> &callback) {
static std::function<void(const std::string&)> s_fileBrowserErrorCallback;
void setFileBrowserErrorCallback(const std::function<void(const std::string&)> &callback) {
s_fileBrowserErrorCallback = 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) {
NFD::Init();
NFD::ClearError();
if (NFD::Init() != NFD_OKAY) {
log::error("NFD init returned an error: {}", NFD::GetError());
if (s_fileBrowserErrorCallback != nullptr)
s_fileBrowserErrorCallback(NFD::GetError() ? NFD::GetError() : "No details");
return false;
}
NFD::UniquePathU8 outPath;
NFD::UniquePathSet outPaths;
@ -64,8 +72,9 @@ namespace hex::fs {
}
}
} else if (result == NFD_ERROR) {
log::error("Requested file dialog returned an error: {}", NFD::GetError());
if (s_fileBrowserErrorCallback != nullptr)
s_fileBrowserErrorCallback();
s_fileBrowserErrorCallback(NFD::GetError() ? NFD::GetError() : "No details");
}
NFD::Quit();

View File

@ -362,8 +362,8 @@
"hex.builtin.popup.close_provider.desc": "There are unsaved changes made to this Provider\nthat haven't been saved to a Project yet.\n\nAre you sure you want to close it?",
"hex.builtin.popup.close_provider.title": "Close Provider?",
"hex.builtin.popup.error.create": "Failed to create new file!",
"hex.builtin.popup.error.file_dialog.common": "An error occurred while opening the file browser!",
"hex.builtin.popup.error.file_dialog.portal": "There was an error while opening the file browser.\nThis might be caused by your system not having a xdg-desktop-portal backend installed correctly.\n\nOn KDE, it's xdg-desktop-portal-kde.\nOn Gnome it's xdg-desktop-portal-gnome.\nOn wlroots it's xdg-desktop-portal-wlr.\nOtherwise, you can try to use xdg-desktop-portal-gtk.\n\nReboot your system after installing it.\n\nIf the file browser still doesn't work after this, submit an issue at https://github.com/WerWolv/ImHex/issues\n\nIn the meantime files can still be opened by dragging them onto the ImHex window!",
"hex.builtin.popup.error.file_dialog.common": "An error occurred while opening the file browser: {}",
"hex.builtin.popup.error.file_dialog.portal": "There was an error while opening the file browser: {}.\nThis might be caused by your system not having a xdg-desktop-portal backend installed correctly.\n\nOn KDE, it's xdg-desktop-portal-kde.\nOn Gnome it's xdg-desktop-portal-gnome.\nOn wlroots it's xdg-desktop-portal-wlr.\nOtherwise, you can try to use xdg-desktop-portal-gtk.\n\nReboot your system after installing it.\n\nIf the file browser still doesn't work after this, submit an issue at https://github.com/WerWolv/ImHex/issues\n\nIn the meantime files can still be opened by dragging them onto the ImHex window!",
"hex.builtin.popup.error.open": "Failed to open file!",
"hex.builtin.popup.error.project.load": "Failed to load project!",
"hex.builtin.popup.error.project.save": "Failed to save project!",

View File

@ -123,11 +123,11 @@ namespace hex::plugin::builtin {
ImHexApi::HexEditor::impl::setCurrentSelection(region);
});
fs::setFileBrowserErrorCallback([]{
fs::setFileBrowserErrorCallback([](const std::string& errMsg){
#if defined(NFD_PORTAL)
View::showErrorPopup("hex.builtin.popup.error.file_dialog.portal"_lang);
View::showErrorPopup(hex::format("hex.builtin.popup.error.file_dialog.portal"_lang, errMsg));
#else
View::showErrorPopup("hex.builtin.popup.error.file_dialog.common"_lang);
View::showErrorPopup(hex::format("hex.builtin.popup.error.file_dialog.common"_lang, errMsg));
#endif
});