Refactor code structure for improved readability and maintainability

This commit is contained in:
scawful
2025-10-02 21:16:15 -04:00
parent 4613722d7a
commit 4098011647
7 changed files with 889 additions and 430 deletions

View File

@@ -258,7 +258,9 @@ if(YAZE_WITH_GRPC)
${CMAKE_SOURCE_DIR}/src/app/core/testing/test_recorder.cc
${CMAKE_SOURCE_DIR}/src/app/core/testing/test_recorder.h
${CMAKE_SOURCE_DIR}/src/app/core/testing/test_script_parser.cc
${CMAKE_SOURCE_DIR}/src/app/core/testing/test_script_parser.h)
${CMAKE_SOURCE_DIR}/src/app/core/testing/test_script_parser.h
${CMAKE_SOURCE_DIR}/src/app/core/widget_state_capture.cc
${CMAKE_SOURCE_DIR}/src/app/core/widget_state_capture.h)
target_include_directories(yaze PRIVATE
${CMAKE_SOURCE_DIR}/third_party/json/include)

View File

@@ -0,0 +1,115 @@
#include "app/core/widget_state_capture.h"
#include "absl/strings/str_format.h"
#include "nlohmann/json.hpp"
namespace yaze {
namespace core {
std::string CaptureWidgetState() {
WidgetState state;
// Check if ImGui context is available
ImGuiContext* ctx = ImGui::GetCurrentContext();
if (!ctx) {
return R"({"error": "ImGui context not available"})";
}
ImGuiIO& io = ImGui::GetIO();
// Capture frame information
state.frame_count = ImGui::GetFrameCount();
state.frame_rate = io.Framerate;
// Capture focused window
ImGuiWindow* current = ImGui::GetCurrentWindow();
if (current && !current->Hidden) {
state.focused_window = current->Name;
}
// Capture active widget (focused for input)
ImGuiID active_id = ImGui::GetActiveID();
if (active_id != 0) {
state.focused_widget = absl::StrFormat("0x%08X", active_id);
}
// Capture hovered widget
ImGuiID hovered_id = ImGui::GetHoveredID();
if (hovered_id != 0) {
state.hovered_widget = absl::StrFormat("0x%08X", hovered_id);
}
// Traverse visible windows
for (ImGuiWindow* window : ctx->Windows) {
if (window && window->Active && !window->Hidden) {
state.visible_windows.push_back(window->Name);
}
}
// Capture open popups
for (int i = 0; i < ctx->OpenPopupStack.Size; i++) {
ImGuiPopupData& popup = ctx->OpenPopupStack[i];
if (popup.Window && !popup.Window->Hidden) {
state.open_popups.push_back(popup.Window->Name);
}
}
// Capture navigation state
state.nav_id = ctx->NavId;
state.nav_active = ctx->NavWindow != nullptr;
// Capture mouse state
for (int i = 0; i < 5; i++) {
state.mouse_down[i] = io.MouseDown[i];
}
state.mouse_pos_x = io.MousePos.x;
state.mouse_pos_y = io.MousePos.y;
// Capture keyboard modifiers
state.ctrl_pressed = io.KeyCtrl;
state.shift_pressed = io.KeyShift;
state.alt_pressed = io.KeyAlt;
return SerializeWidgetStateToJson(state);
}
std::string SerializeWidgetStateToJson(const WidgetState& state) {
nlohmann::json j;
// Basic state
j["frame_count"] = state.frame_count;
j["frame_rate"] = state.frame_rate;
// Window state
j["focused_window"] = state.focused_window;
j["focused_widget"] = state.focused_widget;
j["hovered_widget"] = state.hovered_widget;
j["visible_windows"] = state.visible_windows;
j["open_popups"] = state.open_popups;
// Navigation state
j["navigation"] = {
{"nav_id", absl::StrFormat("0x%08X", state.nav_id)},
{"nav_active", state.nav_active}
};
// Input state
nlohmann::json mouse_buttons;
for (int i = 0; i < 5; i++) {
mouse_buttons.push_back(state.mouse_down[i]);
}
j["input"] = {
{"mouse_buttons", mouse_buttons},
{"mouse_pos", {state.mouse_pos_x, state.mouse_pos_y}},
{"modifiers", {
{"ctrl", state.ctrl_pressed},
{"shift", state.shift_pressed},
{"alt", state.alt_pressed}
}}
};
return j.dump(2); // Pretty print with 2-space indent
}
} // namespace core
} // namespace yaze

View File

@@ -0,0 +1,47 @@
#ifndef YAZE_CORE_WIDGET_STATE_CAPTURE_H
#define YAZE_CORE_WIDGET_STATE_CAPTURE_H
#include <string>
#include <vector>
#include "imgui/imgui.h"
namespace yaze {
namespace core {
// Widget state snapshot for debugging test failures
struct WidgetState {
std::string focused_window;
std::string focused_widget;
std::string hovered_widget;
std::vector<std::string> visible_windows;
std::vector<std::string> open_popups;
int frame_count = 0;
float frame_rate = 0.0f;
// Navigation state
ImGuiID nav_id = 0;
bool nav_active = false;
// Input state
bool mouse_down[5] = {false};
float mouse_pos_x = 0.0f;
float mouse_pos_y = 0.0f;
// Keyboard state
bool ctrl_pressed = false;
bool shift_pressed = false;
bool alt_pressed = false;
};
// Capture current ImGui widget state for debugging
// Returns JSON-formatted string representing the widget hierarchy and state
std::string CaptureWidgetState();
// Serialize widget state to JSON format
std::string SerializeWidgetStateToJson(const WidgetState& state);
} // namespace core
} // namespace yaze
#endif // YAZE_CORE_WIDGET_STATE_CAPTURE_H

File diff suppressed because it is too large Load Diff