Files
bytesize/src/lib/Overlay.svelte
T

143 lines
3.2 KiB
Svelte

<script lang="ts">
import { onMount } from "svelte"
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow"
import { listen } from "@tauri-apps/api/event"
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
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))
}
}
</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>
{#if phase !== "idle"}
<span class="status" class:rec={phase === "recording"}>
{phase === "recording" ? "REC" : "PAUSE"} · {elapsed.toFixed(1)}s · {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;
}
.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;
}
</style>