satdump/src-core/imgui/dialogs/widget.cpp

145 lines
3.9 KiB
C++
Raw Permalink Normal View History

#include "widget.h"
#include "core/style.h"
#include "imgui/imgui.h"
#include "imgui/imgui_stdlib.h"
2025-05-03 12:12:36 +02:00
#include <filesystem>
#ifdef _MSC_VER
#include <direct.h>
#endif
FileSelectWidget::FileSelectWidget(std::string label, std::string selection_text, bool directory, bool allow_url)
: label(label), selection_text(selection_text), directory(directory), allow_url(allow_url)
{
2025-05-03 12:12:36 +02:00
file_select = nullptr;
dir_select = nullptr; // dirselect = nullptr;
waiting_for_res = false;
file_valid = false;
url_valid = false;
default_dir = ".";
id = "##filepathselection" + label;
btnid = u8"\ufc6e Open##filepathselectionbutton" + label;
}
FileSelectWidget::~FileSelectWidget()
{
2025-05-03 12:12:36 +02:00
if (file_select != nullptr)
delete file_select;
if (dir_select != nullptr)
delete dir_select; // delete dirselect;
}
bool FileSelectWidget::draw(std::string hint)
{
bool changed = false;
bool disabled = waiting_for_res;
#ifdef _MSC_VER
if (default_dir == ".")
{
2025-05-03 12:12:36 +02:00
char *cwd;
cwd = _getcwd(NULL, 0);
if (cwd != 0)
default_dir = cwd;
}
#endif
if (disabled)
style::beginDisabled();
if (!file_valid && !url_valid)
2024-01-21 14:57:41 -05:00
ImGui::PushStyleColor(ImGuiCol_Text, style::theme.red.Value);
if (allow_url)
{
2025-05-03 12:12:36 +02:00
ImGuiStyle &style = ImGui::GetStyle();
float textbox_width =
ImGui::GetContentRegionAvail().x - (ImGui::CalcTextSize(btnid.c_str(), nullptr, true).x + ImGui::CalcTextSize("Load").x + style.ItemSpacing.x * 2 + style.FramePadding.x * 4);
if (textbox_width < 20 * ui_scale)
textbox_width = 20 * ui_scale;
ImGui::SetNextItemWidth(textbox_width);
}
ImGui::InputTextWithHint(id.c_str(), hint.c_str(), &path);
changed = ImGui::IsItemDeactivatedAfterEdit();
if (!file_valid && !url_valid)
ImGui::PopStyleColor();
ImGui::SameLine();
if (ImGui::Button(btnid.c_str()))
{
if (!directory)
{
2025-05-03 12:12:36 +02:00
file_select = new fileutils::FileSelTh({{"All Files", "*"}}, default_dir); // TODOREWORK Selection Text?
}
else
{
2025-05-03 12:12:36 +02:00
dir_select = new fileutils::DirSelTh(default_dir); // TODOREWORK Selection Text?
}
waiting_for_res = true;
}
if (disabled)
style::endDisabled();
if (waiting_for_res)
{
std::string get = "";
2026-01-13 21:40:02 +01:00
2025-05-03 12:12:36 +02:00
bool is_ready = (directory ? dir_select->is_ready() : file_select->is_ready());
if (is_ready)
{
if (!directory)
{
2025-05-03 12:12:36 +02:00
get = file_select->result();
delete file_select;
file_select = nullptr;
}
2025-05-03 12:12:36 +02:00
else
{
2025-05-03 12:12:36 +02:00
get = dir_select->result();
delete dir_select;
dir_select = nullptr;
}
if (get == "")
waiting_for_res = false;
else
{
path = get;
changed |= true;
waiting_for_res = false;
}
}
}
bool is_dir = std::filesystem::is_directory(path);
file_valid = std::filesystem::exists(path) && (directory ? is_dir : !is_dir);
if (allow_url)
{
ImGui::SameLine();
url_valid = path.find("http") == 0;
if (disabled || (!file_valid && !url_valid))
style::beginDisabled();
changed |= ImGui::Button(std::string("Load##" + label).c_str());
if (disabled || (!file_valid && !url_valid))
style::endDisabled();
}
return (file_valid || url_valid) && changed;
}
2025-05-03 12:12:36 +02:00
std::string FileSelectWidget::getPath() { return path; }
2025-05-03 12:12:36 +02:00
void FileSelectWidget::setPath(std::string new_path) { path = new_path; }
void FileSelectWidget::setDefaultDir(std::string new_path)
{
default_dir = new_path;
2023-09-24 10:10:21 -04:00
#ifndef _MSC_VER
2025-05-03 12:12:36 +02:00
if (default_dir.back() != '/')
2023-09-24 10:10:21 -04:00
default_dir += "/";
#endif
}
2025-05-03 12:12:36 +02:00
bool FileSelectWidget::isValid() { return file_valid; }