12#include <ImGuiFileDialog.h>
18 for (
const auto& [key, value] : lastPath) {
19 buf.appendf(
"%s=%s\n", key.c_str(), value.c_str());
21 auto bookmarks = ImGuiFileDialog::Instance()->SerializeBookmarks();
22 buf.appendf(
"bookmarks=%s\n", bookmarks.c_str());
27 if (name ==
"bookmarks") {
28 ImGuiFileDialog::Instance()->DeserializeBookmarks(std::string(value));
30 lastPath[std::string(name)] = value;
34[[nodiscard]]
static bool dirOnlyHasReadme(
zstring_view directory)
37 while (
const auto* d = dir.getEntry()) {
38 auto name = std::string_view(d->d_name);
39 if (name ==
one_of(std::string_view(
".."), std::string_view(
".")))
continue;
40 if (name !=
"README")
return false;
45[[nodiscard]]
static std::string makeUniqueName(std::string_view proposedName, std::vector<std::string>& existingNames)
47 std::string name{proposedName};
49 while (
contains(existingNames, name)) {
50 name =
strCat(proposedName,
" (", ++count,
')');
52 existingNames.push_back(name);
56void ImGuiOpenFile::setBookmarks()
58 auto& dialog = *ImGuiFileDialog::Instance();
61 std::vector<std::string> existingNames;
62 for (
const auto& dir : filePool.getDirectories()) {
68 if (dirOnlyHasReadme(path))
continue;
70 dialog.RemoveBookmark(name);
71 dialog.AddBookmark(name, path);
75std::string ImGuiOpenFile::getStartPath(
zstring_view lastLocationHint)
77 if (!lastLocationHint.
empty()) {
80 return std::string(lastLocationHint);
86 auto [it, inserted] = lastPath.try_emplace(lastTitle,
".");
91 const std::function<
void(
const std::string&)>& callback,
95 if (filters.find(
"{.*}") == std::string::npos) {
96 filters +=
",All files (*){.*}";
98 int extraFlags = ImGuiFileDialogFlags_DisableCreateDirectoryButton;
99 common(title, filters.c_str(), callback, lastLocationHint, painter_, extraFlags);
103 const std::function<
void(
const std::string&)>& callback,
107 if (filters.find(
"{.*}") == std::string::npos) {
108 filters +=
",All files (*){.*}";
111 ImGuiFileDialogFlags_DisableCreateDirectoryButton |
112 ImGuiFileDialogFlags_ConfirmOverwrite;
113 common(title, filters.c_str(), callback, lastLocationHint, painter_, extraFlags);
117 const std::function<
void(
const std::string&)>& callback,
122 common(title,
nullptr, callback, lastLocationHint, painter_, extraFlags);
125void ImGuiOpenFile::common(
const std::string& title,
const char* filters,
126 const std::function<
void(
const std::string&)>& callback,
131 chooseDirectory = filters ==
nullptr;
132 activePainter = painter_;
137 auto startPath = getStartPath(lastLocationHint);
138 IGFD::FileDialogConfig config;
139 config.path = startPath;
140 config.flags = extraFlags |
141 ImGuiFileDialogFlags_DontShowHiddenFiles |
142 ImGuiFileDialogFlags_CaseInsensitiveExtention |
143 ImGuiFileDialogFlags_Modal;
144 ImGuiFileDialog::Instance()->OpenDialog(
145 "FileDialog", title, filters, config);
146 openFileCallback = callback;
152 auto* fileDialog = ImGuiFileDialog::Instance();
153 if (fileDialog->Display(
"FileDialog", ImGuiWindowFlags_NoCollapse, ImVec2(560.0f, 360.0f))) {
154 if (fileDialog->IsOk() && openFileCallback) {
155 lastPath[lastTitle] = fileDialog->GetCurrentPath();
157 chooseDirectory ? fileDialog->GetCurrentPath() : fileDialog->GetFilePathName());
158 openFileCallback(filePathName);
159 openFileCallback = {};
167 auto* fileDialog = ImGuiFileDialog::Instance();
168 return fileDialog->GetCurrentFilter();
void loadLine(std::string_view name, zstring_view value) override
void selectNewFile(const std::string &title, std::string filters, const std::function< void(const std::string &)> &callback, zstring_view lastLocationHint={}, Painter painter=Painter::MANAGER)
void selectFile(const std::string &title, std::string filters, const std::function< void(const std::string &)> &callback, zstring_view lastLocationHint={}, Painter painter=Painter::MANAGER)
static std::string getLastFilter()
void selectDirectory(const std::string &title, const std::function< void(const std::string &)> &callback, zstring_view lastLocationHint={}, Painter painter=Painter::MANAGER)
void save(ImGuiTextBuffer &buf) override
Simple wrapper around opendir() / readdir() / closedir() functions.
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
constexpr auto empty() const
ALWAYS_INLINE unsigned count(const uint8_t *pIn, const uint8_t *pMatch, const uint8_t *pInLimit)
string_view getDirName(string_view path)
Returns the directory portion of a path.
const std::string & getNativePath(const std::string &path)
Returns the path in native path-delimiter.
bool isRegularFile(const Stat &st)
bool isDirectory(const Stat &st)
string_view getFilename(string_view path)
Returns the file portion of a path name.
std::optional< Stat > getStat(zstring_view filename)
Call stat() and return the stat structure.
const std::string & getConventionalPath(const std::string &path)
Returns the path in conventional path-delimiter.
This file implemented 3 utility functions:
constexpr bool contains(ITER first, ITER last, const VAL &val)
Check if a range contains a given value, using linear search.