Skip to content

Commit 470abdf

Browse files
fix(desktop): make preview annotation toolbar movable
1 parent 333f7a6 commit 470abdf

3 files changed

Lines changed: 125 additions & 2 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
import { clampToolbarPosition } from "./AnnotationToolbarPosition.ts";
3+
4+
describe("clampToolbarPosition", () => {
5+
it("keeps a dragged toolbar in the viewport, including after a resize", () => {
6+
const toolbar = { width: 200, height: 40 };
7+
expect(
8+
clampToolbarPosition({ left: -10, top: 900 }, { width: 800, height: 600 }, toolbar),
9+
).toEqual({ left: 0, top: 560 });
10+
expect(
11+
clampToolbarPosition({ left: 600, top: 560 }, { width: 400, height: 300 }, toolbar),
12+
).toEqual({ left: 200, top: 260 });
13+
expect(
14+
clampToolbarPosition({ left: 20, top: 20 }, { width: 100, height: 20 }, toolbar),
15+
).toEqual({ left: 0, top: 0 });
16+
});
17+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export type ToolbarPosition = { left: number; top: number };
2+
3+
export function clampToolbarPosition(
4+
position: ToolbarPosition,
5+
viewport: { width: number; height: number },
6+
toolbar: { width: number; height: number },
7+
): ToolbarPosition {
8+
return {
9+
left: Math.max(0, Math.min(position.left, viewport.width - toolbar.width)),
10+
top: Math.max(0, Math.min(position.top, viewport.height - toolbar.height)),
11+
};
12+
}

‎apps/desktop/src/preview/PickPreload.ts‎

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
} from "@t3tools/contracts";
1616

1717
import { createAnnotationIdSource } from "./AnnotationIds.ts";
18+
import { clampToolbarPosition, type ToolbarPosition } from "./AnnotationToolbarPosition.ts";
1819
import { resolveAnnotationSubmission } from "./AnnotationKeyboard.ts";
1920
import { previewAnnotationStyles } from "./AnnotationStyles.generated.ts";
2021
import { installRecordingCursor } from "./RecordingCursor.ts";
@@ -135,6 +136,7 @@ interface AnnotationSession {
135136
}
136137

137138
let activeSession: AnnotationSession | null = null;
139+
let toolbarPosition: ToolbarPosition | null = null;
138140
let annotationTheme: DesktopPreviewAnnotationTheme | null = null;
139141

140142
const applyAnnotationTheme = (
@@ -564,10 +566,26 @@ function startAnnotation(sendEnabled: boolean): void {
564566
const toolbar = document.createElement("div");
565567
toolbar.setAttribute(OVERLAY_ATTRIBUTE, "");
566568
toolbar.className =
567-
"pointer-events-auto fixed top-2.5 left-1/2 flex -translate-x-1/2 gap-0.5 rounded-lg border border-border bg-popover/95 p-1 text-popover-foreground shadow-lg backdrop-blur-xl";
569+
"fixed flex gap-0.5 rounded-lg border border-border bg-popover/95 p-1 text-popover-foreground shadow-lg backdrop-blur-xl";
568570
toolbar.style.zIndex = String(CHROME_LAYER_Z_INDEX);
571+
toolbar.style.maxWidth = "100vw";
572+
toolbar.style.overflowX = "auto";
569573
root.appendChild(toolbar);
570574

575+
const toolbarGrip = createButton("⠿", "Drag annotation toolbar (arrow keys to move)");
576+
toolbarGrip.setAttribute("aria-label", "Move annotation toolbar");
577+
toolbarGrip.className += " h-8 px-2";
578+
toolbarGrip.style.pointerEvents = "auto";
579+
toolbarGrip.style.cursor = "grab";
580+
toolbarGrip.style.touchAction = "none";
581+
toolbar.appendChild(toolbarGrip);
582+
583+
const dock = createButton("↕", "Move annotation toolbar to top or bottom");
584+
dock.setAttribute("aria-label", "Move annotation toolbar to top or bottom");
585+
dock.className += " h-8 px-2";
586+
dock.style.pointerEvents = "auto";
587+
toolbar.appendChild(dock);
588+
571589
const editor = document.createElement("div");
572590
editor.setAttribute(OVERLAY_ATTRIBUTE, "");
573591
editor.className =
@@ -989,9 +1007,77 @@ function startAnnotation(sendEnabled: boolean): void {
9891007
refreshToolButtons();
9901008
});
9911009
toolButtons.set(candidate, button);
1010+
button.style.pointerEvents = "auto";
9921011
toolbar.appendChild(button);
9931012
}
9941013

1014+
const positionToolbar = (position: ToolbarPosition): void => {
1015+
const rect = toolbar.getBoundingClientRect();
1016+
toolbarPosition = clampToolbarPosition(
1017+
position,
1018+
{ width: document.documentElement.clientWidth, height: window.innerHeight },
1019+
rect,
1020+
);
1021+
toolbar.style.left = `${toolbarPosition.left}px`;
1022+
toolbar.style.top = `${toolbarPosition.top}px`;
1023+
};
1024+
let toolbarDrag: { pointerId: number; offsetX: number; offsetY: number } | null = null;
1025+
toolbarGrip.addEventListener("pointerdown", (event) => {
1026+
if (event.button !== 0) return;
1027+
const rect = toolbar.getBoundingClientRect();
1028+
toolbarDrag = {
1029+
pointerId: event.pointerId,
1030+
offsetX: event.clientX - rect.left,
1031+
offsetY: event.clientY - rect.top,
1032+
};
1033+
toolbarGrip.setPointerCapture(event.pointerId);
1034+
toolbarGrip.style.cursor = "grabbing";
1035+
event.preventDefault();
1036+
event.stopPropagation();
1037+
});
1038+
toolbarGrip.addEventListener("pointermove", (event) => {
1039+
if (!toolbarDrag || toolbarDrag.pointerId !== event.pointerId) return;
1040+
positionToolbar({
1041+
left: event.clientX - toolbarDrag.offsetX,
1042+
top: event.clientY - toolbarDrag.offsetY,
1043+
});
1044+
event.preventDefault();
1045+
event.stopPropagation();
1046+
});
1047+
const endToolbarDrag = (event: PointerEvent): void => {
1048+
if (!toolbarDrag || toolbarDrag.pointerId !== event.pointerId) return;
1049+
toolbarDrag = null;
1050+
toolbarGrip.style.cursor = "grab";
1051+
if (toolbarGrip.hasPointerCapture(event.pointerId))
1052+
toolbarGrip.releasePointerCapture(event.pointerId);
1053+
event.stopPropagation();
1054+
};
1055+
toolbarGrip.addEventListener("pointerup", endToolbarDrag);
1056+
toolbarGrip.addEventListener("pointercancel", endToolbarDrag);
1057+
toolbarGrip.addEventListener("keydown", (event) => {
1058+
const delta = event.shiftKey ? 40 : 10;
1059+
const offsets: Record<string, ToolbarPosition> = {
1060+
ArrowLeft: { left: -delta, top: 0 },
1061+
ArrowRight: { left: delta, top: 0 },
1062+
ArrowUp: { left: 0, top: -delta },
1063+
ArrowDown: { left: 0, top: delta },
1064+
};
1065+
const offset = offsets[event.key];
1066+
if (!offset || !toolbarPosition) return;
1067+
positionToolbar({
1068+
left: toolbarPosition.left + offset.left,
1069+
top: toolbarPosition.top + offset.top,
1070+
});
1071+
event.preventDefault();
1072+
});
1073+
dock.addEventListener("click", () => {
1074+
const rect = toolbar.getBoundingClientRect();
1075+
positionToolbar({
1076+
left: (document.documentElement.clientWidth - rect.width) / 2,
1077+
top: rect.top < window.innerHeight / 2 ? window.innerHeight - rect.height : 0,
1078+
});
1079+
});
1080+
9951081
const clampEditorPosition = (left: number, top: number): { left: number; top: number } => {
9961082
const margin = 8;
9971083
const rect = editor.getBoundingClientRect();
@@ -1131,6 +1217,7 @@ function startAnnotation(sendEnabled: boolean): void {
11311217
const repaint = (): void => {
11321218
for (const target of selected.values()) updateSelectedVisual(target);
11331219
queueEditorLayout();
1220+
if (toolbarPosition) positionToolbar(toolbarPosition);
11341221
};
11351222
// A scrollbar appearing narrows the viewport without a window resize.
11361223
const rootResizeObserver = new ResizeObserver(repaint);
@@ -1212,7 +1299,7 @@ function startAnnotation(sendEnabled: boolean): void {
12121299
};
12131300

12141301
const onPointerMove = (event: PointerEvent): void => {
1215-
if (isAnnotationNode(event.target as Element)) {
1302+
if (dragStart === null && isAnnotationNode(event.target as Element)) {
12161303
clearHoverOutline();
12171304
return;
12181305
}
@@ -1470,6 +1557,13 @@ function startAnnotation(sendEnabled: boolean): void {
14701557
inputLayer.focus({ preventScroll: true });
14711558
}
14721559
document.documentElement.appendChild(host);
1560+
const toolbarRect = toolbar.getBoundingClientRect();
1561+
positionToolbar(
1562+
toolbarPosition ?? {
1563+
left: (document.documentElement.clientWidth - toolbarRect.width) / 2,
1564+
top: 10,
1565+
},
1566+
);
14731567
rootResizeObserver.observe(document.documentElement);
14741568
refreshToolButtons();
14751569
updateStatus();

0 commit comments

Comments
 (0)