global hotkeys wired up

This commit is contained in:
2026-05-12 01:33:47 +03:00
parent 72e7a0d33b
commit 2882e78795
10 changed files with 637 additions and 107 deletions
+96 -14
View File
@@ -9,6 +9,7 @@
type RecordedClip,
type ExportResult,
} from "./api"
import { save } from "@tauri-apps/plugin-dialog"
type Phase = "idle" | "recording" | "paused" | "editing" | "exporting" | "done"
@@ -32,12 +33,31 @@
let loadingThumbs: boolean = $state(false)
let trimStart: number = $state(0)
let trimEnd: number = $state(0)
let outputPath: string = $state("")
let pathOverridden: boolean = $state(false)
onMount(async () => {
config = await api.getConfig()
format = config.export.default_format
fps = config.capture.default_fps
keepAudio = config.capture.capture_audio
onMount(() => {
let unlisten: (() => void) | null = null
void (async () => {
config = await api.getConfig()
format = config.export.default_format
fps = config.capture.default_fps
keepAudio = config.capture.capture_audio
unlisten = await api.onHotkey((kind) => {
if (kind === "start" && phase === "idle") {
void startRec()
} else if (kind === "pause") {
if (phase === "recording") void pauseRec()
else if (phase === "paused") void resumeRec()
} else if (kind === "stop") {
if (phase === "recording" || phase === "paused") void stopRec()
}
})
})()
return () => {
unlisten?.()
}
})
function tick() {
@@ -71,7 +91,6 @@
if (useFrame) {
try {
crop = await api.getOverlayRect()
await api.hideOverlay()
} catch (e) {
errMsg = String(e)
return
@@ -83,9 +102,6 @@
tick()
} catch (e) {
errMsg = String(e)
if (useFrame) {
await api.showOverlay().catch(() => {})
}
}
}
@@ -99,12 +115,35 @@
targetMB = Math.min(50, Math.max(minMB(c.duration_secs), sensible))
trimStart = 0
trimEnd = c.duration_secs
pathOverridden = false
outputPath = await api.defaultOutputPath(format)
} catch (e) {
errMsg = String(e)
phase = "idle"
}
}
async function browseOutput() {
const filters = format === "gif"
? [{ name: "GIF", extensions: ["gif"] }]
: format === "webm"
? [{ name: "WebM", extensions: ["webm"] }]
: [{ name: "MP4", extensions: ["mp4"] }]
const picked = await save({ defaultPath: outputPath, filters })
if (picked) {
outputPath = picked
pathOverridden = true
}
}
$effect(() => {
if (phase === "editing" && !pathOverridden && format) {
void api.defaultOutputPath(format).then((p) => {
if (!pathOverridden) outputPath = p
})
}
})
async function pauseRec() {
clearTick()
try {
@@ -180,6 +219,7 @@
deleted_frame_indices: format === "gif" ? Array.from(deletedFrames) : [],
trim_start_secs: tsStart,
trim_end_secs: tsEnd,
output_path: outputPath.trim() || null,
})
exportResult = result
phase = "done"
@@ -228,16 +268,16 @@
<section class="controls">
<button class="primary big" onclick={startRec}>start recording</button>
<button class:on={useFrame} onclick={toggleFrame}>
{useFrame ? "hide capture frame" : "show capture frame"}
capture frame: {useFrame ? "on" : "off"}
</button>
</section>
<p class="hint">
{#if useFrame}
Drag the green frame on screen to position the region you want to
capture. The frame hides automatically while recording.
The green frame stays visible during recording but is excluded from
the capture itself. Drag it to position the region.
{:else}
Captures your full primary monitor. Click "show capture frame" to draw
a region instead.
Captures your full primary monitor. Toggle the capture frame on to
record a region instead.
{/if}
</p>
{:else if phase === "recording" || phase === "paused"}
@@ -398,6 +438,20 @@
</div>
{/if}
<div class="path-row">
<label>
save to
<div class="path-input">
<input
type="text"
bind:value={outputPath}
oninput={() => pathOverridden = true}
/>
<button class="small" onclick={browseOutput}>browse...</button>
</div>
</label>
</div>
<div class="row">
<button class="primary" onclick={doExport}>export</button>
<button onclick={startOver}>discard and start over</button>
@@ -666,6 +720,34 @@
color: #888;
font-size: 0.85rem;
}
.path-row {
margin: 1rem 0;
}
.path-row label {
display: grid;
gap: 0.4rem;
color: #aaa;
font-size: 0.85rem;
}
.path-input {
display: grid;
grid-template-columns: 1fr auto;
gap: 0.4rem;
}
.path-input input {
font: inherit;
font-family: ui-monospace, monospace;
font-size: 0.85rem;
padding: 0.45rem 0.6rem;
border: 1px solid #444;
background: #222;
color: inherit;
border-radius: 4px;
}
.path-input input:focus {
outline: none;
border-color: #4a8;
}
.trim {
margin: 0.5rem 0 1.5rem;
}