Skip to content

Commit 654b8a5

Browse files
committed
Switch file open/save dialog provider to nativefiledialog-extended
This commit replaces the hand-written file dialog code in common.cpp with the library https://github.com/btzy/nativefiledialog-extended. The main consequence of this change is that the Linux implementation of these operations switches from zenity to xdg-desktop-portal, causing the file dialogs to also work in containers (e.g., flatpak) and non-GTK based distributions like KDE. The library also has explicit support for Wayland.
1 parent 1a3b5ce commit 654b8a5

File tree

9 files changed

+197
-275
lines changed

9 files changed

+197
-275
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@
1010
[submodule "ext/nanobind"]
1111
path = ext/nanobind
1212
url = https://github.com/wjakob/nanobind
13+
[submodule "ext/nativefiledialog-extended"]
14+
path = ext/nativefiledialog-extended
15+
url = https://github.com/btzy/nativefiledialog-extended

CMakeLists.txt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -355,11 +355,6 @@ else()
355355
set(NANOGUI_LIBRARY_TYPE "STATIC")
356356
endif()
357357

358-
if (APPLE AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")
359-
# Use automatic reference counting for Objective-C portions
360-
add_compile_options(-fobjc-arc)
361-
endif()
362-
363358
if (NANOGUI_BUILD_GLFW)
364359
list(APPEND NANOGUI_EXTRA $<TARGET_OBJECTS:glfw>)
365360
endif()
@@ -413,6 +408,11 @@ add_library(nanogui ${NANOGUI_LIBRARY_TYPE}
413408
include/nanogui/nanogui.h
414409
)
415410

411+
if (APPLE AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")
412+
# Use automatic reference counting for Objective-C portions
413+
target_compile_options(nanogui PRIVATE -fobjc-arc)
414+
endif()
415+
416416
target_compile_definitions(nanogui
417417
PUBLIC
418418
${NANOGUI_BACKEND_DEFS}
@@ -623,4 +623,9 @@ if (NANOGUI_BUILD_PYTHON AND NANOGUI_MASTER_PROJECT)
623623
)
624624
endif()
625625

626+
# Native file dialog library
627+
set(BUILD_SHARED_LIBS OFF)
628+
add_subdirectory(ext/nativefiledialog-extended)
629+
target_link_libraries(nanogui PRIVATE nfd)
630+
626631
# vim: set et ts=2 sw=2 ft=cmake nospell:

ext/nativefiledialog-extended

include/nanogui/common.h

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -258,22 +258,6 @@ inline bool active() { return run_mode() != RunMode::Stopped; }
258258
*/
259259
extern NANOGUI_EXPORT void async(const std::function<void()> &func);
260260

261-
/**
262-
* \brief Open a native file open/save dialog.
263-
*
264-
* \param filetypes
265-
* Pairs of permissible formats with descriptions like
266-
* ``("png", "Portable Network Graphics")``.
267-
*
268-
* \param save
269-
* Set to ``true`` if you would like subsequent file dialogs to open
270-
* at whatever folder they were in when they close this one.
271-
*/
272-
extern NANOGUI_EXPORT std::string
273-
file_dialog(const std::vector<std::pair<std::string, std::string>> &filetypes,
274-
bool save);
275-
276-
277261
/**
278262
* \brief Check for the availability of displays with 10-bit color and/or
279263
* extended dynamic range (EDR), i.e. the ability to reproduce intensities
@@ -289,24 +273,51 @@ file_dialog(const std::vector<std::pair<std::string, std::string>> &filetypes,
289273
*/
290274
extern NANOGUI_EXPORT std::pair<bool, bool> test_10bit_edr_support();
291275

276+
/// Selection of file/folder dialog types supported by file_dialog()
277+
enum class FileDialogType {
278+
/// Open a single file
279+
Open,
280+
281+
/// Open multiple files
282+
OpenMultiple,
283+
284+
/// Save a single file
285+
Save,
286+
287+
/// Pick a single folder (``filters`` not supported)
288+
PickFolder,
289+
290+
/// Pick multiple folders (``filters`` argument must be empty)
291+
PickFolderMultiple
292+
};
293+
292294
/**
293-
* \brief Open a native file open dialog, which allows multiple selection.
295+
* \brief Open a native file/folder dialog
296+
*
297+
* This function can instantiate variety of file dialogs using native UI
298+
* widgets. This functionality is bsaed on the bundled
299+
* ``nativefiledialog-extended`` [1] library.
300+
*
301+
* [1] https://github.com/btzy/nativefiledialog-extended
294302
*
295-
* \param filetypes
296-
* Pairs of permissible formats with descriptions like
297-
* ``("png", "Portable Network Graphics")``.
303+
* \param type
304+
* The type of dialog. For \ref FileDialogType::Open, \ref FileDialogType::Save,
305+
* and \ref FileDialogType::PickFolder, the output array contains at most one
306+
* entry.
298307
*
299-
* \param save
300-
* Set to ``true`` if you would like subsequent file dialogs to open
301-
* at whatever folder they were in when they close this one.
308+
* \param filter
309+
* Specify file formats with descriptions to indicate a preference for specific
310+
* file types.
302311
*
303-
* \param multiple
304-
* Set to ``true`` if you would like to be able to select multiple
305-
* files at once. May not be simultaneously true with \p save.
312+
* \param default_path
313+
* If specified, the OS dialog will show files/folders at a specified starting
314+
* location.
306315
*/
307316
extern NANOGUI_EXPORT std::vector<std::string>
308-
file_dialog(const std::vector<std::pair<std::string, std::string>> &filetypes,
309-
bool save, bool multiple);
317+
file_dialog(Widget *parent,
318+
FileDialogType type,
319+
const std::vector<std::pair<std::string, std::string>> &filters = {},
320+
const std::string &default_path = {});
310321

311322
#if defined(__APPLE__) || defined(DOXYGEN_DOCUMENTATION_BUILD)
312323
/**

0 commit comments

Comments
 (0)