feat(infra): auto-commit con 11 cambios

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-10 13:30:27 +02:00
parent b8dd7ea018
commit 401d8523b4
10 changed files with 496 additions and 12 deletions
+103
View File
@@ -17,6 +17,7 @@
#include "gfx/gl_loader.h"
#include <GLFW/glfw3.h>
#include <atomic>
#include <cstdio>
#include <cstring>
#include <filesystem>
@@ -28,6 +29,8 @@
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#define GLFW_EXPOSE_NATIVE_WIN32
#include <GLFW/glfw3native.h>
#else
#include <unistd.h>
#endif
@@ -40,6 +43,60 @@ 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.
static std::atomic<bool> g_in_sizemove{false};
static WNDPROC g_orig_wndproc = nullptr;
static HWND g_subclassed_hwnd = nullptr;
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);
break;
case WM_EXITSIZEMOVE:
g_in_sizemove.store(false, std::memory_order_release);
break;
default: break;
}
return CallWindowProcW(g_orig_wndproc, hwnd, msg, wp, lp);
}
static void install_sizemove_subclass(GLFWwindow* w) {
HWND hwnd = glfwGetWin32Window(w);
if (!hwnd) return;
g_subclassed_hwnd = hwnd;
g_orig_wndproc = (WNDPROC)SetWindowLongPtrW(
hwnd, GWLP_WNDPROC, (LONG_PTR)fn_subclass_wndproc);
}
static void uninstall_sizemove_subclass() {
if (g_subclassed_hwnd && g_orig_wndproc) {
SetWindowLongPtrW(g_subclassed_hwnd, GWLP_WNDPROC, (LONG_PTR)g_orig_wndproc);
}
g_subclassed_hwnd = nullptr;
g_orig_wndproc = nullptr;
}
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 {
// ============================================================================
@@ -225,6 +282,14 @@ int run_app(AppConfig config, std::function<void()> render_fn) {
}
});
#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) {
@@ -277,6 +342,17 @@ int run_app(AppConfig config, std::function<void()> render_fn) {
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);
}
@@ -335,6 +411,19 @@ int run_app(AppConfig config, std::function<void()> render_fn) {
continue;
}
// 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.
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
@@ -449,6 +538,17 @@ int run_app(AppConfig config, std::function<void()> render_fn) {
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);
@@ -461,6 +561,9 @@ int run_app(AppConfig config, std::function<void()> render_fn) {
ImPlot3D::DestroyContext();
ImPlot::DestroyContext();
ImGui::DestroyContext();
#ifdef _WIN32
uninstall_sizemove_subclass();
#endif
glfwDestroyWindow(window);
glfwTerminate();