Files
bytesize/src/lib/Overlay.svelte
T

220 lines
7.0 KiB
Svelte

<script lang="ts">
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"
const win = getCurrentWebviewWindow()
type Rect = { x: number; y: number; width: number; height: number }
let width: number = $state(0)
let height: number = $state(0)
let phase: "idle" | "recording" | "paused" = $state("idle")
let fps: number = $state(30)
let elapsed: number = $state(0)
let timer: number | null = null
let lockedRatio: number | null = null
let resizeDebounce: number | null = null
let enforcing = false
let resizing = false
onMount(() => {
const updateSize = async () => {
const sz = await win.innerSize()
const sf = await win.scaleFactor()
width = Math.round(sz.width / sf)
height = Math.round(sz.height / sf)
}
// Query aspect ratio from Rust on startup
const syncRatio = async () => {
try { lockedRatio = await invoke<number | null>("get_overlay_aspect") } catch {}
}
updateSize()
syncRatio()
let cleanup: (() => void)[] = []
win.onResized(async () => {
if (enforcing) return
await updateSize()
// Re-sync ratio from Rust in case it changed
await syncRatio()
if (lockedRatio === null) return
// Only enforce AFTER the user stops dragging.
// Reset debounce on every resize event - enforcement fires only when
// resize events stop coming (user released mouse).
resizing = true
if (resizeDebounce !== null) clearTimeout(resizeDebounce)
resizeDebounce = window.setTimeout(async () => {
resizing = false
try { await invoke("enforce_overlay_aspect") } catch {}
await updateSize()
enforcing = false
}, 350)
}).then(fn => cleanup.push(fn))
listen<{ phase: "idle"|"recording"|"paused"; fps: number }>(
"recording-state", (e) => {
fps = e.payload.fps
if (e.payload.phase === "recording") {
if (phase !== "recording") elapsed = 0
startTimer()
} else {
stopTimer()
}
if (e.payload.phase === "idle") { elapsed = 0; phase = "idle" }
phase = e.payload.phase
}
).then(fn => cleanup.push(fn))
listen<Rect>("picker-pick", async (e: { payload: Rect }) => {
try { await invoke("set_overlay_to_window", e.payload) } catch {}
try { await invoke("close_window_picker") } catch {}
}).then(fn => cleanup.push(fn))
listen("picker-cancel", async () => {
try { await invoke("close_window_picker") } catch {}
}).then(fn => cleanup.push(fn))
return () => {
stopTimer()
if (resizeDebounce !== null) clearTimeout(resizeDebounce)
cleanup.forEach(fn => fn())
}
})
function startTimer() {
if (timer !== null) return
timer = window.setInterval(() => { elapsed += 0.1 }, 100)
}
function stopTimer() {
if (timer !== null) { clearInterval(timer); timer = null }
}
function drag(e: MouseEvent) {
if (e.button === 0) void win.startDragging()
}
async function openPicker() {
try { await invoke("open_window_picker") } catch (err) { console.error(err) }
}
async function closeOverlay() {
try { await invoke("hide_overlay") } catch {}
}
</script>
<div class="overlay">
<div class="titlebar" onmousedown={drag} role="presentation">
<span class="grip" aria-hidden="true">::</span>
<span class="dims" aria-label="dimensions">{width} x {height}</span>
<button class="pick-btn" onclick={openPicker} title="snap to window" aria-label="snap capture frame to a window">
<svg width="10" height="10" viewBox="0 0 12 12" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round">
<rect x="1" y="1" width="4" height="4" rx="0.5"/>
<rect x="7" y="1" width="4" height="4" rx="0.5"/>
<rect x="1" y="7" width="4" height="4" rx="0.5"/>
<rect x="7" y="7" width="4" height="4" rx="0.5"/>
</svg>
</button>
<button class="close-btn" onclick={closeOverlay} title="close frame" aria-label="close capture frame">
<svg width="10" height="10" viewBox="0 0 10 10" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round">
<line x1="2" y1="2" x2="8" y2="8"/>
<line x1="8" y1="2" x2="2" y2="8"/>
</svg>
</button>
{#if phase !== "idle"}
<span class="status" class:recording={phase === "recording"}>
{phase === "recording" ? "REC" : "PAUSE"}
<span class="sep">/</span>
{elapsed.toFixed(1)}s
<span class="sep">/</span>
{fps}fps
</span>
{/if}
</div>
</div>
<style>
:global(html), :global(body), :global(#overlay) {
margin: 0; padding: 0;
background: transparent;
height: 100vh; width: 100vw;
overflow: hidden;
}
.overlay {
box-sizing: border-box;
height: 100vh; width: 100vw;
border: 2px solid #14B8A6;
display: flex; flex-direction: column;
background: transparent;
border-radius: var(--r-sm);
}
.titlebar {
height: 24px;
background: rgba(9, 9, 11, 0.92);
backdrop-filter: blur(8px);
color: rgba(228, 228, 231, 0.7);
display: flex; align-items: center; gap: 8px;
padding: 0 8px;
font: 10px/1 var(--mono, 'JetBrains Mono', monospace);
cursor: grab;
flex-shrink: 0;
user-select: none;
border-bottom: 1px solid rgba(20, 184, 166, 0.2);
border-radius: 2px 2px 0 0;
}
.titlebar:active { cursor: grabbing; }
.grip { opacity: 0.35; font-size: 11px; flex-shrink: 0; letter-spacing: 1px; }
.dims {
font-family: var(--mono, 'JetBrains Mono', monospace);
font-size: 10px;
color: rgba(228, 228, 231, 0.4);
letter-spacing: 0.02em;
}
.pick-btn {
display: grid; place-items: center;
width: 20px; height: 20px;
background: rgba(20, 184, 166, 0.1);
border: 1px solid rgba(20, 184, 166, 0.25);
color: rgba(20, 184, 166, 0.7);
border-radius: var(--r-sm);
cursor: pointer;
flex-shrink: 0;
transition: all 80ms;
}
.pick-btn:hover { background: rgba(20, 184, 166, 0.18); border-color: rgba(20, 184, 166, 0.5); color: #14B8A6; }
.close-btn {
display: grid; place-items: center;
width: 20px; height: 20px;
background: rgba(239, 68, 68, 0.1);
border: 1px solid rgba(239, 68, 68, 0.25);
color: rgba(239, 68, 68, 0.7);
border-radius: var(--r-sm);
cursor: pointer;
flex-shrink: 0;
transition: all 80ms;
margin-left: auto;
}
.close-btn:hover { background: rgba(239, 68, 68, 0.18); border-color: rgba(239, 68, 68, 0.5); color: #EF4444; }
.status {
margin-left: auto;
font-family: var(--mono, 'JetBrains Mono', monospace);
font-size: 10px; font-weight: 500;
color: rgba(228, 228, 231, 0.5);
display: flex; align-items: center; gap: 4px;
background: rgba(255,255,255,0.04);
padding: 1px 6px;
border-radius: 2px;
}
.status.recording {
background: rgba(34, 197, 94, 0.15);
color: #22C55E;
}
.sep { opacity: 0.3; }
</style>