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

@@ -4,7 +4,7 @@
// Include Windows-specific headers // Include Windows-specific headers
#include <shobjidl.h> #include <shobjidl.h>
#include <windows.h> #include <windows.h>
#else // Linux and MacOS #else // Linux and MacOS
#include <dirent.h> #include <dirent.h>
#include <sys/stat.h> #include <sys/stat.h>
#endif #endif
@@ -80,15 +80,15 @@ void SaveFile(const std::string &filename, const std::string &contents,
std::string GetConfigDirectory(Platform platform) { std::string GetConfigDirectory(Platform platform) {
std::string config_directory = ".yaze"; std::string config_directory = ".yaze";
switch (platform) { switch (platform) {
case Platform::kWindows: case Platform::kWindows:
config_directory = "~/AppData/Roaming/yaze"; config_directory = "~/AppData/Roaming/yaze";
break; break;
case Platform::kMacOS: case Platform::kMacOS:
case Platform::kLinux: case Platform::kLinux:
config_directory = "~/.config/yaze"; config_directory = "~/.config/yaze";
break; break;
default: default:
break; break;
} }
return config_directory; return config_directory;
} }
@@ -208,8 +208,8 @@ std::vector<std::string> FileDialogWrapper::GetFilesInFolder(
std::string FileDialogWrapper::ShowOpenFileDialog() { std::string FileDialogWrapper::ShowOpenFileDialog() {
NFD_Init(); NFD_Init();
nfdu8char_t *out_path = NULL; nfdu8char_t *out_path = NULL;
nfdu8filter_item_t filters[1] = {{ "Rom File", "sfc,smc" } }; nfdu8filter_item_t filters[1] = {{"Rom File", "sfc,smc"}};
nfdopendialogu8args_t args = { 0 }; nfdopendialogu8args_t args = {0};
args.filterList = filters; args.filterList = filters;
args.filterCount = 1; args.filterCount = 1;
nfdresult_t result = NFD_OpenDialogU8_With(&out_path, &args); nfdresult_t result = NFD_OpenDialogU8_With(&out_path, &args);
@@ -221,23 +221,60 @@ std::string FileDialogWrapper::ShowOpenFileDialog() {
} else if (result == NFD_CANCEL) { } else if (result == NFD_CANCEL) {
NFD_Quit(); NFD_Quit();
return ""; return "";
} }
NFD_Quit(); NFD_Quit();
return "Error: NFD_OpenDialog"; return "Error: NFD_OpenDialog";
} }
std::string FileDialogWrapper::ShowOpenFolderDialog() { 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( std::vector<std::string> FileDialogWrapper::GetSubdirectoriesInFolder(
const std::string& folder_path) { 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( std::vector<std::string> FileDialogWrapper::GetFilesInFolder(
const std::string& folder_path) { 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 #endif