window-edge snap during drag via WM_MOVING subclass
This commit is contained in:
@@ -18,7 +18,7 @@ tauri = { version = "2.11", features = ["protocol-asset"] }
|
|||||||
tauri-plugin-dialog = "2"
|
tauri-plugin-dialog = "2"
|
||||||
tauri-plugin-global-shortcut = "2"
|
tauri-plugin-global-shortcut = "2"
|
||||||
windows-capture = "2.0"
|
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"
|
cpal = "0.16"
|
||||||
tokio = { version = "1.47", features = ["full"] }
|
tokio = { version = "1.47", features = ["full"] }
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ mod capture;
|
|||||||
mod clicks;
|
mod clicks;
|
||||||
mod config;
|
mod config;
|
||||||
mod encode;
|
mod encode;
|
||||||
|
mod snap;
|
||||||
mod updates;
|
mod updates;
|
||||||
|
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
@@ -973,6 +974,11 @@ pub fn run() {
|
|||||||
(cfg.updates.check_on_launch, cfg)
|
(cfg.updates.check_on_launch, cfg)
|
||||||
};
|
};
|
||||||
register_hotkeys(app.handle(), &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 {
|
if check_on_launch {
|
||||||
let handle = app.handle().clone();
|
let handle = app.handle().clone();
|
||||||
tauri::async_runtime::spawn(async move {
|
tauri::async_runtime::spawn(async move {
|
||||||
|
|||||||
@@ -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<isize> = 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<RECT> = Vec::new();
|
||||||
|
let _ = EnumWindows(
|
||||||
|
Some(enum_cb),
|
||||||
|
LPARAM(&mut others as *mut Vec<RECT> as isize),
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut best_dx: Option<i32> = None;
|
||||||
|
let mut best_dy: Option<i32> = 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<RECT>);
|
||||||
|
list.push(rect);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
BOOL(1)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user