From 16a7b855ac473b334a7d4791ec71bd001d1c4f6a Mon Sep 17 00:00:00 2001 From: scawful Date: Sat, 4 Oct 2025 16:30:56 -0400 Subject: [PATCH] feat: Enhance AutoWidgetScope with proper destructor and streamline label extraction --- src/app/gui/widget_auto_register.cc | 18 ++++++++++-------- src/app/gui/widget_auto_register.h | 12 ++++-------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/app/gui/widget_auto_register.cc b/src/app/gui/widget_auto_register.cc index 014b08ac..63c0dd8b 100644 --- a/src/app/gui/widget_auto_register.cc +++ b/src/app/gui/widget_auto_register.cc @@ -1,6 +1,9 @@ #include "app/gui/widget_auto_register.h" +#include + #include "imgui/imgui_internal.h" +#include "absl/strings/str_join.h" #include "absl/strings/string_view.h" namespace yaze { @@ -14,6 +17,12 @@ AutoWidgetScope::AutoWidgetScope(const std::string& name) g_auto_scope_stack_.push_back(name); } +AutoWidgetScope::~AutoWidgetScope() { + if (!g_auto_scope_stack_.empty()) { + g_auto_scope_stack_.pop_back(); + } +} + void AutoRegisterLastItem(const std::string& widget_type, const std::string& explicit_label, const std::string& description) { @@ -31,14 +40,7 @@ void AutoRegisterLastItem(const std::string& widget_type, // Extract label std::string label = explicit_label; if (label.empty()) { - // Try to get label from ImGui - const char* imgui_label = ImGui::GetItemLabel(); - if (imgui_label && imgui_label[0] != '\0') { - label = imgui_label; - } else { - // Fallback to widget type + ID - label = absl::StrCat(widget_type, "_", imgui_id); - } + label = absl::StrCat(widget_type, "_", imgui_id); } // Build full hierarchical path diff --git a/src/app/gui/widget_auto_register.h b/src/app/gui/widget_auto_register.h index 872a6481..737401d5 100644 --- a/src/app/gui/widget_auto_register.h +++ b/src/app/gui/widget_auto_register.h @@ -1,6 +1,8 @@ #ifndef YAZE_APP_GUI_WIDGET_AUTO_REGISTER_H_ #define YAZE_APP_GUI_WIDGET_AUTO_REGISTER_H_ +#include + #include "imgui/imgui.h" #include "app/gui/widget_id_registry.h" #include "absl/strings/str_cat.h" @@ -39,7 +41,7 @@ namespace gui { class AutoWidgetScope { public: explicit AutoWidgetScope(const std::string& name); - ~AutoWidgetScope() = default; + ~AutoWidgetScope(); // Get current scope path std::string GetPath() const { return scope_.GetFullPath(); } @@ -242,12 +244,7 @@ inline bool AutoCollapsingHeader(const char* label, ImGuiTreeNodeFlags flags = 0 * @param description Optional description of the canvas purpose */ inline void RegisterCanvas(const char* canvas_name, const std::string& description = "") { - // Get the child window ID - ImGuiContext* ctx = ImGui::GetCurrentContext(); - if (ctx && ctx->CurrentWindow) { - ImGuiID canvas_id = ImGui::GetID(canvas_name); - AutoRegisterLastItem("canvas", canvas_name, description); - } + AutoRegisterLastItem("canvas", canvas_name, description); } /** @@ -257,7 +254,6 @@ inline void RegisterCanvas(const char* canvas_name, const std::string& descripti * @param description Optional description */ inline void RegisterTable(const char* table_name, const std::string& description = "") { - ImGuiID table_id = ImGui::GetID(table_name); AutoRegisterLastItem("table", table_name, description); }