visual picker: marching ants, DWM-aware overlay sizing

This commit is contained in:
2026-05-12 13:07:35 +03:00
parent 509017d56b
commit 5eb27ab779
5 changed files with 301 additions and 51 deletions
+132 -16
View File
@@ -798,6 +798,12 @@ fn open_window_picker(
}
}
}
tracing::info!(
"open_window_picker: monitor={}x{} exclude={:?}",
mw,
mh,
exclude
);
let picker = app
.get_webview_window("picker")
@@ -812,6 +818,28 @@ fn open_window_picker(
let _ = picker.set_ignore_cursor_events(true);
let _ = picker.set_always_on_top(true);
picker.show().map_err(|e| e.to_string())?;
if let Ok(h) = picker.hwnd() {
use windows::Win32::Foundation::{HWND, RECT};
use windows::Win32::Graphics::Dwm::{DwmGetWindowAttribute, DWMWA_EXTENDED_FRAME_BOUNDS};
use windows::Win32::UI::WindowsAndMessaging::GetWindowRect;
let hwnd = HWND(h.0 as *mut _);
let mut gr = RECT::default();
let _ = unsafe { GetWindowRect(hwnd, &mut gr) };
let mut dr = RECT::default();
let _ = unsafe {
DwmGetWindowAttribute(
hwnd,
DWMWA_EXTENDED_FRAME_BOUNDS,
&mut dr as *mut RECT as *mut std::ffi::c_void,
std::mem::size_of::<RECT>() as u32,
)
};
tracing::info!(
"picker window: GetWindowRect=({},{},{},{}) DwmBounds=({},{},{},{})",
gr.left, gr.top, gr.right, gr.bottom,
dr.left, dr.top, dr.right, dr.bottom
);
}
{
let mut slot = state.picker.lock().expect("picker mutex");
@@ -837,6 +865,36 @@ fn close_window_picker(
Ok(())
}
fn dwm_insets(hwnd_raw: isize) -> (i32, i32, i32, i32) {
use windows::Win32::Foundation::{HWND, RECT};
use windows::Win32::Graphics::Dwm::{DwmGetWindowAttribute, DWMWA_EXTENDED_FRAME_BOUNDS};
use windows::Win32::UI::WindowsAndMessaging::GetWindowRect;
let hwnd = HWND(hwnd_raw as *mut _);
let mut outer = RECT::default();
if unsafe { GetWindowRect(hwnd, &mut outer) }.is_err() {
return (0, 0, 0, 0);
}
let mut dwm = RECT::default();
let r = unsafe {
DwmGetWindowAttribute(
hwnd,
DWMWA_EXTENDED_FRAME_BOUNDS,
&mut dwm as *mut RECT as *mut std::ffi::c_void,
std::mem::size_of::<RECT>() as u32,
)
};
if r.is_err() || dwm.right <= dwm.left || dwm.bottom <= dwm.top {
return (0, 0, 0, 0);
}
(
dwm.left - outer.left,
dwm.top - outer.top,
outer.right - dwm.right,
outer.bottom - dwm.bottom,
)
}
#[tauri::command]
fn set_overlay_to_window(
app: AppHandle,
@@ -845,7 +903,6 @@ fn set_overlay_to_window(
width: u32,
height: u32,
) -> Result<(), String> {
use tauri::{PhysicalPosition, PhysicalSize, Position, Size};
let win = app
.get_webview_window("overlay")
.ok_or_else(|| "overlay window not found".to_string())?;
@@ -853,41 +910,100 @@ fn set_overlay_to_window(
let border_phys = (OVERLAY_BORDER_LOGICAL_PX * scale).round() as i32;
let titlebar_phys = (OVERLAY_TITLEBAR_LOGICAL_PX * scale).round() as i32;
let outer_x = x - border_phys;
let outer_y = y - border_phys - titlebar_phys;
let outer_w = (width as i32 + border_phys * 2).max(2) as u32;
let outer_h = (height as i32 + border_phys * 2 + titlebar_phys).max(2) as u32;
// Desired visible bounds of the overlay window (the green frame).
// The capture region is inside the border + below the titlebar.
let visible_left = x - border_phys;
let visible_top = y - border_phys - titlebar_phys;
let visible_w = width as i32 + border_phys * 2;
let visible_h = height as i32 + border_phys * 2 + titlebar_phys;
win.set_position(Position::Physical(PhysicalPosition::new(outer_x, outer_y)))
.map_err(|e| e.to_string())?;
win.set_size(Size::Physical(PhysicalSize::new(outer_w, outer_h)))
.map_err(|e| e.to_string())
// Compensate for the invisible DWM border that Windows adds around
// top-level windows (Tauri's set_position sets the outer rect, but the
// visible content sits inside the DWM inset).
let (li, ti, ri, bi) = win
.hwnd()
.map(|h| dwm_insets(h.0 as isize))
.unwrap_or((0, 0, 0, 0));
let outer_x = visible_left - li;
let outer_y = visible_top - ti;
let outer_w = (visible_w + li + ri).max(2) as u32;
let outer_h = (visible_h + ti + bi).max(2) as u32;
// Bypass Tauri's set_position/set_size which adds extra pixels for
// some unknown reason. Use Win32 SetWindowPos with explicit outer rect.
let raw = win.hwnd().map_err(|e| format!("hwnd: {e}"))?;
use windows::Win32::Foundation::HWND;
use windows::Win32::UI::WindowsAndMessaging::{
SetWindowPos, HWND_TOP, SWP_NOACTIVATE, SWP_NOZORDER,
};
let hwnd = HWND(raw.0 as *mut _);
unsafe {
SetWindowPos(
hwnd,
Some(HWND_TOP),
outer_x,
outer_y,
outer_w as i32,
outer_h as i32,
SWP_NOACTIVATE | SWP_NOZORDER,
)
}
.map_err(|e| format!("SetWindowPos: {e}"))?;
let _ = (visible_left, visible_top, visible_w, visible_h, scale, border_phys, titlebar_phys);
Ok(())
}
#[tauri::command]
fn get_overlay_rect(app: AppHandle) -> Result<CaptureRect, String> {
use windows::Win32::Foundation::{HWND, RECT};
use windows::Win32::Graphics::Dwm::{DwmGetWindowAttribute, DWMWA_EXTENDED_FRAME_BOUNDS};
use windows::Win32::UI::WindowsAndMessaging::GetWindowRect;
let win = app
.get_webview_window("overlay")
.ok_or_else(|| "overlay window not found".to_string())?;
let pos = win.outer_position().map_err(|e| e.to_string())?;
let size = win.outer_size().map_err(|e| e.to_string())?;
let scale = win.scale_factor().map_err(|e| e.to_string())?;
let raw = win.hwnd().map_err(|e| format!("hwnd: {e}"))?;
let hwnd = HWND(raw.0 as *mut _);
let mut dwm = RECT::default();
let dwm_ok = unsafe {
DwmGetWindowAttribute(
hwnd,
DWMWA_EXTENDED_FRAME_BOUNDS,
&mut dwm as *mut RECT as *mut std::ffi::c_void,
std::mem::size_of::<RECT>() as u32,
)
}
.is_ok();
let visible = if dwm_ok && dwm.right > dwm.left && dwm.bottom > dwm.top {
dwm
} else {
let mut r = RECT::default();
unsafe { GetWindowRect(hwnd, &mut r) }
.map_err(|e| format!("GetWindowRect: {e}"))?;
r
};
let border_phys = (OVERLAY_BORDER_LOGICAL_PX * scale).round() as i32;
let titlebar_phys = (OVERLAY_TITLEBAR_LOGICAL_PX * scale).round() as i32;
let inset_top = border_phys + titlebar_phys;
let inset_sides = border_phys;
let inset_bottom = border_phys;
let width = (size.width as i32 - inset_sides * 2).max(2) as u32;
let height = (size.height as i32 - inset_top - inset_bottom).max(2) as u32;
let visible_w = visible.right - visible.left;
let visible_h = visible.bottom - visible.top;
let width = (visible_w - inset_sides * 2).max(2) as u32;
let height = (visible_h - inset_top - inset_sides).max(2) as u32;
let width = (width / 2) * 2;
let height = (height / 2) * 2;
Ok(CaptureRect {
x: pos.x + inset_sides,
y: pos.y + inset_top,
x: visible.left + inset_sides,
y: visible.top + inset_top,
width,
height,
})