Files
fn_registry/cpp/functions/viz/kpi_card.cpp
T
egutierrez 4a95407d0e feat(shaders_lab): add gl_loader + Windows cross-compile
- cpp/functions/gfx/gl_loader.{h,cpp,md}: mini loader para OpenGL 2.0+
  (Linux no-op via GL_GLEXT_PROTOTYPES, Windows wglGetProcAddress)
- Portar gl_shader/gl_framebuffer/fullscreen_quad/shader_canvas al loader
- CMakeLists: WIN32_EXECUTABLE para lanzar sin consola en Windows
- apps/shaders_lab/shaders_lab.exe: binario PE32+ precompilado

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-24 20:52:37 +02:00

73 lines
2.6 KiB
C++

#include "kpi_card.h"
#include "sparkline.h"
#include "core/tokens.h"
#include <imgui.h>
#include <cstdio>
void kpi_card(const char* label, float value, float delta_percent,
const float* history, int history_count,
const char* format) {
using namespace fn_tokens;
// Card container — surface bg, border, rounded, padding.
// Mirrors Mantine <Paper withBorder shadow="xs" radius="md" p="md" /> used in
// @fn_library/kpi_card.tsx, adapted for ImGui dark theme.
ImGui::PushStyleColor(ImGuiCol_ChildBg, colors::surface);
ImGui::PushStyleColor(ImGuiCol_Border, colors::border);
ImGui::PushStyleVar(ImGuiStyleVar_ChildRounding, radius::md);
ImGui::PushStyleVar(ImGuiStyleVar_ChildBorderSize, 1.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(spacing::md, spacing::md));
// Unique child id per label so multiple cards in the same window don't collide.
char child_id[96];
std::snprintf(child_id, sizeof(child_id), "##kpi_%s", label);
float width = ImGui::GetContentRegionAvail().x;
if (width < 1.0f) width = 1.0f;
ImGui::BeginChild(child_id, ImVec2(width, 0),
ImGuiChildFlags_Borders | ImGuiChildFlags_AutoResizeY);
// Label — muted (Mantine "dimmed" text)
ImGui::PushStyleColor(ImGuiCol_Text, colors::text_muted);
ImGui::TextUnformatted(label);
ImGui::PopStyleColor();
// Value — scaled up (Mantine fw={700} fontSize=1.875rem)
ImGui::SetWindowFontScale(1.8f);
char value_buf[64];
std::snprintf(value_buf, sizeof(value_buf), format, value);
ImGui::TextUnformatted(value_buf);
ImGui::SetWindowFontScale(1.0f);
// Delta badge — only render when meaningful
const bool has_delta = delta_percent != 0.0f;
const bool has_history = history != nullptr && history_count > 0;
if (has_delta || has_history) {
const bool positive = delta_percent >= 0.0f;
const ImVec4 delta_color = positive ? colors::success : colors::error;
if (has_delta) {
char delta_buf[32];
if (positive) {
std::snprintf(delta_buf, sizeof(delta_buf), "\xe2\x96\xb2 +%.1f%%", delta_percent);
} else {
std::snprintf(delta_buf, sizeof(delta_buf), "\xe2\x96\xbc %.1f%%", delta_percent);
}
ImGui::PushStyleColor(ImGuiCol_Text, delta_color);
ImGui::TextUnformatted(delta_buf);
ImGui::PopStyleColor();
}
if (has_history) {
sparkline(label, history, history_count, delta_color, 120.0f, 24.0f);
}
}
ImGui::EndChild();
ImGui::PopStyleVar(3);
ImGui::PopStyleColor(2);
}