fix file open dialog default path not having a way to specify filename

This commit is contained in:
HJfod 2023-02-26 20:37:13 +02:00
parent deadb58b18
commit 5c9ee08922
2 changed files with 48 additions and 3 deletions

View file

@ -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<std::string> 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<ghc::filesystem::path> defaultPath;
/**
* File extension filters to show on the file picker
*/
std::vector<Filter> 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<ghc::filesystem::path> 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<std::vector<ghc::filesystem::path>> pickFiles(FilePickOptions const& options);
}

View file

@ -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<class T>
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) {