Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(desktop): expand captions and keyboard editor side panels

Richie McIlroy403f39c80816
diff --git a/apps/desktop/src/routes/editor/CaptionsTab.tsx b/apps/desktop/src/routes/editor/CaptionsTab.tsx
index be2289045..17ea6a762 100644
--- a/apps/desktop/src/routes/editor/CaptionsTab.tsx
+++ b/apps/desktop/src/routes/editor/CaptionsTab.tsx
@@ -1,6 +1,5 @@
import { Button } from "@cap/ui-solid";
import { Select as KSelect } from "@kobalte/core/select";
-import { createWritableMemo } from "@solid-primitives/memo";
import { appLocalDataDir, join } from "@tauri-apps/api/path";
import { exists } from "@tauri-apps/plugin-fs";
import { cx } from "cva";
@@ -13,6 +12,7 @@ import {
onMount,
Show,
} from "solid-js";
+import { produce } from "solid-js/store";
import toast from "solid-toast";
import { Toggle } from "~/components/Toggle";
import { defaultCaptionSettings } from "~/store/captions";
@@ -22,9 +22,22 @@ import IconCapChevronDown from "~icons/cap/chevron-down";
import IconCapCircleCheck from "~icons/cap/circle-check";
import IconLucideCheck from "~icons/lucide/check";
import IconLucideDownload from "~icons/lucide/download";
-import { getColorPreviewBorderColor } from "./color-utils";
+import {
+ CAPTION_MODEL_FOLDER,
+ createCaptionTrackSegments,
+ DEFAULT_CAPTION_MODEL,
+ getCaptionGenerationErrorMessage,
+ syncCaptionWordsWithText,
+ transcribeEditorCaptions,
+} from "./captions";
import { useEditorContext } from "./context";
-import { TextInput } from "./TextInput";
+import {
+ CAPTION_POSITION_OPTIONS,
+ FONT_OPTIONS,
+ getTextWeightLabel,
+ HexColorInput,
+ TEXT_WEIGHT_OPTIONS,
+} from "./text-style";
import {
Field,
Input,
@@ -97,80 +110,48 @@ const LANGUAGE_OPTIONS: LanguageOption[] = [
{ code: "ta", label: "Tamil" },
];
-interface PositionOption {
- value: string;
- label: string;
-}
-
-const POSITION_OPTIONS: PositionOption[] = [
- { value: "top-left", label: "Top Left" },
- { value: "top-center", label: "Top Center" },
- { value: "top-right", label: "Top Right" },
- { value: "bottom-left", label: "Bottom Left" },
- { value: "bottom-center", label: "Bottom Center" },
- { value: "bottom-right", label: "Bottom Right" },
-];
-
-const DEFAULT_MODEL = "small";
-const MODEL_FOLDER = "transcription_models";
-
-const fontOptions = [
- { value: "System Sans-Serif", label: "System Sans-Serif" },
- { value: "System Serif", label: "System Serif" },
- { value: "System Monospace", label: "System Monospace" },
-];
-
-function RgbInput(props: { value: string; onChange: (value: string) => void }) {
- const [text, setText] = createWritableMemo(() => props.value);
- let prevColor = props.value;
- let colorInput!: HTMLInputElement;
-
- return (
- <div class="flex flex-row items-center gap-[0.75rem] relative">
- <button
- type="button"
- class="size-[2rem] rounded-[0.5rem]"
- style={{
- "background-color": text(),
- "box-shadow": `inset 0 0 0 1px ${getColorPreviewBorderColor(text())}`,
- }}
- onClick={() => colorInput.click()}
- />
- <input
- ref={colorInput}
- type="color"
- class="absolute left-0 bottom-0 size-[2rem] opacity-0"
- value={text()}
- onChange={(e) => {
- setText(e.target.value);
- props.onChange(e.target.value);
- }}
- />
- <TextInput
- class="w-[5rem] p-[0.375rem] border border-gray-3 text-gray-12 rounded-[0.5rem] bg-gray-2"
- value={text()}
- onFocus={() => {
- prevColor = props.value;
- }}
- onInput={(e) => {
- setText(e.currentTarget.value);
- props.onChange(e.currentTarget.value);
- }}
- onBlur={(e) => {
- if (!/^#[0-9A-F]{6}$/i.test(e.target.value)) {
- setText(prevColor);
- props.onChange(prevColor);
- }
- }}
- />
- </div>
- );
-}
-
export function CaptionsTab() {
const { project, setProject, editorInstance, editorState, setEditorState } =
useEditorContext();
+ const selectedCaptionIndex = () =>
+ editorState.timeline.selection?.type === "caption" &&
+ editorState.timeline.selection.indices.length === 1
+ ? editorState.timeline.selection.indices[0]
+ : -1;
+
+ const selectedCaptionSegment = () =>
+ project.timeline?.captionSegments?.[selectedCaptionIndex()];
+
+ const updateSelectedCaption = (
+ update: (
+ segment: NonNullable<ReturnType<typeof selectedCaptionSegment>>,
+ ) => void,
+ ) => {
+ const index = selectedCaptionIndex();
+ if (index < 0) return;
+
+ setProject(
+ produce((currentProject: typeof project) => {
+ const timelineSegment =
+ currentProject.timeline?.captionSegments?.[index];
+ if (!timelineSegment) return;
+
+ update(timelineSegment);
+
+ const captionSegment = currentProject.captions?.segments?.[index];
+ if (!captionSegment) return;
+
+ captionSegment.start = timelineSegment.start;
+ captionSegment.end = timelineSegment.end;
+ captionSegment.text = timelineSegment.text;
+ captionSegment.words = timelineSegment.words?.map((word) => ({
+ ...word,
+ }));
+ }),
+ );
+ };
+
const getSetting = <K extends keyof CaptionSettings>(
key: K,
): NonNullable<CaptionSettings[K]> =>
@@ -186,7 +167,7 @@ export function CaptionsTab() {
setProject("captions", "settings", key, value);
};
- const [selectedModel, setSelectedModel] = createSignal(DEFAULT_MODEL);
+ const [selectedModel, setSelectedModel] = createSignal(DEFAULT_CAPTION_MODEL);
const [selectedLanguage, setSelectedLanguage] = createSignal("auto");
const [downloadedModels, setDownloadedModels] = createSignal<string[]>([]);
@@ -221,7 +202,7 @@ export function CaptionsTab() {
onMount(async () => {
try {
const appDataDirPath = await appLocalDataDir();
- const modelsPath = await join(appDataDirPath, MODEL_FOLDER);
+ const modelsPath = await join(appDataDirPath, CAPTION_MODEL_FOLDER);
if (!(await exists(modelsPath))) {
await commands.createDir(modelsPath, true);
@@ -316,7 +297,7 @@ export function CaptionsTab() {
const checkModelExists = async (modelName: string) => {
const appDataDirPath = await appLocalDataDir();
- const modelsPath = await join(appDataDirPath, MODEL_FOLDER);
+ const modelsPath = await join(appDataDirPath, CAPTION_MODEL_FOLDER);
const path = await join(modelsPath, `${modelName}.bin`);
return await commands.checkModelExists(path);
};
@@ -329,7 +310,7 @@ export function CaptionsTab() {
setDownloadingModel(modelToDownload);
const appDataDirPath = await appLocalDataDir();
- const modelsPath = await join(appDataDirPath, MODEL_FOLDER);
+ const modelsPath = await join(appDataDirPath, CAPTION_MODEL_FOLDER);
const modelPath = await join(modelsPath, `${modelToDownload}.bin`);
try {
@@ -365,46 +346,21 @@ export function CaptionsTab() {
setIsGenerating(true);
try {
- const videoPath = editorInstance.path;
- const lang = selectedLanguage();
- const currentModelPath = await join(
- await appLocalDataDir(),
- MODEL_FOLDER,
- `${selectedModel()}.bin`,
- );
-
- const result = await commands.transcribeAudio(
- videoPath,
- currentModelPath,
- lang,
+ const result = await transcribeEditorCaptions(
+ editorInstance.path,
+ selectedModel(),
+ selectedLanguage(),
);
if (result && result.segments.length > 0) {
setProject("captions", "segments", result.segments);
updateCaptionSetting("enabled", true);
- const trackSegments = result.segments.map(
- (seg: {
- id: string;
- start: number;
- end: number;
- text: string;
- words?: Array<{ text: string; start: number; end: number }>;
- }) => ({
- id: seg.id,
- start: seg.start,
- end: seg.end,
- text: seg.text,
- words: seg.words ?? [],
- fadeDurationOverride: null,
- lingerDurationOverride: null,
- positionOverride: null,
- colorOverride: null,
- backgroundColorOverride: null,
- fontSizeOverride: null,
- }),
+ setProject(
+ "timeline",
+ "captionSegments",
+ createCaptionTrackSegments(result.segments),
);
- setProject("timeline", "captionSegments", trackSegments);
setEditorState("timeline", "tracks", "caption", true);
toast.success("Captions generated successfully!");
@@ -415,23 +371,7 @@ export function CaptionsTab() {
}
} catch (error) {
console.error("Error generating captions:", error);
- let errorMessage = "Unknown error occurred";
-
- if (error instanceof Error) {
- errorMessage = error.message;
- } else if (typeof error === "string") {
- errorMessage = error;
- }
-
- if (errorMessage.includes("No audio stream found")) {
- errorMessage = "No audio found in the video file";
- } else if (errorMessage.includes("Model file not found")) {
- errorMessage = "Caption model not found. Please download it first";
- } else if (errorMessage.includes("Failed to load Whisper model")) {
- errorMessage =
- "Failed to load the caption model. Try downloading it again";
- }
-
+ const errorMessage = getCaptionGenerationErrorMessage(error);
toast.error(`Failed to generate captions: ${errorMessage}`);
} finally {
setIsGenerating(false);
@@ -613,7 +553,7 @@ export function CaptionsTab() {
<div class="flex flex-col gap-2">
<span class="text-gray-11 text-sm">Font Family</span>
<KSelect<string>
- options={fontOptions.map((f) => f.value)}
+ options={FONT_OPTIONS.map((f) => f.value)}
value={getSetting("font")}
onChange={(value) => {
if (value === null) return;
@@ -627,7 +567,7 @@ export function CaptionsTab() {
>
<KSelect.ItemLabel class="flex-1">
{
- fontOptions.find(
+ FONT_OPTIONS.find(
(f) => f.value === props.item.rawValue,
)?.label
}
@@ -638,7 +578,7 @@ export function CaptionsTab() {
<KSelect.Trigger class="w-full flex items-center justify-between rounded-lg px-3 py-2 bg-gray-2 border border-gray-3 text-gray-12 hover:border-gray-4 hover:bg-gray-3 focus:border-blue-9 focus:ring-1 focus:ring-blue-9 transition-colors">
<KSelect.Value<string>>
{(state) =>
- fontOptions.find(
+ FONT_OPTIONS.find(
(f) => f.value === state.selectedOption(),
)?.label
}
@@ -695,8 +635,8 @@ export function CaptionsTab() {
</div>
<div class="flex flex-col gap-2">
- <span class="text-gray-11 text-sm">Font Color</span>
- <RgbInput
+ <span class="text-gray-11 text-sm">Text Color</span>
+ <HexColorInput
value={getSetting("color")}
onChange={(value) => updateCaptionSetting("color", value)}
/>
@@ -708,7 +648,7 @@ export function CaptionsTab() {
<div class="space-y-3">
<div class="flex flex-col gap-2">
<span class="text-gray-11 text-sm">Background Color</span>
- <RgbInput
+ <HexColorInput
value={getSetting("backgroundColor")}
onChange={(value) =>
updateCaptionSetting("backgroundColor", value)
@@ -734,7 +674,7 @@ export function CaptionsTab() {
<Field name="Position" icon={<IconCapMessageBubble />}>
<KSelect<string>
- options={POSITION_OPTIONS.map((p) => p.value)}
+ options={CAPTION_POSITION_OPTIONS.map((p) => p.value)}
value={getSetting("position")}
onChange={(value) => {
if (value === null) return;
@@ -748,7 +688,7 @@ export function CaptionsTab() {
>
<KSelect.ItemLabel class="flex-1">
{
- POSITION_OPTIONS.find(
+ CAPTION_POSITION_OPTIONS.find(
(p) => p.value === props.item.rawValue,
)?.label
}
@@ -761,7 +701,7 @@ export function CaptionsTab() {
{(state) => (
<span>
{
- POSITION_OPTIONS.find(
+ CAPTION_POSITION_OPTIONS.find(
(p) => p.value === state.selectedOption(),
)?.label
}
@@ -789,7 +729,7 @@ export function CaptionsTab() {
<div class="space-y-3">
<div class="flex flex-col gap-2">
<span class="text-gray-11 text-sm">Highlight Color</span>
- <RgbInput
+ <HexColorInput
value={getSetting("highlightColor")}
onChange={(value) =>
updateCaptionSetting("highlightColor", value)
@@ -817,11 +757,7 @@ export function CaptionsTab() {
<Field name="Font Weight" icon={<IconCapMessageBubble />}>
<KSelect
- options={[
- { label: "Normal", value: 400 },
- { label: "Medium", value: 500 },
- { label: "Bold", value: 700 },
- ]}
+ options={TEXT_WEIGHT_OPTIONS}
optionValue="value"
optionTextValue="label"
value={{
@@ -852,17 +788,10 @@ export function CaptionsTab() {
label: string;
value: number;
}> class="truncate">
- {(state) => {
- const selected = state.selectedOption();
- if (selected) return selected.label;
- const weight = getSetting("fontWeight");
- const option = [
- { label: "Normal", value: 400 },
- { label: "Medium", value: 500 },
- { label: "Bold", value: 700 },
- ].find((o) => o.value === weight);
- return option ? option.label : "Bold";
- }}
+ {(state) =>
+ state.selectedOption()?.label ??
+ getTextWeightLabel(getSetting("fontWeight"))
+ }
</KSelect.Value>
<KSelect.Icon>
<IconCapChevronDown class="size-4 shrink-0 transform transition-transform ui-expanded:rotate-180 text-[--gray-500]" />
@@ -902,19 +831,12 @@ export function CaptionsTab() {
}
>
{(() => {
- const selectedIndex = () =>
- editorState.timeline.selection?.type === "caption"
- ? editorState.timeline.selection.indices[0]
- : -1;
- const selectedSegment = () =>
- project.timeline?.captionSegments?.[selectedIndex()];
-
return (
<Field
name="Selected Caption Override"
icon={<IconCapMessageBubble />}
>
- <Show when={selectedSegment()}>
+ <Show when={selectedCaptionSegment()}>
{(seg) => (
<div class="space-y-3">
<Subfield name="Start Time">
@@ -924,13 +846,11 @@ export function CaptionsTab() {
step="0.1"
min={0}
onChange={(e) =>
- setProject(
- "timeline",
- "captionSegments",
- selectedIndex(),
- "start",
- Number.parseFloat(e.target.value),
- )
+ updateSelectedCaption((segment) => {
+ segment.start = Number.parseFloat(
+ e.target.value,
+ );
+ })
}
/>
</Subfield>
@@ -941,13 +861,9 @@ export function CaptionsTab() {
step="0.1"
min={seg().start}
onChange={(e) =>
- setProject(
- "timeline",
- "captionSegments",
- selectedIndex(),
- "end",
- Number.parseFloat(e.target.value),
- )
+ updateSelectedCaption((segment) => {
+ segment.end = Number.parseFloat(e.target.value);
+ })
}
/>
</Subfield>
@@ -956,13 +872,15 @@ export function CaptionsTab() {
type="text"
value={seg().text}
onChange={(e) =>
- setProject(
- "timeline",
- "captionSegments",
- selectedIndex(),
- "text",
- e.target.value,
- )
+ updateSelectedCaption((segment) => {
+ segment.text = e.target.value;
+ segment.words = syncCaptionWordsWithText(
+ e.target.value,
+ segment.words,
+ segment.start,
+ segment.end,
+ );
+ })
}
/>
</Subfield>
@@ -973,13 +891,9 @@ export function CaptionsTab() {
getSetting("fadeDuration")) * 100,
]}
onChange={(v) =>
- setProject(
- "timeline",
- "captionSegments",
- selectedIndex(),
- "fadeDurationOverride",
- v[0] / 100,
- )
+ updateSelectedCaption((segment) => {
+ segment.fadeDurationOverride = v[0] / 100;
+ })
}
minValue={0}
maxValue={50}
diff --git a/apps/desktop/src/routes/editor/ConfigSidebar.tsx b/apps/desktop/src/routes/editor/ConfigSidebar.tsx
index 91117b678..442d28c59 100644
--- a/apps/desktop/src/routes/editor/ConfigSidebar.tsx
+++ b/apps/desktop/src/routes/editor/ConfigSidebar.tsx
@@ -15,7 +15,6 @@ import { createWritableMemo } from "@solid-primitives/memo";
import { convertFileSrc } from "@tauri-apps/api/core";
import { appDataDir, resolveResource } from "@tauri-apps/api/path";
import { BaseDirectory, writeFile } from "@tauri-apps/plugin-fs";
-import { type as ostype } from "@tauri-apps/plugin-os";
import { cx } from "cva";
import {
batch,
@@ -41,21 +40,23 @@ import imageBg from "~/assets/illustrations/image.webp";
import transparentBg from "~/assets/illustrations/transparent.webp";
import { Toggle } from "~/components/Toggle";
import { generalSettingsStore } from "~/store";
-import {
- type BackgroundSource,
- type CameraShape,
- type ClipOffsets,
- type CursorAnimationStyle,
- type CursorType,
- commands,
- type SceneSegment,
- type StereoMode,
- type TimelineSegment,
- type ZoomSegment,
+import type {
+ BackgroundSource,
+ CameraShape,
+ CaptionTrackSegment,
+ ClipOffsets,
+ CursorAnimationStyle,
+ CursorType,
+ KeyboardTrackSegment,
+ SceneSegment,
+ StereoMode,
+ TimelineSegment,
+ ZoomSegment,
} from "~/utils/tauri";
import IconLucideBoxSelect from "~icons/lucide/box-select";
import IconLucideGauge from "~icons/lucide/gauge";
import IconLucideGrid from "~icons/lucide/grid";
+import IconLucideKeyboard from "~icons/lucide/keyboard";
import IconLucideMonitor from "~icons/lucide/monitor";
import IconLucideMoon from "~icons/lucide/moon";
import IconLucidePalette from "~icons/lucide/palette";
@@ -65,20 +66,13 @@ import IconLucideTimer from "~icons/lucide/timer";
import IconLucideType from "~icons/lucide/type";
import IconLucideWind from "~icons/lucide/wind";
import { CaptionsTab } from "./CaptionsTab";
-import {
- getColorPreviewBorderColor,
- hexToRgb,
- RgbInput,
- rgbToHex,
-} from "./color-utils";
+import { syncCaptionWordsWithText } from "./captions";
+import { getColorPreviewBorderColor, hexToRgb, RgbInput } from "./color-utils";
import { type CornerRoundingType, useEditorContext } from "./context";
import { GradientEditor } from "./GradientEditor";
+import { KeyboardTab } from "./KeyboardTab";
import { evaluateMask, type MaskKind, type MaskSegment } from "./masks";
-import {
- DEFAULT_GRADIENT_FROM,
- DEFAULT_GRADIENT_TO,
- type RGBColor,
-} from "./projectConfig";
+import { DEFAULT_GRADIENT_FROM, DEFAULT_GRADIENT_TO } from "./projectConfig";
import ShadowSettings from "./ShadowSettings";
import { TextInput } from "./TextInput";
import type { TextSegment } from "./text";
@@ -86,6 +80,7 @@ import {
ComingSoonTooltip,
EditorButton,
Field,
+ Input,
MenuItem,
MenuItemList,
PopperContent,
@@ -93,6 +88,7 @@ import {
Subfield,
topSlideAnimateClasses,
} from "./ui";
+import { formatTime } from "./utils";
const BACKGROUND_SOURCES = {
wallpaper: "Wallpaper",
@@ -293,7 +289,9 @@ const TAB_IDS = {
transcript: "transcript",
audio: "audio",
cursor: "cursor",
+ keyboard: "keyboard",
hotkeys: "hotkeys",
+ captions: "captions",
} as const;
export function ConfigSidebar() {
@@ -355,6 +353,7 @@ export function ConfigSidebar() {
| "transcript"
| "audio"
| "cursor"
+ | "keyboard"
| "hotkeys"
| "captions",
});
@@ -386,7 +385,11 @@ export function ConfigSidebar() {
),
},
{
- id: "captions" as const,
+ id: TAB_IDS.keyboard,
+ icon: IconLucideKeyboard,
+ },
+ {
+ id: TAB_IDS.captions,
icon: IconCapMessageBubble,
},
// { id: "hotkeys" as const, icon: IconCapHotkeys },
@@ -810,11 +813,17 @@ export function ConfigSidebar() {
</Field>
</KTabs.Content>
<KTabs.Content
- value="captions"
+ value={TAB_IDS.captions}
class="flex flex-col flex-1 gap-6 p-4 min-h-0"
>
<CaptionsTab />
</KTabs.Content>
+ <KTabs.Content
+ value={TAB_IDS.keyboard}
+ class="flex flex-col flex-1 gap-6 p-4 min-h-0"
+ >
+ <KeyboardTab />
+ </KTabs.Content>
</div>
<div
style={{
@@ -830,6 +839,151 @@ export function ConfigSidebar() {
<Show when={editorState.timeline.selection}>
{(selection) => (
<Suspense>
+ <Show
+ when={(() => {
+ const captionSelection = selection();
+ if (captionSelection.type !== "caption") return;
+
+ const segments = captionSelection.indices
+ .map((index) => ({
+ index,
+ segment: project.timeline?.captionSegments?.[index],
+ }))
+ .filter(
+ (
+ item,
+ ): item is {
+ index: number;
+ segment: CaptionTrackSegment;
+ } => item.segment !== undefined,
+ );
+
+ if (segments.length === 0) {
+ setEditorState("timeline", "selection", null);
+ return;
+ }
+
+ return { selection: captionSelection, segments };
+ })()}
+ >
+ {(value) => (
+ <div class="space-y-4">
+ <div class="flex flex-row justify-between items-center">
+ <div class="flex gap-2 items-center">
+ <EditorButton
+ onClick={() =>
+ setEditorState("timeline", "selection", null)
+ }
+ leftIcon={<IconLucideCheck />}
+ >
+ Done
+ </EditorButton>
+ <span class="text-sm text-gray-10">
+ {value().segments.length} caption{" "}
+ {value().segments.length === 1
+ ? "segment"
+ : "segments"}{" "}
+ selected
+ </span>
+ </div>
+ <EditorButton
+ variant="danger"
+ onClick={() =>
+ projectActions.deleteCaptionSegments(
+ value().segments.map((s) => s.index),
+ )
+ }
+ leftIcon={<IconCapTrash />}
+ >
+ Delete
+ </EditorButton>
+ </div>
+ <For each={value().segments}>
+ {(item) => (
+ <div class="p-4 rounded-lg border border-gray-200">
+ <CaptionSegmentConfig
+ segment={item.segment}
+ segmentIndex={item.index}
+ />
+ </div>
+ )}
+ </For>
+ </div>
+ )}
+ </Show>
+ <Show
+ when={(() => {
+ const keyboardSelection = selection();
+ if (keyboardSelection.type !== "keyboard") return;
+
+ const segments = keyboardSelection.indices
+ .map((index) => ({
+ index,
+ segment: project.timeline?.keyboardSegments?.[index],
+ }))
+ .filter(
+ (
+ item,
+ ): item is {
+ index: number;
+ segment: KeyboardTrackSegment;
+ } => item.segment !== undefined,
+ );
+
+ if (segments.length === 0) {
+ setEditorState("timeline", "selection", null);
+ return;
+ }
+
+ return { selection: keyboardSelection, segments };
+ })()}
+ >
+ {(value) => (
+ <div class="space-y-4">
+ <div class="flex flex-row justify-between items-center">
+ <div class="flex gap-2 items-center">
+ <EditorButton
+ onClick={() => {
+ setEditorState("timeline", "selection", null);
+ setState("selectedTab", TAB_IDS.keyboard);
+ }}
+ leftIcon={<IconLucideCheck />}
+ >
+ Done
+ </EditorButton>
+ <span class="text-sm text-gray-10">
+ {value().segments.length} keyboard{" "}
+ {value().segments.length === 1
+ ? "segment"
+ : "segments"}{" "}
+ selected
+ </span>
+ </div>
+ <EditorButton
+ variant="danger"
+ onClick={() =>
+ projectActions.deleteKeyboardSegments(
+ value().segments.map((s) => s.index),
+ )
+ }
+ leftIcon={<IconCapTrash />}
+ >
+ Delete
+ </EditorButton>
+ </div>
+ <For each={value().segments}>
+ {(item) => (
+ <div class="p-4 rounded-lg border border-gray-200">
+ <KeyboardSegmentConfig
+ segment={item.segment}
+ segmentIndex={item.index}
+ />
+ </div>
+ )}
+ </For>
+ </div>
+ )}
+ </Show>
<Show
when={(() => {
const textSelection = selection();
@@ -2613,6 +2767,215 @@ function TextSegmentConfig(props: {
);
}
+function KeyboardSegmentConfig(props: {
+ segmentIndex: number;
+ segment: KeyboardTrackSegment;
+}) {
+ const { setProject } = useEditorContext();
+
+ const updateSegment = (fn: (segment: KeyboardTrackSegment) => void) => {
+ setProject(
+ "timeline",
+ "keyboardSegments",
+ produce((segments) => {
+ const segment = segments?.[props.segmentIndex];
+ if (!segment) return;
+ fn(segment);
+ }),
+ );
+ };
+
+ return (
+ <div class="space-y-4">
+ <Field
+ name={`Keyboard ${props.segmentIndex + 1}`}
+ icon={<IconLucideKeyboard class="size-4" />}
+ >
+ <Input
+ type="text"
+ value={props.segment.displayText}
+ onChange={(e) =>
+ updateSegment((segment) => {
+ segment.displayText = e.currentTarget.value;
+ })
+ }
+ />
+ </Field>
+ <Field name="Timing" icon={<IconLucideTimer class="size-4" />}>
+ <div class="rounded-xl border border-gray-3 bg-gray-2/70 p-3 space-y-3">
+ <div class="grid grid-cols-[1fr_auto_1fr] gap-2 items-start">
+ <div class="rounded-lg border border-gray-3 bg-gray-1/80 p-2.5 space-y-2">
+ <div class="flex items-center justify-between text-[10px] uppercase tracking-[0.08em] text-gray-10">
+ <span>Start</span>
+ <span>{formatTime(props.segment.start)}</span>
+ </div>
+ <Input
+ type="number"
+ value={props.segment.start.toFixed(2)}
+ step="0.1"
+ min={0}
+ onChange={(e) =>
+ updateSegment((segment) => {
+ segment.start = Number.parseFloat(e.currentTarget.value);
+ })
+ }
+ />
+ </div>
+ <div class="pt-10 text-xs font-medium text-gray-10">to</div>
+ <div class="rounded-lg border border-gray-3 bg-gray-1/80 p-2.5 space-y-2">
+ <div class="flex items-center justify-between text-[10px] uppercase tracking-[0.08em] text-gray-10">
+ <span>End</span>
+ <span>{formatTime(props.segment.end)}</span>
+ </div>
+ <Input
+ type="number"
+ value={props.segment.end.toFixed(2)}
+ step="0.1"
+ min={props.segment.start}
+ onChange={(e) =>
+ updateSegment((segment) => {
+ segment.end = Number.parseFloat(e.currentTarget.value);
+ })
+ }
+ />
+ </div>
+ </div>
+ <div class="flex items-center justify-between rounded-lg bg-gray-1/70 px-3 py-2 text-xs text-gray-11">
+ <span>Duration</span>
+ <span class="font-medium text-gray-12">
+ {Math.max(0, props.segment.end - props.segment.start).toFixed(2)}s
+ </span>
+ </div>
+ </div>
+ </Field>
+ <Field name="Fade Duration" icon={<IconLucideTimer class="size-4" />}>
+ <Slider
+ value={[(props.segment.fadeDurationOverride ?? 0.15) * 100]}
+ onChange={([value]) =>
+ updateSegment((segment) => {
+ segment.fadeDurationOverride = value / 100;
+ })
+ }
+ minValue={0}
+ maxValue={50}
+ step={1}
+ />
+ </Field>
+ </div>
+ );
+}
+
+function CaptionSegmentConfig(props: {
+ segmentIndex: number;
+ segment: CaptionTrackSegment;
+}) {
+ const { setProject } = useEditorContext();
+
+ const updateSegment = (fn: (segment: CaptionTrackSegment) => void) => {
+ setProject(
+ produce((project) => {
+ const timelineSegment =
+ project.timeline?.captionSegments?.[props.segmentIndex];
+ if (!timelineSegment) return;
+
+ fn(timelineSegment);
+
+ const captionSegment = project.captions?.segments?.[props.segmentIndex];
+ if (!captionSegment) return;
+
+ captionSegment.start = timelineSegment.start;
+ captionSegment.end = timelineSegment.end;
+ captionSegment.text = timelineSegment.text;
+ captionSegment.words = timelineSegment.words?.map((word) => ({
+ ...word,
+ }));
+ }),
+ );
+ };
+
+ return (
+ <div class="space-y-4">
+ <Field
+ name={`Transcript ${props.segmentIndex + 1}`}
+ icon={<IconCapMessageBubble />}
+ >
+ <textarea
+ class="flex-1 px-3 py-2 rounded-lg border border-gray-3 bg-gray-2 text-gray-12 resize-none min-h-[96px] w-full"
+ value={props.segment.text}
+ onInput={(e) =>
+ updateSegment((segment) => {
+ segment.text = e.currentTarget.value;
+ segment.words = syncCaptionWordsWithText(
+ e.currentTarget.value,
+ segment.words,
+ segment.start,
+ segment.end,
+ );
+ })
+ }
+ />
+ </Field>
+ <Field name="Timing" icon={<IconLucideTimer class="size-4" />}>
+ <div class="rounded-xl border border-gray-3 bg-gray-2/70 p-3 space-y-3">
+ <div class="grid grid-cols-[1fr_auto_1fr] gap-2 items-start">
+ <div class="rounded-lg border border-gray-3 bg-gray-1/80 p-2.5 space-y-2">
+ <div class="flex items-center justify-between text-[10px] uppercase tracking-[0.08em] text-gray-10">
+ <span>Start</span>
+ <span>{formatTime(props.segment.start)}</span>
+ </div>
+ <Input
+ type="number"
+ value={props.segment.start.toFixed(2)}
+ step="0.1"
+ min={0}
+ onChange={(
+ e: Event & {
+ currentTarget: HTMLInputElement;
+ target: HTMLInputElement;
+ },
+ ) =>
+ updateSegment((segment) => {
+ segment.start = Number.parseFloat(e.target.value);
+ })
+ }
+ />
+ </div>
+ <div class="pt-10 text-xs font-medium text-gray-10">to</div>
+ <div class="rounded-lg border border-gray-3 bg-gray-1/80 p-2.5 space-y-2">
+ <div class="flex items-center justify-between text-[10px] uppercase tracking-[0.08em] text-gray-10">
+ <span>End</span>
+ <span>{formatTime(props.segment.end)}</span>
+ </div>
+ <Input
+ type="number"
+ value={props.segment.end.toFixed(2)}
+ step="0.1"
+ min={props.segment.start}
+ onChange={(
+ e: Event & {
+ currentTarget: HTMLInputElement;
+ target: HTMLInputElement;
+ },
+ ) =>
+ updateSegment((segment) => {
+ segment.end = Number.parseFloat(e.target.value);
+ })
+ }
+ />
+ </div>
+ </div>
+ <div class="flex items-center justify-between rounded-lg bg-gray-1/70 px-3 py-2 text-xs text-gray-11">
+ <span>Duration</span>
+ <span class="font-medium text-gray-12">
+ {Math.max(0, props.segment.end - props.segment.start).toFixed(2)}s
+ </span>
+ </div>
+ </div>
+ </Field>
+ </div>
+ );
+}
+
function MaskSegmentConfig(props: {
segmentIndex: number;
segment: MaskSegment;
diff --git a/apps/desktop/src/routes/editor/KeyboardTab.tsx b/apps/desktop/src/routes/editor/KeyboardTab.tsx
index bed8612e5..6233da9a9 100644
--- a/apps/desktop/src/routes/editor/KeyboardTab.tsx
+++ b/apps/desktop/src/routes/editor/KeyboardTab.tsx
@@ -1,7 +1,7 @@
import { Button } from "@cap/ui-solid";
import { Select as KSelect } from "@kobalte/core/select";
import { cx } from "cva";
-import { createMemo, createSignal, Show } from "solid-js";
+import { batch, createMemo, createSignal, Show } from "solid-js";
import { Toggle } from "~/components/Toggle";
import {
defaultKeyboardSettings,
@@ -9,7 +9,15 @@ import {
} from "~/store/keyboard";
import { commands } from "~/utils/tauri";
import IconCapChevronDown from "~icons/cap/chevron-down";
+import IconCapCircleCheck from "~icons/cap/circle-check";
import { useEditorContext } from "./context";
+import {
+ FONT_OPTIONS,
+ getTextWeightLabel,
+ HexColorInput,
+ KEYBOARD_POSITION_OPTIONS,
+ TEXT_WEIGHT_OPTIONS,
+} from "./text-style";
import {
Field,
Input,
@@ -47,7 +55,7 @@ export function KeyboardTab() {
});
return;
}
- setProject("keyboard", "settings", key as string, value);
+ setProject("keyboard", "settings", key, value);
};
const hasKeyboardSegments = createMemo(
@@ -56,6 +64,26 @@ export function KeyboardTab() {
const [isGenerating, setIsGenerating] = createSignal(false);
+ const ensureKeyboardSettings = (enabled: boolean) => {
+ if (!project?.keyboard) {
+ setProject("keyboard", {
+ settings: { ...defaultKeyboardSettings, enabled },
+ });
+ return;
+ }
+ setProject("keyboard", "settings", "enabled", enabled);
+ };
+
+ const setKeyboardVisible = (enabled: boolean) => {
+ batch(() => {
+ ensureKeyboardSettings(enabled);
+ setEditorState("timeline", "tracks", "keyboard", enabled);
+ if (!enabled && editorState.timeline.selection?.type === "keyboard") {
+ setEditorState("timeline", "selection", null);
+ }
+ });
+ };
+
const generateSegments = async () => {
setIsGenerating(true);
try {
@@ -67,8 +95,11 @@ export function KeyboardTab() {
);
if (segments.length > 0) {
- setProject("timeline", "keyboardSegments", segments);
- setEditorState("timeline", "tracks", "keyboard", true);
+ batch(() => {
+ ensureKeyboardSettings(true);
+ setProject("timeline", "keyboardSegments", segments);
+ setEditorState("timeline", "tracks", "keyboard", true);
+ });
}
} catch (e) {
console.error("Failed to generate keyboard segments:", e);
@@ -97,7 +128,7 @@ export function KeyboardTab() {
<Subfield name="Show Keyboard Presses">
<Toggle
checked={getSetting("enabled")}
- onChange={(checked) => updateSetting("enabled", checked)}
+ onChange={setKeyboardVisible}
/>
</Subfield>
@@ -109,17 +140,87 @@ export function KeyboardTab() {
>
<Field name="Font Settings" icon={<IconLucideKeyboard />}>
<div class="space-y-3">
+ <div class="flex flex-col gap-2">
+ <span class="text-gray-11 text-sm">Font Family</span>
+ <KSelect<string>
+ options={FONT_OPTIONS.map((f) => f.value)}
+ value={getSetting("font")}
+ onChange={(value) => {
+ if (value === null) return;
+ updateSetting("font", value);
+ }}
+ itemComponent={(props) => (
+ <MenuItem<typeof KSelect.Item>
+ as={KSelect.Item}
+ item={props.item}
+ >
+ <KSelect.ItemLabel class="flex-1">
+ {
+ FONT_OPTIONS.find(
+ (f) => f.value === props.item.rawValue,
+ )?.label
+ }
+ </KSelect.ItemLabel>
+ </MenuItem>
+ )}
+ >
+ <KSelect.Trigger class="w-full flex items-center justify-between rounded-lg px-3 py-2 bg-gray-2 border border-gray-3 text-gray-12 hover:border-gray-4 hover:bg-gray-3 focus:border-blue-9 focus:ring-1 focus:ring-blue-9 transition-colors">
+ <KSelect.Value<string>>
+ {(state) =>
+ FONT_OPTIONS.find(
+ (f) => f.value === state.selectedOption(),
+ )?.label
+ }
+ </KSelect.Value>
+ <KSelect.Icon>
+ <IconCapChevronDown />
+ </KSelect.Icon>
+ </KSelect.Trigger>
+ <KSelect.Portal>
+ <PopperContent<typeof KSelect.Content>
+ as={KSelect.Content}
+ class={topSlideAnimateClasses}
+ >
+ <MenuItemList<typeof KSelect.Listbox>
+ class="max-h-48 overflow-y-auto"
+ as={KSelect.Listbox}
+ />
+ </PopperContent>
+ </KSelect.Portal>
+ </KSelect>
+ </div>
+
<div class="flex flex-col gap-2">
<span class="text-gray-11 text-sm">Size</span>
<Slider
value={[getSetting("size")]}
onChange={(v) => updateSetting("size", v[0])}
minValue={12}
- maxValue={72}
+ maxValue={100}
step={1}
/>
</div>
+ <div class="flex flex-col gap-2">
+ <span class="text-gray-11 text-sm">Text Color</span>
+ <HexColorInput
+ value={getSetting("color")}
+ onChange={(value) => updateSetting("color", value)}
+ />
+ </div>
+ </div>
+ </Field>
+
+ <Field name="Background Settings" icon={<IconLucideKeyboard />}>
+ <div class="space-y-3">
+ <div class="flex flex-col gap-2">
+ <span class="text-gray-11 text-sm">Background Color</span>
+ <HexColorInput
+ value={getSetting("backgroundColor")}
+ onChange={(value) => updateSetting("backgroundColor", value)}
+ />
+ </div>
+
<div class="flex flex-col gap-2">
<span class="text-gray-11 text-sm">Background Opacity</span>
<Slider
@@ -133,13 +234,59 @@ export function KeyboardTab() {
</div>
</Field>
+ <Field name="Position" icon={<IconLucideKeyboard />}>
+ <KSelect<string>
+ options={KEYBOARD_POSITION_OPTIONS.map((p) => p.value)}
+ value={getSetting("position")}
+ onChange={(value) => {
+ if (value === null) return;
+ updateSetting("position", value);
+ }}
+ itemComponent={(props) => (
+ <MenuItem<typeof KSelect.Item>
+ as={KSelect.Item}
+ item={props.item}
+ >
+ <KSelect.ItemLabel class="flex-1">
+ {
+ KEYBOARD_POSITION_OPTIONS.find(
+ (p) => p.value === props.item.rawValue,
+ )?.label
+ }
+ </KSelect.ItemLabel>
+ </MenuItem>
+ )}
+ >
+ <KSelect.Trigger class="w-full flex items-center justify-between rounded-lg px-3 py-2 bg-gray-2 border border-gray-3 text-gray-12 hover:border-gray-4 hover:bg-gray-3 focus:border-blue-9 focus:ring-1 focus:ring-blue-9 transition-colors">
+ <KSelect.Value<string>>
+ {(state) => (
+ <span>
+ {
+ KEYBOARD_POSITION_OPTIONS.find(
+ (p) => p.value === state.selectedOption(),
+ )?.label
+ }
+ </span>
+ )}
+ </KSelect.Value>
+ <KSelect.Icon>
+ <IconCapChevronDown />
+ </KSelect.Icon>
+ </KSelect.Trigger>
+ <KSelect.Portal>
+ <PopperContent<typeof KSelect.Content>
+ as={KSelect.Content}
+ class={topSlideAnimateClasses}
+ >
+ <MenuItemList<typeof KSelect.Listbox> as={KSelect.Listbox} />
+ </PopperContent>
+ </KSelect.Portal>
+ </KSelect>
+ </Field>
+
<Field name="Font Weight" icon={<IconLucideKeyboard />}>
<KSelect
- options={[
- { label: "Normal", value: 400 },
- { label: "Medium", value: 500 },
- { label: "Bold", value: 700 },
- ]}
+ options={TEXT_WEIGHT_OPTIONS}
optionValue="value"
optionTextValue="label"
value={{
@@ -158,6 +305,9 @@ export function KeyboardTab() {
<KSelect.ItemLabel class="flex-1">
{selectItemProps.item.rawValue.label}
</KSelect.ItemLabel>
+ <KSelect.ItemIndicator class="ml-auto text-blue-9">
+ <IconCapCircleCheck />
+ </KSelect.ItemIndicator>
</MenuItem>
)}
>
@@ -166,17 +316,10 @@ export function KeyboardTab() {
label: string;
value: number;
}> class="truncate">
- {(state) => {
- const selected = state.selectedOption();
- if (selected) return selected.label;
- const weight = getSetting("fontWeight");
- const option = [
- { label: "Normal", value: 400 },
- { label: "Medium", value: 500 },
- { label: "Bold", value: 700 },
- ].find((o) => o.value === weight);
- return option ? option.label : "Bold";
- }}
+ {(state) =>
+ state.selectedOption()?.label ??
+ getTextWeightLabel(getSetting("fontWeight"))
+ }
</KSelect.Value>
<KSelect.Icon>
<IconCapChevronDown class="size-4 shrink-0 transform transition-transform ui-expanded:rotate-180 text-[--gray-500]" />
diff --git a/apps/desktop/src/routes/editor/ui.tsx b/apps/desktop/src/routes/editor/ui.tsx
index 932de6427..16b08142e 100644
--- a/apps/desktop/src/routes/editor/ui.tsx
+++ b/apps/desktop/src/routes/editor/ui.tsx
@@ -441,7 +441,7 @@ export const topSlideAnimateClasses =
"ui-expanded:animate-in ui-expanded:fade-in ui-expanded:slide-in-from-top-1 ui-closed:animate-out ui-closed:fade-out ui-closed:slide-out-to-top-1 origin-top-center";
export function ComingSoonTooltip(
- props: ComponentProps<typeof KTooltip> & any,
+ props: ComponentProps<typeof KTooltip> & Record<string, unknown>,
) {
const [trigger, root] = splitProps(props, ["children", "as"]);
return (