207 lines
4.8 KiB
Svelte
207 lines
4.8 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()
|
|
|
|
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))
|
|
}
|
|
}
|
|
|
|
async function openPicker() {
|
|
try {
|
|
await invoke("open_window_picker")
|
|
} catch (e) {
|
|
console.error("open_window_picker", e)
|
|
}
|
|
}
|
|
|
|
onMount(() => {
|
|
let unlistenPick: (() => void) | null = null
|
|
let unlistenCancel: (() => void) | null = null
|
|
void (async () => {
|
|
unlistenPick = await listen<{ x: number; y: number; width: number; height: number }>(
|
|
"picker-pick",
|
|
async (e) => {
|
|
try {
|
|
await invoke("set_overlay_to_window", e.payload)
|
|
} catch (err) {
|
|
console.error("set_overlay_to_window", err)
|
|
}
|
|
try {
|
|
await invoke("close_window_picker")
|
|
} catch (err) {
|
|
console.error("close_window_picker", err)
|
|
}
|
|
},
|
|
)
|
|
unlistenCancel = await listen("picker-cancel", async () => {
|
|
try {
|
|
await invoke("close_window_picker")
|
|
} catch (err) {
|
|
console.error("close_window_picker", err)
|
|
}
|
|
})
|
|
})()
|
|
return () => {
|
|
unlistenPick?.()
|
|
unlistenCancel?.()
|
|
}
|
|
})
|
|
</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={openPicker}
|
|
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>
|
|
</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;
|
|
}
|
|
</style>
|