Skip to content

Commit

Permalink
Update nativefiledialog and keep dialogs on top
Browse files Browse the repository at this point in the history
  • Loading branch information
btzy committed Jun 24, 2024
1 parent b93fd52 commit 279b2aa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
41 changes: 37 additions & 4 deletions lib/libimhex/source/helpers/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,37 @@
#include <emscripten.h>
#else
#include <nfd.hpp>
#if defined(OS_WINDOWS)
#define GLFW_EXPOSE_NATIVE_WIN32
#endif
#if defined(OS_MACOS)
// macOS platform headers can't be compiled with gcc
#define GLFW_NATIVE_INCLUDE_NONE
typedef uint32_t CGDirectDisplayID;
typedef void *id;
typedef void NSWindow;
#define GLFW_EXPOSE_NATIVE_COCOA
#endif
#if defined(OS_LINUX)
#define GLFW_EXPOSE_NATIVE_X11
#endif
#if defined(OS_LINUX) && defined(GLFW_WAYLAND_APP_ID)
#define GLFW_EXPOSE_NATIVE_WAYLAND
#endif
#include <nfd_glfw3.h>
#if defined(OS_LINUX) && defined(GLFW_WAYLAND_APP_ID)
#undef GLFW_EXPOSE_NATIVE_WAYLAND
#endif
#if defined(OS_LINUX)
#undef GLFW_EXPOSE_NATIVE_X11
#endif
#if defined(OS_MACOS)
#undef GLFW_EXPOSE_NATIVE_COCOA
#undef GLFW_NATIVE_INCLUDE_NONE
#endif
#if defined(OS_WINDOWS)
#undef GLFW_EXPOSE_NATIVE_WIN32
#endif
#endif

#include <filesystem>
Expand Down Expand Up @@ -232,20 +263,22 @@ namespace hex::fs {
NFD::UniquePathU8 outPath;
NFD::UniquePathSet outPaths;
nfdresult_t result = NFD_ERROR;
nfdwindowhandle_t nativeWindow{};
NFD_GetNativeWindowFromGLFWWindow(ImHexApi::System::getMainWindowHandle(), &nativeWindow);

// Open the correct file dialog based on the mode
switch (mode) {
case DialogMode::Open:
if (multiple)
result = NFD::OpenDialogMultiple(outPaths, validExtensionsNfd.data(), validExtensionsNfd.size(), defaultPath.empty() ? nullptr : defaultPath.c_str());
result = NFD::OpenDialogMultiple(outPaths, validExtensionsNfd.data(), validExtensionsNfd.size(), defaultPath.empty() ? nullptr : defaultPath.c_str(), nativeWindow);
else
result = NFD::OpenDialog(outPath, validExtensionsNfd.data(), validExtensionsNfd.size(), defaultPath.empty() ? nullptr : defaultPath.c_str());
result = NFD::OpenDialog(outPath, validExtensionsNfd.data(), validExtensionsNfd.size(), defaultPath.empty() ? nullptr : defaultPath.c_str(), nativeWindow);
break;
case DialogMode::Save:
result = NFD::SaveDialog(outPath, validExtensionsNfd.data(), validExtensionsNfd.size(), defaultPath.empty() ? nullptr : defaultPath.c_str());
result = NFD::SaveDialog(outPath, validExtensionsNfd.data(), validExtensionsNfd.size(), defaultPath.empty() ? nullptr : defaultPath.c_str(), nullptr, nativeWindow);
break;
case DialogMode::Folder:
result = NFD::PickFolder(outPath, defaultPath.empty() ? nullptr : defaultPath.c_str());
result = NFD::PickFolder(outPath, defaultPath.empty() ? nullptr : defaultPath.c_str(), nativeWindow);
break;
}

Expand Down

0 comments on commit 279b2aa

Please sign in to comment.