Files
bytesize/src/lib/Picker.svelte
T

160 lines
4.7 KiB
Svelte

<script lang="ts">
import { onMount } from "svelte"
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow"
import { listen, emit } from "@tauri-apps/api/event"
type Rect = { x: number; y: number; width: number; height: number }
let hover: Rect | null = $state(null)
let originX = 0, originY = 0, scale = 1
let mounted = true
onMount(() => {
const win = getCurrentWebviewWindow()
const unlisten: (() => void)[] = []
let hoverDebounce = 0
// A registration that lands after the window is gone still has to
// unregister, so a late unlisten runs immediately rather than being dropped.
const keep = (p: Promise<() => void>) => {
void p.then(fn => { if (mounted) unlisten.push(fn); else fn() })
.catch(e => console.warn("could not register a listener:", e))
}
const refresh = async () => {
scale = await win.scaleFactor()
const pos = await win.innerPosition()
originX = pos.x; originY = pos.y
}
void (async () => {
try {
await refresh()
} catch (e) {
console.warn("could not read the picker window geometry:", e)
}
keep(win.onMoved(refresh))
keep(listen<Rect>("picker-hover", (e) => {
clearTimeout(hoverDebounce)
hoverDebounce = window.setTimeout(() => { hover = e.payload }, 30)
}))
})()
return () => {
mounted = false
clearTimeout(hoverDebounce)
unlisten.forEach(fn => fn())
emit("picker-cancel", {}).catch(() => {})
}
})
let rectStyle = $derived.by(() => {
if (!hover) return ""
const l = (hover.x - originX) / scale
const t = (hover.y - originY) / scale
const w = hover.width / scale
const h = hover.height / scale
return `left:${l}px;top:${t}px;width:${w}px;height:${h}px`
})
</script>
<div class="picker" aria-label="window picker - click a window to capture it, Esc to cancel">
<div class="dim" aria-hidden="true"></div>
{#if hover}
<div class="frame" style={rectStyle} aria-hidden="true">
<span class="frame-label">{hover.width} x {hover.height}</span>
</div>
{/if}
<div class="hint" aria-live="polite">
{#if hover}
click to capture - <kbd>{hover.width}x{hover.height}</kbd>
{:else}
move mouse over a window to select it - <kbd>Esc</kbd> to cancel
{/if}
</div>
</div>
<style>
:global(html), :global(body), :global(#picker) {
margin: 0; padding: 0;
background: transparent;
height: 100vh; width: 100vw;
overflow: hidden;
cursor: crosshair;
}
.picker {
position: relative;
width: 100vw; height: 100vh;
font-family: var(--mono, 'JetBrains Mono', monospace);
}
.dim {
position: absolute; inset: 0;
background: rgba(9, 9, 11, 0.15);
}
.frame {
position: absolute;
pointer-events: none;
border: 2px solid var(--accent);
background: transparent;
background-image:
repeating-linear-gradient(90deg, var(--accent) 0px, var(--accent) 8px, transparent 8px, transparent 12px),
repeating-linear-gradient(0deg, var(--accent) 0px, var(--accent) 8px, transparent 8px, transparent 12px),
repeating-linear-gradient(90deg, var(--accent) 0px, var(--accent) 8px, transparent 8px, transparent 12px),
repeating-linear-gradient(0deg, var(--accent) 0px, var(--accent) 8px, transparent 8px, transparent 12px);
background-position: 0 0, 0 0, 0 100%, 100% 0;
background-size: 12px 2px, 2px 12px, 12px 2px, 2px 12px;
background-repeat: repeat-x, repeat-y, repeat-x, repeat-y;
animation: march 0.4s linear infinite;
transition: left 0.08s ease, top 0.08s ease, width 0.08s ease, height 0.08s ease;
}
@keyframes march {
to { background-position: 12px 0, 0 12px, -12px 100%, 100% -12px; }
}
.frame-label {
position: absolute;
top: -20px; left: 0;
padding: 2px 6px;
background: var(--accent);
color: #020617;
font-size: 9px;
font-weight: 600;
font-family: var(--mono, 'JetBrains Mono', monospace);
letter-spacing: 0.04em;
border-radius: 2px;
white-space: nowrap;
}
.hint {
position: fixed;
bottom: 20px; left: 50%; transform: translateX(-50%);
padding: 7px 18px;
background: rgba(9, 9, 11, 0.9);
backdrop-filter: blur(10px);
color: rgba(228, 228, 231, 0.85);
font-size: 12px;
font-family: var(--sans, system-ui, sans-serif);
border: 1px solid rgba(20, 184, 166, 0.2);
border-radius: 999px;
white-space: nowrap;
pointer-events: none;
}
kbd {
display: inline-block;
padding: 0 4px;
background: rgba(20, 184, 166, 0.12);
border: 1px solid rgba(20, 184, 166, 0.3);
border-radius: 2px;
font-family: var(--mono, 'JetBrains Mono', monospace);
font-size: 10px;
color: var(--accent);
}
</style>