From 07521b4a8b5707be5014258f3970660539f288cb Mon Sep 17 00:00:00 2001 From: lashman Date: Wed, 27 May 2026 21:46:52 +0300 Subject: [PATCH] enforce aspect ratio via Win32 SetWindowPos from Rust instead of Tauri JS setSize which wasn't working --- src-tauri/src/lib.rs | 35 +++++++++++++++++++++++++++++++++++ src/lib/Overlay.svelte | 20 +++++--------------- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 85fe75c..0b581dc 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -951,6 +951,40 @@ fn get_overlay_aspect(state: State<'_, AppState>) -> Option { *lock(&state.overlay_aspect_ratio) } +#[tauri::command] +fn enforce_overlay_aspect(app: AppHandle, state: State<'_, AppState>) -> Result<(), String> { + let ratio = match *lock(&state.overlay_aspect_ratio) { + Some(r) => r, + None => return Ok(()), + }; + let win = app + .get_webview_window("overlay") + .ok_or_else(|| "overlay window not found".to_string())?; + let scale = win.scale_factor().unwrap_or(1.0); + let sz = win.inner_size().map_err(|e| e.to_string())?; + let cur_w = sz.width as f64 / scale; + let cur_h = sz.height as f64 / scale; + let target_h = (cur_w / ratio).round(); + if (target_h - cur_h).abs() <= 2.0 { + return Ok(()); // already correct + } + // Use Win32 SetWindowPos for reliable enforcement + use windows::Win32::Foundation::HWND; + use windows::Win32::UI::WindowsAndMessaging::{SetWindowPos, HWND_TOP, SWP_NOACTIVATE, SWP_NOMOVE, SWP_NOZORDER}; + let raw = win.hwnd().map_err(|e| format!("hwnd: {e}"))?; + let hwnd = HWND(raw.0 as *mut _); + // Compute outer size including borders + let border = (OVERLAY_BORDER_LOGICAL_PX * scale).round() as i32; + let titlebar = (OVERLAY_TITLEBAR_LOGICAL_PX * scale).round() as i32; + let outer_w = (sz.width as i32).max(2); + let outer_h = (target_h * scale + titlebar as f64 + border as f64 * 2.0).round() as i32; + unsafe { + SetWindowPos(hwnd, Some(HWND_TOP), 0, 0, outer_w, outer_h, + SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOMOVE) + }.map_err(|e| format!("SetWindowPos: {e}"))?; + Ok(()) +} + #[tauri::command] fn list_windows() -> Vec { windows_list::list_visible_windows() @@ -1507,6 +1541,7 @@ pub fn run() { set_overlay_size, set_overlay_aspect, get_overlay_aspect, + enforce_overlay_aspect, set_overlay_to_window, list_windows, open_window_picker, diff --git a/src/lib/Overlay.svelte b/src/lib/Overlay.svelte index 7ac3224..9d6228b 100644 --- a/src/lib/Overlay.svelte +++ b/src/lib/Overlay.svelte @@ -35,23 +35,13 @@ win.onResized(async () => { if (enforcing) return await updateSize() - // Re-sync ratio in case it changed - await syncRatio() - if (lockedRatio === null) return + // Let Rust handle ratio enforcement via Win32 API + enforcing = true if (resizeDebounce !== null) clearTimeout(resizeDebounce) resizeDebounce = window.setTimeout(async () => { - const ratio = lockedRatio! - const targetH = Math.round(width / ratio) - const minH = 90 - const clampedH = Math.max(minH, targetH) - if (Math.abs(clampedH - height) > 2) { - enforcing = true - try { - await win.setSize({ type: "Logical", width, height: clampedH }) - await updateSize() - } catch {} - enforcing = false - } + try { await invoke("enforce_overlay_aspect") } catch {} + await updateSize() + enforcing = false }, 80) }).then(fn => cleanup.push(fn))