visual picker: marching ants, DWM-aware overlay sizing
This commit is contained in:
+131
-24
@@ -5,12 +5,34 @@ use std::time::Duration;
|
||||
|
||||
use serde::Serialize;
|
||||
use tauri::{AppHandle, Emitter};
|
||||
use windows::Win32::Foundation::{HWND, POINT, RECT};
|
||||
use windows::core::BOOL;
|
||||
use windows::Win32::Foundation::{HWND, LPARAM, POINT, RECT};
|
||||
use windows::Win32::UI::Input::KeyboardAndMouse::GetAsyncKeyState;
|
||||
use windows::Win32::Graphics::Dwm::{DwmGetWindowAttribute, DWMWA_EXTENDED_FRAME_BOUNDS};
|
||||
use windows::Win32::UI::WindowsAndMessaging::{
|
||||
GetAncestor, GetCursorPos, GetWindowRect, IsIconic, IsWindowVisible, WindowFromPoint, GA_ROOT,
|
||||
EnumWindows, GetClassNameW, GetCursorPos, GetWindowLongW, GetWindowRect, IsIconic,
|
||||
IsWindowVisible, GWL_EXSTYLE, WS_EX_TOOLWINDOW,
|
||||
};
|
||||
|
||||
unsafe fn visible_window_rect(hwnd: HWND) -> Option<RECT> {
|
||||
let mut rect = RECT::default();
|
||||
let r = DwmGetWindowAttribute(
|
||||
hwnd,
|
||||
DWMWA_EXTENDED_FRAME_BOUNDS,
|
||||
&mut rect as *mut RECT as *mut std::ffi::c_void,
|
||||
std::mem::size_of::<RECT>() as u32,
|
||||
);
|
||||
if r.is_ok() && rect.right > rect.left && rect.bottom > rect.top {
|
||||
return Some(rect);
|
||||
}
|
||||
let mut fallback = RECT::default();
|
||||
if GetWindowRect(hwnd, &mut fallback).is_ok() {
|
||||
Some(fallback)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
const VK_LBUTTON: i32 = 0x01;
|
||||
const VK_ESCAPE: i32 = 0x1B;
|
||||
|
||||
@@ -27,13 +49,99 @@ pub struct PickerSession {
|
||||
handle: Option<std::thread::JoinHandle<()>>,
|
||||
}
|
||||
|
||||
struct EnumCtx {
|
||||
point: POINT,
|
||||
exclude: Vec<isize>,
|
||||
found: Option<(isize, RECT)>,
|
||||
}
|
||||
|
||||
const SHELL_CLASSES: &[&str] = &[
|
||||
"Progman",
|
||||
"WorkerW",
|
||||
"Shell_TrayWnd",
|
||||
"Shell_SecondaryTrayWnd",
|
||||
"TrayNotifyWnd",
|
||||
"Button",
|
||||
"DV2ControlHost",
|
||||
"MultitaskingViewFrame",
|
||||
"ForegroundStaging",
|
||||
"Windows.UI.Core.CoreWindow",
|
||||
];
|
||||
|
||||
unsafe fn class_of(hwnd: HWND) -> String {
|
||||
let mut buf = [0u16; 128];
|
||||
let n = GetClassNameW(hwnd, &mut buf);
|
||||
if n <= 0 {
|
||||
return String::new();
|
||||
}
|
||||
String::from_utf16_lossy(&buf[..n as usize])
|
||||
}
|
||||
|
||||
unsafe extern "system" fn enum_at_point(hwnd: HWND, lparam: LPARAM) -> BOOL {
|
||||
let ctx = &mut *(lparam.0 as *mut EnumCtx);
|
||||
if ctx.found.is_some() {
|
||||
return BOOL(1);
|
||||
}
|
||||
let id = hwnd.0 as isize;
|
||||
if ctx.exclude.contains(&id) {
|
||||
return BOOL(1);
|
||||
}
|
||||
if !IsWindowVisible(hwnd).as_bool() {
|
||||
return BOOL(1);
|
||||
}
|
||||
if IsIconic(hwnd).as_bool() {
|
||||
return BOOL(1);
|
||||
}
|
||||
let ex = GetWindowLongW(hwnd, GWL_EXSTYLE) as u32;
|
||||
if (ex & WS_EX_TOOLWINDOW.0) != 0 {
|
||||
return BOOL(1);
|
||||
}
|
||||
let class_name = class_of(hwnd);
|
||||
if SHELL_CLASSES.iter().any(|c| class_name == *c) {
|
||||
return BOOL(1);
|
||||
}
|
||||
let rect = match visible_window_rect(hwnd) {
|
||||
Some(r) => r,
|
||||
None => return BOOL(1),
|
||||
};
|
||||
if ctx.point.x < rect.left
|
||||
|| ctx.point.x >= rect.right
|
||||
|| ctx.point.y < rect.top
|
||||
|| ctx.point.y >= rect.bottom
|
||||
{
|
||||
return BOOL(1);
|
||||
}
|
||||
let w = rect.right - rect.left;
|
||||
let h = rect.bottom - rect.top;
|
||||
if w < 40 || h < 40 {
|
||||
return BOOL(1);
|
||||
}
|
||||
ctx.found = Some((id, rect));
|
||||
BOOL(0)
|
||||
}
|
||||
|
||||
fn top_window_at(pt: POINT, exclude: &[isize]) -> Option<(isize, RECT)> {
|
||||
let mut ctx = EnumCtx {
|
||||
point: pt,
|
||||
exclude: exclude.to_vec(),
|
||||
found: None,
|
||||
};
|
||||
unsafe {
|
||||
let _ = EnumWindows(
|
||||
Some(enum_at_point),
|
||||
LPARAM(&mut ctx as *mut EnumCtx as isize),
|
||||
);
|
||||
}
|
||||
ctx.found
|
||||
}
|
||||
|
||||
pub fn start(app: AppHandle, exclude_hwnds: Vec<isize>) -> PickerSession {
|
||||
let running = Arc::new(AtomicBool::new(true));
|
||||
let running_clone = running.clone();
|
||||
let handle = thread::spawn(move || {
|
||||
let mut last_hwnd: isize = 0;
|
||||
let mut last_pressed = true;
|
||||
let mut last_esc = false;
|
||||
let mut last_esc = true;
|
||||
|
||||
loop {
|
||||
if !running_clone.load(Ordering::Relaxed) {
|
||||
@@ -46,22 +154,20 @@ pub fn start(app: AppHandle, exclude_hwnds: Vec<isize>) -> PickerSession {
|
||||
continue;
|
||||
}
|
||||
|
||||
let hwnd = unsafe { WindowFromPoint(pt) };
|
||||
let top = unsafe { GetAncestor(hwnd, GA_ROOT) };
|
||||
let top_id = top.0 as isize;
|
||||
let target = top_window_at(pt, &exclude_hwnds);
|
||||
|
||||
if top_id == 0
|
||||
|| exclude_hwnds.contains(&top_id)
|
||||
|| unsafe { !IsWindowVisible(top).as_bool() }
|
||||
|| unsafe { IsIconic(top).as_bool() }
|
||||
{
|
||||
thread::sleep(Duration::from_millis(30));
|
||||
continue;
|
||||
}
|
||||
|
||||
if top_id != last_hwnd {
|
||||
let mut rect = RECT::default();
|
||||
if unsafe { GetWindowRect(top, &mut rect) }.is_ok() {
|
||||
if let Some((id, rect)) = target {
|
||||
if id != last_hwnd {
|
||||
let class = unsafe { class_of(HWND(id as *mut _)) };
|
||||
tracing::info!(
|
||||
"picker hover: hwnd={:x} class={} rect=({},{}) {}x{}",
|
||||
id,
|
||||
class,
|
||||
rect.left,
|
||||
rect.top,
|
||||
rect.right - rect.left,
|
||||
rect.bottom - rect.top
|
||||
);
|
||||
let _ = app.emit(
|
||||
"picker-hover",
|
||||
Rect {
|
||||
@@ -71,16 +177,17 @@ pub fn start(app: AppHandle, exclude_hwnds: Vec<isize>) -> PickerSession {
|
||||
height: (rect.bottom - rect.top).max(0) as u32,
|
||||
},
|
||||
);
|
||||
last_hwnd = id;
|
||||
}
|
||||
last_hwnd = top_id;
|
||||
} else if last_hwnd != 0 {
|
||||
tracing::info!("picker hover: no target found at ({}, {})", pt.x, pt.y);
|
||||
last_hwnd = 0;
|
||||
}
|
||||
|
||||
let pressed =
|
||||
unsafe { (GetAsyncKeyState(VK_LBUTTON) as u16 & 0x8000) != 0 };
|
||||
if pressed && !last_pressed && last_hwnd != 0 {
|
||||
let target = HWND(last_hwnd as *mut _);
|
||||
let mut rect = RECT::default();
|
||||
if unsafe { GetWindowRect(target, &mut rect) }.is_ok() {
|
||||
if pressed && !last_pressed {
|
||||
if let Some((_, rect)) = target {
|
||||
let _ = app.emit(
|
||||
"picker-pick",
|
||||
Rect {
|
||||
@@ -90,8 +197,8 @@ pub fn start(app: AppHandle, exclude_hwnds: Vec<isize>) -> PickerSession {
|
||||
height: (rect.bottom - rect.top).max(0) as u32,
|
||||
},
|
||||
);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
last_pressed = pressed;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user