From 5c9ee08922989219cda92c5d31ae36ccca412446 Mon Sep 17 00:00:00 2001 From: HJfod <60038575+HJfod@users.noreply.github.com> Date: Sun, 26 Feb 2023 20:37:13 +0200 Subject: [PATCH] fix file open dialog default path not having a way to specify filename --- loader/include/Geode/utils/file.hpp | 23 ++++++++++++++++++++- loader/src/platform/windows/nfdwin.cpp | 28 ++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/loader/include/Geode/utils/file.hpp b/loader/include/Geode/utils/file.hpp index 52a3afdc..57936ab1 100644 --- a/loader/include/Geode/utils/file.hpp +++ b/loader/include/Geode/utils/file.hpp @@ -187,6 +187,10 @@ namespace geode::utils::file { ); }; + /** + * Open a folder / file in the system's file explorer + * @param path Folder / file to open + */ GEODE_DLL bool openFolder(ghc::filesystem::path const& path); enum class PickMode { @@ -203,10 +207,27 @@ namespace geode::utils::file { std::unordered_set files; }; - ghc::filesystem::path defaultPath; + /** + * On PickMode::SaveFile and PickMode::OpenFile, last item is assumed + * to be a filename, unless it points to an extant directory. + * On PickMode::OpenFolder, path is treated as leading up to a directory + */ + std::optional defaultPath; + /** + * File extension filters to show on the file picker + */ std::vector filters; }; + /** + * Prompt the user to pick a file using the system's file system picker + * @param mode Type of file selection prompt to show + * @param options Picker options + */ GEODE_DLL Result pickFile(PickMode mode, FilePickOptions const& options); + /** + * Prompt the user to pick a bunch of files for opening using the system's file system picker + * @param options Picker options + */ GEODE_DLL Result> pickFiles(FilePickOptions const& options); } diff --git a/loader/src/platform/windows/nfdwin.cpp b/loader/src/platform/windows/nfdwin.cpp index fdf0819c..ad660a4b 100644 --- a/loader/src/platform/windows/nfdwin.cpp +++ b/loader/src/platform/windows/nfdwin.cpp @@ -125,6 +125,14 @@ static bool setDefaultPath( return true; } +static bool setDefaultFile( + IFileDialog* dialog, + ghc::filesystem::path const& fileName +) { + dialog->SetFileName(fileName.wstring().c_str()); + return true; +} + template struct Holder { T m_deallocator; @@ -173,8 +181,24 @@ Result<> nfdPick( if (!addFiltersToDialog(dialog, options.filters)) { return Err("Unable to add filters to dialog"); } - if (!setDefaultPath(dialog, options.defaultPath)) { - return Err("Unable to set default path to dialog"); + if (options.defaultPath && options.defaultPath.value().wstring().size()) { + ghc::filesystem::path path = options.defaultPath.value(); + if (mode == NFDMode::OpenFile || mode == NFDMode::SaveFile) { + if (!ghc::filesystem::exists(path) || !ghc::filesystem::is_directory(path)) { + if (path.has_filename()) { + setDefaultFile(dialog, path.filename()); + } + if (path.has_parent_path()) { + path = path.parent_path(); + } + else { + path = ""; + } + } + } + if (path.wstring().size() && !setDefaultPath(dialog, path)) { + return Err("Unable to set default path to dialog"); + } } if (mode == NFDMode::OpenFiles) {