Skip to content

Commit

Permalink
build: Update nativefiledialog and keep dialogs on top (#1771)
Browse files Browse the repository at this point in the history
This PR updates the nativefiledialog submodule and uses its new feature
to set the ImHex main window as the parent of the dialog window. This
ensures that the dialog stays on top of the main window. This is
currently supported by NFDe on Windows, macOS, and Linux/X11.
Linux/Wayland behaves as it did previously due to limitations in NFDe.

Note that macOS file dialogs have already been parented properly as NFDe
previously used the key window (the window currently receiving keyboard
events) on macOS. However, it's probably better to do the correct thing
and pass the main window to NFDe even on macOS.

### Problem description
The file dialog go behind the main window if the main window is clicked
while the file dialog is open.

### Implementation description
Update nativefiledialog and pass the `GLFWwindow*` of the main window to
the library function.

### Screenshots
Before:


https://github.com/WerWolv/ImHex/assets/6948096/589c3401-702a-4b0a-99ed-02d3e4d9080e

After:


https://github.com/WerWolv/ImHex/assets/6948096/8fef4900-eedc-48d5-8a4e-7bd81e37e3c0

### Additional things
I have tested this on Windows and Linux/X11, but did not test this on
macOS. It would be ideal if someone can help with this. (But as far as
NFDe is concerned, macOS `NSWindow*` handles have been tested (with
SDL2) and works.)

Co-authored-by: Nik <werwolv98@gmail.com>
  • Loading branch information
btzy and WerWolv committed Jul 3, 2024
1 parent 2595feb commit dd60762
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 @@ -233,20 +264,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 dd60762

Please sign in to comment.