#include "kpi_card.h" #include "sparkline.h" #include "core/tokens.h" #include #include 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 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); }