replace crashing snap with window picker icon on overlay

This commit is contained in:
2026-05-12 02:15:07 +03:00
parent 87384f5113
commit 2904a7ad29
5 changed files with 287 additions and 122 deletions
+170
View File
@@ -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>
+11
View File
@@ -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 }),