Files
fn_registry/cpp/framework/app_base.cpp
T
egutierrez 424e913566 feat(cpp,bash): app_about + Settings submenu, ensure_repo_synced pipeline
cpp/core: nuevo modulo app_about — ventana About con project/version/desc,
componible via about_window_set_info() en el init de la app y rendererizada
automaticamente por fn::run_app al final de cada frame.

app_menubar: el item top-level "Settings..." pasa a ser un BeginMenu
"Settings" con dos subitems: "Settings..." (existente) y "About..." (nuevo).

bash/infra: nueva pipeline ensure_repo_synced que compone gitea_create_repo
y gitea_push_directory para garantizar repo Gitea existente + sync de un
directorio local en una sola llamada idempotente.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-28 22:05:31 +02:00

186 lines
5.7 KiB
C++

#include "app_base.h"
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include "implot.h"
#include "implot3d.h"
#include "core/tokens.h"
#include "core/icon_font.h"
#include "core/app_settings.h"
#include "core/app_about.h"
#include "core/fps_overlay.h"
#include <GLFW/glfw3.h>
#include <cstdio>
#ifdef TRACY_ENABLE
#include "tracy/Tracy.hpp"
#endif
static void glfw_error_callback(int error, const char* description) {
fprintf(stderr, "GLFW Error %d: %s\n", error, description);
}
namespace fn {
int run_app(AppConfig config, std::function<void()> render_fn) {
glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit()) {
fprintf(stderr, "Failed to initialize GLFW\n");
return 1;
}
// OpenGL 3.3 core
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
GLFWwindow* window = glfwCreateWindow(config.width, config.height, config.title, nullptr, nullptr);
if (!window) {
fprintf(stderr, "Failed to create GLFW window\n");
glfwTerminate();
return 1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(config.vsync ? 1 : 0);
// Setup ImGui
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImPlot::CreateContext();
ImPlot3D::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
// Lee app_settings.ini (font_id, font_size_px, show_fps) antes de cargar
// fuentes. Si no existe el .ini, los defaults se aplican.
fn_ui::settings_load();
// Texto vectorial (Karla / Roboto / DroidSans / Cousine, segun settings)
// + iconos Tabler mergeados al mismo tamaño en el mismo ImFont.
fn_ui::load_fonts_from_settings();
// ImGui 1.92+ usa style.FontSizeBase como tamaño activo (escalable sin
// rebuild de atlas). Inicializa al valor del .ini para que el primer
// frame ya respete el setting.
{
ImGuiStyle& style = ImGui::GetStyle();
style.FontSizeBase = fn_ui::settings().font_size_px;
style._NextFrameFontSizeBase = style.FontSizeBase;
}
if (config.viewports) {
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
}
// Identidad visual — ver cpp/DESIGN_SYSTEM.md
switch (config.theme) {
case ThemeMode::FnDark: fn_tokens::apply_dark_theme(); break;
case ThemeMode::ImGuiDark: ImGui::StyleColorsDark(); break;
case ThemeMode::ImGuiLight: ImGui::StyleColorsLight(); break;
case ThemeMode::None: break;
}
// When viewports are enabled, tweak WindowRounding/WindowBg so
// platform windows look consistent with the main window
if (config.viewports) {
ImGuiStyle& style = ImGui::GetStyle();
style.WindowRounding = 0.0f;
style.Colors[ImGuiCol_WindowBg].w = 1.0f;
}
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 330");
// Main loop
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
if (glfwGetWindowAttrib(window, GLFW_ICONIFIED)) {
glfwWaitEvents();
continue;
}
// Tamaño de fuente: aplica via style.FontSizeBase cada frame. Cambios
// se ven al instante (ImGui 1.92+ escala el atlas dinamicamente, no
// hace falta rebuild).
ImGuiStyle& style = ImGui::GetStyle();
if (style.FontSizeBase != fn_ui::settings().font_size_px) {
style.FontSizeBase = fn_ui::settings().font_size_px;
style._NextFrameFontSizeBase = style.FontSizeBase; // FIXME-ImGui hack
}
// Cambio de fuente (font_id): rebuild atlas. ImGui_ImplOpenGL3
// refresca la GPU texture via UpdateTexture en RenderDrawData.
if (fn_ui::settings_consume_font_dirty()) {
fn_ui::load_fonts_from_settings();
}
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
render_fn();
// Ventana de Settings (no-op si esta cerrada).
fn_ui::settings_window_render();
// Ventana About (no-op si esta cerrada).
fn_ui::about_window_render();
// FPS overlay si esta activado en Settings.
if (fn_ui::settings().show_fps) {
fps_overlay();
}
ImGui::Render();
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glClearColor(config.bg_r, config.bg_g, config.bg_b, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
// Multi-viewport: update and render platform windows
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) {
GLFWwindow* backup_ctx = glfwGetCurrentContext();
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
glfwMakeContextCurrent(backup_ctx);
}
glfwSwapBuffers(window);
#ifdef TRACY_ENABLE
FrameMark;
#endif
}
// Persiste settings al exit (idempotente con auto-saves del menu).
fn_ui::settings_save();
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImPlot3D::DestroyContext();
ImPlot::DestroyContext();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
int run_app(std::function<void()> render_fn) {
return run_app(AppConfig{}, render_fn);
}
} // namespace fn