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)
|
||||
}
|
||||
@@ -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<WindowInfo[]>("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
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class="frame">
|
||||
@@ -76,12 +119,44 @@
|
||||
>
|
||||
<span class="grip" data-tauri-drag-region>⠿</span>
|
||||
<span class="dim" data-tauri-drag-region>{width} x {height}</span>
|
||||
<button
|
||||
class="pick-btn"
|
||||
onclick={togglePicker}
|
||||
onmousedown={(e) => e.stopPropagation()}
|
||||
title="snap to window"
|
||||
type="button"
|
||||
>▦</button>
|
||||
{#if phase !== "idle"}
|
||||
<span class="status" class:rec={phase === "recording"}>
|
||||
{phase === "recording" ? "REC" : "PAUSE"} · {elapsed.toFixed(1)}s · {fps}fps
|
||||
</span>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
{#if pickerOpen}
|
||||
<div class="picker" role="dialog" tabindex="-1" onmousedown={(e) => e.stopPropagation()}>
|
||||
<div class="picker-head">
|
||||
<span>pick a window</span>
|
||||
<button onclick={() => (pickerOpen = false)} type="button">×</button>
|
||||
</div>
|
||||
{#if loadingWindows}
|
||||
<div class="picker-empty">listing...</div>
|
||||
{:else if windows.length === 0}
|
||||
<div class="picker-empty">no windows found</div>
|
||||
{:else}
|
||||
<ul class="picker-list">
|
||||
{#each windows as w}
|
||||
<li>
|
||||
<button onclick={() => pickWindow(w)} type="button">
|
||||
<span class="w-title">{w.title}</span>
|
||||
<span class="w-dim">{w.width}x{w.height}</span>
|
||||
</button>
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
{/if}
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
@@ -139,4 +214,99 @@
|
||||
background: #c33;
|
||||
color: #fff;
|
||||
}
|
||||
.pick-btn {
|
||||
background: rgba(0, 0, 0, 0.2);
|
||||
border: 1px solid rgba(14, 26, 22, 0.4);
|
||||
color: #0e1a16;
|
||||
font: inherit;
|
||||
font-size: 12px;
|
||||
padding: 1px 6px;
|
||||
border-radius: 3px;
|
||||
cursor: pointer;
|
||||
line-height: 1;
|
||||
}
|
||||
.pick-btn:hover {
|
||||
background: rgba(0, 0, 0, 0.35);
|
||||
color: #fff;
|
||||
}
|
||||
.picker {
|
||||
background: #1a1a1a;
|
||||
border: 1px solid #4a8;
|
||||
border-radius: 4px;
|
||||
margin: 4px;
|
||||
max-height: calc(100vh - 32px);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
overflow: hidden;
|
||||
font: 12px ui-monospace, monospace;
|
||||
color: #ddd;
|
||||
}
|
||||
.picker-head {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 4px 8px;
|
||||
background: #222;
|
||||
border-bottom: 1px solid #2a2a2a;
|
||||
color: #aaa;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.05em;
|
||||
font-size: 10px;
|
||||
}
|
||||
.picker-head button {
|
||||
background: transparent;
|
||||
border: none;
|
||||
color: #888;
|
||||
font-size: 14px;
|
||||
cursor: pointer;
|
||||
padding: 0 4px;
|
||||
}
|
||||
.picker-head button:hover {
|
||||
color: #fff;
|
||||
}
|
||||
.picker-empty {
|
||||
padding: 12px;
|
||||
text-align: center;
|
||||
color: #888;
|
||||
}
|
||||
.picker-list {
|
||||
list-style: none;
|
||||
margin: 0;
|
||||
padding: 4px;
|
||||
overflow-y: auto;
|
||||
flex: 1;
|
||||
}
|
||||
.picker-list li {
|
||||
margin: 0;
|
||||
}
|
||||
.picker-list button {
|
||||
width: 100%;
|
||||
background: transparent;
|
||||
border: none;
|
||||
color: #ddd;
|
||||
font: inherit;
|
||||
text-align: left;
|
||||
padding: 4px 6px;
|
||||
border-radius: 2px;
|
||||
cursor: pointer;
|
||||
display: flex;
|
||||
gap: 6px;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
}
|
||||
.picker-list button:hover {
|
||||
background: #2a3a35;
|
||||
color: #fff;
|
||||
}
|
||||
.w-title {
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
flex: 1;
|
||||
}
|
||||
.w-dim {
|
||||
color: #888;
|
||||
font-size: 10px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -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<void>("hide_overlay"),
|
||||
setOverlaySize: (width: number, height: number) =>
|
||||
invoke<void>("set_overlay_size", { width, height }),
|
||||
setOverlayToWindow: (x: number, y: number, width: number, height: number) =>
|
||||
invoke<void>("set_overlay_to_window", { x, y, width, height }),
|
||||
listWindows: () => invoke<WindowInfo[]>("list_windows"),
|
||||
getOverlayRect: () => invoke<CaptureRect>("get_overlay_rect"),
|
||||
extractThumbnails: (fps: number) =>
|
||||
invoke<string[]>("extract_thumbnails", { fps }),
|
||||
|
||||
Reference in New Issue
Block a user