10#include "absl/strings/ascii.h"
11#include "absl/strings/str_cat.h"
12#include "absl/strings/str_format.h"
13#include "absl/strings/str_join.h"
14#include "absl/strings/str_split.h"
15#include "absl/strings/strip.h"
16#include "absl/time/clock.h"
17#include "imgui/imgui_internal.h"
29 ImGuiContext* ctx = ImGui::GetCurrentContext();
30 if (ctx && ctx->CurrentWindow && !ctx->Windows.empty()) {
31 ImGui::PushID(name.c_str());
49 const std::string& widget_name)
const {
54 return absl::StrCat(path, widget_type,
":", widget_name);
65 ImGuiContext* ctx = ImGui::GetCurrentContext();
75 info.seen_in_current_frame =
false;
81 if (!info.seen_in_current_frame) {
84 info.bounds.valid =
false;
85 info.stale_frame_count += 1;
87 info.seen_in_current_frame =
false;
88 info.stale_frame_count = 0;
97 size_t slash = path.find(
'/');
98 if (slash == absl::string_view::npos) {
99 return std::string(path);
101 return std::string(path.substr(0, slash));
105 size_t colon = path.rfind(
':');
106 if (colon == absl::string_view::npos) {
107 size_t slash = path.rfind(
'/');
108 if (slash == absl::string_view::npos) {
109 return std::string(path);
111 return std::string(path.substr(slash + 1));
113 return std::string(path.substr(colon + 1));
117 if (timestamp == absl::Time()) {
121 std::chrono::system_clock::time_point chrono_time =
122 absl::ToChronoTime(timestamp);
123 std::time_t time_value = std::chrono::system_clock::to_time_t(chrono_time);
127 if (gmtime_s(&tm_buffer, &time_value) != 0) {
131 if (gmtime_r(&time_value, &tm_buffer) ==
nullptr) {
137 if (std::snprintf(buffer,
sizeof(buffer),
"%04d-%02d-%02dT%02d:%02d:%02dZ",
138 tm_buffer.tm_year + 1900, tm_buffer.tm_mon + 1,
139 tm_buffer.tm_mday, tm_buffer.tm_hour, tm_buffer.tm_min,
140 tm_buffer.tm_sec) <= 0) {
144 return std::string(buffer);
149 bounds.
min_x = rect.Min.x;
150 bounds.
min_y = rect.Min.y;
151 bounds.
max_x = rect.Max.x;
152 bounds.
max_y = rect.Max.y;
160 const std::string& type, ImGuiID imgui_id,
161 const std::string& description,
169 if (metadata.
label.has_value()) {
174 if (info.
label.empty()) {
175 info.
label = ExtractLabelFromPath(full_path);
184 info.
window_name = ExtractWindowFromPath(full_path);
187 ImGuiContext* ctx = ImGui::GetCurrentContext();
188 absl::Time observed_at = absl::Now();
191 const ImGuiLastItemData& last = ctx->LastItemData;
192 if (metadata.
visible.has_value()) {
195 info.
visible = (last.StatusFlags & ImGuiItemStatusFlags_Visible) != 0;
198 if (metadata.
enabled.has_value()) {
201 info.
enabled = (last.ItemFlags & ImGuiItemFlags_Disabled) == 0;
204 if (metadata.
bounds.has_value()) {
207 info.
bounds = BoundsFromImGui(last.Rect);
214 if (metadata.
bounds.has_value()) {
230 const std::string& pattern)
const {
231 std::vector<std::string> matches;
235 for (
const auto& [path, info] :
widgets_) {
238 if (pattern ==
"*") {
240 }
else if (pattern.find(
'*') != std::string::npos) {
242 std::string search = pattern;
243 search.erase(std::remove(search.begin(), search.end(),
'*'),
245 if (!search.empty() && path.find(search) != std::string::npos) {
250 if (path == pattern) {
256 matches.push_back(path);
261 std::sort(matches.begin(), matches.end());
268 return it->second.imgui_id;
274 const std::string& full_path)
const {
288 std::ostringstream ss;
290 if (format ==
"json") {
292 ss <<
" \"widgets\": [\n";
295 for (
const auto& [path, info] :
widgets_) {
301 ss << absl::StrFormat(
" \"path\": \"%s\",\n", path);
302 ss << absl::StrFormat(
" \"type\": \"%s\",\n", info.type);
303 ss << absl::StrFormat(
" \"imgui_id\": %u,\n", info.imgui_id);
304 ss << absl::StrFormat(
" \"label\": \"%s\",\n", info.label);
305 ss << absl::StrFormat(
" \"window\": \"%s\",\n", info.window_name);
306 ss << absl::StrFormat(
" \"visible\": %s,\n",
307 info.visible ?
"true" :
"false");
308 ss << absl::StrFormat(
" \"enabled\": %s,\n",
309 info.enabled ?
"true" :
"false");
310 if (info.bounds.valid) {
311 ss << absl::StrFormat(
312 " \"bounds\": {\"min\": [%0.1f, %0.1f], \"max\": [%0.1f, "
314 info.bounds.min_x, info.bounds.min_y, info.bounds.max_x,
317 ss <<
" \"bounds\": null,\n";
319 ss << absl::StrFormat(
" \"last_seen_frame\": %d,\n",
320 info.last_seen_frame);
321 std::string iso_timestamp = FormatTimestampUTC(info.last_seen_time);
322 ss << absl::StrFormat(
" \"last_seen_at\": \"%s\",\n", iso_timestamp);
323 ss << absl::StrFormat(
" \"stale\": %s",
324 info.stale_frame_count > 0 ?
"true" :
"false");
325 if (!info.description.empty()) {
327 ss << absl::StrFormat(
" \"description\": \"%s\"\n",
341 for (
const auto& [path, info] :
widgets_) {
342 ss << absl::StrFormat(
" - path: \"%s\"\n", path);
343 ss << absl::StrFormat(
" type: %s\n", info.type);
344 ss << absl::StrFormat(
" imgui_id: %u\n", info.imgui_id);
345 ss << absl::StrFormat(
" label: \"%s\"\n", info.label);
346 ss << absl::StrFormat(
" window: \"%s\"\n", info.window_name);
347 ss << absl::StrFormat(
" visible: %s\n",
348 info.visible ?
"true" :
"false");
349 ss << absl::StrFormat(
" enabled: %s\n",
350 info.enabled ?
"true" :
"false");
351 if (info.bounds.valid) {
353 ss << absl::StrFormat(
" min: [%0.1f, %0.1f]\n", info.bounds.min_x,
355 ss << absl::StrFormat(
" max: [%0.1f, %0.1f]\n", info.bounds.max_x,
358 ss << absl::StrFormat(
" last_seen_frame: %d\n", info.last_seen_frame);
359 std::string iso_timestamp = FormatTimestampUTC(info.last_seen_time);
360 ss << absl::StrFormat(
" last_seen_at: %s\n", iso_timestamp);
361 ss << absl::StrFormat(
" stale: %s\n",
362 info.stale_frame_count > 0 ?
"true" :
"false");
365 std::vector<std::string> segments = absl::StrSplit(path,
'/');
366 if (!segments.empty()) {
368 if (segments.size() > 0) {
369 ss << absl::StrFormat(
" editor: %s\n", segments[0]);
371 if (segments.size() > 1) {
372 ss << absl::StrFormat(
" tab: %s\n", segments[1]);
374 if (segments.size() > 2) {
375 ss << absl::StrFormat(
" section: %s\n", segments[2]);
379 if (!info.description.empty()) {
380 ss << absl::StrFormat(
" description: %s\n", info.description);
385 if (info.type ==
"button") {
387 }
else if (info.type ==
"input") {
389 }
else if (info.type ==
"canvas") {
390 ss <<
"click, drag, scroll";
391 }
else if (info.type ==
"checkbox") {
393 }
else if (info.type ==
"slider") {
406 const std::string& format)
const {
408 std::ofstream file(output_file);
409 if (file.is_open()) {
416 size_t pos = label.find(
"##");
417 if (pos != absl::string_view::npos) {
418 label = label.substr(0, pos);
420 std::string sanitized = std::string(absl::StripAsciiWhitespace(label));