Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
Add preview quality selection to editor player
Richie McIlroy02d2260293e5
diff --git a/apps/desktop/src/routes/editor/CaptionsTab.tsx b/apps/desktop/src/routes/editor/CaptionsTab.tsxindex ff46d7745..d6677d16b 100644--- a/apps/desktop/src/routes/editor/CaptionsTab.tsx+++ b/apps/desktop/src/routes/editor/CaptionsTab.tsx@@ -10,7 +10,7 @@ import toast from "solid-toast";import { Toggle } from "~/components/Toggle";import type { CaptionSegment, CaptionSettings } from "~/utils/tauri";import { commands, events } from "~/utils/tauri";-import { FPS, OUTPUT_SIZE, useEditorContext } from "./context";+import { FPS, useEditorContext } from "./context";import { TextInput } from "./TextInput";import {Field,@@ -166,8 +166,13 @@ function RgbInput(props: { value: string; onChange: (value: string) => void }) {// Add scroll position preservation for the containerexport function CaptionsTab() {- const { project, setProject, editorInstance, editorState } =- useEditorContext();+ const {+ project,+ setProject,+ editorInstance,+ editorState,+ previewResolutionBase,+ } = useEditorContext();// Scroll managementlet scrollContainerRef: HTMLDivElement | undefined;@@ -215,7 +220,7 @@ export function CaptionsTab() {events.renderFrameEvent.emit({frame_number: Math.floor(editorState.playbackTime * FPS),fps: FPS,- resolution_base: OUTPUT_SIZE,+ resolution_base: previewResolutionBase(),});});}@@ -248,7 +253,7 @@ export function CaptionsTab() {events.renderFrameEvent.emit({frame_number: Math.floor(editorState.playbackTime * FPS),fps: FPS,- resolution_base: OUTPUT_SIZE,+ resolution_base: previewResolutionBase(),});}};diff --git a/apps/desktop/src/routes/editor/Editor.tsx b/apps/desktop/src/routes/editor/Editor.tsxindex 8b84d86db..87a6a89a7 100644--- a/apps/desktop/src/routes/editor/Editor.tsx+++ b/apps/desktop/src/routes/editor/Editor.tsx@@ -35,7 +35,6 @@ import {EditorContextProvider,EditorInstanceContextProvider,FPS,- OUTPUT_SIZE,useEditorContext,useEditorInstanceContext,} from "./context";@@ -82,7 +81,8 @@ export function Editor() {}function Inner() {- const { project, editorState, setEditorState } = useEditorContext();+ const { project, editorState, setEditorState, previewResolutionBase } =+ useEditorContext();createTauriEventListener(events.editorStateChanged, (payload) => {throttledRenderFrame.clear();@@ -95,7 +95,7 @@ function Inner() {events.renderFrameEvent.emit({frame_number: Math.max(Math.floor(time * FPS), 0),fps: FPS,- resolution_base: OUTPUT_SIZE,+ resolution_base: previewResolutionBase(),});}};@@ -116,10 +116,13 @@ function Inner() {});createEffect(- on(frameNumberToRender, (number) => {- if (editorState.playing) return;- renderFrame(number);- }),+ on(+ () => [frameNumberToRender(), previewResolutionBase()],+ ([number]) => {+ if (editorState.playing) return;+ renderFrame(number);+ },+ ),);createEffect(diff --git a/apps/desktop/src/routes/editor/Player.tsx b/apps/desktop/src/routes/editor/Player.tsxindex 9b0245d12..ac38bdc06 100644--- a/apps/desktop/src/routes/editor/Player.tsx+++ b/apps/desktop/src/routes/editor/Player.tsx@@ -1,3 +1,4 @@+import { Select as KSelect } from "@kobalte/core/select";import { ToggleButton as KToggleButton } from "@kobalte/core/toggle-button";import { createElementBounds } from "@solid-primitives/bounds";import { cx } from "cva";@@ -9,11 +10,18 @@ import { commands } from "~/utils/tauri";import AspectRatioSelect from "./AspectRatioSelect";import {FPS,- OUTPUT_SIZE,+ type PreviewQuality,serializeProjectConfiguration,useEditorContext,} from "./context";-import { EditorButton, Slider } from "./ui";+import {+ EditorButton,+ MenuItem,+ MenuItemList,+ PopperContent,+ Slider,+ topLeftAnimateClasses,+} from "./ui";import { useEditorShortcuts } from "./useEditorShortcuts";import { formatTime } from "./utils";@@ -27,8 +35,16 @@ export function Player() {setEditorState,zoomOutLimit,setProject,+ previewResolutionBase,+ previewQuality,+ setPreviewQuality,} = useEditorContext();+ const previewOptions = [+ { label: "Full", value: "full" as PreviewQuality },+ { label: "Half", value: "half" as PreviewQuality },+ ];+// Load captions on mountonMount(async () => {if (editorInstance && editorInstance.path) {@@ -95,10 +111,6 @@ export function Player() {}});- const [canvasContainerRef, setCanvasContainerRef] =- createSignal<HTMLDivElement>();- const containerBounds = createElementBounds(canvasContainerRef);-const isAtEnd = () => {const total = totalDuration();return total > 0 && total - editorState.playbackTime <= 0.1;@@ -123,6 +135,31 @@ export function Player() {setEditorState("playing", false);};+ const handlePreviewQualityChange = async (quality: PreviewQuality) => {+ if (quality === previewQuality()) return;++ const wasPlaying = editorState.playing;+ const currentFrame = Math.max(+ Math.floor(editorState.playbackTime * FPS),+ 0,+ );++ setPreviewQuality(quality);++ if (!wasPlaying) return;++ try {+ await commands.stopPlayback();+ setEditorState("playing", false);+ await commands.seekTo(currentFrame);+ await commands.startPlayback(FPS, previewResolutionBase());+ setEditorState("playing", true);+ } catch (error) {+ console.error("Failed to update preview quality:", error);+ setEditorState("playing", false);+ }+ };+createEffect(() => {if (isAtEnd() && editorState.playing) {commands.stopPlayback();@@ -136,7 +173,7 @@ export function Player() {await commands.stopPlayback();setEditorState("playbackTime", 0);await commands.seekTo(0);- await commands.startPlayback(FPS, OUTPUT_SIZE);+ await commands.startPlayback(FPS, previewResolutionBase());setEditorState("playing", true);} else if (editorState.playing) {await commands.stopPlayback();@@ -144,7 +181,7 @@ export function Player() {} else {// Ensure we seek to the current playback time before starting playbackawait commands.seekTo(Math.floor(editorState.playbackTime * FPS));- await commands.startPlayback(FPS, OUTPUT_SIZE);+ await commands.startPlayback(FPS, previewResolutionBase());setEditorState("playing", true);}if (editorState.playing) setEditorState("previewTime", null);@@ -209,15 +246,73 @@ export function Player() {return (<div class="flex flex-col flex-1 rounded-xl border bg-gray-1 dark:bg-gray-2 border-gray-3">- <div class="flex gap-3 justify-center p-3">- <AspectRatioSelect />- <EditorButton- tooltipText="Crop Video"- onClick={cropDialogHandler}- leftIcon={<IconCapCrop class="w-5 text-gray-12" />}- >- Crop- </EditorButton>+ <div class="flex items-center justify-between gap-3 p-3">+ <div class="flex items-center gap-3">+ <AspectRatioSelect />+ <EditorButton+ tooltipText="Crop Video"+ onClick={cropDialogHandler}+ leftIcon={<IconCapCrop class="w-5 text-gray-12" />}+ >+ Crop+ </EditorButton>+ </div>+ <div class="flex items-center gap-2">+ <span class="text-xs font-medium text-gray-11">Preview</span>+ <KSelect<{ label: string; value: PreviewQuality }>+ options={previewOptions}+ optionValue="value"+ optionTextValue="label"+ value={previewOptions.find(+ (option) => option.value === previewQuality(),+ )}+ onChange={(next) => {+ if (next) handlePreviewQualityChange(next.value);+ }}+ disallowEmptySelection+ itemComponent={(props) => (+ <MenuItem<typeof KSelect.Item>+ as={KSelect.Item}+ item={props.item}+ >+ <KSelect.ItemLabel class="flex-1">+ {props.item.rawValue.label}+ </KSelect.ItemLabel>+ <KSelect.ItemIndicator class="ml-auto text-blue-9">+ <IconCapCircleCheck />+ </KSelect.ItemIndicator>+ </MenuItem>+ )}+ >+ <KSelect.Trigger class="flex items-center gap-2 h-9 px-3 rounded-lg border border-gray-3 bg-gray-2 dark:bg-gray-3 text-sm text-gray-12">+ <KSelect.Value<{ label: string; value: PreviewQuality }>+ class="flex-1 text-left truncate"+ placeholder="Select preview quality"+ >+ {(state) =>+ state.selectedOption()?.label ?? "Select preview quality"+ }+ </KSelect.Value>+ <KSelect.Icon>+ <IconCapChevronDown class="size-4 text-gray-11" />+ </KSelect.Icon>+ </KSelect.Trigger>+ <KSelect.Portal>+ <PopperContent<typeof KSelect.Content>+ as={KSelect.Content}+ class={cx(topLeftAnimateClasses, "w-44")}+ >+ <MenuItem as="div" class="text-gray-11" data-disabled="true">+ Select preview quality+ </MenuItem>+ <MenuItemList<typeof KSelect.Listbox>+ as={KSelect.Listbox}+ class="max-h-40"+ />+ </PopperContent>+ </KSelect.Portal>+ </KSelect>+ </div></div><PreviewCanvas /><div class="flex overflow-hidden z-10 flex-row gap-3 justify-between items-center p-5">diff --git a/apps/desktop/src/routes/editor/context.ts b/apps/desktop/src/routes/editor/context.tsindex 3aae121e9..15c17e091 100644--- a/apps/desktop/src/routes/editor/context.ts+++ b/apps/desktop/src/routes/editor/context.ts@@ -50,6 +50,23 @@ export const OUTPUT_SIZE = {y: 1080,};+export type PreviewQuality = "half" | "full";++export const DEFAULT_PREVIEW_QUALITY: PreviewQuality = "full";++const previewQualityScale: Record<PreviewQuality, number> = {+ full: 1,+ half: 0.5,+};++export const getPreviewResolution = (quality: PreviewQuality): XY<number> => {+ const scale = previewQualityScale[quality];+ const width = (Math.max(2, Math.round(OUTPUT_SIZE.x * scale)) + 1) & ~1;+ const height = (Math.max(2, Math.round(OUTPUT_SIZE.y * scale)) + 1) & ~1;++ return { x: width, y: height };+};+export type TimelineTrackType = "clip" | "zoom" | "scene";export const MAX_ZOOM_IN = 3;@@ -358,6 +375,12 @@ export const [EditorContextProvider, useEditorContext] = createContextProvider(),);+ const [previewQuality, setPreviewQuality] = createSignal<PreviewQuality>(+ DEFAULT_PREVIEW_QUALITY,+ );++ const previewResolutionBase = () => getPreviewResolution(previewQuality());+const [dialog, setDialog] = createSignal<DialogState>({open: false,});@@ -516,6 +539,9 @@ export const [EditorContextProvider, useEditorContext] = createContextProvider(setExportState,micWaveforms,systemAudioWaveforms,+ previewQuality,+ setPreviewQuality,+ previewResolutionBase,};},// biome-ignore lint/style/noNonNullAssertion: it's ok@@ -584,7 +610,7 @@ export const [EditorInstanceContextProvider, useEditorInstanceContext] =events.renderFrameEvent.emit({frame_number: Math.floor(0),fps: FPS,- resolution_base: OUTPUT_SIZE,+ resolution_base: getPreviewResolution(DEFAULT_PREVIEW_QUALITY),});}});