#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/app_menubar.h" #include "core/fps_overlay.h" #include "core/logger.h" #include "core/log_window.h" #include "core/layout_storage.h" #include "gfx/gl_loader.h" #include #include #include #include #include #include #include #include #ifdef _WIN32 #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif #include #include // GET_X_LPARAM / GET_Y_LPARAM #define GLFW_EXPOSE_NATIVE_WIN32 #include #else #include #endif #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); } #ifdef _WIN32 // AltSnap (and other external window movers — tiling WMs, snap-assist) bracket // their drag with WM_ENTERSIZEMOVE / WM_EXITSIZEMOVE messages but, unlike the // native title-bar drag, do NOT block the application thread inside the // modal DefWindowProc move loop. Result: the app keeps rendering and swapping // buffers while the OS posts SetWindowPos(SWP_ASYNCWINDOWPOS) calls, racing // the framebuffer presentation against the live window position and producing // the visible jitter / "grab and release" flicker the user reports. // // Native title-bar drag has no jitter precisely because Windows enters the // modal sizemove loop and the app stops drawing — the DWM compositor moves // the existing buffer pixels. We replicate that contract: while sizemove is // active, skip render + glfwSwapBuffers, only pump the message queue. As soon // as WM_EXITSIZEMOVE arrives, normal rendering resumes. // // IMPORTANT: the subclass must cover EVERY HWND owned by the process — main // window AND every secondary viewport platform window the ImGui GLFW backend // creates when the user drags a panel outside the main. Otherwise AltSnap on // a secondary HWND would not be observed, the main loop would keep rendering, // and the visible jitter would return on that panel. g_in_sizemove stays // global on purpose: any external move on ANY of our HWNDs pauses the whole // render pipeline, exactly like the native title-bar drag contract. static std::atomic g_in_sizemove{false}; // Test observability — monotonic counters. fn::internal exposes accessors. static std::atomic g_sizemove_enter_count{0}; static std::atomic g_alt_rmb_resize_count{0}; static std::atomic g_alt_lmb_move_count{0}; // Test hook — bypasses GetAsyncKeyState(VK_MENU) so headless tests can drive // the Alt+RMB / Alt+LMB paths without UI-access for keybd_event. static std::atomic g_force_alt_for_test{false}; // Diagnostic: every WM_RBUTTONDOWN this subclass sees (Alt-or-not). Used to // distinguish "message never arrived" from "Alt check failed". static std::atomic g_rbuttondown_seen_count{0}; // Accessed only from the main (render) thread. Map value is the original // WNDPROC for that HWND so we can restore and chain CallWindowProcW. static std::unordered_map g_subclassed; // Pick the WMSZ_* direction whose modal resize will feel natural depending // on which quadrant of the client rect the cursor is in. Matches AltSnap's // quadrant rule (top-left -> shrink toward top-left, etc.). static int alt_rmb_resize_direction(HWND hwnd, int client_x, int client_y) { RECT rc{}; if (!GetClientRect(hwnd, &rc)) return 8 /* WMSZ_BOTTOMRIGHT */; int cx = (rc.right - rc.left) / 2; int cy = (rc.bottom - rc.top) / 2; bool top = (client_y < cy); bool left = (client_x < cx); if (top && left) return 4; // WMSZ_TOPLEFT if (top && !left) return 5; // WMSZ_TOPRIGHT if (!top && left) return 7; // WMSZ_BOTTOMLEFT return 8; // WMSZ_BOTTOMRIGHT } static LRESULT CALLBACK fn_subclass_wndproc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) { switch (msg) { case WM_ENTERSIZEMOVE: g_in_sizemove.store(true, std::memory_order_release); g_sizemove_enter_count.fetch_add(1, std::memory_order_acq_rel); break; case WM_EXITSIZEMOVE: g_in_sizemove.store(false, std::memory_order_release); break; case WM_LBUTTONDOWN: // Alt + LMB anywhere on the window initiates a native modal MOVE // via WM_SYSCOMMAND, SC_MOVE | HTCAPTION. Same pattern as our // Alt+RMB resize: ReleaseCapture, post the syscommand, return 0 // to consume the click. Windows then drives a normal move modal // (DefWindowProc blocks the thread) and our existing // WM_ENTERSIZEMOVE gate pauses render so there's no jitter. { bool alt_real = (GetAsyncKeyState(VK_MENU) & 0x8000) != 0; bool alt_test = g_force_alt_for_test.load(std::memory_order_acquire); if (alt_real || alt_test) { g_alt_lmb_move_count.fetch_add(1, std::memory_order_acq_rel); if (alt_test) { // Test mode: skip SC_MOVE post to keep the harness // out of Windows' modal move loop. return 0; } ReleaseCapture(); PostMessageW(hwnd, WM_SYSCOMMAND, (WPARAM)(0xF010 /* SC_MOVE */ | 2 /* HTCAPTION */), 0); return 0; } } break; case WM_RBUTTONDOWN: g_rbuttondown_seen_count.fetch_add(1, std::memory_order_acq_rel); // Alt + RMB anywhere on the window initiates a native modal // resize. Direction is chosen by cursor quadrant relative to // window center so dragging "feels" like grabbing the nearest // corner. ReleaseCapture is required before WM_SYSCOMMAND // SC_SIZE so the modal loop can take input. The subsequent // WM_ENTERSIZEMOVE bracket is observed above, so render is // gated for free and no jitter appears. { bool alt_real = (GetAsyncKeyState(VK_MENU) & 0x8000) != 0; bool alt_test = g_force_alt_for_test.load(std::memory_order_acquire); if (alt_real || alt_test) { int cx = GET_X_LPARAM(lp); int cy = GET_Y_LPARAM(lp); int dir = alt_rmb_resize_direction(hwnd, cx, cy); g_alt_rmb_resize_count.fetch_add(1, std::memory_order_acq_rel); if (alt_test) { // Test mode: skip the SC_SIZE post to keep the modal // out of the headless test harness. The counter is // sufficient to verify the path was taken; the modal // entry is exercised in the real-input manual test. return 0; } ReleaseCapture(); PostMessageW(hwnd, WM_SYSCOMMAND, (WPARAM)(0xF000 /* SC_SIZE */ | dir), 0); return 0; // consume so ImGui doesn't see a right-click } } break; default: break; } auto it = g_subclassed.find(hwnd); WNDPROC orig = (it != g_subclassed.end()) ? it->second : nullptr; if (orig) return CallWindowProcW(orig, hwnd, msg, wp, lp); return DefWindowProcW(hwnd, msg, wp, lp); } static void install_sizemove_subclass_hwnd(HWND hwnd) { if (!hwnd) return; if (g_subclassed.find(hwnd) != g_subclassed.end()) return; // idempotent WNDPROC orig = (WNDPROC)SetWindowLongPtrW( hwnd, GWLP_WNDPROC, (LONG_PTR)fn_subclass_wndproc); g_subclassed[hwnd] = orig; } static void install_sizemove_subclass(GLFWwindow* w) { if (!w) return; install_sizemove_subclass_hwnd(glfwGetWin32Window(w)); } // Reap stale entries: when a secondary viewport is destroyed (panel re-docked // back into main), the GLFW backend calls glfwDestroyWindow and the HWND is // invalidated. Drop those entries so we don't hold dangling pointers and so a // fresh HWND at the same address gets re-subclassed cleanly. static void prune_dead_subclassed() { for (auto it = g_subclassed.begin(); it != g_subclassed.end();) { if (!IsWindow(it->first)) it = g_subclassed.erase(it); else ++it; } } static void uninstall_sizemove_subclass_all() { for (auto& kv : g_subclassed) { if (IsWindow(kv.first) && kv.second) { SetWindowLongPtrW(kv.first, GWLP_WNDPROC, (LONG_PTR)kv.second); } } g_subclassed.clear(); } static inline bool external_sizemove_active() { return g_in_sizemove.load(std::memory_order_acquire); } #else static inline bool external_sizemove_active() { return false; } #endif namespace fn { // ============================================================================ // Local files // ============================================================================ namespace { std::string compute_exe_dir() { #ifdef _WIN32 wchar_t buf[MAX_PATH * 2]; DWORD n = GetModuleFileNameW(nullptr, buf, (DWORD)(sizeof(buf) / sizeof(buf[0]))); if (n == 0 || n >= sizeof(buf)/sizeof(buf[0])) return ""; int u8n = WideCharToMultiByte(CP_UTF8, 0, buf, (int)n, nullptr, 0, nullptr, nullptr); std::string out(u8n, 0); WideCharToMultiByte(CP_UTF8, 0, buf, (int)n, out.data(), u8n, nullptr, nullptr); size_t slash = out.find_last_of("/\\"); return (slash == std::string::npos) ? "" : out.substr(0, slash); #else char buf[4096]; ssize_t n = readlink("/proc/self/exe", buf, sizeof(buf) - 1); if (n <= 0) return ""; buf[n] = 0; std::string out(buf); size_t slash = out.find_last_of('/'); return (slash == std::string::npos) ? "" : out.substr(0, slash); #endif } const std::string& exe_dir_ref() { static std::string cached = compute_exe_dir(); return cached; } const std::string& local_dir_ref() { static std::string cached; static bool inited = false; if (inited) return cached; const std::string& edir = exe_dir_ref(); if (edir.empty()) { cached = "local_files"; // fallback: relativo al cwd } else { cached = edir + "/local_files"; } std::error_code ec; std::filesystem::create_directories(cached, ec); inited = true; return cached; } } // namespace const char* exe_dir() { return exe_dir_ref().c_str(); } const char* local_dir() { return local_dir_ref().c_str(); } const char* local_path(const char* name) { static thread_local std::string buf; buf = local_dir_ref(); if (name && *name) { if (!buf.empty() && buf.back() != '/' && buf.back() != '\\') buf += '/'; buf += name; } return buf.c_str(); } namespace { const std::string& asset_dir_ref() { static std::string cached; static bool inited = false; if (inited) return cached; const std::string& edir = exe_dir_ref(); cached = edir.empty() ? std::string("assets") : edir + "/assets"; inited = true; return cached; } } // namespace const char* asset_dir() { return asset_dir_ref().c_str(); } const char* asset_path(const char* name) { static thread_local std::string buf; buf = asset_dir_ref(); if (name && *name) { if (!buf.empty() && buf.back() != '/' && buf.back() != '\\') buf += '/'; buf += name; } return buf.c_str(); } void migrate_to_local_files(const char* const* names, std::size_t n) { if (!names || n == 0) return; const std::string& ldir = local_dir_ref(); const std::string& edir = exe_dir_ref(); for (std::size_t i = 0; i < n; ++i) { const char* name = names[i]; if (!name || !*name) continue; std::string dst = ldir + "/" + name; struct stat st{}; if (::stat(dst.c_str(), &st) == 0) continue; // ya existe en local // Buscar en exe_dir y en cwd. Mover el primero que aparezca. std::string cands[] = { edir.empty() ? std::string() : (edir + "/" + name), std::string(name), }; for (const auto& src : cands) { if (src.empty() || src == dst) continue; if (::stat(src.c_str(), &st) != 0) continue; std::error_code ec; std::filesystem::rename(src, dst, ec); if (ec) { // Cross-device o permisos — fallback a copy + remove. std::filesystem::copy(src, dst, std::filesystem::copy_options::recursive | std::filesystem::copy_options::overwrite_existing, ec); if (!ec) std::filesystem::remove_all(src, ec); } std::fprintf(stdout, "[local_files] migrado: %s -> %s\n", src.c_str(), dst.c_str()); break; } } } int run_app(AppConfig config, std::function render_fn) { // Logger primero para capturar fallos del propio init (GLFW, ventana, GL). if (config.log.file_path != nullptr) { fn_log::logger_init( config.log.file_path, static_cast(config.log.level)); fn_log::log_info("app start: %s", config.title ? config.title : "(no title)"); } glfwSetErrorCallback(glfw_error_callback); if (!glfwInit()) { fprintf(stderr, "Failed to initialize GLFW\n"); fn_log::log_error("GLFW init failed"); if (config.log.file_path != nullptr) fn_log::logger_close(); return 1; } // OpenGL 4.3 core (issue 0049b) — habilita compute shaders, SSBOs, image // load/store, atomic counters y debug output. Backward-compatible con // shaders #version 330 y con todo lo escrito para 3.3 core. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); GLFWwindow* window = glfwCreateWindow(config.width, config.height, config.title, nullptr, nullptr); if (!window) { fprintf(stderr, "Failed to create GLFW window\n"); fn_log::log_error("GLFW createWindow failed (%dx%d)", config.width, config.height); if (config.log.file_path != nullptr) fn_log::logger_close(); glfwTerminate(); return 1; } glfwMakeContextCurrent(window); glfwSwapInterval(config.vsync ? 1 : 0); // Anti-jitter: when the OS moves/resizes the window externally (Windows // tools like AltSnap, tiling WMs, snap-assist), ImGui's viewport pos can // lag one frame and `UpdatePlatformWindows` reapplies the stale value via // glfwSetWindowPos, fighting the OS and producing visible jitter. // Updating the viewport struct directly from the GLFW callback closes the // loop in the same tick — no stale Pos can ever reach the platform sync. // ImGui_ImplGlfw_InitForOpenGL does NOT install pos/size callbacks, so we // can install ours without breaking the backend's own callback chain. glfwSetWindowPosCallback(window, [](GLFWwindow* w, int x, int y) { if (ImGui::GetCurrentContext() == nullptr) return; if (ImGuiViewport* vp = ImGui::FindViewportByPlatformHandle(w)) { vp->Pos = ImVec2((float)x, (float)y); } }); glfwSetWindowSizeCallback(window, [](GLFWwindow* w, int cx, int cy) { if (ImGui::GetCurrentContext() == nullptr) return; if (ImGuiViewport* vp = ImGui::FindViewportByPlatformHandle(w)) { vp->Size = ImVec2((float)cx, (float)cy); } }); #ifdef _WIN32 // Install Win32 WndProc subclass to detect WM_ENTERSIZEMOVE / WM_EXITSIZEMOVE. // External movers (AltSnap) fake these brackets without blocking the app // thread; we observe them and skip render+swap so the compositor moves // the existing buffer (same contract as native title-bar drag). install_sizemove_subclass(window); #endif // Carga punteros a funciones GL >= 2.0 si la app lo pide. En Linux es // no-op; en Windows usa wglGetProcAddress (requiere ctx GL activo). if (config.init_gl_loader) { if (!fn::gfx::gl_loader_init()) { fprintf(stderr, "Failed to initialize GL function loader\n"); fn_log::log_error("gl_loader_init failed"); if (config.log.file_path != nullptr) fn_log::logger_close(); glfwDestroyWindow(window); glfwTerminate(); return 1; } } // Setup ImGui IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImPlot::CreateContext(); ImPlot3D::CreateContext(); ImGuiIO& io = ImGui::GetIO(); io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Title-bar-only move for ImGui windows. Critical for secondary viewports // (floating panels) whose entire OS window is a single borderless ImGui // window: without this flag, ImGui moves the window when the user drags // any empty client-area pixel, which translates to the OS viewport // following the mouse "from anywhere" with no modifier. With this flag, // floating panels obey the same "header only" contract as a native // decorated window. Alt+LMB anywhere still moves via our WndProc subclass // (consumed before ImGui sees the click). io.ConfigWindowsMoveFromTitleBarOnly = true; // Convencion local_files: imgui.ini y app_settings.ini viven en // /local_files/. Migra automaticamente desde el cwd o // exe_dir si vienen de una version previa. { static const char* legacy_names[] = {"imgui.ini", "app_settings.ini"}; migrate_to_local_files(legacy_names, sizeof(legacy_names) / sizeof(legacy_names[0])); } static std::string s_imgui_ini = local_path("imgui.ini"); io.IniFilename = s_imgui_ini.c_str(); // 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(); // Auto-wiring del menu Layouts: si la app no proporciono layouts_cb y no // ha desactivado auto_layouts, abrimos un LayoutStorage por defecto con // SQLite en `/` y generamos los callbacks // estandar (list/save/apply/delete/reset). Asi toda app C++ obtiene el // menu Layouts gratis sin codigo. fn_ui::LayoutStorage* auto_layouts_storage = nullptr; fn_ui::LayoutCallbacks auto_layouts_cb; if (config.layouts_cb == nullptr && config.auto_layouts) { const char* db_name = (config.auto_layouts_db && *config.auto_layouts_db) ? config.auto_layouts_db : "layouts.db"; auto_layouts_storage = fn_ui::layout_storage_open(local_path(db_name)); if (auto_layouts_storage) { fn_ui::layout_storage_make_callbacks(auto_layouts_storage, auto_layouts_cb); config.layouts_cb = &auto_layouts_cb; // Restore-on-open: si hay un layout activo persistido, lo dejamos // pendiente para que el primer frame del main loop lo aplique via // layout_storage_apply_pending. Asi la app abre con el ultimo // layout que el usuario tenia activo. active_name se setea ya // optimista para reflejarlo en el menu desde el primer frame. std::string last = fn_ui::layout_storage_get_last_active(auto_layouts_storage); if (!last.empty() && fn_ui::layout_storage_apply(auto_layouts_storage, last)) { auto_layouts_cb.active_name = last; fn_log::log_info("auto_layouts: restaurado layout '%s'", last.c_str()); } } else { fn_log::log_warn("auto_layouts: layout_storage_open fallo (%s)", db_name); } } // Registra info de la ventana About si la app la proveyo en AppConfig. if (config.about.name != nullptr) { fn_ui::about_window_set_info( config.about.name, config.about.version ? config.about.version : "", config.about.description ? config.about.description : ""); } // 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(); // When the main window is iconified we used to glfwWaitEvents+continue // to save CPU. That is wrong when floating panels (secondary // viewports) exist: skipping the frame stops UpdatePlatformWindows / // RenderPlatformWindowsDefault so those panels go blank or get // ungrouped by the WM. We therefore detect secondary viewports first // and, if any are alive, fall through to a normal frame (main GL // clear/swap is harmless on the hidden main HWND, secondary GL // contexts keep refreshing). Only when there are NO floating panels // do we sleep on glfwWaitEvents the way we used to. if (glfwGetWindowAttrib(window, GLFW_ICONIFIED)) { bool has_secondary_viewport = false; if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) { ImGuiPlatformIO& pio_ic = ImGui::GetPlatformIO(); for (int i = 0; i < pio_ic.Viewports.Size; ++i) { ImGuiViewport* vp = pio_ic.Viewports[i]; if (!vp || !vp->PlatformHandle) continue; if ((GLFWwindow*)vp->PlatformHandle == window) continue; has_secondary_viewport = true; break; } } if (!has_secondary_viewport) { glfwWaitEvents(); continue; } // fallthrough: render normally so floating panels stay alive. } #ifdef _WIN32 // Subclass any platform window we haven't subclassed yet. Covers the // main window AND every secondary viewport (panels dragged outside // main) so AltSnap's WM_ENTERSIZEMOVE/WM_EXITSIZEMOVE brackets are // observed regardless of which HWND it targets. Runs BEFORE the // sizemove gate below so newly-created secondaries are protected from // their very first frame onwards. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) { prune_dead_subclassed(); ImGuiPlatformIO& pio_sub = ImGui::GetPlatformIO(); for (int i = 0; i < pio_sub.Viewports.Size; ++i) { ImGuiViewport* vp = pio_sub.Viewports[i]; if (!vp || !vp->PlatformHandle) continue; install_sizemove_subclass((GLFWwindow*)vp->PlatformHandle); } } #endif // While an external mover (AltSnap on Win32, tiling WMs) is dragging // the window we mirror the native title-bar contract: do not render, // do not swap, just pump events. The DWM compositor scrolls the last // presented framebuffer with the window — no race between SetWindowPos // (async) and glfwSwapBuffers, so no jitter. WM_EXITSIZEMOVE clears // the flag and the main loop resumes normal rendering. Applies to // brackets on ANY subclassed HWND (main or secondary viewports). if (external_sizemove_active()) { // Bound the busy loop so the message queue gets drained but we // don't burn CPU when AltSnap pauses between mouse moves. glfwWaitEventsTimeout(0.016); continue; } // Anti-jitter pass 2: covers secondary viewport windows that the // backend creates dynamically (panels dragged outside the main). // Sync each viewport's Pos/Size to the OS-reported state BEFORE // NewFrame, so ImGui logic this tick already sees the up-to-date // values and UpdatePlatformWindows can't stomp them with stale data. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) { ImGuiPlatformIO& pio = ImGui::GetPlatformIO(); for (int i = 0; i < pio.Viewports.Size; ++i) { ImGuiViewport* vp = pio.Viewports[i]; if (!vp || !vp->PlatformHandle) continue; GLFWwindow* gw = (GLFWwindow*)vp->PlatformHandle; int x = 0, y = 0, cx = 0, cy = 0; glfwGetWindowPos(gw, &x, &y); glfwGetWindowSize(gw, &cx, &cy); vp->Pos = ImVec2((float)x, (float)y); vp->Size = ImVec2((float)cx, (float)cy); } } // 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(); // Si auto_layouts esta gestionando el storage, aplica el layout // pendiente ANTES de que el render_fn cree ventanas. Si la app gestiona // su propio storage, debe usar cfg.pre_frame para llamar // layout_storage_apply_pending en el mismo punto. if (auto_layouts_storage) { std::string applied = fn_ui::layout_storage_apply_pending(auto_layouts_storage); if (!applied.empty()) auto_layouts_cb.active_name = applied; } // Hook pre-frame de la app — se ejecuta despues de NewFrame y antes // de menubar/auto-dockspace. Punto correcto para LoadIniSettingsFromMemory. if (config.pre_frame) { config.pre_frame(); } // Menubar canonica (View / Layouts / Settings / About) — siempre se // renderiza para que Settings/Logs/About esten disponibles aunque la // app no declare panels/layouts/view_extras propios. Se dibuja ANTES // del render_fn para que pueda hacer DockSpaceOverViewport debajo. { // Adapter: std::function -> ViewMenuExtrasFn(void*). fn_ui::ViewMenuExtrasFn extras_fn = nullptr; void* extras_user = nullptr; if ((bool)config.view_extras) { extras_fn = [](void* ud) -> bool { auto* fn_ptr = static_cast*>(ud); return (*fn_ptr) ? (*fn_ptr)() : false; }; extras_user = (void*)&config.view_extras; } fn_ui::app_menubar(config.panels, config.panel_count, config.layouts_cb, extras_fn, extras_user); } // Auto-dockspace central. Permite re-anclar ventanas flotantes al // main viewport sin que cada app llame DockSpaceOverViewport en su // render(). Apps con layout custom ponen cfg.auto_dockspace=false. if (config.auto_dockspace) { ImGui::DockSpaceOverViewport(0, ImGui::GetMainViewport(), ImGuiDockNodeFlags_PassthruCentralNode); } render_fn(); // Ventana de Settings (no-op si esta cerrada). fn_ui::settings_window_render(); // Ventana de Logs (no-op si esta cerrada). fn_ui::log_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(); // Cierra el archivo de log (si la app lo abrio). if (config.log.file_path != nullptr) { fn_log::log_info("app exit"); fn_log::logger_close(); } // Save-on-close: si hay un layout activo, persiste el INI actual en su // slot para que la proxima apertura cargue exactamente el mismo estado // (incluye los retoques de docking/posiciones que el usuario hizo // durante la sesion). Tambien reescribe last_active por si el callback // se salto. Hecho ANTES de cerrar el storage. Necesita ImGui context // vivo (SaveIniSettingsToMemory), por eso va antes de DestroyContext. if (auto_layouts_storage && !auto_layouts_cb.active_name.empty()) { fn_ui::layout_storage_save(auto_layouts_storage, auto_layouts_cb.active_name); fn_ui::layout_storage_set_last_active(auto_layouts_storage, auto_layouts_cb.active_name); } // Cierra el storage de layouts auto-creado, si lo hay. if (auto_layouts_storage) { fn_ui::layout_storage_close(auto_layouts_storage); auto_layouts_storage = nullptr; } // Cleanup ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImPlot3D::DestroyContext(); ImPlot::DestroyContext(); ImGui::DestroyContext(); #ifdef _WIN32 uninstall_sizemove_subclass_all(); #endif glfwDestroyWindow(window); glfwTerminate(); return 0; } int run_app(std::function render_fn) { return run_app(AppConfig{}, render_fn); } // Test-only observability of the Win32 subclass. Always defined (zero cost); // on non-Windows the counters never increment. namespace internal { int sizemove_enter_count() { #ifdef _WIN32 return g_sizemove_enter_count.load(std::memory_order_acquire); #else return 0; #endif } int alt_rmb_resize_count() { #ifdef _WIN32 return g_alt_rmb_resize_count.load(std::memory_order_acquire); #else return 0; #endif } void set_force_alt_for_test(bool v) { #ifdef _WIN32 g_force_alt_for_test.store(v, std::memory_order_release); #else (void)v; #endif } int rbuttondown_seen_count() { #ifdef _WIN32 return g_rbuttondown_seen_count.load(std::memory_order_acquire); #else return 0; #endif } int alt_lmb_move_count() { #ifdef _WIN32 return g_alt_lmb_move_count.load(std::memory_order_acquire); #else return 0; #endif } } // namespace internal } // namespace fn #ifdef IMGUI_ENABLE_TEST_ENGINE #include "imgui_te_engine.h" #include "imgui_te_ui.h" #include "imgui_te_context.h" #include "imgui_te_exporters.h" namespace fn { int run_app_test(AppConfig config, std::function render_fn, std::function register_tests, const char* filter) { if (!register_tests) { fprintf(stderr, "run_app_test: register_tests callback is null\n"); return 1; } glfwSetErrorCallback(glfw_error_callback); if (!glfwInit()) { fprintf(stderr, "GLFW init failed\n"); return 1; } glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); GLFWwindow* window = glfwCreateWindow( config.width, config.height, config.title ? config.title : "fn_test", nullptr, nullptr); if (!window) { glfwTerminate(); fprintf(stderr, "createWindow failed\n"); return 1; } glfwMakeContextCurrent(window); glfwSwapInterval(0); // tests run as fast as possible — no vsync if (config.init_gl_loader) { if (!fn::gfx::gl_loader_init()) { glfwDestroyWindow(window); glfwTerminate(); fprintf(stderr, "gl_loader_init failed\n"); return 1; } } IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImPlot::CreateContext(); ImPlot3D::CreateContext(); ImGuiIO& io = ImGui::GetIO(); io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // No viewports in tests — the engine drives the main window only. io.IniFilename = nullptr; // tests don't persist .ini fn_ui::settings_load(); fn_ui::load_fonts_from_settings(); 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; } ImGui_ImplGlfw_InitForOpenGL(window, true); ImGui_ImplOpenGL3_Init("#version 330"); // --- Test engine setup --- ImGuiTestEngine* engine = ImGuiTestEngine_CreateContext(); ImGuiTestEngineIO& te_io = ImGuiTestEngine_GetIO(engine); te_io.ConfigVerboseLevel = ImGuiTestVerboseLevel_Info; te_io.ConfigVerboseLevelOnError = ImGuiTestVerboseLevel_Debug; te_io.ConfigRunSpeed = ImGuiTestRunSpeed_Fast; te_io.ConfigStopOnError = false; te_io.ConfigCaptureEnabled = false; te_io.ConfigSavedSettings = false; register_tests(engine); ImGuiTestEngine_Start(engine, ImGui::GetCurrentContext()); ImGuiTestEngine_QueueTests(engine, ImGuiTestGroup_Tests, filter, ImGuiTestRunFlags_RunFromCommandLine); // --- Loop until tests finish --- bool tests_queued_done = false; int frames_after_done = 0; while (!glfwWindowShouldClose(window)) { glfwPollEvents(); ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); if (config.pre_frame) config.pre_frame(); render_fn(); 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()); glfwSwapBuffers(window); if (!tests_queued_done && ImGuiTestEngine_IsTestQueueEmpty(engine)) { tests_queued_done = true; } if (tests_queued_done) { // Let the engine flush its final state for a few frames before exit. if (++frames_after_done > 2) break; } } int count_tested = 0, count_success = 0; ImGuiTestEngine_GetResult(engine, count_tested, count_success); bool all_passed = (count_tested > 0) && (count_tested == count_success); ImGuiTestEngine_PrintResultSummary(engine); ImGuiTestEngine_Stop(engine); ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImPlot3D::DestroyContext(); ImPlot::DestroyContext(); ImGui::DestroyContext(); ImGuiTestEngine_DestroyContext(engine); glfwDestroyWindow(window); glfwTerminate(); fprintf(stdout, "\n[fn::run_app_test] %d/%d tests passed%s\n", count_success, count_tested, all_passed ? "" : " — FAILED"); return all_passed ? 0 : 1; } } // namespace fn #endif // IMGUI_ENABLE_TEST_ENGINE