Files
bytesize/src/lib/Overlay.svelte
T

313 lines
6.9 KiB
Svelte
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<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"
type WindowInfo = {
title: string
x: number
y: number
width: number
height: number
}
const win = getCurrentWebviewWindow()
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 pickerOpen: boolean = $state(false)
let windows: WindowInfo[] = $state([])
let loadingWindows: boolean = $state(false)
onMount(() => {
const updateSize = async () => {
const size = await win.innerSize()
const scale = await win.scaleFactor()
width = Math.round(size.width / scale)
height = Math.round(size.height / scale)
}
updateSize()
const unlistenResize = win.onResized(updateSize)
const unlistenStatePromise = listen<{
phase: "idle" | "recording" | "paused"
fps: number
}>("recording-state", (e) => {
const next = e.payload.phase
fps = e.payload.fps
if (next === "recording") {
if (phase === "idle") elapsed = 0
startTimer()
} else {
stopTimer()
}
if (next === "idle") elapsed = 0
phase = next
})
return () => {
stopTimer()
unlistenResize.then((fn) => fn())
unlistenStatePromise.then((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 startDrag(e: MouseEvent) {
if (e.button === 0) {
e.preventDefault()
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">
<div
class="titlebar"
data-tauri-drag-region
onmousedown={startDrag}
role="presentation"
>
<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>
:global(html), :global(body), :global(#overlay) {
margin: 0;
padding: 0;
background: transparent;
height: 100vh;
width: 100vw;
overflow: hidden;
}
.frame {
box-sizing: border-box;
height: 100vh;
width: 100vw;
border: 2px solid #4a8;
display: flex;
flex-direction: column;
background: transparent;
}
.titlebar {
height: 22px;
background: rgba(74, 170, 136, 0.85);
color: #0e1a16;
display: flex;
align-items: center;
gap: 8px;
padding: 0 8px;
font: 11px ui-monospace, monospace;
cursor: grab;
flex-shrink: 0;
user-select: none;
}
.titlebar:active {
cursor: grabbing;
}
.grip {
font-size: 14px;
opacity: 0.7;
}
.dim {
font-variant-numeric: tabular-nums;
}
.status {
margin-left: auto;
font-variant-numeric: tabular-nums;
padding: 1px 6px;
border-radius: 999px;
background: rgba(0, 0, 0, 0.25);
color: #eee;
}
.status.rec {
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>