add nf7::gui::Config
This commit is contained in:
59
common/gui_config.hh
Normal file
59
common/gui_config.hh
Normal file
@@ -0,0 +1,59 @@
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
#include <imgui.h>
|
||||
#include <imgui_stdlib.h>
|
||||
|
||||
#include "nf7.hh"
|
||||
|
||||
#include "common/generic_memento.hh"
|
||||
|
||||
|
||||
namespace nf7::gui {
|
||||
|
||||
template <typename T>
|
||||
concept ConfigData = requires (T& x) {
|
||||
{ x.Stringify() } -> std::convertible_to<std::string>;
|
||||
x.Parse(std::string {});
|
||||
};
|
||||
|
||||
template <ConfigData T>
|
||||
void Config(nf7::GenericMemento<T>& mem) noexcept {
|
||||
static std::string text_;
|
||||
static std::string msg_;
|
||||
static bool mod_;
|
||||
|
||||
if (ImGui::IsWindowAppearing()) {
|
||||
text_ = mem->Stringify();
|
||||
msg_ = "";
|
||||
mod_ = false;
|
||||
}
|
||||
|
||||
mod_ |= ImGui::InputTextMultiline("##config", &text_);
|
||||
|
||||
ImGui::BeginDisabled(!mod_);
|
||||
if (ImGui::Button("apply")) {
|
||||
try {
|
||||
mem->Parse(text_);
|
||||
mem.Commit();
|
||||
msg_ = "";
|
||||
mod_ = false;
|
||||
} catch (nf7::Exception& e) {
|
||||
msg_ = e.msg();
|
||||
}
|
||||
}
|
||||
ImGui::EndDisabled();
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("restore")) {
|
||||
text_ = mem->Stringify();
|
||||
msg_ = "";
|
||||
mod_ = false;
|
||||
}
|
||||
|
||||
if (msg_.size()) {
|
||||
ImGui::Bullet();
|
||||
ImGui::TextUnformatted(msg_.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace nf7::gui
|
||||
@@ -45,31 +45,4 @@ void IOSocketListPopup::Update() noexcept {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ConfigPopup::Update() noexcept {
|
||||
if (nf7::gui::Popup::Begin()) {
|
||||
ImGui::TextUnformatted(name());
|
||||
ImGui::InputTextMultiline("##text", &text_);
|
||||
|
||||
if (ImGui::Button("apply")) {
|
||||
try {
|
||||
onApply(text_);
|
||||
msg_ = "OK";
|
||||
} catch (nf7::Exception& e) {
|
||||
msg_ = e.msg();
|
||||
}
|
||||
}
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("reset")) {
|
||||
text_ = onOpen();
|
||||
}
|
||||
|
||||
if (msg_.size()) {
|
||||
ImGui::Bullet();
|
||||
ImGui::TextUnformatted(msg_.c_str());
|
||||
}
|
||||
ImGui::EndPopup();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace nf7::gui
|
||||
|
||||
@@ -64,26 +64,4 @@ class IOSocketListPopup final :
|
||||
std::string is_, os_;
|
||||
};
|
||||
|
||||
|
||||
class ConfigPopup final :
|
||||
public nf7::FileBase::Feature, private Popup {
|
||||
public:
|
||||
ConfigPopup(const char* name = "ConfigPopup") noexcept : Popup(name) {
|
||||
}
|
||||
|
||||
void Open() noexcept {
|
||||
msg_ = "";
|
||||
text_ = onOpen();
|
||||
nf7::gui::Popup::Open();
|
||||
}
|
||||
void Update() noexcept override;
|
||||
|
||||
std::function<std::string()> onOpen;
|
||||
std::function<void(const std::string&)> onApply;
|
||||
|
||||
private:
|
||||
std::string msg_;
|
||||
std::string text_;
|
||||
};
|
||||
|
||||
} // namespace nf7::gui
|
||||
|
||||
Reference in New Issue
Block a user