enforce aspect ratio via Win32 SetWindowPos from Rust instead of Tauri JS setSize which wasn't working
This commit is contained in:
@@ -951,6 +951,40 @@ fn get_overlay_aspect(state: State<'_, AppState>) -> Option<f64> {
|
||||
*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::WindowInfo> {
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user