add nf7::gui::Popup

This commit is contained in:
falsycat 2022-08-08 10:45:03 +09:00
parent cfa5a1dd27
commit e0c79edc55
5 changed files with 48 additions and 11 deletions

View File

@ -61,6 +61,7 @@ target_sources(nf7
common/gui_dnd.hh
common/gui_file.hh
common/gui_node.hh
common/gui_popup.hh
common/gui_resizer.hh
common/gui_window.hh
common/history.hh

View File

@ -5,6 +5,9 @@
#include <string_view>
#include <vector>
#include <imgui.h>
#include <imgui_stdlib.h>
#include "nf7.hh"
#include "common/gui_dnd.hh"
@ -12,16 +15,16 @@
namespace nf7::gui {
enum FileCreatePopupFlag : uint8_t {
enum FileFactoryFlag : uint8_t {
kNameInput = 1 << 0,
kNameDupCheck = 1 << 1,
};
template <uint8_t kFlags>
struct FileCreatePopup final {
struct FileFactory final {
public:
FileCreatePopup(std::vector<std::string>&& type_flags_and,
std::vector<std::string>&& type_flags_or = {},
std::string_view default_name = "new_file") noexcept :
FileFactory(std::vector<std::string>&& type_flags_and,
std::vector<std::string>&& type_flags_or = {},
std::string_view default_name = "new_file") noexcept :
type_flags_and_(std::move(type_flags_and)),
type_flags_or_(std::move(type_flags_or)),
default_name_(default_name) {
@ -36,7 +39,7 @@ struct FileCreatePopup final {
type_filter_ = "";
}
if constexpr (kFlags & FileCreatePopupFlag::kNameInput) {
if constexpr (kFlags & FileFactoryFlag::kNameInput) {
if (ImGui::IsWindowAppearing()) ImGui::SetKeyboardFocusHere();
ImGui::InputText("name", &name_);
ImGui::Spacing();
@ -88,14 +91,14 @@ struct FileCreatePopup final {
ImGui::Bullet(); ImGui::TextUnformatted("type is not selected");
err = true;
}
if constexpr (kFlags & FileCreatePopupFlag::kNameInput) {
if constexpr (kFlags & FileFactoryFlag::kNameInput) {
try {
nf7::File::Path::ValidateTerm(name_);
} catch (Exception& e) {
ImGui::Bullet(); ImGui::Text("invalid name: %s", e.msg().c_str());
err = true;
}
if constexpr ((kFlags & FileCreatePopupFlag::kNameDupCheck) != 0) {
if constexpr ((kFlags & FileFactoryFlag::kNameDupCheck) != 0) {
if (owner.Find(name_)) {
ImGui::Bullet(); ImGui::Text("name duplicated");
err = true;
@ -111,7 +114,7 @@ struct FileCreatePopup final {
}
if (ImGui::IsItemHovered()) {
const auto path = owner.abspath().Stringify();
if constexpr (kFlags & FileCreatePopupFlag::kNameInput) {
if constexpr (kFlags & FileFactoryFlag::kNameInput) {
ImGui::SetTooltip(
"create %s as '%s' on '%s'", type_->name().c_str(), name_.c_str(), path.c_str());
} else {

33
common/gui_popup.hh Normal file
View File

@ -0,0 +1,33 @@
#include <optional>
#include <utility>
#include <imgui.h>
namespace nf7::gui {
struct Popup {
public:
Popup(const char* name, ImGuiWindowFlags flags = 0) noexcept :
name_(name), flags_(flags) {
}
void Open(ImGuiPopupFlags flags = 0) noexcept {
open_flags_ = flags;
}
bool Begin() noexcept {
if (auto flags = std::exchange(open_flags_, std::nullopt)) {
ImGui::OpenPopup(name_, *flags);
}
return ImGui::BeginPopup(name_, flags_);
}
private:
const char* name_;
ImGuiWindowFlags flags_;
std::optional<ImGuiPopupFlags> open_flags_;
};
} // namespace nf7::gui

View File

@ -1097,7 +1097,7 @@ void Network::Update() noexcept {
// node add popup
if (ImGui::BeginPopup("AddPopup")) {
static nf7::gui::FileCreatePopup<0> p({"File_Factory",}, {"Node"});
static nf7::gui::FileFactory<0> p({"File_Factory",}, {"Node"});
ImGui::TextUnformatted("Node/Network: adding new Node...");
if (p.Update(*this)) {

View File

@ -150,7 +150,7 @@ void Dir::Update() noexcept {
// new item popup
if (ImGui::BeginPopup("NewItemPopup")) {
static nf7::gui::FileCreatePopup<
static nf7::gui::FileFactory<
nf7::gui::kNameInput | nf7::gui::kNameDupCheck> p(
{"File_Factory", "DirItem"});
ImGui::TextUnformatted("System/Dir: adding new file...");