commonize file menu and tooltip

This commit is contained in:
2022-11-06 13:10:22 +09:00
parent 0d60b2401a
commit 3a4d801f95
16 changed files with 119 additions and 123 deletions

View File

@@ -10,31 +10,102 @@
#include "nf7.hh"
#include "common/config.hh"
#include "common/dir_item.hh"
#include "common/gui_dnd.hh"
namespace nf7::gui {
bool PathButton(const char* id, nf7::File::Path& p, nf7::File&) noexcept {
void FileMenuItems(nf7::File& f) noexcept {
auto ditem = f.interface<nf7::DirItem>();
auto config = f.interface<nf7::Config>();
if (ImGui::MenuItem("request focus")) {
f.env().Handle({.id = f.id(), .type = nf7::File::Event::kReqFocus});
}
if (ImGui::MenuItem("copy path")) {
ImGui::SetClipboardText(f.abspath().Stringify().c_str());
}
if (ditem && (ditem->flags() & nf7::DirItem::kMenu)) {
ImGui::Separator();
ditem->UpdateMenu();
}
if (config) {
ImGui::Separator();
if (ImGui::BeginMenu("config")) {
static nf7::gui::ConfigEditor ed;
ed(*config);
ImGui::EndMenu();
}
}
}
void FileTooltip(nf7::File& f) noexcept {
auto ditem = f.interface<nf7::DirItem>();
ImGui::TextUnformatted(f.type().name().c_str());
ImGui::SameLine();
ImGui::TextDisabled(f.abspath().Stringify().c_str());
if (ditem && (ditem->flags() & nf7::DirItem::kTooltip)) {
ImGui::Indent();
ditem->UpdateTooltip();
ImGui::Unindent();
}
}
bool PathButton(const char* id, nf7::File::Path& p, nf7::File& base) noexcept {
bool ret = false;
const auto w = ImGui::CalcItemWidth();
const auto pstr = p.Stringify();
const auto w = ImGui::CalcItemWidth();
ImGui::PushID(id);
const auto pstr = p.Stringify();
if (ImGui::Button(pstr.c_str(), {w, 0})) {
ImGui::OpenPopup("editor");
}
if (ImGui::BeginDragDropTarget()) {
if (auto dp = nf7::gui::dnd::Accept<nf7::File::Path>(nf7::gui::dnd::kFilePath)) {
p = std::move(*dp);
ret = true;
// widget body
{
nf7::File* file = nullptr;
try {
file = &base.ResolveOrThrow(p);
} catch (nf7::Exception&) {
}
ImGui::EndDragDropTarget();
}
ImGui::SameLine();
ImGui::TextUnformatted(id);
const auto display = pstr.empty()? "(empty)": pstr;
if (ImGui::Button(display.c_str(), {w, 0})) {
ImGui::OpenPopup("editor");
}
if (ImGui::IsItemHovered()) {
ImGui::BeginTooltip();
if (file) {
FileTooltip(*file);
} else {
ImGui::TextDisabled("(file missing)");
}
ImGui::EndTooltip();
}
if (ImGui::BeginPopupContextItem()) {
if (file) {
nf7::gui::FileMenuItems(*file);
} else {
ImGui::TextDisabled("(file missing)");
}
ImGui::EndPopup();
}
if (ImGui::BeginDragDropTarget()) {
if (auto dp = nf7::gui::dnd::Accept<nf7::File::Path>(nf7::gui::dnd::kFilePath)) {
p = std::move(*dp);
ret = true;
}
ImGui::EndDragDropTarget();
}
if (id[0] != '#') {
ImGui::SameLine();
ImGui::TextUnformatted(id);
}
}
// editor popup
if (ImGui::BeginPopup("editor")) {
static std::string editing_str;
if (ImGui::IsWindowAppearing()) {
@@ -134,6 +205,8 @@ void NodeOutputSockets(std::span<const std::string> names) noexcept {
}
void ConfigEditor::operator()(nf7::Config& config) noexcept {
ImGui::PushID(this);
if (ImGui::IsWindowAppearing()) {
text_ = config.Stringify();
msg_ = "";
@@ -166,6 +239,8 @@ void ConfigEditor::operator()(nf7::Config& config) noexcept {
ImGui::Bullet();
ImGui::TextUnformatted(msg_.c_str());
}
ImGui::PopID();
}
} // namespace nf7::gui

View File

@@ -13,6 +13,9 @@
namespace nf7::gui {
// widgets
void FileMenuItems(nf7::File& f) noexcept;
void FileTooltip(nf7::File& f) noexcept;
bool PathButton(const char* id, nf7::File::Path&, nf7::File&) noexcept;
void ContextStack(const nf7::Context&) noexcept;

View File

@@ -22,10 +22,9 @@ class Node : public File::Interface {
class Lambda;
enum Flag : uint8_t {
kNone = 0,
kCustomNode = 1 << 0,
kMenu = 1 << 1,
kMenu_DirItem = 1 << 2, // use DirItem::UpdateMenu() method instead of Node's
kNone = 0,
kCustomNode = 1 << 0,
kMenu = 1 << 1,
};
using Flags = uint8_t;

View File

@@ -402,11 +402,6 @@ try {
void Device::UpdateMenu() noexcept {
if (ImGui::BeginMenu("config")) {
static nf7::gui::ConfigEditor ed;
ed(*this);
ImGui::EndMenu();
}
if (ImGui::MenuItem("build")) {
Build();
}

View File

@@ -230,11 +230,6 @@ void FontFace::UpdateMenu() noexcept {
if (ImGui::MenuItem("load")) {
Create();
}
if (ImGui::BeginMenu("config")) {
static nf7::gui::ConfigEditor ed;
ed(*this);
ImGui::EndMenu();
}
}
void FontFace::UpdateTooltip() noexcept {
ImGui::Text("npath : %s", mem_->npath.generic_string().c_str());

View File

@@ -194,11 +194,6 @@ class ObjBase : public nf7::FileBase,
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip("these actions can cause CORRUPTION of running lambdas");
}
if (ImGui::BeginMenu("config")) {
static nf7::gui::ConfigEditor ed;
ed(*this);
ImGui::EndMenu();
}
if constexpr (HasWindow<T>) {
ImGui::Separator();
win_->MenuItem();

View File

@@ -64,7 +64,7 @@ class Node final : public nf7::FileBase,
Node(nf7::Env& env, Data&& data = {}) noexcept :
nf7::FileBase(kType, env),
nf7::GenericConfig(mem_),
nf7::DirItem(nf7::DirItem::kMenu),
nf7::DirItem(nf7::DirItem::kNone),
nf7::Node(nf7::Node::kCustomNode),
life_(*this),
log_(std::make_shared<nf7::LoggerRef>(*this)),
@@ -100,7 +100,6 @@ class Node final : public nf7::FileBase,
nf7::Future<std::shared_ptr<nf7::luajit::Ref>> Build() noexcept;
void Update() noexcept override;
void UpdateMenu() noexcept override;
void UpdateNode(nf7::Node::Editor&) noexcept override;
File::Interface* interface(const std::type_info& t) noexcept override {
@@ -237,13 +236,6 @@ void Node::Update() noexcept {
}
}
void Node::UpdateMenu() noexcept {
if (ImGui::BeginMenu("config")) {
static nf7::gui::ConfigEditor ed;
ed(*this);
ImGui::EndMenu();
}
}
void Node::UpdateNode(nf7::Node::Editor&) noexcept {
const auto em = ImGui::GetFontSize();

View File

@@ -990,10 +990,6 @@ void Network::Update() noexcept {
}
void Network::UpdateMenu() noexcept {
win_.MenuItem();
if (ImGui::BeginMenu("config")) {
Config();
ImGui::EndMenu();
}
}
void Network::UpdateTooltip() noexcept {
ImGui::Text("nodes active: %zu", items_.size());
@@ -1231,12 +1227,10 @@ void Network::Item::UpdateNode(Node::Editor& ed) noexcept {
owner_->ExecAddItem(
std::make_unique<Item>(owner_->next_++, file_->Clone(env())), pos);
}
if (node_->flags() & nf7::Node::kMenu_DirItem) {
ImGui::Separator();
auto dir = file_->interface<nf7::DirItem>();
assert(dir);
dir->UpdateMenu();
}
ImGui::Separator();
nf7::gui::FileMenuItems(*file_);
if (node_->flags() & nf7::Node::kMenu) {
ImGui::Separator();
node_->UpdateMenu(ed);

View File

@@ -254,13 +254,8 @@ void Ref::UpdateNode(Node::Editor&) noexcept {
ExecSync();
}
const auto pathstr = mem_->target.Stringify();
auto w = 6*em;
{
auto pw = ImGui::CalcTextSize(pathstr.c_str()).x+style.FramePadding.x*2;
w = std::max(w, std::min(pw, 8*em));
auto iw = 3*em;
for (const auto& v : mem_->inputs) {
iw = std::max(iw, ImGui::CalcTextSize(v.c_str()).x);
@@ -272,7 +267,10 @@ void Ref::UpdateNode(Node::Editor&) noexcept {
w = std::max(w, 1*em+style.ItemSpacing.x+iw +1*em+ ow+style.ItemSpacing.x+1*em);
}
if (ImGui::Button(pathstr.c_str(), {w, 0})) {
auto newpath = mem_->target;
ImGui::SetNextItemWidth(w);
if (nf7::gui::PathButton("##target", newpath, *this)) {
ExecChangeTarget(std::move(newpath));
}
if (ImGui::BeginDragDropTarget()) {
if (auto p = gui::dnd::Accept<Path>(gui::dnd::kFilePath)) {
@@ -314,18 +312,10 @@ void Ref::UpdateMenu(nf7::Node::Editor& ed) noexcept {
try {
auto& f = target();
auto& n = f.interfaceOrThrow<nf7::Node>();
auto d = f.interface<nf7::DirItem>();
const bool dmenu = n.flags() & nf7::Node::kMenu_DirItem;
const bool menu = n.flags() & nf7::Node::kMenu;
if ((dmenu || menu) && ImGui::BeginMenu("target")) {
if (dmenu) {
assert(d);
ImGui::Separator();
d->UpdateMenu();
}
if (menu) {
if (ImGui::BeginMenu("target")) {
nf7::gui::FileMenuItems(f);
if (n.flags() & nf7::Node::kMenu) {
ImGui::Separator();
n.UpdateMenu(ed);
}

View File

@@ -69,7 +69,7 @@ class TL final : public nf7::FileBase, public nf7::DirItem, public nf7::Node {
ItemId next = 1) noexcept :
nf7::FileBase(kType, env),
nf7::DirItem(nf7::DirItem::kMenu),
nf7::Node(nf7::Node::kMenu_DirItem),
nf7::Node(nf7::Node::kNone),
life_(*this), log_(*this),
layers_(std::move(layers)), next_(next),
win_(*this, "Timeline Editor"), tl_("timeline") {
@@ -1533,6 +1533,8 @@ void TL::Item::Update() noexcept {
if (ImGui::MenuItem("remove")) {
layer_->ExecRemoveItem(*this);
}
nf7::gui::FileMenuItems(*file_);
if (seq_->flags() & nf7::Sequencer::kMenu) {
ImGui::Separator();
seq_->UpdateMenu(ed);

View File

@@ -14,11 +14,13 @@
#include "nf7.hh"
#include "common/config.hh"
#include "common/dir.hh"
#include "common/dir_item.hh"
#include "common/file_base.hh"
#include "common/generic_context.hh"
#include "common/generic_type_info.hh"
#include "common/gui.hh"
#include "common/gui_dnd.hh"
#include "common/gui_window.hh"
#include "common/ptr_selector.hh"
@@ -226,14 +228,7 @@ void Dir::UpdateTree() noexcept {
// tooltip
if (ImGui::IsItemHovered()) {
ImGui::BeginTooltip();
ImGui::TextUnformatted(file.type().name().c_str());
ImGui::SameLine();
ImGui::TextDisabled(file.abspath().Stringify().c_str());
if (ditem && (ditem->flags() & DirItem::kTooltip)) {
ImGui::Indent();
ditem->UpdateTooltip();
ImGui::Unindent();
}
nf7::gui::FileTooltip(file);
ImGui::EndTooltip();
}
@@ -244,11 +239,6 @@ void Dir::UpdateTree() noexcept {
// context menu
if (ImGui::BeginPopupContextItem()) {
if (ImGui::MenuItem("copy path")) {
ImGui::SetClipboardText(file.abspath().Stringify().c_str());
}
ImGui::Separator();
if (ImGui::MenuItem("remove")) {
env().ExecMain(
std::make_shared<nf7::GenericContext>(*this, "removing item"),
@@ -274,10 +264,9 @@ void Dir::UpdateTree() noexcept {
ImGui::EndMenu();
}
if (ditem && (ditem->flags() & DirItem::kMenu)) {
ImGui::Separator();
ditem->UpdateMenu();
}
ImGui::Separator();
nf7::gui::FileMenuItems(file);
ImGui::EndPopup();
}

View File

@@ -74,7 +74,7 @@ class Event final : public nf7::FileBase,
nf7::FileBase(kType, env),
nf7::GenericConfig(mem_),
nf7::DirItem(nf7::DirItem::kMenu),
nf7::Node(nf7::Node::kMenu_DirItem),
nf7::Node(nf7::Node::kNone),
life_(*this), log_(*this),
la_root_(std::make_shared<nf7::Node::Lambda>(*this)),
mem_(std::move(d), *this) {
@@ -204,12 +204,6 @@ void Event::UpdateMenu() noexcept {
if (ImGui::MenuItem("drop handler's lambda")) {
la_ = nullptr;
}
ImGui::Separator();
if (ImGui::BeginMenu("config")) {
static nf7::gui::ConfigEditor ed;
ed(*this);
ImGui::EndMenu();
}
}
} // namespace

View File

@@ -155,11 +155,6 @@ void ImGui_::UpdateMenu() noexcept {
}
}
}
if (ImGui::BeginMenu("config")) {
static nf7::gui::ConfigEditor ed;
ed(*this);
ImGui::EndMenu();
}
}
}

View File

@@ -279,12 +279,6 @@ class Logger final : public nf7::FileBase,
void Logger::UpdateMenu() noexcept {
if (ImGui::BeginMenu("config")) {
static nf7::gui::ConfigEditor ed;
ed(*this);
ImGui::EndMenu();
}
ImGui::Separator();
win_.MenuItem();
}
void Logger::UpdateRowMenu(const Row& row) noexcept {

View File

@@ -115,9 +115,8 @@ class NFile final : public nf7::FileBase,
NFile(nf7::Env& env, Data&& data = {}) noexcept :
nf7::FileBase(kType, env),
nf7::GenericConfig(mem_),
nf7::DirItem(nf7::DirItem::kMenu |
nf7::DirItem::kTooltip),
nf7::Node(nf7::Node::kMenu_DirItem),
nf7::DirItem(nf7::DirItem::kTooltip),
nf7::Node(nf7::Node::kNone),
life_(*this), nwatch_(*this),
shared_(std::make_shared<SharedData>(*this)),
th_(std::make_shared<Thread>(*this, Runner {shared_})),
@@ -154,7 +153,6 @@ class NFile final : public nf7::FileBase,
return kOutputs;
}
void UpdateMenu() noexcept override;
void UpdateTooltip() noexcept override;
File::Interface* interface(const std::type_info& t) noexcept override {
@@ -275,13 +273,6 @@ std::shared_ptr<nf7::Node::Lambda> NFile::CreateLambda(
}
void NFile::UpdateMenu() noexcept {
if (ImGui::BeginMenu("config")) {
static nf7::gui::ConfigEditor ed;
ed(*this);
ImGui::EndMenu();
}
}
void NFile::UpdateTooltip() noexcept {
ImGui::Text("npath: %s", mem_->npath.generic_string().c_str());
ImGui::Text("mode : %s", mem_->mode.c_str());

View File

@@ -24,7 +24,6 @@
#include "common/generic_config.hh"
#include "common/generic_memento.hh"
#include "common/generic_type_info.hh"
#include "common/gui.hh"
#include "common/gui_window.hh"
#include "common/life.hh"
#include "common/logger_ref.hh"
@@ -242,12 +241,6 @@ std::shared_ptr<nf7::Node::Lambda> Plot::CreateLambda(
void Plot::UpdateMenu() noexcept {
win_.MenuItem();
if (ImGui::BeginMenu("config")) {
static nf7::gui::ConfigEditor ed;
ed(*this);
ImGui::EndMenu();
}
}
void Plot::PlotGraph() noexcept {