yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
file_dialog_nfd.cc
Go to the documentation of this file.
1// Windows and Linux implementation of FileDialogWrapper using nativefiledialog-extended
2#include "util/file_util.h"
3
4#include <nfd.h>
5#include <filesystem>
6#include <vector>
7#include <string>
8
9namespace yaze {
10namespace util {
11
13 nfdchar_t* outPath = nullptr;
14 nfdfilteritem_t filterItem[2] = {{"ROM Files", "sfc,smc"}, {"All Files", "*"}};
15 nfdresult_t result = NFD_OpenDialog(&outPath, filterItem, 2, nullptr);
16
17 if (result == NFD_OKAY) {
18 std::string path(outPath);
19 NFD_FreePath(outPath);
20 return path;
21 }
22
23 return "";
24}
25
27 nfdchar_t* outPath = nullptr;
28 nfdresult_t result = NFD_PickFolder(&outPath, nullptr);
29
30 if (result == NFD_OKAY) {
31 std::string path(outPath);
32 NFD_FreePath(outPath);
33 return path;
34 }
35
36 return "";
37}
38
39std::string FileDialogWrapper::ShowSaveFileDialog(const std::string& default_name,
40 const std::string& default_extension) {
41 nfdchar_t* outPath = nullptr;
42 nfdfilteritem_t filterItem[1] = {{default_extension.empty() ? "All Files" : default_extension.c_str(),
43 default_extension.empty() ? "*" : default_extension.c_str()}};
44
45 nfdresult_t result = NFD_SaveDialog(&outPath,
46 default_extension.empty() ? nullptr : filterItem,
47 default_extension.empty() ? 0 : 1,
48 nullptr,
49 default_name.c_str());
50
51 if (result == NFD_OKAY) {
52 std::string path(outPath);
53 NFD_FreePath(outPath);
54 return path;
55 }
56
57 return "";
58}
59
61 const std::string& folder_path) {
62 std::vector<std::string> subdirs;
63
64 try {
65 for (const auto& entry : std::filesystem::directory_iterator(folder_path)) {
66 if (entry.is_directory()) {
67 subdirs.push_back(entry.path().string());
68 }
69 }
70 } catch (...) {
71 // Return empty vector on error
72 }
73
74 return subdirs;
75}
76
77std::vector<std::string> FileDialogWrapper::GetFilesInFolder(
78 const std::string& folder_path) {
79 std::vector<std::string> files;
80
81 try {
82 for (const auto& entry : std::filesystem::directory_iterator(folder_path)) {
83 if (entry.is_regular_file()) {
84 files.push_back(entry.path().string());
85 }
86 }
87 } catch (...) {
88 // Return empty vector on error
89 }
90
91 return files;
92}
93
94// Delegate to main implementations
98
102
103std::string FileDialogWrapper::ShowSaveFileDialogNFD(const std::string& default_name,
104 const std::string& default_extension) {
105 return ShowSaveFileDialog(default_name, default_extension);
106}
107
108std::string FileDialogWrapper::ShowSaveFileDialogBespoke(const std::string& default_name,
109 const std::string& default_extension) {
110 return ShowSaveFileDialog(default_name, default_extension);
111}
112
116
120
121} // namespace util
122} // namespace yaze
123
static std::string ShowSaveFileDialogBespoke(const std::string &default_name="", const std::string &default_extension="")
static std::string ShowOpenFileDialogBespoke()
static std::string ShowSaveFileDialogNFD(const std::string &default_name="", const std::string &default_extension="")
static std::string ShowOpenFolderDialogNFD()
static std::string ShowSaveFileDialog(const std::string &default_name="", const std::string &default_extension="")
ShowSaveFileDialog opens a save file dialog and returns the selected filepath. Uses global feature fl...
static std::string ShowOpenFileDialog()
ShowOpenFileDialog opens a file dialog and returns the selected filepath. Uses global feature flag to...
static std::string ShowOpenFolderDialog()
ShowOpenFolderDialog opens a file dialog and returns the selected folder path. Uses global feature fl...
static std::vector< std::string > GetFilesInFolder(const std::string &folder_path)
static std::vector< std::string > GetSubdirectoriesInFolder(const std::string &folder_path)
static std::string ShowOpenFolderDialogBespoke()
static std::string ShowOpenFileDialogNFD()
Main namespace for the application.