global hotkeys wired up
This commit is contained in:
+96
-14
@@ -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;
|
||||
}
|
||||
|
||||
+18
-4
@@ -2,11 +2,12 @@
|
||||
import { onMount } from "svelte"
|
||||
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow"
|
||||
|
||||
const win = getCurrentWebviewWindow()
|
||||
|
||||
let width: number = $state(0)
|
||||
let height: number = $state(0)
|
||||
|
||||
onMount(() => {
|
||||
const win = getCurrentWebviewWindow()
|
||||
const updateSize = async () => {
|
||||
const size = await win.innerSize()
|
||||
const scale = await win.scaleFactor()
|
||||
@@ -14,15 +15,27 @@
|
||||
height = Math.round(size.height / scale)
|
||||
}
|
||||
updateSize()
|
||||
const unlisten = win.onResized(updateSize)
|
||||
const unlistenPromise = win.onResized(updateSize)
|
||||
return () => {
|
||||
unlisten.then((fn) => fn())
|
||||
unlistenPromise.then((fn) => fn())
|
||||
}
|
||||
})
|
||||
|
||||
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>
|
||||
<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>
|
||||
</div>
|
||||
@@ -59,6 +72,7 @@
|
||||
font: 11px ui-monospace, monospace;
|
||||
cursor: grab;
|
||||
flex-shrink: 0;
|
||||
user-select: none;
|
||||
}
|
||||
.titlebar:active {
|
||||
cursor: grabbing;
|
||||
|
||||
@@ -85,6 +85,7 @@ export type ExportRequest = {
|
||||
deleted_frame_indices: number[]
|
||||
trim_start_secs?: number | null
|
||||
trim_end_secs?: number | null
|
||||
output_path?: string | null
|
||||
}
|
||||
|
||||
export type ExportResult = {
|
||||
@@ -111,9 +112,13 @@ export const api = {
|
||||
getOverlayRect: () => invoke<CaptureRect>("get_overlay_rect"),
|
||||
extractThumbnails: (fps: number) =>
|
||||
invoke<string[]>("extract_thumbnails", { fps }),
|
||||
defaultOutputPath: (format: Format) =>
|
||||
invoke<string>("default_output_path", { format }),
|
||||
openUrl: (url: string) => invoke<void>("open_url", { url }),
|
||||
onUpdateAvailable: (handler: (info: UpdateInfo) => void): Promise<UnlistenFn> =>
|
||||
listen<UpdateInfo>("update-available", (e) => handler(e.payload)),
|
||||
onHotkey: (handler: (kind: "start" | "stop" | "pause") => void): Promise<UnlistenFn> =>
|
||||
listen<"start" | "stop" | "pause">("hotkey", (e) => handler(e.payload)),
|
||||
}
|
||||
|
||||
export function formatBytes(n: number): string {
|
||||
|
||||
Reference in New Issue
Block a user