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
2// nativefiledialog-extended
3#include <nfd.h>
4
5#include <filesystem>
6#include <string>
7#include <vector>
8
9#include "util/file_util.h"
10
11namespace yaze {
12namespace util {
13
15 const FileDialogOptions& options) {
16 nfdchar_t* outPath = nullptr;
17 const nfdfilteritem_t* filter_list = nullptr;
18 size_t filter_count = 0;
19 std::vector<nfdfilteritem_t> filter_items;
20 std::vector<std::string> filter_names;
21 std::vector<std::string> filter_specs;
22
23 if (!options.filters.empty()) {
24 filter_items.reserve(options.filters.size());
25 filter_names.reserve(options.filters.size());
26 filter_specs.reserve(options.filters.size());
27
28 for (const auto& filter : options.filters) {
29 std::string label = filter.label.empty() ? "Files" : filter.label;
30 std::string spec = filter.spec.empty() ? "*" : filter.spec;
31 filter_names.push_back(label);
32 filter_specs.push_back(spec);
33 filter_items.push_back(
34 {filter_names.back().c_str(), filter_specs.back().c_str()});
35 }
36
37 filter_list = filter_items.data();
38 filter_count = filter_items.size();
39 }
40
41 nfdresult_t result =
42 NFD_OpenDialog(&outPath, filter_list, filter_count, nullptr);
43
44 if (result == NFD_OKAY) {
45 std::string path(outPath);
46 NFD_FreePath(outPath);
47 return path;
48 }
49
50 return "";
51}
52
56
58 const FileDialogOptions& options,
59 std::function<void(const std::string&)> callback) {
60 if (!callback) {
61 return;
62 }
63 callback(ShowOpenFileDialog(options));
64}
65
67 nfdchar_t* outPath = nullptr;
68 nfdresult_t result = NFD_PickFolder(&outPath, nullptr);
69
70 if (result == NFD_OKAY) {
71 std::string path(outPath);
72 NFD_FreePath(outPath);
73 return path;
74 }
75
76 return "";
77}
78
80 const std::string& default_name, const std::string& default_extension) {
81 nfdchar_t* outPath = nullptr;
82 nfdfilteritem_t filterItem[1] = {
83 {default_extension.empty() ? "All Files" : default_extension.c_str(),
84 default_extension.empty() ? "*" : default_extension.c_str()}};
85
86 nfdresult_t result = NFD_SaveDialog(
87 &outPath, default_extension.empty() ? nullptr : filterItem,
88 default_extension.empty() ? 0 : 1, nullptr, default_name.c_str());
89
90 if (result == NFD_OKAY) {
91 std::string path(outPath);
92 NFD_FreePath(outPath);
93 return path;
94 }
95
96 return "";
97}
98
100 const std::string& folder_path) {
101 std::vector<std::string> subdirs;
102
103 try {
104 for (const auto& entry : std::filesystem::directory_iterator(folder_path)) {
105 if (entry.is_directory()) {
106 subdirs.push_back(entry.path().string());
107 }
108 }
109 } catch (...) {
110 // Return empty vector on error
111 }
112
113 return subdirs;
114}
115
116std::vector<std::string> FileDialogWrapper::GetFilesInFolder(
117 const std::string& folder_path) {
118 std::vector<std::string> files;
119
120 try {
121 for (const auto& entry : std::filesystem::directory_iterator(folder_path)) {
122 if (entry.is_regular_file()) {
123 files.push_back(entry.path().string());
124 }
125 }
126 } catch (...) {
127 // Return empty vector on error
128 }
129
130 return files;
131}
132
133// Delegate to main implementations
137
141
143 const std::string& default_name, const std::string& default_extension) {
144 return ShowSaveFileDialog(default_name, default_extension);
145}
146
148 const std::string& default_name, const std::string& default_extension) {
149 return ShowSaveFileDialog(default_name, default_extension);
150}
151
155
159
160} // namespace util
161} // namespace yaze
static void ShowOpenFileDialogAsync(const FileDialogOptions &options, std::function< void(const std::string &)> callback)
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()
std::vector< FileDialogFilter > filters
Definition file_util.h:17