From 6ba3c0fc8459a0bd4177a2653c308d2634ee7c37 Mon Sep 17 00:00:00 2001 From: scawful Date: Wed, 14 Aug 2024 00:05:15 -0400 Subject: [PATCH] chore: Refactor file_dialog header to be platform agnostic --- src/app/core/platform/file_dialog.cc | 116 ++++++++++++++++++++++++- src/app/core/platform/file_dialog.h | 121 --------------------------- src/app/core/platform/file_dialog.mm | 12 ++- 3 files changed, 123 insertions(+), 126 deletions(-) diff --git a/src/app/core/platform/file_dialog.cc b/src/app/core/platform/file_dialog.cc index 08c9669b..b78dc366 100644 --- a/src/app/core/platform/file_dialog.cc +++ b/src/app/core/platform/file_dialog.cc @@ -1,10 +1,124 @@ #include "file_dialog.h" +#ifdef _WIN32 +// Include Windows-specific headers +#include +#include +#endif // _WIN32 + namespace yaze { namespace app { namespace core { -#if defined(__linux__) +#ifdef _WIN32 + +static std::string ShowOpenFileDialog() { + CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); + + IFileDialog *pfd = NULL; + HRESULT hr = + CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileDialog, + reinterpret_cast(&pfd)); + std::string file_path_windows; + if (SUCCEEDED(hr)) { + // Show the dialog + hr = pfd->Show(NULL); + if (SUCCEEDED(hr)) { + IShellItem *psiResult; + hr = pfd->GetResult(&psiResult); + if (SUCCEEDED(hr)) { + // Get the file path + PWSTR pszFilePath; + psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); + char str[128]; + wcstombs(str, pszFilePath, 128); + file_path_windows = str; + psiResult->Release(); + CoTaskMemFree(pszFilePath); + } + } + pfd->Release(); + } + + CoUninitialize(); + return file_path_windows; +} + +static std::string ShowOpenFolderDialog() { + CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); + + IFileDialog *pfd = NULL; + HRESULT hr = + CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, IID_IFileDialog, + reinterpret_cast(&pfd)); + std::string folder_path_windows; + if (SUCCEEDED(hr)) { + // Show the dialog + DWORD dwOptions; + hr = pfd->GetOptions(&dwOptions); + if (SUCCEEDED(hr)) { + hr = pfd->SetOptions(dwOptions | FOS_PICKFOLDERS); + if (SUCCEEDED(hr)) { + hr = pfd->Show(NULL); + if (SUCCEEDED(hr)) { + IShellItem *psiResult; + hr = pfd->GetResult(&psiResult); + if (SUCCEEDED(hr)) { + // Get the folder path + PWSTR pszFolderPath; + psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFolderPath); + char str[128]; + wcstombs(str, pszFolderPath, 128); + folder_path_windows = str; + psiResult->Release(); + CoTaskMemFree(pszFolderPath); + } + } + } + } + pfd->Release(); + } + + CoUninitialize(); + return folder_path_windows; +} + +static std::vector GetSubdirectoriesInFolder( + const std::string &folder_path) { + std::vector subdirectories; + WIN32_FIND_DATA findFileData; + HANDLE hFind = FindFirstFile((folder_path + "\\*").c_str(), &findFileData); + if (hFind != INVALID_HANDLE_VALUE) { + do { + if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + if (strcmp(findFileData.cFileName, ".") != 0 && + strcmp(findFileData.cFileName, "..") != 0) { + subdirectories.push_back(findFileData.cFileName); + } + } + } while (FindNextFile(hFind, &findFileData) != 0); + FindClose(hFind); + } + return subdirectories; +} + +static std::vector GetFilesInFolder( + const std::string &folder_path) { + std::vector files; + WIN32_FIND_DATA findFileData; + HANDLE hFind = FindFirstFile((folder_path + "\\*").c_str(), &findFileData); + if (hFind != INVALID_HANDLE_VALUE) { + do { + if (!(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { + files.push_back(findFileData.cFileName); + } + } while (FindNextFile(hFind, &findFileData) != 0); + FindClose(hFind); + } + return files; +} + +#elif defined(__linux__) std::string FileDialogWrapper::ShowOpenFileDialog() { return "Linux: Open file dialog"; diff --git a/src/app/core/platform/file_dialog.h b/src/app/core/platform/file_dialog.h index a4732636..edc270c5 100644 --- a/src/app/core/platform/file_dialog.h +++ b/src/app/core/platform/file_dialog.h @@ -8,125 +8,6 @@ namespace yaze { namespace app { namespace core { -#ifdef _WIN32 -// Include Windows-specific headers -#include -#include - -class FileDialogWrapper { - public: - static std::string ShowOpenFileDialog() { - CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); - - IFileDialog *pfd = NULL; - HRESULT hr = - CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, - IID_IFileDialog, reinterpret_cast(&pfd)); - std::string file_path_windows; - if (SUCCEEDED(hr)) { - // Show the dialog - hr = pfd->Show(NULL); - if (SUCCEEDED(hr)) { - IShellItem *psiResult; - hr = pfd->GetResult(&psiResult); - if (SUCCEEDED(hr)) { - // Get the file path - PWSTR pszFilePath; - psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); - char str[128]; - wcstombs(str, pszFilePath, 128); - file_path_windows = str; - psiResult->Release(); - CoTaskMemFree(pszFilePath); - } - } - pfd->Release(); - } - - CoUninitialize(); - return file_path_windows; - } - - static std::string ShowOpenFolderDialog() { - CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); - - IFileDialog *pfd = NULL; - HRESULT hr = - CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL, - IID_IFileDialog, reinterpret_cast(&pfd)); - std::string folder_path_windows; - if (SUCCEEDED(hr)) { - // Show the dialog - DWORD dwOptions; - hr = pfd->GetOptions(&dwOptions); - if (SUCCEEDED(hr)) { - hr = pfd->SetOptions(dwOptions | FOS_PICKFOLDERS); - if (SUCCEEDED(hr)) { - hr = pfd->Show(NULL); - if (SUCCEEDED(hr)) { - IShellItem *psiResult; - hr = pfd->GetResult(&psiResult); - if (SUCCEEDED(hr)) { - // Get the folder path - PWSTR pszFolderPath; - psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFolderPath); - char str[128]; - wcstombs(str, pszFolderPath, 128); - folder_path_windows = str; - psiResult->Release(); - CoTaskMemFree(pszFolderPath); - } - } - } - } - pfd->Release(); - } - - CoUninitialize(); - return folder_path_windows; - } - - static std::vector GetSubdirectoriesInFolder( - const std::string &folder_path) { - std::vector subdirectories; - WIN32_FIND_DATA findFileData; - HANDLE hFind = FindFirstFile((folder_path + "\\*").c_str(), &findFileData); - if (hFind != INVALID_HANDLE_VALUE) { - do { - if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { - if (strcmp(findFileData.cFileName, ".") != 0 && - strcmp(findFileData.cFileName, "..") != 0) { - subdirectories.push_back(findFileData.cFileName); - } - } - } while (FindNextFile(hFind, &findFileData) != 0); - FindClose(hFind); - } - return subdirectories; - } - - static std::vector GetFilesInFolder( - const std::string &folder_path) { - std::vector files; - WIN32_FIND_DATA findFileData; - HANDLE hFind = FindFirstFile((folder_path + "\\*").c_str(), &findFileData); - if (hFind != INVALID_HANDLE_VALUE) { - do { - if (!(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { - files.push_back(findFileData.cFileName); - } - } while (FindNextFile(hFind, &findFileData) != 0); - FindClose(hFind); - } - return files; - } -}; - -#else - -#include -#include - class FileDialogWrapper { public: static std::string ShowOpenFileDialog(); @@ -137,8 +18,6 @@ class FileDialogWrapper { const std::string& folder_path); }; -#endif - } // namespace core } // namespace app } // namespace yaze diff --git a/src/app/core/platform/file_dialog.mm b/src/app/core/platform/file_dialog.mm index 31f677b3..8b11a3d6 100644 --- a/src/app/core/platform/file_dialog.mm +++ b/src/app/core/platform/file_dialog.mm @@ -39,15 +39,19 @@ std::string ShowOpenFileDialogSync() { } } -std::string FileDialogWrapper::ShowOpenFileDialog() { return ShowOpenFileDialogSync(); } +std::string yaze::app::core::FileDialogWrapper::ShowOpenFileDialog() { + return ShowOpenFileDialogSync(); +} -std::string FileDialogWrapper::ShowOpenFolderDialog() { return ""; } +std::string yaze::app::core::FileDialogWrapper::ShowOpenFolderDialog() { return ""; } -std::vector FileDialogWrapper::GetFilesInFolder(const std::string &folder) { +std::vector yaze::app::core::FileDialogWrapper::GetFilesInFolder( + const std::string &folder) { return {}; } -std::vector FileDialogWrapper::GetSubdirectoriesInFolder(const std::string &folder) { +std::vector yaze::app::core::FileDialogWrapper::GetSubdirectoriesInFolder( + const std::string &folder) { return {}; }