Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Add LayersPanel and improve crop dialog logic

Richie McIlroy2b0195cf1942
diff --git a/apps/desktop/src/routes/screenshot-editor/Editor.tsx b/apps/desktop/src/routes/screenshot-editor/Editor.tsx
index 2785ae3d1..d5ec4e1cf 100644
--- a/apps/desktop/src/routes/screenshot-editor/Editor.tsx
+++ b/apps/desktop/src/routes/screenshot-editor/Editor.tsx
@@ -26,8 +26,10 @@ import { composeEventHandlers } from "~/utils/composeEventHandlers";
import IconCapCircleX from "~icons/cap/circle-x";
import IconLucideMaximize from "~icons/lucide/maximize";
import IconLucideRatio from "~icons/lucide/ratio";
+import { AnnotationConfigBar } from "./AnnotationConfig";
import { useScreenshotEditorContext } from "./context";
import { Header } from "./Header";
+import { LayersPanel } from "./LayersPanel";
import { Preview } from "./Preview";
import { Dialog, EditorButton } from "./ui";
@@ -39,6 +41,8 @@ export function Editor() {
setProject,
project,
setSelectedAnnotationId,
+ layersPanelOpen,
+ setLayersPanelOpen,
} = useScreenshotEditorContext();
createEffect(() => {
@@ -103,13 +107,14 @@ export function Editor() {
setSelectedAnnotationId(null);
break;
case "p": {
- // Toggle Padding
- // We need to push history here too if we want undo for padding
projectHistory.push();
const currentPadding = project.background.padding;
setProject("background", "padding", currentPadding === 0 ? 20 : 0);
break;
}
+ case "l":
+ setLayersPanelOpen(!layersPanelOpen());
+ break;
}
}
};
@@ -120,11 +125,17 @@ export function Editor() {
return (
<>
- <Header />
+ <div class="relative">
+ <Header />
+ <AnnotationConfigBar />
+ </div>
<div
- class="flex overflow-y-hidden flex-col flex-1 gap-0 pb-0 w-full min-h-0 leading-5 animate-in fade-in"
+ class="flex overflow-y-hidden flex-1 gap-0 pb-0 w-full min-h-0 leading-5 animate-in fade-in"
data-tauri-drag-region
>
+ <Show when={layersPanelOpen()}>
+ <LayersPanel />
+ </Show>
<div class="flex overflow-hidden flex-col flex-1 min-h-0">
<div class="flex overflow-y-hidden flex-row flex-1 min-h-0">
<Preview zoom={zoom()} setZoom={setZoom} />
@@ -192,12 +203,21 @@ function Dialogs() {
);
});
- const initialBounds = {
- x: cropDialog().position.x,
- y: cropDialog().position.y,
- width: cropDialog().size.x,
- height: cropDialog().size.y,
- };
+ const originalSize = cropDialog().originalSize;
+ const existingCrop = cropDialog().currentCrop;
+ const initialBounds = existingCrop
+ ? {
+ x: existingCrop.position.x,
+ y: existingCrop.position.y,
+ width: existingCrop.size.x,
+ height: existingCrop.size.y,
+ }
+ : {
+ x: 0,
+ y: 0,
+ width: originalSize.x,
+ height: originalSize.y,
+ };
const [snapToRatio, setSnapToRatioEnabled] = makePersisted(
createSignal(true),
@@ -260,17 +280,11 @@ function Dialogs() {
<div class="flex flex-row items-center space-x-[0.75rem] text-gray-11">
<span>Size</span>
<div class="w-[3.25rem]">
- <BoundInput
- field="width"
- max={cropDialog().size.x}
- />
+ <BoundInput field="width" max={originalSize.x} />
</div>
<span>×</span>
<div class="w-[3.25rem]">
- <BoundInput
- field="height"
- max={cropDialog().size.y}
- />
+ <BoundInput field="height" max={originalSize.y} />
</div>
</div>
<div class="flex flex-row items-center space-x-[0.75rem] text-gray-11">
@@ -321,8 +335,8 @@ function Dialogs() {
leftIcon={<IconLucideMaximize />}
onClick={() => cropperRef?.fill()}
disabled={
- crop().width === cropDialog().size.x &&
- crop().height === cropDialog().size.y
+ crop().width === originalSize.x &&
+ crop().height === originalSize.y
}
>
Full
@@ -334,10 +348,10 @@ function Dialogs() {
setAspect(null);
}}
disabled={
- crop().x === cropDialog().position.x &&
- crop().y === cropDialog().position.y &&
- crop().width === cropDialog().size.x &&
- crop().height === cropDialog().size.y
+ crop().x === initialBounds.x &&
+ crop().y === initialBounds.y &&
+ crop().width === initialBounds.width &&
+ crop().height === initialBounds.height
}
>
Reset
@@ -350,8 +364,8 @@ function Dialogs() {
class="rounded overflow-hidden relative select-none"
style={{
width: (() => {
- const srcW = cropDialog().size.x;
- const srcH = cropDialog().size.y;
+ const srcW = originalSize.x;
+ const srcH = originalSize.y;
const maxW = Math.min(
windowSize().width * 0.8,
768,
@@ -361,8 +375,8 @@ function Dialogs() {
return `${srcW * ratio}px`;
})(),
height: (() => {
- const srcW = cropDialog().size.x;
- const srcH = cropDialog().size.y;
+ const srcW = originalSize.x;
+ const srcH = originalSize.y;
const maxW = Math.min(
windowSize().width * 0.8,
768,
@@ -378,8 +392,8 @@ function Dialogs() {
onCropChange={setCrop}
aspectRatio={aspect() ?? undefined}
targetSize={{
- x: cropDialog().size.x,
- y: cropDialog().size.y,
+ x: originalSize.x,
+ y: originalSize.y,
}}
initialCrop={initialBounds}
snapToRatioEnabled={snapToRatio()}
diff --git a/apps/desktop/src/routes/screenshot-editor/LayersPanel.tsx b/apps/desktop/src/routes/screenshot-editor/LayersPanel.tsx
new file mode 100644
index 000000000..358703116
--- /dev/null
+++ b/apps/desktop/src/routes/screenshot-editor/LayersPanel.tsx
@@ -0,0 +1,295 @@
+import { cx } from "cva";
+import { createEffect, createSignal, For, on, Show } from "solid-js";
+import IconLucideArrowUpRight from "~icons/lucide/arrow-up-right";
+import IconLucideCircle from "~icons/lucide/circle";
+import IconLucideEyeOff from "~icons/lucide/eye-off";
+import IconLucideGripVertical from "~icons/lucide/grip-vertical";
+import IconLucideLayers from "~icons/lucide/layers";
+import IconLucideSquare from "~icons/lucide/square";
+import IconLucideType from "~icons/lucide/type";
+import IconLucideX from "~icons/lucide/x";
+import { type Annotation, useScreenshotEditorContext } from "./context";
+
+const ANNOTATION_TYPE_ICONS = {
+ arrow: IconLucideArrowUpRight,
+ rectangle: IconLucideSquare,
+ circle: IconLucideCircle,
+ mask: IconLucideEyeOff,
+ text: IconLucideType,
+};
+
+const ANNOTATION_TYPE_LABELS = {
+ arrow: "Arrow",
+ rectangle: "Rectangle",
+ circle: "Circle",
+ mask: "Mask",
+ text: "Text",
+};
+
+export function LayersPanel() {
+ const {
+ annotations,
+ setAnnotations,
+ selectedAnnotationId,
+ setSelectedAnnotationId,
+ setLayersPanelOpen,
+ projectHistory,
+ setActiveTool,
+ setFocusAnnotationId,
+ } = useScreenshotEditorContext();
+
+ const [dragState, setDragState] = createSignal<{
+ draggedId: string;
+ startY: number;
+ currentY: number;
+ } | null>(null);
+
+ const [dropTargetIndex, setDropTargetIndex] = createSignal<number | null>(
+ null,
+ );
+
+ const getTypeLabel = (ann: Annotation) => {
+ if (ann.type === "text" && ann.text) {
+ const truncated =
+ ann.text.length > 12 ? `${ann.text.slice(0, 12)}...` : ann.text;
+ return truncated;
+ }
+ return ANNOTATION_TYPE_LABELS[ann.type];
+ };
+
+ const reversedAnnotations = () => [...annotations].reverse();
+
+ const getActualIndex = (reversedIdx: number) =>
+ annotations.length - 1 - reversedIdx;
+
+ const handleMouseDown = (ann: Annotation, e: MouseEvent) => {
+ if ((e.target as HTMLElement).closest("button")) return;
+
+ const gripHandle = (e.target as HTMLElement).closest("[data-grip-handle]");
+ if (!gripHandle) return;
+
+ e.preventDefault();
+ e.stopPropagation();
+
+ setDragState({
+ draggedId: ann.id,
+ startY: e.clientY,
+ currentY: e.clientY,
+ });
+
+ const handleMouseMove = (moveEvent: MouseEvent) => {
+ setDragState((prev) =>
+ prev ? { ...prev, currentY: moveEvent.clientY } : null,
+ );
+
+ const listEl = document.querySelector("[data-layers-list]");
+ if (!listEl) return;
+
+ const items = listEl.querySelectorAll("[data-layer-item]");
+ let targetIdx: number | null = null;
+
+ for (let i = 0; i < items.length; i++) {
+ const item = items[i] as HTMLElement;
+ const rect = item.getBoundingClientRect();
+ const midY = rect.top + rect.height / 2;
+
+ if (moveEvent.clientY < midY) {
+ targetIdx = i;
+ break;
+ }
+ targetIdx = i + 1;
+ }
+
+ setDropTargetIndex(targetIdx);
+ };
+
+ const handleMouseUp = () => {
+ const state = dragState();
+ const targetIdx = dropTargetIndex();
+
+ if (state && targetIdx !== null) {
+ const draggedReversedIdx = reversedAnnotations().findIndex(
+ (a) => a.id === state.draggedId,
+ );
+
+ if (draggedReversedIdx !== -1 && draggedReversedIdx !== targetIdx) {
+ const fromActual = getActualIndex(draggedReversedIdx);
+ let toActual: number;
+
+ if (targetIdx > draggedReversedIdx) {
+ toActual = getActualIndex(targetIdx - 1);
+ } else {
+ toActual = getActualIndex(targetIdx);
+ }
+
+ if (fromActual !== toActual) {
+ projectHistory.push();
+ const newAnnotations = [...annotations];
+ const [removed] = newAnnotations.splice(fromActual, 1);
+ newAnnotations.splice(toActual, 0, removed);
+ setAnnotations(newAnnotations);
+ }
+ }
+ }
+
+ setDragState(null);
+ setDropTargetIndex(null);
+ window.removeEventListener("mousemove", handleMouseMove);
+ window.removeEventListener("mouseup", handleMouseUp);
+ };
+
+ window.addEventListener("mousemove", handleMouseMove);
+ window.addEventListener("mouseup", handleMouseUp);
+ };
+
+ const handleLayerClick = (ann: Annotation, e: MouseEvent) => {
+ if ((e.target as HTMLElement).closest("[data-grip-handle]")) return;
+ setSelectedAnnotationId(ann.id);
+ setActiveTool("select");
+ setFocusAnnotationId(ann.id);
+ };
+
+ const handleDelete = (id: string, e: MouseEvent) => {
+ e.stopPropagation();
+ projectHistory.push();
+ setAnnotations((prev) => prev.filter((a) => a.id !== id));
+ if (selectedAnnotationId() === id) {
+ setSelectedAnnotationId(null);
+ }
+ };
+
+ createEffect(
+ on(
+ () => annotations.length,
+ () => {
+ setDragState(null);
+ setDropTargetIndex(null);
+ },
+ ),
+ );
+
+ return (
+ <div class="flex flex-col h-full w-56 border-r border-gray-3 bg-gray-1 dark:bg-gray-2 select-none z-20">
+ <div class="flex items-center justify-between px-3 h-10 border-b border-gray-3">
+ <div class="flex items-center gap-2 text-sm font-medium text-gray-12">
+ <IconLucideLayers class="size-4" />
+ <span>Layers</span>
+ </div>
+ <button
+ type="button"
+ onClick={() => setLayersPanelOpen(false)}
+ class="p-1 rounded hover:bg-gray-3 text-gray-11 hover:text-gray-12 transition-colors"
+ >
+ <IconLucideX class="size-4" />
+ </button>
+ </div>
+
+ <div class="flex-1 overflow-y-auto py-1" data-layers-list>
+ <Show
+ when={annotations.length > 0}
+ fallback={
+ <div class="flex flex-col items-center justify-center h-full px-4 text-center">
+ <IconLucideLayers class="size-8 text-gray-7 mb-2" />
+ <p class="text-xs text-gray-10">No layers yet</p>
+ <p class="text-[10px] text-gray-8 mt-1">
+ Use the tools above to add annotations
+ </p>
+ </div>
+ }
+ >
+ <For each={reversedAnnotations()}>
+ {(ann, reversedIdx) => {
+ const Icon = ANNOTATION_TYPE_ICONS[ann.type];
+ const isSelected = () => selectedAnnotationId() === ann.id;
+ const isDragging = () => dragState()?.draggedId === ann.id;
+ const isDropTarget = () => {
+ const target = dropTargetIndex();
+ return target !== null && target === reversedIdx();
+ };
+ const showDropIndicatorAfter = () => {
+ const target = dropTargetIndex();
+ const state = dragState();
+ if (!state || target === null) return false;
+ const draggedIdx = reversedAnnotations().findIndex(
+ (a) => a.id === state.draggedId,
+ );
+ return (
+ target === reversedIdx() + 1 && target !== draggedIdx + 1
+ );
+ };
+
+ return (
+ <>
+ <Show when={isDropTarget() && !isDragging()}>
+ <div class="h-0.5 bg-blue-9 mx-2 rounded-full" />
+ </Show>
+ <div
+ data-layer-item
+ onMouseDown={(e) => handleMouseDown(ann, e)}
+ onClick={(e) => handleLayerClick(ann, e)}
+ class={cx(
+ "flex items-center gap-2 px-2 py-1.5 mx-1 rounded-md cursor-pointer transition-all group",
+ isSelected()
+ ? "bg-blue-3 dark:bg-blue-4"
+ : "hover:bg-gray-3",
+ isDragging() && "opacity-50 bg-gray-3",
+ )}
+ >
+ <div
+ data-grip-handle
+ class="cursor-grab active:cursor-grabbing text-gray-8 hover:text-gray-11 transition-colors"
+ >
+ <IconLucideGripVertical class="size-3.5" />
+ </div>
+
+ <div
+ class={cx(
+ "flex items-center justify-center size-6 rounded",
+ isSelected()
+ ? "bg-blue-5 text-blue-11"
+ : "bg-gray-3 text-gray-11",
+ )}
+ >
+ <Icon class="size-3.5" />
+ </div>
+
+ <span
+ class={cx(
+ "flex-1 text-xs truncate",
+ isSelected() ? "text-blue-12" : "text-gray-12",
+ )}
+ >
+ {getTypeLabel(ann)}
+ </span>
+
+ <button
+ type="button"
+ onClick={(e) => handleDelete(ann.id, e)}
+ class="opacity-0 group-hover:opacity-100 p-0.5 rounded hover:bg-red-3 text-gray-9 hover:text-red-11 transition-all"
+ >
+ <IconLucideX class="size-3" />
+ </button>
+ </div>
+ <Show when={showDropIndicatorAfter()}>
+ <div class="h-0.5 bg-blue-9 mx-2 rounded-full" />
+ </Show>
+ </>
+ );
+ }}
+ </For>
+ <Show
+ when={
+ dropTargetIndex() === reversedAnnotations().length && dragState()
+ }
+ >
+ <div class="h-0.5 bg-blue-9 mx-2 rounded-full" />
+ </Show>
+ </Show>
+ </div>
+
+ <div class="px-3 py-2 border-t border-gray-3 text-[10px] text-gray-9">
+ Drag to reorder • Top = front
+ </div>
+ </div>
+ );
+}
diff --git a/apps/desktop/src/routes/screenshot-editor/context.tsx b/apps/desktop/src/routes/screenshot-editor/context.tsx
index b3e5d3cd5..67220a16b 100644
--- a/apps/desktop/src/routes/screenshot-editor/context.tsx
+++ b/apps/desktop/src/routes/screenshot-editor/context.tsx
@@ -1,6 +1,7 @@
import { createContextProvider } from "@solid-primitives/context";
import { trackStore } from "@solid-primitives/deep";
import { debounce } from "@solid-primitives/scheduled";
+import { makePersisted } from "@solid-primitives/storage";
import { convertFileSrc } from "@tauri-apps/api/core";
import { createEffect, createResource, createSignal, on } from "solid-js";
import { createStore, reconcile, unwrap } from "solid-js/store";
@@ -24,7 +25,11 @@ export type CurrentDialog =
| { type: "createPreset" }
| { type: "renamePreset"; presetIndex: number }
| { type: "deletePreset"; presetIndex: number }
- | { type: "crop"; position: XY<number>; size: XY<number> };
+ | {
+ type: "crop";
+ originalSize: XY<number>;
+ currentCrop: { position: XY<number>; size: XY<number> } | null;
+ };
export type DialogState = { open: false } | ({ open: boolean } & CurrentDialog);
@@ -105,6 +110,14 @@ function createScreenshotEditorContext() {
"select",
);
+ const [layersPanelOpen, setLayersPanelOpen] = makePersisted(
+ createSignal(false),
+ { name: "screenshotEditorLayersPanelOpen" },
+ );
+ const [focusAnnotationId, setFocusAnnotationId] = createSignal<string | null>(
+ null,
+ );
+
const [dialog, setDialog] = createSignal<DialogState>({
open: false,
});
@@ -304,6 +317,10 @@ function createScreenshotEditorContext() {
setSelectedAnnotationId,
activeTool,
setActiveTool,
+ layersPanelOpen,
+ setLayersPanelOpen,
+ focusAnnotationId,
+ setFocusAnnotationId,
projectHistory,
dialog,
setDialog,