Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(editor): redesign the crop dialog and inspector

Richie McIlroy38001d4c350a
diff --git a/apps/desktop/src/routes/editor/Editor.tsx b/apps/desktop/src/routes/editor/Editor.tsx
index bfbae6605..aac65a6c3 100644
--- a/apps/desktop/src/routes/editor/Editor.tsx
+++ b/apps/desktop/src/routes/editor/Editor.tsx
@@ -1,7 +1,8 @@
-import { Button } from "@cap/ui-solid";
+import { Dialog as KDialog } from "@kobalte/core/dialog";
import { NumberField } from "@kobalte/core/number-field";
import { createElementBounds } from "@solid-primitives/bounds";
import { trackDeep } from "@solid-primitives/deep";
+import { createEventListener } from "@solid-primitives/event-listener";
import { debounce, throttle } from "@solid-primitives/scheduled";
import { makePersisted } from "@solid-primitives/storage";
import { createMutation, createQuery, skipToken } from "@tanstack/solid-query";
@@ -11,12 +12,14 @@ import { emitTo } from "@tauri-apps/api/event";
import { Menu } from "@tauri-apps/api/menu";
import { getCurrentWindow } from "@tauri-apps/api/window";
import { ask } from "@tauri-apps/plugin-dialog";
+import { cx } from "cva";
import {
createEffect,
createMemo,
createResource,
createSignal,
ErrorBoundary,
+ For,
lazy,
Match,
on,
@@ -28,8 +31,8 @@ import {
} from "solid-js";
import { createStore } from "solid-js/store";
import toast from "solid-toast";
-import { Transition } from "solid-transition-group";
import {
+ COMMON_RATIOS,
CROP_ZERO,
type CropBounds,
Cropper,
@@ -1458,6 +1461,77 @@ function Dialogs() {
await menu.popup(pos);
}
+ const saveCrop = () => {
+ const bounds = crop();
+ if (!frameLoaded() || bounds.width <= 0 || bounds.height <= 0)
+ return;
+ if (!cropTargetValid()) {
+ toast.error(
+ "Crop target changed. Reopen Crop to continue.",
+ );
+ return;
+ }
+ const nextCrop = {
+ position: { x: bounds.x, y: bounds.y },
+ size: { x: bounds.width, y: bounds.height },
+ };
+ if (cropTarget === null)
+ setState("background", "crop", nextCrop);
+ else
+ setState(
+ "timeline",
+ "styleSegments",
+ cropTarget,
+ "overrides",
+ "background",
+ "crop",
+ nextCrop,
+ );
+ setDialog((d) => ({ ...d, open: false }));
+ };
+
+ const closeCrop = () =>
+ setDialog((d) => ({ ...d, open: false }));
+ const styleScopeLabel =
+ cropTarget === null
+ ? null
+ : `Editing Style ${cropTarget + 1} only`;
+
+ createEventListener(window, "keydown", (e: KeyboardEvent) => {
+ if (e.key !== "Enter" || e.isComposing) return;
+ const target = e.target as HTMLElement | null;
+ if (
+ target?.tagName === "INPUT" ||
+ target?.tagName === "TEXTAREA" ||
+ target?.isContentEditable ||
+ target?.closest("button")
+ )
+ return;
+ e.preventDefault();
+ saveCrop();
+ });
+
+ const isFull = () =>
+ crop().width === display.width &&
+ crop().height === display.height;
+ const isCentered = () =>
+ crop().x === Math.round((display.width - crop().width) / 2) &&
+ crop().y === Math.round((display.height - crop().height) / 2);
+ const isUntouched = () =>
+ crop().x === dialog().position.x &&
+ crop().y === dialog().position.y &&
+ crop().width === dialog().size.x &&
+ crop().height === dialog().size.y &&
+ aspect() === null;
+
+ const ratioLabel = (ratio: Ratio | null) =>
+ ratio ? `${ratio[0]}:${ratio[1]}` : "Free";
+ const ratioSelected = (ratio: Ratio | null) => {
+ const current = aspect();
+ if (!ratio || !current) return ratio === current;
+ return current[0] === ratio[0] && current[1] === ratio[1];
+ };
+
function BoundInput(props: {
field: keyof CropBounds;
min?: number;
@@ -1473,9 +1547,10 @@ function Dialogs() {
}}
changeOnWheel={true}
format={false}
+ class="w-[60px]"
>
<NumberField.Input
- class="rounded-[7px] bg-ed-ctl border-0 py-[18px] h-8 font-normal placeholder:text-ed-text-3 text-xs caret-ed-accent transition-shadow duration-200 hover:bg-ed-ctl-hover focus:bg-ed-ctl-hover focus:ring-1 focus:ring-ed-accent px-2 w-full text-[13px] outline-hidden text-ed-text-1"
+ class="h-7 w-full rounded-[7px] border-0 bg-ed-ctl px-2 text-[12.5px] text-ed-text-1 caret-ed-accent tabular-nums outline-hidden transition-[background-color,box-shadow] duration-150 hover:bg-ed-ctl-hover focus:bg-ed-ctl-hover focus:ring-1 focus:ring-ed-accent"
onKeyDown={composeEventHandlers<HTMLInputElement>([
(e) => e.stopPropagation(),
])}
@@ -1484,270 +1559,244 @@ function Dialogs() {
);
}
+ const inspectorLabel = "text-xs font-medium text-ed-text-2";
+ const inspectorRow =
+ "flex h-7 items-center justify-between gap-3";
+ const actionButton =
+ "flex h-7 flex-1 items-center justify-center rounded-[7px] bg-ed-ctl text-xs font-medium text-ed-text-1 outline-hidden transition-colors duration-100 enabled:hover:bg-ed-ctl-hover enabled:active:bg-ed-ctl-active focus-visible:ring-1 focus-visible:ring-ed-accent disabled:opacity-45";
+
return (
<>
- <Dialog.Header>
- <div class="flex flex-row space-x-8">
- <div class="flex flex-row items-center space-x-3 text-gray-11">
- <span>Size</span>
- <div class="w-13">
- <BoundInput field="width" max={display.width} />
+ <div class="flex h-[52px] shrink-0 items-center justify-between px-5">
+ <KDialog.Title class="text-sm font-semibold text-ed-text-1">
+ Crop
+ </KDialog.Title>
+ <EditorButton
+ leftIcon={<IconLucideX />}
+ tooltipText="Close"
+ onClick={closeCrop}
+ />
+ </div>
+ <div class="flex items-stretch gap-4 px-5">
+ <div
+ class="relative flex items-center justify-center rounded-xl bg-ed-stage p-4"
+ style={{ width: `${boxSize().w + 32}px` }}
+ >
+ <div
+ class="relative transition-opacity duration-200"
+ classList={{ "opacity-0": !frameLoaded() }}
+ style={{
+ width: `${boxSize().w}px`,
+ height: `${boxSize().h}px`,
+ }}
+ >
+ <Cropper
+ ref={cropperRef}
+ onCropChange={setCrop}
+ aspectRatio={aspect() ?? undefined}
+ targetSize={{
+ x: display.width,
+ y: display.height,
+ }}
+ initialCrop={initialBounds}
+ snapToRatioEnabled={snapToRatio()}
+ useBackdropFilter={true}
+ allowLightMode={true}
+ appearance="editor"
+ snapToAlignmentEnabled={true}
+ onContextMenu={(e) => showCropOptionsMenu(e, true)}
+ >
+ <img
+ class="block h-full w-full select-none pointer-events-none"
+ alt="Current frame"
+ onError={() => {
+ const url = frameUrl();
+ if (url) {
+ setFrameUrl(null);
+ URL.revokeObjectURL(url);
+ }
+ setFrameError(true);
+ }}
+ onLoad={() => setFrameLoaded(true)}
+ src={
+ frameUrl() ??
+ (frameError() ? screenshotSrc : undefined)
+ }
+ />
+ </Cropper>
+ </div>
+ <Show when={!frameLoaded()}>
+ <div class="absolute inset-0 z-40 flex flex-col items-center justify-center gap-3 rounded-xl bg-ed-stage">
+ <div class="size-6 animate-spin rounded-full border-2 border-ed-line-strong border-t-ed-accent" />
+ <span class="text-xs font-medium text-ed-text-2">
+ Loading frame…
+ </span>
</div>
- <span>×</span>
- <div class="w-13">
+ </Show>
+ </div>
+
+ <div class="flex w-[300px] shrink-0 flex-col gap-3 rounded-xl bg-ed-card-2 p-3.5">
+ <span class={inspectorLabel}>Preview</span>
+ <div class="relative flex aspect-video w-full items-center justify-center overflow-hidden rounded-lg bg-ed-stage">
+ <canvas
+ ref={previewCanvas}
+ class="block max-h-full max-w-full object-contain"
+ />
+ <Show when={!previewReady()}>
+ <div class="absolute inset-0 flex flex-col items-center justify-center gap-2 bg-ed-stage">
+ <div class="size-5 animate-spin rounded-full border-2 border-ed-line-strong border-t-ed-accent" />
+ <span class="text-[11px] font-medium text-ed-text-3">
+ Rendering preview…
+ </span>
+ </div>
+ </Show>
+ </div>
+
+ <div class="h-px bg-ed-line" />
+
+ <span class={inspectorLabel}>Aspect ratio</span>
+ <div class="grid grid-cols-3 gap-1.5">
+ <For each={[null, ...COMMON_RATIOS]}>
+ {(ratio) => (
+ <button
+ type="button"
+ class="flex h-7 items-center justify-center rounded-[7px] text-xs font-medium tabular-nums outline-hidden transition-colors duration-100 focus-visible:ring-1 focus-visible:ring-ed-accent"
+ classList={{
+ "bg-ed-accent/12 text-ed-accent ring-1 ring-inset ring-ed-accent":
+ ratioSelected(ratio),
+ "bg-ed-ctl text-ed-text-2 hover:bg-ed-ctl-hover hover:text-ed-text-1":
+ !ratioSelected(ratio),
+ }}
+ aria-pressed={ratioSelected(ratio)}
+ onClick={() => setAspect(ratio)}
+ >
+ {ratioLabel(ratio)}
+ </button>
+ )}
+ </For>
+ </div>
+ <div class={inspectorRow}>
+ <span class="text-xs text-ed-text-2">
+ Snap to ratios
+ </span>
+ <Toggle
+ size="sm"
+ checked={snapToRatio()}
+ onChange={setSnapToRatioEnabled}
+ />
+ </div>
+
+ <div class="h-px bg-ed-line" />
+
+ <div class={inspectorRow}>
+ <span class="text-xs text-ed-text-2">Size</span>
+ <div class="flex items-center gap-1.5">
+ <BoundInput field="width" max={display.width} />
+ <span class="w-2 text-center text-xs text-ed-text-3">
+ ×
+ </span>
<BoundInput field="height" max={display.height} />
</div>
</div>
- <div class="flex flex-row items-center space-x-3 text-gray-11">
- <span>Position</span>
- <div class="w-13">
+ <div class={inspectorRow}>
+ <span class="text-xs text-ed-text-2">Position</span>
+ <div class="flex items-center gap-1.5">
<BoundInput field="x" />
- </div>
- <span>×</span>
- <div class="w-13">
+ <span class="w-2" />
<BoundInput field="y" />
</div>
</div>
- </div>
- <div class="flex flex-row gap-3 justify-end items-center w-full">
- <div class="flex flex-row items-center space-x-2 text-gray-11"></div>
-
- <Button
- variant="white"
- size="xs"
- class="flex items-center justify-center text-center rounded-full h-8 w-8 border focus:border-blue-9"
- onClick={showCropOptionsMenu}
- >
- <div class="relative pointer-events-none size-4">
- <Show when={!aspect()}>
- <IconLucideRatio class="group-active:scale-90 transition-transform size-4 pointer-events-none *:pointer-events-none" />
- </Show>
- <Transition
- enterClass="scale-50 opacity-0 blur-md"
- enterActiveClass="duration-200 ease-[cubic-bezier(0.215,0.61,0.355,1)]"
- enterToClass="scale-100 opacity-100 blur-0"
- exitClass="opacity-0"
- exitActiveClass="duration-0"
- exitToClass="opacity-0"
- >
- <Show when={aspect()} keyed>
- {(ratio) => (
- <span class="flex absolute inset-0 justify-center items-center text-xs font-medium tracking-tight leading-none pointer-events-none text text-blue-10">
- {ratio[0]}:{ratio[1]}
- </span>
- )}
- </Show>
- </Transition>
- </div>
- </Button>
- <EditorButton
- leftIcon={<IconLucideMaximize />}
- onClick={() => cropperRef?.fill()}
- disabled={
- crop().width === display.width &&
- crop().height === display.height
- }
- >
- Full
- </EditorButton>
- <EditorButton
- leftIcon={<IconCapCircleX />}
- onClick={() => {
- cropperRef?.reset();
- setAspect(null);
- }}
- disabled={
- crop().x === dialog().position.x &&
- crop().y === dialog().position.y &&
- crop().width === dialog().size.x &&
- crop().height === dialog().size.y
- }
- >
- Reset
- </EditorButton>
- </div>
- </Dialog.Header>
- <Dialog.Content>
- <div class="flex flex-row gap-3 justify-center items-stretch">
- <div class="flex flex-col gap-2.5">
- <span class="px-1 text-[11px] font-medium tracking-wide uppercase text-gray-10">
- Crop area
- </span>
- <div
- class="relative rounded-xl border p-4 shadow-sm border-gray-5 bg-gray-3"
- style={{
- width: `${boxSize().w + 32}px`,
- height: `${boxSize().h + 32}px`,
+ <div class="h-px bg-ed-line" />
+
+ <div class="flex gap-1.5">
+ <button
+ type="button"
+ class={actionButton}
+ disabled={isCentered()}
+ onClick={() => {
+ const bounds = crop();
+ cropperRef?.animateTo({
+ ...bounds,
+ x: Math.round(
+ (display.width - bounds.width) / 2,
+ ),
+ y: Math.round(
+ (display.height - bounds.height) / 2,
+ ),
+ });
}}
>
- <div
- class="relative w-full h-full transition-opacity duration-200"
- classList={{ "opacity-0": !frameLoaded() }}
- >
- <Cropper
- ref={cropperRef}
- onCropChange={setCrop}
- aspectRatio={aspect() ?? undefined}
- targetSize={{
- x: display.width,
- y: display.height,
- }}
- initialCrop={initialBounds}
- snapToRatioEnabled={snapToRatio()}
- useBackdropFilter={true}
- allowLightMode={true}
- appearance="editor"
- snapToAlignmentEnabled={true}
- onContextMenu={(e) =>
- showCropOptionsMenu(e, true)
- }
- >
- <img
- class="block w-full h-full pointer-events-none select-none"
- alt="Current frame"
- onError={() => {
- const url = frameUrl();
- if (url) {
- setFrameUrl(null);
- URL.revokeObjectURL(url);
- }
- setFrameError(true);
- }}
- onLoad={() => setFrameLoaded(true)}
- src={
- frameUrl() ??
- (frameError() ? screenshotSrc : undefined)
- }
- />
- </Cropper>
- </div>
- <Show when={!frameLoaded()}>
- <div class="flex absolute inset-0 z-40 flex-col gap-3 justify-center items-center bg-gray-3">
- <div class="rounded-full border-2 animate-spin size-7 border-gray-5 border-t-blue-9" />
- <span class="text-xs font-medium text-gray-10">
- Loading frame…
- </span>
- </div>
- </Show>
- </div>
- </div>
-
- <div
- class="flex justify-center items-center self-end text-gray-8"
- style={{ height: `${boxSize().h}px` }}
- >
- <svg
- aria-hidden="true"
- viewBox="0 0 24 24"
- fill="none"
- stroke="currentColor"
- stroke-width="2"
- stroke-linecap="round"
- stroke-linejoin="round"
- class="size-5"
+ Center
+ </button>
+ <button
+ type="button"
+ class={actionButton}
+ disabled={isFull()}
+ onClick={() => cropperRef?.fill()}
>
- <path d="m9 18 6-6-6-6" />
- </svg>
- </div>
-
- <div class="flex flex-col gap-2.5">
- <span class="px-1 text-[11px] font-medium tracking-wide uppercase text-gray-10">
- Preview
- </span>
- <div
- class="flex overflow-hidden relative justify-center items-center rounded-xl border shadow-sm border-gray-3 bg-gray-3"
- style={{
- width: `${Math.round(boxSize().w * 0.44)}px`,
- height: `${boxSize().h + 32}px`,
+ Full
+ </button>
+ <button
+ type="button"
+ class={actionButton}
+ disabled={isUntouched()}
+ onClick={() => {
+ cropperRef?.reset();
+ setAspect(null);
}}
>
- <canvas
- ref={previewCanvas}
- class="block object-contain max-w-full max-h-full"
- />
- <Show when={!previewReady()}>
- <div class="flex absolute inset-0 z-40 flex-col gap-3 justify-center items-center bg-gray-3">
- <div class="rounded-full border-2 animate-spin size-7 border-gray-5 border-t-blue-9" />
- <span class="text-xs font-medium text-gray-10">
- Rendering preview…
- </span>
- </div>
- </Show>
- </div>
+ Reset
+ </button>
</div>
</div>
- <div class="flex items-center justify-between gap-3 pt-4 text-xs text-gray-11">
- <span>
- Drag to move · Snap to center, halves and quarters ·
- Hold Shift to skip snapping
- </span>
- <EditorButton
- onClick={() => {
- const bounds = crop();
- cropperRef?.animateTo({
- ...bounds,
- x: Math.round((display.width - bounds.width) / 2),
- y: Math.round(
- (display.height - bounds.height) / 2,
- ),
- });
- }}
+ </div>
+ <p class="px-5 pt-3 pb-5 text-center text-xs text-ed-text-3">
+ Drag to move · Arrow keys nudge · Hold{" "}
+ <kbd class="rounded-[4px] bg-ed-ctl px-1 font-sans text-ed-text-2">
+ ⇧
+ </kbd>{" "}
+ to skip snapping
+ </p>
+ <div class="flex h-14 shrink-0 items-center justify-between gap-3 border-t border-ed-line px-5">
+ <div class="min-w-0 text-xs text-ed-text-2">
+ <Show
+ when={cropTargetValid()}
+ fallback={
+ <p role="alert" class="text-orange-11">
+ Crop target changed. Close and reopen Crop to
+ continue.
+ </p>
+ }
>
- Center crop
- </EditorButton>
+ <Show when={styleScopeLabel}>
+ {(label) => <p>{label()}</p>}
+ </Show>
+ </Show>
</div>
- </Dialog.Content>
- <Dialog.Footer>
- <Show when={!cropTargetValid()}>
- <p role="alert" class="text-sm text-orange-11">
- Crop target changed. Close and reopen Crop to
- continue.
- </p>
- </Show>
- <Button
- disabled={
- !frameLoaded() ||
- crop().width <= 0 ||
- crop().height <= 0 ||
- !cropTargetValid()
- }
- onClick={() => {
- const bounds = crop();
- const nextCrop = {
- position: {
- x: bounds.x,
- y: bounds.y,
- },
- size: {
- x: bounds.width,
- y: bounds.height,
- },
- };
- if (!cropTargetValid()) {
- toast.error(
- "Crop target changed. Reopen Crop to continue.",
- );
- return;
+ <div class="flex shrink-0 items-center gap-2">
+ <EditorButton onClick={closeCrop}>Cancel</EditorButton>
+ <button
+ type="button"
+ class={cx(
+ "flex h-[30px] shrink-0 items-center justify-center rounded-lg px-3.5 text-[13px] font-medium text-white outline-hidden",
+ "bg-linear-to-b from-ed-accent-2 to-ed-accent",
+ "shadow-[inset_0_1px_0_rgba(255,255,255,0.22),0_1px_2px_rgba(0,60,160,0.25)]",
+ "transition-[filter,opacity] duration-150 ease-out",
+ "enabled:hover:brightness-[1.06] enabled:active:brightness-[0.96] disabled:opacity-45",
+ )}
+ disabled={
+ !frameLoaded() ||
+ crop().width <= 0 ||
+ crop().height <= 0 ||
+ !cropTargetValid()
}
- if (cropTarget === null)
- setState("background", "crop", nextCrop);
- else
- setState(
- "timeline",
- "styleSegments",
- cropTarget,
- "overrides",
- "background",
- "crop",
- nextCrop,
- );
- setDialog((d) => ({ ...d, open: false }));
- }}
- >
- Save
- </Button>
- </Dialog.Footer>
+ onClick={saveCrop}
+ >
+ Save
+ </button>
+ </div>
+ </div>
</>
);
}}