From 2904a7ad29b945cef0da732265361f59b21235f5 Mon Sep 17 00:00:00 2001 From: lashman Date: Tue, 12 May 2026 02:15:07 +0300 Subject: [PATCH] replace crashing snap with window picker icon on overlay --- src-tauri/src/lib.rs | 41 ++++++-- src-tauri/src/snap.rs | 116 ----------------------- src-tauri/src/windows_list.rs | 71 ++++++++++++++ src/lib/Overlay.svelte | 170 ++++++++++++++++++++++++++++++++++ src/lib/api.ts | 11 +++ 5 files changed, 287 insertions(+), 122 deletions(-) delete mode 100644 src-tauri/src/snap.rs create mode 100644 src-tauri/src/windows_list.rs diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index b5fe825..04a1ff6 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -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::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 { 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!()) diff --git a/src-tauri/src/snap.rs b/src-tauri/src/snap.rs deleted file mode 100644 index 68e071b..0000000 --- a/src-tauri/src/snap.rs +++ /dev/null @@ -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 = 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) -} diff --git a/src-tauri/src/windows_list.rs b/src-tauri/src/windows_list.rs new file mode 100644 index 0000000..8f75469 --- /dev/null +++ b/src-tauri/src/windows_list.rs @@ -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 { + let mut out: Vec = Vec::new(); + unsafe { + let _ = EnumWindows( + Some(enum_cb), + LPARAM(&mut out as *mut Vec 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); + list.push(WindowInfo { + title, + x: rect.left, + y: rect.top, + width: w as u32, + height: h as u32, + }); + BOOL(1) +} diff --git a/src/lib/Overlay.svelte b/src/lib/Overlay.svelte index 09eae48..2a4fc6e 100644 --- a/src/lib/Overlay.svelte +++ b/src/lib/Overlay.svelte @@ -2,6 +2,15 @@ import { onMount } from "svelte" import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow" import { listen } from "@tauri-apps/api/event" + import { invoke } from "@tauri-apps/api/core" + + type WindowInfo = { + title: string + x: number + y: number + width: number + height: number + } const win = getCurrentWebviewWindow() @@ -11,6 +20,9 @@ let fps: number = $state(30) let elapsed: number = $state(0) let timer: number | null = null + let pickerOpen: boolean = $state(false) + let windows: WindowInfo[] = $state([]) + let loadingWindows: boolean = $state(false) onMount(() => { const updateSize = async () => { @@ -65,6 +77,37 @@ void win.startDragging().catch((err) => console.error("startDragging", err)) } } + + async function togglePicker() { + if (pickerOpen) { + pickerOpen = false + return + } + loadingWindows = true + pickerOpen = true + try { + windows = await invoke("list_windows") + } catch (e) { + console.error("list_windows", e) + windows = [] + } finally { + loadingWindows = false + } + } + + async function pickWindow(w: WindowInfo) { + try { + await invoke("set_overlay_to_window", { + x: w.x, + y: w.y, + width: w.width, + height: w.height, + }) + } catch (e) { + console.error("set_overlay_to_window", e) + } + pickerOpen = false + }
@@ -76,12 +119,44 @@ > ⠿ {width} x {height} + {#if phase !== "idle"} {phase === "recording" ? "REC" : "PAUSE"} · {elapsed.toFixed(1)}s · {fps}fps {/if}
+ + {#if pickerOpen} + + {/if} diff --git a/src/lib/api.ts b/src/lib/api.ts index 87501ab..bad4396 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -71,6 +71,14 @@ export type CaptureRect = { height: number } +export type WindowInfo = { + title: string + x: number + y: number + width: number + height: number +} + export type ClickEvent = { time_secs: number x: number @@ -126,6 +134,9 @@ export const api = { hideOverlay: () => invoke("hide_overlay"), setOverlaySize: (width: number, height: number) => invoke("set_overlay_size", { width, height }), + setOverlayToWindow: (x: number, y: number, width: number, height: number) => + invoke("set_overlay_to_window", { x, y, width, height }), + listWindows: () => invoke("list_windows"), getOverlayRect: () => invoke("get_overlay_rect"), extractThumbnails: (fps: number) => invoke("extract_thumbnails", { fps }),