file selection callback

This commit is contained in:
tildearrow 2025-10-03 19:18:10 -05:00
parent b716194602
commit 6fe211f5c8
3 changed files with 25 additions and 4 deletions

View file

@ -191,9 +191,8 @@ bool FurnaceGUIFileDialog::openLoad(String header, std::vector<String> filter, S
}
#endif
// TODO: clickCallback
newFilePicker->setHomeDir(getHomeDir());
newFilePicker->open(header+"###FileDialog",path,hint,FP_FLAGS_MODAL|(allowMultiple?FP_FLAGS_MULTI_SELECT:0),filter);
newFilePicker->open(header+"###FileDialog",path,hint,FP_FLAGS_MODAL|(allowMultiple?FP_FLAGS_MULTI_SELECT:0),filter,clickCallback);
}
opened=true;
return true;

View file

@ -1029,6 +1029,21 @@ void FurnaceFilePicker::drawFileList(ImVec2& tableSize, bool& acknowledged) {
acknowledged=true;
}
}
// trigger callback if set
if (selCallback!=NULL) {
String callbackPath;
if (path.empty()) {
callbackPath=i->name;
} else {
if (*path.rbegin()==DIR_SEPARATOR) {
callbackPath=path+i->name;
} else {
callbackPath=path+DIR_SEPARATOR+i->name;
}
}
selCallback(callbackPath.c_str());
}
}
}
ImGui::PopID();
@ -1720,7 +1735,7 @@ bool FurnaceFilePicker::isOpened() {
return isOpen;
}
bool FurnaceFilePicker::open(String name, String pa, String hint, int flags, const std::vector<String>& filter) {
bool FurnaceFilePicker::open(String name, String pa, String hint, int flags, const std::vector<String>& filter, FilePickerSelectCallback selectCallback) {
if (isOpen) return false;
if (filter.size()&1) {
logE("invalid filter data! it should be an even-sized vector with even elements containing names and odd ones being the filters.");
@ -1742,6 +1757,8 @@ bool FurnaceFilePicker::open(String name, String pa, String hint, int flags, con
}
curFilterType=0;
selectCallback=selCallback;
if (!isSearch || windowName!=name) {
if (isSearch) this->filter="";
isSearch=false;
@ -1846,6 +1863,7 @@ FurnaceFilePicker::FurnaceFilePicker():
curFilterType(0),
sortMode(FP_SORT_NAME),
curStatus(FP_STATUS_WAITING),
selCallback(NULL),
editingPath(false),
showBookmarks(true),
showHiddenFiles(true),

View file

@ -23,6 +23,7 @@
#include "../ta-utils.h"
#include "../engine/config.h"
#include <stdint.h>
#include <functional>
#include <thread>
#include "imgui.h"
@ -61,6 +62,8 @@ enum FilePickerFlags {
FP_FLAGS_EMBEDDABLE=32
};
typedef std::function<void(const char*)> FilePickerSelectCallback;
class FurnaceFilePicker {
enum SortModes {
FP_SORT_NAME=0,
@ -115,6 +118,7 @@ class FurnaceFilePicker {
size_t curFilterType;
SortModes sortMode;
FilePickerStatus curStatus;
FilePickerSelectCallback selCallback;
std::vector<FileTypeStyle> fileTypeRegistry;
FileTypeStyle defaultTypeStyle[FP_TYPE_MAX];
@ -166,7 +170,7 @@ class FurnaceFilePicker {
bool draw(ImGuiWindowFlags winFlags=0);
bool isOpened();
void close();
bool open(String name, String path, String hint, int flags, const std::vector<String>& filter);
bool open(String name, String path, String hint, int flags, const std::vector<String>& filter, FilePickerSelectCallback selectCallback=NULL);
void loadSettings(DivConfig& conf);
void saveSettings(DivConfig& conf);
void setTypeStyle(FileType type, ImVec4 color, String icon);