From 2d15833b0d84e492abbc148261042c04fb8939c6 Mon Sep 17 00:00:00 2001 From: scawful Date: Mon, 6 Jan 2025 13:25:49 -0500 Subject: [PATCH] Implement folder dialog, subdirs, files with nfd --- src/app/core/platform/file_dialog.cc | 73 +++++++++++++++++++++------- 1 file changed, 55 insertions(+), 18 deletions(-) diff --git a/src/app/core/platform/file_dialog.cc b/src/app/core/platform/file_dialog.cc index 41baaa84..9565340f 100644 --- a/src/app/core/platform/file_dialog.cc +++ b/src/app/core/platform/file_dialog.cc @@ -4,7 +4,7 @@ // Include Windows-specific headers #include #include -#else // Linux and MacOS +#else // Linux and MacOS #include #include #endif @@ -80,15 +80,15 @@ void SaveFile(const std::string &filename, const std::string &contents, std::string GetConfigDirectory(Platform platform) { std::string config_directory = ".yaze"; switch (platform) { - case Platform::kWindows: - config_directory = "~/AppData/Roaming/yaze"; - break; - case Platform::kMacOS: - case Platform::kLinux: - config_directory = "~/.config/yaze"; - break; - default: - break; + case Platform::kWindows: + config_directory = "~/AppData/Roaming/yaze"; + break; + case Platform::kMacOS: + case Platform::kLinux: + config_directory = "~/.config/yaze"; + break; + default: + break; } return config_directory; } @@ -208,8 +208,8 @@ std::vector FileDialogWrapper::GetFilesInFolder( std::string FileDialogWrapper::ShowOpenFileDialog() { NFD_Init(); nfdu8char_t *out_path = NULL; - nfdu8filter_item_t filters[1] = {{ "Rom File", "sfc,smc" } }; - nfdopendialogu8args_t args = { 0 }; + nfdu8filter_item_t filters[1] = {{"Rom File", "sfc,smc"}}; + nfdopendialogu8args_t args = {0}; args.filterList = filters; args.filterCount = 1; nfdresult_t result = NFD_OpenDialogU8_With(&out_path, &args); @@ -221,23 +221,60 @@ std::string FileDialogWrapper::ShowOpenFileDialog() { } else if (result == NFD_CANCEL) { NFD_Quit(); return ""; - } + } NFD_Quit(); return "Error: NFD_OpenDialog"; } 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 FileDialogWrapper::GetSubdirectoriesInFolder( - const std::string& folder_path) { - return {"Linux: Subdirectories in folder"}; + const std::string &folder_path) { + std::vector 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 FileDialogWrapper::GetFilesInFolder( - const std::string& folder_path) { - return {"Linux: Files in folder"}; + const std::string &folder_path) { + std::vector 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