live elapsed and fps readout on capture frame

This commit is contained in:
2026-05-12 01:49:46 +03:00
parent b7ac517381
commit 27d7453ea2
2 changed files with 62 additions and 2 deletions
+5
View File
@@ -10,6 +10,7 @@
type ExportResult, type ExportResult,
} from "./api" } from "./api"
import { save } from "@tauri-apps/plugin-dialog" import { save } from "@tauri-apps/plugin-dialog"
import { emit } from "@tauri-apps/api/event"
type Phase = "idle" | "recording" | "paused" | "editing" | "exporting" | "done" type Phase = "idle" | "recording" | "paused" | "editing" | "exporting" | "done"
@@ -100,6 +101,7 @@
await api.startCapture({ crop }) await api.startCapture({ crop })
phase = "recording" phase = "recording"
tick() tick()
void emit("recording-state", { phase: "recording", fps })
} catch (e) { } catch (e) {
errMsg = String(e) errMsg = String(e)
} }
@@ -107,6 +109,7 @@
async function stopRec() { async function stopRec() {
clearTick() clearTick()
void emit("recording-state", { phase: "idle", fps })
try { try {
const c = await api.stopCapture() const c = await api.stopCapture()
clip = c clip = c
@@ -149,6 +152,7 @@
try { try {
await api.pauseCapture() await api.pauseCapture()
phase = "paused" phase = "paused"
void emit("recording-state", { phase: "paused", fps })
} catch (e) { } catch (e) {
errMsg = String(e) errMsg = String(e)
tick() tick()
@@ -160,6 +164,7 @@
await api.resumeCapture() await api.resumeCapture()
phase = "recording" phase = "recording"
tick() tick()
void emit("recording-state", { phase: "recording", fps })
} catch (e) { } catch (e) {
errMsg = String(e) errMsg = String(e)
} }
+57 -2
View File
@@ -1,11 +1,16 @@
<script lang="ts"> <script lang="ts">
import { onMount } from "svelte" import { onMount } from "svelte"
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow" import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow"
import { listen } from "@tauri-apps/api/event"
const win = getCurrentWebviewWindow() const win = getCurrentWebviewWindow()
let width: number = $state(0) let width: number = $state(0)
let height: 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(() => { onMount(() => {
const updateSize = async () => { const updateSize = async () => {
@@ -15,12 +20,45 @@
height = Math.round(size.height / scale) height = Math.round(size.height / scale)
} }
updateSize() updateSize()
const unlistenPromise = win.onResized(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 () => { return () => {
unlistenPromise.then((fn) => fn()) 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) { function startDrag(e: MouseEvent) {
if (e.button === 0) { if (e.button === 0) {
e.preventDefault() e.preventDefault()
@@ -38,6 +76,11 @@
> >
<span class="grip" data-tauri-drag-region>⠿</span> <span class="grip" data-tauri-drag-region>⠿</span>
<span class="dim" data-tauri-drag-region>{width} x {height}</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>
</div> </div>
@@ -84,4 +127,16 @@
.dim { .dim {
font-variant-numeric: tabular-nums; 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> </style>