feat: Enhance AutoWidgetScope with proper destructor and streamline label extraction

This commit is contained in:
scawful
2025-10-04 16:30:56 -04:00
parent 867fa820ba
commit 16a7b855ac
2 changed files with 14 additions and 16 deletions

View File

@@ -1,6 +1,9 @@
#include "app/gui/widget_auto_register.h"
#include <vector>
#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

View File

@@ -1,6 +1,8 @@
#ifndef YAZE_APP_GUI_WIDGET_AUTO_REGISTER_H_
#define YAZE_APP_GUI_WIDGET_AUTO_REGISTER_H_
#include <string>
#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);
}