From 953263028bd0d2cb298851d017816a554b3065dc Mon Sep 17 00:00:00 2001 From: lashman Date: Tue, 12 May 2026 01:56:24 +0300 Subject: [PATCH] window-edge snap during drag via WM_MOVING subclass --- src-tauri/Cargo.toml | 2 +- src-tauri/src/lib.rs | 6 +++ src-tauri/src/snap.rs | 116 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 src-tauri/src/snap.rs diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 5f62867..77c1380 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -18,7 +18,7 @@ tauri = { version = "2.11", features = ["protocol-asset"] } tauri-plugin-dialog = "2" tauri-plugin-global-shortcut = "2" windows-capture = "2.0" -windows = { version = "0.62", features = ["Win32_UI_WindowsAndMessaging", "Win32_UI_Input_KeyboardAndMouse", "Win32_Foundation"] } +windows = { version = "0.62", features = ["Win32_UI_WindowsAndMessaging", "Win32_UI_Input_KeyboardAndMouse", "Win32_UI_Shell", "Win32_Foundation"] } cpal = "0.16" tokio = { version = "1.47", features = ["full"] } serde = { version = "1.0", features = ["derive"] } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 22c445d..be9ec5e 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -4,6 +4,7 @@ mod capture; mod clicks; mod config; mod encode; +mod snap; mod updates; use std::path::{Path, PathBuf}; @@ -973,6 +974,11 @@ pub fn run() { (cfg.updates.check_on_launch, cfg) }; register_hotkeys(app.handle(), &cfg); + if let Some(overlay) = app.get_webview_window("overlay") { + if let Ok(raw) = overlay.hwnd() { + snap::attach(raw.0 as isize); + } + } if check_on_launch { let handle = app.handle().clone(); tauri::async_runtime::spawn(async move { diff --git a/src-tauri/src/snap.rs b/src-tauri/src/snap.rs new file mode 100644 index 0000000..68e071b --- /dev/null +++ b/src-tauri/src/snap.rs @@ -0,0 +1,116 @@ +use std::sync::OnceLock; + +use windows::core::BOOL; +use windows::Win32::Foundation::{HWND, LPARAM, LRESULT, RECT, WPARAM}; +use windows::Win32::UI::Shell::{DefSubclassProc, SetWindowSubclass}; +use windows::Win32::UI::WindowsAndMessaging::{ + EnumWindows, GetWindowRect, IsIconic, IsWindowVisible, WM_MOVING, +}; + +const SUBCLASS_ID: usize = 0xBBC1; +const SNAP_THRESHOLD: i32 = 8; +const MIN_OTHER_DIM: i32 = 60; + +static OVERLAY_HWND: OnceLock = OnceLock::new(); + +pub fn attach(hwnd_raw: isize) { + let _ = OVERLAY_HWND.set(hwnd_raw); + let hwnd = HWND(hwnd_raw as *mut _); + unsafe { + let _ = SetWindowSubclass(hwnd, Some(subclass_proc), SUBCLASS_ID, 0); + } +} + +unsafe extern "system" fn subclass_proc( + hwnd: HWND, + msg: u32, + wparam: WPARAM, + lparam: LPARAM, + _id: usize, + _data: usize, +) -> LRESULT { + if msg == WM_MOVING { + let rect_ptr = lparam.0 as *mut RECT; + if !rect_ptr.is_null() { + apply_snap(rect_ptr); + return LRESULT(1); + } + } + DefSubclassProc(hwnd, msg, wparam, lparam) +} + +unsafe fn apply_snap(rect: *mut RECT) { + let r = *rect; + + let mut others: Vec = Vec::new(); + let _ = EnumWindows( + Some(enum_cb), + LPARAM(&mut others as *mut Vec as isize), + ); + + let mut best_dx: Option = None; + let mut best_dy: Option = None; + + for other in &others { + let candidates_x = [ + other.left - r.left, + other.right - r.left, + other.left - r.right, + other.right - r.right, + ]; + for dx in candidates_x { + if dx.abs() <= SNAP_THRESHOLD { + match best_dx { + None => best_dx = Some(dx), + Some(b) if dx.abs() < b.abs() => best_dx = Some(dx), + _ => {} + } + } + } + let candidates_y = [ + other.top - r.top, + other.bottom - r.top, + other.top - r.bottom, + other.bottom - r.bottom, + ]; + for dy in candidates_y { + if dy.abs() <= SNAP_THRESHOLD { + match best_dy { + None => best_dy = Some(dy), + Some(b) if dy.abs() < b.abs() => best_dy = Some(dy), + _ => {} + } + } + } + } + + if let Some(dx) = best_dx { + (*rect).left += dx; + (*rect).right += dx; + } + if let Some(dy) = best_dy { + (*rect).top += dy; + (*rect).bottom += dy; + } +} + +unsafe extern "system" fn enum_cb(hwnd: HWND, lparam: LPARAM) -> BOOL { + let overlay = OVERLAY_HWND.get().copied().unwrap_or(0); + if hwnd.0 as isize == overlay { + return BOOL(1); + } + if !IsWindowVisible(hwnd).as_bool() { + return BOOL(1); + } + if IsIconic(hwnd).as_bool() { + return BOOL(1); + } + let mut rect = RECT::default(); + if GetWindowRect(hwnd, &mut rect).is_ok() { + if (rect.right - rect.left) > MIN_OTHER_DIM && (rect.bottom - rect.top) > MIN_OTHER_DIM { + let list = &mut *(lparam.0 as *mut Vec); + list.push(rect); + } + } + BOOL(1) +}