From d6f9ea2e47e5fb6b8cc6205af9bdc96033e9f023 Mon Sep 17 00:00:00 2001 From: Dunkrick Date: Mon, 9 Nov 2026 13:30:29 +0530 Subject: [PATCH] fix(frontend): make canvas viewport responsive to laptop displays and window resize MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Calculate true rectangular bounding box in getCameraFrame to eliminate clipping - Frame empty workspace notes within visible bounds on small laptop screens - Apply safe UI insets (84px top/left, 64px bottom/right) to clear toolbar and palette - Add debounced window resize handler to adapt camera on resolution changes - Add ⌘0 / Ctrl+0 keyboard shortcut to reframe all thoughts on demand - Update viewport hint copy and add unit tests for 13.6", 14.2", and Windows displays --- frontend/src/components/CanvasController.tsx | 156 ++++++++++++++----- frontend/src/components/Viewport.tsx | 2 +- frontend/src/lib/canvas.test.ts | 124 +++++++++++++-- frontend/src/lib/canvas.ts | 93 +++++++---- 4 files changed, 292 insertions(+), 83 deletions(-) diff --git a/frontend/src/components/CanvasController.tsx b/frontend/src/components/CanvasController.tsx index e8070ec..933c2a0 100644 --- a/frontend/src/components/CanvasController.tsx +++ b/frontend/src/components/CanvasController.tsx @@ -1,4 +1,4 @@ -import { useEffect, useRef } from "react"; +import { useCallback, useEffect, useRef } from "react"; import { useCanvasCamera } from "../hooks/useCanvasCamera"; import { getArtifactCardSize, getCameraFrame } from "../lib/canvas"; import { @@ -8,6 +8,13 @@ import { import type { Artifact } from "../types/artifacts"; import type { Workspace } from "../services/workspaces"; +const VIEWPORT_INSETS = { + paddingTop: 84, + paddingLeft: 84, + paddingBottom: 64, + paddingRight: 64, +}; + interface Props { artifacts: Artifact[]; workspace: Workspace | null; @@ -19,54 +26,121 @@ export default function CanvasController({ workspace, isReady, }: Props) { - const camera = useCanvasCamera(); const focusedWorkspaceId = useRef(null); - useEffect(() => { - if (!workspace || !isReady || focusedWorkspaceId.current === workspace.id) return; + const frameWorkspace = useCallback( + (animate = true) => { + if (!workspace) return; + + const cards = artifacts.map((artifact) => { + const element = document.querySelector( + `[data-artifact-id="${artifact.id}"]`, + ); + const size = element + ? { width: element.offsetWidth, height: element.offsetHeight } + : getArtifactCardSize(artifact); + + return { x: artifact.x, y: artifact.y, ...size }; + }); + + const cameraFrame = getCameraFrame( + cards, + { width: window.innerWidth, height: window.innerHeight }, + { + ...VIEWPORT_INSETS, + workspaceHome: getWorkspaceHome(workspace), + }, + ); + + const renderCenter = toRenderPosition(cameraFrame.center); + const zoom = cameraFrame.zoom; + + if (!animate) { + // Start close to home, then settle into it. This makes entering a + // workspace feel like arriving at a place instead of a camera jump. + camera.setInitialPosition({ + x: window.innerWidth / 2 - renderCenter.x * zoom + 54, + y: window.innerHeight / 2 - renderCenter.y * zoom + 36, + zoom, + }); + + const frame = window.requestAnimationFrame(() => { + camera.centerOn( + renderCenter, + { + width: window.innerWidth, + height: window.innerHeight, + }, + { zoom }, + ); + }); - const cards = artifacts.map((artifact) => { - const element = document.querySelector( - `[data-artifact-id="${artifact.id}"]`, + return () => window.cancelAnimationFrame(frame); + } + + camera.centerOn( + renderCenter, + { + width: window.innerWidth, + height: window.innerHeight, + }, + { zoom, animate: true }, ); - const size = element - ? { width: element.offsetWidth, height: element.offsetHeight } - : getArtifactCardSize(artifact); - - return { x: artifact.x, y: artifact.y, ...size }; - }); - const cameraFrame = getCameraFrame( - cards, - { width: window.innerWidth, height: window.innerHeight }, - ); - const logicalCenter = artifacts.length === 0 - ? getWorkspaceHome(workspace) - : cameraFrame.center; - const zoom = artifacts.length === 0 ? 1 : cameraFrame.zoom; - - const renderCenter = - toRenderPosition(logicalCenter); - - // Start close to home, then settle into it. This makes entering a - // workspace feel like arriving at a place instead of a camera jump. - camera.setInitialPosition({ - x: window.innerWidth / 2 - renderCenter.x * zoom + 54, - y: window.innerHeight / 2 - renderCenter.y * zoom + 36, - zoom, - }); - - const frame = window.requestAnimationFrame(() => { - camera.centerOn(renderCenter, { - width: window.innerWidth, - height: window.innerHeight, - }, { zoom }); - }); + }, + [artifacts, camera, workspace], + ); + + useEffect(() => { + if (!workspace || !isReady || focusedWorkspaceId.current === workspace.id) return; focusedWorkspaceId.current = workspace.id; + const cleanup = frameWorkspace(false); + return () => { + if (cleanup) cleanup(); + }; + }, [frameWorkspace, isReady, workspace]); + + useEffect(() => { + if (!workspace || !isReady) return; + + let resizeTimer: ReturnType | null = null; + const handleResize = () => { + if (resizeTimer) clearTimeout(resizeTimer); + resizeTimer = setTimeout(() => { + frameWorkspace(true); + }, 150); + }; + + window.addEventListener("resize", handleResize); + return () => { + if (resizeTimer) clearTimeout(resizeTimer); + window.removeEventListener("resize", handleResize); + }; + }, [frameWorkspace, isReady, workspace]); + + useEffect(() => { + if (!workspace || !isReady) return; + + const handleKeyDown = (event: KeyboardEvent) => { + if ((event.metaKey || event.ctrlKey) && (event.key === "0" || event.code === "Digit0")) { + const target = event.target as HTMLElement | null; + if ( + target && + (target.tagName === "INPUT" || + target.tagName === "TEXTAREA" || + target.isContentEditable) + ) { + return; + } + event.preventDefault(); + frameWorkspace(true); + } + }; - return () => window.cancelAnimationFrame(frame); - }, [artifacts, camera, isReady, workspace]); + window.addEventListener("keydown", handleKeyDown); + return () => window.removeEventListener("keydown", handleKeyDown); + }, [frameWorkspace, isReady, workspace]); return null; } diff --git a/frontend/src/components/Viewport.tsx b/frontend/src/components/Viewport.tsx index 87d894e..024b3df 100644 --- a/frontend/src/components/Viewport.tsx +++ b/frontend/src/components/Viewport.tsx @@ -29,7 +29,7 @@ export default function Viewport({ > {children} - + diff --git a/frontend/src/lib/canvas.test.ts b/frontend/src/lib/canvas.test.ts index 3f67fd2..8d9fbe4 100644 --- a/frontend/src/lib/canvas.test.ts +++ b/frontend/src/lib/canvas.test.ts @@ -1,5 +1,9 @@ import { describe, expect, it } from "vitest"; -import { getCameraFrame, getMainArtifactCluster } from "./canvas"; +import { + EMPTY_WORKSPACE_CARDS, + getCameraFrame, + getMainArtifactCluster, +} from "./canvas"; describe("canvas spatial framing", () => { it("keeps a cluster at zoom one when padding already fits", () => { @@ -15,7 +19,7 @@ describe("canvas spatial framing", () => { expect(frame.zoom).toBe(1); }); - it("selects largest spatial group and calculates centers from card centers", () => { + it("selects largest spatial group and calculates center from true bounding box", () => { const nearby = [ { x: 100, y: 100, width: 260, height: 140 }, { x: 440, y: 100, width: 280, height: 300 }, @@ -31,16 +35,13 @@ describe("canvas spatial framing", () => { expect(cluster).toHaveLength(3); const frame = getCameraFrame(cards, { width: 1200, height: 900 }); - const centers = nearby.map((card) => ({ - x: card.x + card.width / 2, - y: card.y + card.height / 2, - })); - expect(frame.center.x).toBeCloseTo( - centers.reduce((sum, point) => sum + point.x, 0) / centers.length, - ); - expect(frame.center.y).toBeCloseTo( - centers.reduce((sum, point) => sum + point.y, 0) / centers.length, - ); + const minX = Math.min(...nearby.map((c) => c.x)); + const maxX = Math.max(...nearby.map((c) => c.x + c.width)); + const minY = Math.min(...nearby.map((c) => c.y)); + const maxY = Math.max(...nearby.map((c) => c.y + c.height)); + + expect(frame.center.x).toBeCloseTo((minX + maxX) / 2); + expect(frame.center.y).toBeCloseTo((minY + maxY) / 2); }); it("fits selected card bounds with padding and never zooms above one", () => { @@ -72,4 +73,103 @@ describe("canvas spatial framing", () => { expect(screenBounds.top).toBeGreaterThanOrEqual(padding - 0.001); expect(screenBounds.bottom).toBeLessThanOrEqual(viewport.height - padding + 0.001); }); + + describe("empty workspace laptop responsiveness", () => { + const safeInsets = { + paddingTop: 84, + paddingLeft: 84, + paddingBottom: 64, + paddingRight: 64, + }; + + const targetLaptopViewports = [ + { name: "13.6-inch MacBook Air (1470x956)", width: 1470, height: 956 }, + { name: "13.6-inch MacBook Air scaled (1280x832)", width: 1280, height: 832 }, + { name: "14.2-inch MacBook Pro (1512x982)", width: 1512, height: 982 }, + { name: "Windows 14-inch 150% scaling (1280x720)", width: 1280, height: 720 }, + { name: "Windows 14-inch 125% scaling (1536x864)", width: 1536, height: 864 }, + { name: "Standard laptop 1366x768", width: 1366, height: 768 }, + ]; + + targetLaptopViewports.forEach(({ name, width, height }) => { + it(`fits empty workspace notes within screen on ${name}`, () => { + const viewport = { width, height }; + const frame = getCameraFrame([], viewport, safeInsets); + + expect(frame.zoom).toBeLessThanOrEqual(1); + expect(frame.zoom).toBeGreaterThan(0.1); + + const minX = Math.min(...EMPTY_WORKSPACE_CARDS.map((c) => c.x)); + const maxX = Math.max(...EMPTY_WORKSPACE_CARDS.map((c) => c.x + c.width)); + const minY = Math.min(...EMPTY_WORKSPACE_CARDS.map((c) => c.y)); + const maxY = Math.max(...EMPTY_WORKSPACE_CARDS.map((c) => c.y + c.height)); + + const screenLeft = viewport.width / 2 + (minX - frame.center.x) * frame.zoom; + const screenRight = viewport.width / 2 + (maxX - frame.center.x) * frame.zoom; + const screenTop = viewport.height / 2 + (minY - frame.center.y) * frame.zoom; + const screenBottom = viewport.height / 2 + (maxY - frame.center.y) * frame.zoom; + + // Empty notes must stay strictly within the visible clear area without scrolling + expect(screenLeft).toBeGreaterThanOrEqual(safeInsets.paddingLeft - 0.001); + expect(screenRight).toBeLessThanOrEqual(viewport.width - safeInsets.paddingRight + 0.001); + expect(screenTop).toBeGreaterThanOrEqual(safeInsets.paddingTop - 0.001); + expect(screenBottom).toBeLessThanOrEqual(viewport.height - safeInsets.paddingBottom + 0.001); + }); + }); + + it("respects custom workspaceHome offset for empty workspace", () => { + const home = { x: 500, y: -300 }; + const viewport = { width: 1280, height: 720 }; + const frame = getCameraFrame([], viewport, { ...safeInsets, workspaceHome: home }); + + const minX = Math.min(...EMPTY_WORKSPACE_CARDS.map((c) => c.x + home.x)); + const maxX = Math.max(...EMPTY_WORKSPACE_CARDS.map((c) => c.x + home.x + c.width)); + const minY = Math.min(...EMPTY_WORKSPACE_CARDS.map((c) => c.y + home.y)); + const maxY = Math.max(...EMPTY_WORKSPACE_CARDS.map((c) => c.y + home.y + c.height)); + + const screenLeft = viewport.width / 2 + (minX - frame.center.x) * frame.zoom; + const screenRight = viewport.width / 2 + (maxX - frame.center.x) * frame.zoom; + const screenTop = viewport.height / 2 + (minY - frame.center.y) * frame.zoom; + const screenBottom = viewport.height / 2 + (maxY - frame.center.y) * frame.zoom; + + expect(screenLeft).toBeGreaterThanOrEqual(safeInsets.paddingLeft - 0.001); + expect(screenRight).toBeLessThanOrEqual(viewport.width - safeInsets.paddingRight + 0.001); + expect(screenTop).toBeGreaterThanOrEqual(safeInsets.paddingTop - 0.001); + expect(screenBottom).toBeLessThanOrEqual(viewport.height - safeInsets.paddingBottom + 0.001); + }); + }); + + describe("populated workspace laptop responsiveness", () => { + const safeInsets = { + paddingTop: 84, + paddingLeft: 84, + paddingBottom: 64, + paddingRight: 64, + }; + + it("fits multiple cards spaced across the workspace on smaller 1280x720 display", () => { + const cards = [ + { x: -400, y: -200, width: 380, height: 220 }, + { x: 100, y: -100, width: 280, height: 320 }, + { x: 300, y: 150, width: 385, height: 240 }, + ]; + const viewport = { width: 1280, height: 720 }; + const frame = getCameraFrame(cards, viewport, safeInsets); + + const minX = Math.min(...cards.map((c) => c.x)); + const maxX = Math.max(...cards.map((c) => c.x + c.width)); + const minY = Math.min(...cards.map((c) => c.y)); + const maxY = Math.max(...cards.map((c) => c.y + c.height)); + + const screenLeft = viewport.width / 2 + (minX - frame.center.x) * frame.zoom; + const screenRight = viewport.width / 2 + (maxX - frame.center.x) * frame.zoom; + const screenTop = viewport.height / 2 + (minY - frame.center.y) * frame.zoom; + const screenBottom = viewport.height / 2 + (maxY - frame.center.y) * frame.zoom; + + expect(screenLeft).toBeGreaterThanOrEqual(safeInsets.paddingLeft - 0.001); + expect(screenRight).toBeLessThanOrEqual(viewport.width - safeInsets.paddingRight + 0.001); + expect(screenTop).toBeGreaterThanOrEqual(safeInsets.paddingTop - 0.001); + expect(screenBottom).toBeLessThanOrEqual(viewport.height - safeInsets.paddingBottom + 0.001); + }); + }); }); diff --git a/frontend/src/lib/canvas.ts b/frontend/src/lib/canvas.ts index 434b3df..8df9ade 100644 --- a/frontend/src/lib/canvas.ts +++ b/frontend/src/lib/canvas.ts @@ -12,6 +12,22 @@ export interface CameraFrame { zoom: number; } +export interface CameraFrameOptions { + padding?: number; + paddingTop?: number; + paddingBottom?: number; + paddingLeft?: number; + paddingRight?: number; + maxZoom?: number; + minZoom?: number; + workspaceHome?: { x: number; y: number }; +} + +export const EMPTY_WORKSPACE_CARDS: ArtifactCardLayout[] = [ + { x: -270, y: -245, width: 340, height: 240 }, + { x: 340, y: 170, width: 275, height: 180 }, +]; + const DEFAULT_CARD_SIZE: Record = { TEXT: { width: 380, height: 220 }, LINK: { width: 380, height: 220 }, @@ -32,9 +48,9 @@ function proximityThreshold(cards: ArtifactCardLayout[]) { .sort((a, b) => a - b); const median = diagonals[Math.floor(diagonals.length / 2)] ?? 0; - // Neighboring cards can have a modest gap; large coordinate jumps stay - // outside cluster even when one card has unusual dimensions. - return median * 1.5; + // Normal workspace thoughts can be placed across the canvas. A generous + // threshold preserves all user thoughts while filtering extreme outliers. + return Math.max(median * 4, 2500); } export function getMainArtifactCluster(cards: ArtifactCardLayout[]) { @@ -85,41 +101,57 @@ export function getArtifactCardSize(artifact: Artifact) { export function getCameraFrame( cards: ArtifactCardLayout[], viewport: { width: number; height: number }, - options: { padding?: number; maxZoom?: number; minZoom?: number } = {}, + options: CameraFrameOptions = {}, ): CameraFrame { + const defaultPadding = options.padding ?? 64; + const paddingTop = options.paddingTop ?? defaultPadding; + const paddingBottom = options.paddingBottom ?? defaultPadding; + const paddingLeft = options.paddingLeft ?? defaultPadding; + const paddingRight = options.paddingRight ?? defaultPadding; + + const availableWidth = Math.max(1, viewport.width - (paddingLeft + paddingRight)); + const availableHeight = Math.max(1, viewport.height - (paddingTop + paddingBottom)); + + let targetCards = cards; if (cards.length === 0) { - return { center: { x: 0, y: 0 }, zoom: 1 }; + const home = options.workspaceHome ?? { x: 0, y: 0 }; + targetCards = EMPTY_WORKSPACE_CARDS.map((card) => ({ + ...card, + x: card.x + home.x, + y: card.y + home.y, + })); } - const cluster = getMainArtifactCluster(cards); - const centers = cluster.map(centerOf); - const center = { - x: centers.reduce((sum, point) => sum + point.x, 0) / centers.length, - y: centers.reduce((sum, point) => sum + point.y, 0) / centers.length, + const cluster = getMainArtifactCluster(targetCards); + const minX = Math.min(...cluster.map((card) => card.x)); + const maxX = Math.max(...cluster.map((card) => card.x + card.width)); + const minY = Math.min(...cluster.map((card) => card.y)); + const maxY = Math.max(...cluster.map((card) => card.y + card.height)); + + const contentWidth = Math.max(1, maxX - minX); + const contentHeight = Math.max(1, maxY - minY); + const boundingCenter = { + x: (minX + maxX) / 2, + y: (minY + maxY) / 2, }; - const halfWidth = Math.max( - ...cluster.map((card) => Math.abs(card.x + card.width - center.x)), - ...cluster.map((card) => Math.abs(card.x - center.x)), - ); - const halfHeight = Math.max( - ...cluster.map((card) => Math.abs(card.y + card.height - center.y)), - ...cluster.map((card) => Math.abs(card.y - center.y)), - ); - const padding = options.padding ?? 64; - const availableWidth = Math.max(1, viewport.width - padding * 2); - const availableHeight = Math.max(1, viewport.height - padding * 2); + const maxZoom = Math.min(1, options.maxZoom ?? 1); const minZoom = options.minZoom ?? 0.1; const fitZoom = Math.min( maxZoom, - availableWidth / (halfWidth * 2), - availableHeight / (halfHeight * 2), + availableWidth / contentWidth, + availableHeight / contentHeight, ); + const zoom = Math.max(minZoom, fitZoom); - return { - center, - zoom: Math.max(minZoom, fitZoom), + // Shift center to compensate for asymmetric UI chrome (e.g. toolbar at top, palette at left) + // so the bounding box is visually centered in the remaining clear area. + const center = { + x: boundingCenter.x - ((paddingLeft - paddingRight) / 2) / zoom, + y: boundingCenter.y - ((paddingTop - paddingBottom) / 2) / zoom, }; + + return { center, zoom }; } export function getArtifactCenter(artifacts: Artifact[]) { @@ -130,10 +162,13 @@ export function getArtifactCenter(artifacts: Artifact[]) { return { x: artifact.x, y: artifact.y, ...size }; }); const cluster = getMainArtifactCluster(cards); - const centers = cluster.map(centerOf); + const minX = Math.min(...cluster.map((card) => card.x)); + const maxX = Math.max(...cluster.map((card) => card.x + card.width)); + const minY = Math.min(...cluster.map((card) => card.y)); + const maxY = Math.max(...cluster.map((card) => card.y + card.height)); return { - x: centers.reduce((sum, point) => sum + point.x, 0) / centers.length, - y: centers.reduce((sum, point) => sum + point.y, 0) / centers.length, + x: (minX + maxX) / 2, + y: (minY + maxY) / 2, }; }