feat: Enhance AutoWidgetScope with proper destructor and streamline label extraction
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
#include "app/gui/widget_auto_register.h"
|
#include "app/gui/widget_auto_register.h"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "imgui/imgui_internal.h"
|
#include "imgui/imgui_internal.h"
|
||||||
|
#include "absl/strings/str_join.h"
|
||||||
#include "absl/strings/string_view.h"
|
#include "absl/strings/string_view.h"
|
||||||
|
|
||||||
namespace yaze {
|
namespace yaze {
|
||||||
@@ -14,6 +17,12 @@ AutoWidgetScope::AutoWidgetScope(const std::string& name)
|
|||||||
g_auto_scope_stack_.push_back(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,
|
void AutoRegisterLastItem(const std::string& widget_type,
|
||||||
const std::string& explicit_label,
|
const std::string& explicit_label,
|
||||||
const std::string& description) {
|
const std::string& description) {
|
||||||
@@ -31,15 +40,8 @@ void AutoRegisterLastItem(const std::string& widget_type,
|
|||||||
// Extract label
|
// Extract label
|
||||||
std::string label = explicit_label;
|
std::string label = explicit_label;
|
||||||
if (label.empty()) {
|
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
|
// Build full hierarchical path
|
||||||
std::string full_path;
|
std::string full_path;
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
#ifndef YAZE_APP_GUI_WIDGET_AUTO_REGISTER_H_
|
#ifndef YAZE_APP_GUI_WIDGET_AUTO_REGISTER_H_
|
||||||
#define YAZE_APP_GUI_WIDGET_AUTO_REGISTER_H_
|
#define YAZE_APP_GUI_WIDGET_AUTO_REGISTER_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "imgui/imgui.h"
|
#include "imgui/imgui.h"
|
||||||
#include "app/gui/widget_id_registry.h"
|
#include "app/gui/widget_id_registry.h"
|
||||||
#include "absl/strings/str_cat.h"
|
#include "absl/strings/str_cat.h"
|
||||||
@@ -39,7 +41,7 @@ namespace gui {
|
|||||||
class AutoWidgetScope {
|
class AutoWidgetScope {
|
||||||
public:
|
public:
|
||||||
explicit AutoWidgetScope(const std::string& name);
|
explicit AutoWidgetScope(const std::string& name);
|
||||||
~AutoWidgetScope() = default;
|
~AutoWidgetScope();
|
||||||
|
|
||||||
// Get current scope path
|
// Get current scope path
|
||||||
std::string GetPath() const { return scope_.GetFullPath(); }
|
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
|
* @param description Optional description of the canvas purpose
|
||||||
*/
|
*/
|
||||||
inline void RegisterCanvas(const char* canvas_name, const std::string& description = "") {
|
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
|
* @param description Optional description
|
||||||
*/
|
*/
|
||||||
inline void RegisterTable(const char* table_name, const std::string& description = "") {
|
inline void RegisterTable(const char* table_name, const std::string& description = "") {
|
||||||
ImGuiID table_id = ImGui::GetID(table_name);
|
|
||||||
AutoRegisterLastItem("table", table_name, description);
|
AutoRegisterLastItem("table", table_name, description);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user