replace crashing snap with window picker icon on overlay
This commit is contained in:
+35
-6
@@ -4,8 +4,8 @@ mod capture;
|
||||
mod clicks;
|
||||
mod config;
|
||||
mod encode;
|
||||
mod snap;
|
||||
mod updates;
|
||||
mod windows_list;
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Mutex;
|
||||
@@ -775,6 +775,38 @@ fn set_overlay_size(app: AppHandle, width: u32, height: u32) -> Result<(), Strin
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn list_windows() -> Vec<windows_list::WindowInfo> {
|
||||
windows_list::list_visible_windows()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn set_overlay_to_window(
|
||||
app: AppHandle,
|
||||
x: i32,
|
||||
y: i32,
|
||||
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())?;
|
||||
let scale = win.scale_factor().map_err(|e| e.to_string())?;
|
||||
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;
|
||||
|
||||
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())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn get_overlay_rect(app: AppHandle) -> Result<CaptureRect, String> {
|
||||
let win = app
|
||||
@@ -1032,11 +1064,6 @@ 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 {
|
||||
@@ -1073,6 +1100,8 @@ pub fn run() {
|
||||
show_overlay,
|
||||
hide_overlay,
|
||||
set_overlay_size,
|
||||
set_overlay_to_window,
|
||||
list_windows,
|
||||
get_overlay_rect,
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
|
||||
@@ -1,116 +0,0 @@
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
use serde::Serialize;
|
||||
use windows::core::BOOL;
|
||||
use windows::Win32::Foundation::{HWND, LPARAM, RECT};
|
||||
use windows::Win32::UI::WindowsAndMessaging::{
|
||||
EnumWindows, GetWindowRect, GetWindowTextLengthW, GetWindowTextW, IsIconic, IsWindowVisible,
|
||||
};
|
||||
|
||||
const MIN_DIM: i32 = 80;
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct WindowInfo {
|
||||
pub title: String,
|
||||
pub x: i32,
|
||||
pub y: i32,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
}
|
||||
|
||||
pub fn list_visible_windows() -> Vec<WindowInfo> {
|
||||
let mut out: Vec<WindowInfo> = Vec::new();
|
||||
unsafe {
|
||||
let _ = EnumWindows(
|
||||
Some(enum_cb),
|
||||
LPARAM(&mut out as *mut Vec<WindowInfo> as isize),
|
||||
);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
unsafe extern "system" fn enum_cb(hwnd: HWND, lparam: LPARAM) -> BOOL {
|
||||
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_err() {
|
||||
return BOOL(1);
|
||||
}
|
||||
let w = rect.right - rect.left;
|
||||
let h = rect.bottom - rect.top;
|
||||
if w < MIN_DIM || h < MIN_DIM {
|
||||
return BOOL(1);
|
||||
}
|
||||
|
||||
let title_len = GetWindowTextLengthW(hwnd);
|
||||
if title_len <= 0 {
|
||||
return BOOL(1);
|
||||
}
|
||||
let mut buf = vec![0u16; (title_len as usize) + 1];
|
||||
let copied = GetWindowTextW(hwnd, &mut buf);
|
||||
if copied <= 0 {
|
||||
return BOOL(1);
|
||||
}
|
||||
let title = String::from_utf16_lossy(&buf[..copied as usize]);
|
||||
if title.trim().is_empty() {
|
||||
return BOOL(1);
|
||||
}
|
||||
|
||||
let list = &mut *(lparam.0 as *mut Vec<WindowInfo>);
|
||||
list.push(WindowInfo {
|
||||
title,
|
||||
x: rect.left,
|
||||
y: rect.top,
|
||||
width: w as u32,
|
||||
height: h as u32,
|
||||
});
|
||||
BOOL(1)
|
||||
}
|
||||
Reference in New Issue
Block a user