enforce aspect ratio via Win32 SetWindowPos from Rust instead of Tauri JS setSize which wasn't working

This commit is contained in:
2026-05-27 21:46:52 +03:00
parent 1c89bb6b0f
commit 07521b4a8b
2 changed files with 40 additions and 15 deletions
+35
View File
@@ -951,6 +951,40 @@ fn get_overlay_aspect(state: State<'_, AppState>) -> Option<f64> {
*lock(&state.overlay_aspect_ratio) *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] #[tauri::command]
fn list_windows() -> Vec<windows_list::WindowInfo> { fn list_windows() -> Vec<windows_list::WindowInfo> {
windows_list::list_visible_windows() windows_list::list_visible_windows()
@@ -1507,6 +1541,7 @@ pub fn run() {
set_overlay_size, set_overlay_size,
set_overlay_aspect, set_overlay_aspect,
get_overlay_aspect, get_overlay_aspect,
enforce_overlay_aspect,
set_overlay_to_window, set_overlay_to_window,
list_windows, list_windows,
open_window_picker, open_window_picker,
+3 -13
View File
@@ -35,23 +35,13 @@
win.onResized(async () => { win.onResized(async () => {
if (enforcing) return if (enforcing) return
await updateSize() await updateSize()
// Re-sync ratio in case it changed // Let Rust handle ratio enforcement via Win32 API
await syncRatio() enforcing = true
if (lockedRatio === null) return
if (resizeDebounce !== null) clearTimeout(resizeDebounce) if (resizeDebounce !== null) clearTimeout(resizeDebounce)
resizeDebounce = window.setTimeout(async () => { resizeDebounce = window.setTimeout(async () => {
const ratio = lockedRatio! try { await invoke("enforce_overlay_aspect") } catch {}
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() await updateSize()
} catch {}
enforcing = false enforcing = false
}
}, 80) }, 80)
}).then(fn => cleanup.push(fn)) }).then(fn => cleanup.push(fn))