Implement folder dialog, subdirs, files with nfd

This commit is contained in:
scawful
2025-01-06 13:25:49 -05:00
parent 65be2d0bd2
commit 2d15833b0d

View File

@@ -227,17 +227,54 @@ std::string FileDialogWrapper::ShowOpenFileDialog() {
}
std::string FileDialogWrapper::ShowOpenFolderDialog() {
return "Linux: Open folder dialog";
NFD_Init();
nfdu8char_t *out_path = NULL;
nfdresult_t result = NFD_PickFolderU8(&out_path);
if (result == NFD_OKAY) {
std::string folder_path_linux(out_path);
NFD_Free(out_path);
NFD_Quit();
return folder_path_linux;
} else if (result == NFD_CANCEL) {
NFD_Quit();
return "";
}
NFD_Quit();
return "Error: NFD_PickFolder";
}
std::vector<std::string> FileDialogWrapper::GetSubdirectoriesInFolder(
const std::string &folder_path) {
return {"Linux: Subdirectories in folder"};
std::vector<std::string> subdirectories;
DIR *dir;
struct dirent *ent;
if ((dir = opendir(folder_path.c_str())) != NULL) {
while ((ent = readdir(dir)) != NULL) {
if (ent->d_type == DT_DIR) {
if (strcmp(ent->d_name, ".") != 0 && strcmp(ent->d_name, "..") != 0) {
subdirectories.push_back(ent->d_name);
}
}
}
closedir(dir);
}
return subdirectories;
}
std::vector<std::string> FileDialogWrapper::GetFilesInFolder(
const std::string &folder_path) {
return {"Linux: Files in folder"};
std::vector<std::string> files;
DIR *dir;
struct dirent *ent;
if ((dir = opendir(folder_path.c_str())) != NULL) {
while ((ent = readdir(dir)) != NULL) {
if (ent->d_type == DT_REG) {
files.push_back(ent->d_name);
}
}
closedir(dir);
}
return files;
}
#endif