Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 37 additions & 32 deletions src/renderer/CanvasView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
} from '../core';
import {nodeContents} from './nodeContents';
import {cameraActionForResize} from './keepCameraOnResize';
import {fitCamera} from './fitCamera';
import {SkiaCanvasLayer} from './SkiaCanvasLayer';
import {CanvasMinimap, type Insets, type MinimapPosition} from './CanvasMinimap';
import {nodesMatching} from './findNodes';
Expand Down Expand Up @@ -64,7 +65,7 @@
* sidebar bookkeeping. Mobile / web pass nothing.
*/
onReady?: (controls: {
fitToViewport: (leftOverlayWidth?: number) => void;
fitToViewport: (leftOverlayWidth?: number, options?: {animated?: boolean}) => void;
recenter: (leftOverlayWidth?: number) => void;
/**
* Returns the user's most recent explicit camera intent. Used by the
Expand Down Expand Up @@ -174,6 +175,13 @@
* the canvas. Defaults to 0 (the canvas pans from anywhere).
*/
systemEdge?: number;
/**
* Kept clear above and below the content when it's fitted (on opening, and
* by Fit), in points: the content stays centred down the whole view, and no
* nearer its top or bottom than this. A host whose bar sits over the
* canvas's top passes the bar's height. Defaults to 0.
*/
fitMargin?: number;
}

/** A long-press on a node, as `onNodeLongPress` tells a host. */
Expand Down Expand Up @@ -235,7 +243,7 @@
* break rendering). Content changes flow through React reconciliation via
* useMemo/useLayoutEffect on the content prop.
*/
export function CanvasView({content, basePath, initialViewState, onViewStateChange, onReady, doubleTapMaxDelayMs, minimap = 'bottom-right', minimapBottomInset = 0, safeAreaInsets, leftOverlayWidth, minimapBackground, minimapCornerRadius, minimapSize, imageNames = 'onReveal', onNodeLongPress, onNodeContextMenu, systemEdge = 0}: Props) {
export function CanvasView({content, basePath, initialViewState, onViewStateChange, onReady, doubleTapMaxDelayMs, minimap = 'bottom-right', minimapBottomInset = 0, safeAreaInsets, leftOverlayWidth, minimapBackground, minimapCornerRadius, minimapSize, imageNames = 'onReveal', onNodeLongPress, onNodeContextMenu, systemEdge = 0, fitMargin = 0}: Props) {
// `useWindowDimensions` returns the whole application window — on desktop
// that includes the sidebar pane that sits to the left of the canvas
// viewport. Computing "centre" against full-window dimensions lands the
Expand Down Expand Up @@ -411,16 +419,13 @@
ty = initialViewState.translateY;
s = initialViewState.scale;
} else {
// Fit content to viewport, never zooming beyond 1:1
const sw = initialScreenRef.current.w;
const sh = initialScreenRef.current.h;
// Same fit-to-bounds maths as fitToViewport — see that comment.
s = Math.max(
MIN_SCALE,
Math.min(MAX_SCALE, Math.min(sw / bounds.width, sh / bounds.height)),
);
tx = sw / 2 - (bounds.x + bounds.width / 2) * s;
ty = sh / 2 - (bounds.y + bounds.height / 2) * s;
// Opened as Fit leaves it: the same fit, and a fit a resize keeps.
({tx, ty, s} = fitCamera(
{width: initialScreenRef.current.w, height: initialScreenRef.current.h, left: leftOverlayWidth?.value ?? 0, margin: fitMargin},
bounds,
{min: MIN_SCALE, max: MAX_SCALE},
));
lastActionRef.current = 'fit';
}

scale.value = s;
Expand Down Expand Up @@ -547,29 +552,29 @@
// (handleDoubleTap, tapGesture) does NOT recompose when the sidebar
// changes. That's load-bearing for #154 — earlier attempts that put
// sidebar state in deps regressed double-tap.
const fitToViewport = useCallback((leftOverlayWidth: number = 0) => {
const fitToViewport = useCallback((leftOverlayWidth: number = 0, options?: {animated?: boolean}) => {

Check warning on line 555 in src/renderer/CanvasView.tsx

View workflow job for this annotation

GitHub Actions / typecheck · lint · test

'leftOverlayWidth' is already declared in the upper scope on line 246 column 181
lastActionRef.current = 'fit';
const sw = viewportWidth;
const sh = viewportHeight;
const visibleW = Math.max(1, sw - leftOverlayWidth);
const centreX = leftOverlayWidth + visibleW / 2;

// Empty / degenerate bounds (e.g. a canvas with no nodes) — centre the
// world origin in the viewport at 1:1 rather than dividing by zero
// and hoping Infinity propagates through Math.min cleanly.
if (bounds.width <= 0 || bounds.height <= 0) {
animateCamera(centreX, sh / 2, 1);
const {tx, ty, s} = fitCamera(
{width: viewportWidth, height: viewportHeight, left: leftOverlayWidth, margin: fitMargin},
bounds,
{min: MIN_SCALE, max: MAX_SCALE},
);
if (options?.animated === false) {
// Straight there, as opening does.
cancelCameraAnim();
translateX.value = tx;
translateY.value = ty;
scale.value = s;
prevTranslateX.value = tx;
prevTranslateY.value = ty;
updateBounds(tx, ty, s);
saveViewState();
return;
}

const s = Math.max(
MIN_SCALE,
Math.min(MAX_SCALE, Math.min(visibleW / bounds.width, sh / bounds.height)),
);
const tx = centreX - (bounds.x + bounds.width / 2) * s;
const ty = sh / 2 - (bounds.y + bounds.height / 2) * s;
animateCamera(tx, ty, s);
}, [viewportWidth, viewportHeight, bounds, animateCamera]);
// `updateBounds` is declared below; read when called, as animateCamera does.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [viewportWidth, viewportHeight, bounds, animateCamera, fitMargin, cancelCameraAnim, saveViewState]);

// Zoom to fit a single node in the viewport with a small screen-space
// padding. Soft-capped at NODE_FIT_SOFT_MAX_SCALE so tiny stencils don't
Expand All @@ -581,7 +586,7 @@
// from `leftOverlayWidth` and passes it here per call. Kept as a parameter, not
// a closure dep, to preserve callback identity — see the matching
// discussion above `fitToViewport`.
const zoomToNode = useCallback((node: CanvasNode, leftOverlayWidth: number = 0) => {

Check warning on line 589 in src/renderer/CanvasView.tsx

View workflow job for this annotation

GitHub Actions / typecheck · lint · test

'leftOverlayWidth' is already declared in the upper scope on line 246 column 181
const sw = viewportWidth;
const sh = viewportHeight;
const visibleW = Math.max(1, sw - leftOverlayWidth);
Expand Down Expand Up @@ -654,7 +659,7 @@

// Recenter content at current zoom level, against the visible canvas pane.
// See `fitToViewport` for the leftOverlayWidth rationale.
const recenter = useCallback((leftOverlayWidth: number = 0) => {

Check warning on line 662 in src/renderer/CanvasView.tsx

View workflow job for this annotation

GitHub Actions / typecheck · lint · test

'leftOverlayWidth' is already declared in the upper scope on line 246 column 181
lastActionRef.current = 'recenter';
const sw = viewportWidth;
const sh = viewportHeight;
Expand Down Expand Up @@ -724,8 +729,8 @@

useEffect(() => {
onReady?.({
fitToViewport: (leftOverlayWidth?: number) => fitRef.current(leftOverlayWidth),
fitToViewport: (leftOverlayWidth?: number, options?: {animated?: boolean}) => fitRef.current(leftOverlayWidth, options),

Check warning on line 732 in src/renderer/CanvasView.tsx

View workflow job for this annotation

GitHub Actions / typecheck · lint · test

'leftOverlayWidth' is already declared in the upper scope on line 246 column 181
recenter: (leftOverlayWidth?: number) => recenterRef.current(leftOverlayWidth),

Check warning on line 733 in src/renderer/CanvasView.tsx

View workflow job for this annotation

GitHub Actions / typecheck · lint · test

'leftOverlayWidth' is already declared in the upper scope on line 246 column 181
getLastAction: () => lastActionRef.current,
camera: {translateX, translateY, scale, isPinching},
find,
Expand Down
37 changes: 37 additions & 0 deletions src/renderer/__tests__/fitCamera.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {fitCamera} from '../fitCamera';

const limits = {min: 0.1, max: 6};

describe('fitCamera', () => {
it('centres the content in the view at the scale that shows all of it', () => {
const cam = fitCamera({width: 1000, height: 500}, {x: 0, y: 0, width: 200, height: 100}, limits);
expect(cam.s).toBe(5);
expect(cam.tx).toBe(0);
expect(cam.ty).toBe(0);
});

it('centres across what the left strip leaves', () => {
const cam = fitCamera({width: 1300, height: 500, left: 300}, {x: 0, y: 0, width: 1000, height: 100}, limits);
expect(cam.s).toBe(1);
// The content's centre (500) lands at the visible area's centre (300 + 500).
expect(cam.tx + 500 * cam.s).toBe(800);
});

it('keeps the margin clear above and below, still centred down the whole view', () => {
const cam = fitCamera({width: 2000, height: 600, margin: 50}, {x: 0, y: 0, width: 100, height: 100}, limits);
expect(cam.s).toBe(5);
const top = cam.ty;
const bottom = cam.ty + 100 * cam.s;
expect(top).toBe(50);
expect(bottom).toBe(550);
});

it('puts the origin at the centre when there is nothing to fit', () => {
expect(fitCamera({width: 800, height: 600, left: 200}, {x: 0, y: 0, width: 0, height: 0}, limits)).toEqual({tx: 500, ty: 300, s: 1});
});

it('keeps to the scale limits', () => {
expect(fitCamera({width: 1000, height: 1000}, {x: 0, y: 0, width: 1, height: 1}, limits).s).toBe(6);
expect(fitCamera({width: 100, height: 100}, {x: 0, y: 0, width: 100000, height: 100000}, limits).s).toBe(0.1);
});
});
46 changes: 46 additions & 0 deletions src/renderer/fitCamera.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/** Where a canvas's camera goes to show all of it: its scale and translation. */
export interface FitCamera {
tx: number;
ty: number;
s: number;
}

export interface FitArea {
/** The canvas view's size, in points. */
width: number;
height: number;
/** A strip along the left edge that something covers (a sidebar): centred in what's left. */
left?: number;
/** Kept clear above and below the content: it's centred in the view, and no nearer either edge than this. */
margin?: number;
}

/**
* The content's bounds at the largest scale that shows all of it, centred:
* across what the left strip leaves, and down the whole view with `margin`
* kept clear at the top and the bottom. What opening a canvas shows, and
* what Fit does.
*/
export function fitCamera(
area: FitArea,
bounds: {x: number; y: number; width: number; height: number},
scaleLimits: {min: number; max: number},
): FitCamera {
const left = area.left ?? 0;
const margin = area.margin ?? 0;
const visibleW = Math.max(1, area.width - left);
const visibleH = Math.max(1, area.height - margin * 2);
const centreX = left + visibleW / 2;
const centreY = area.height / 2;
// Nothing to fit (no nodes): the world's origin at the centre, at 1:1.
if (bounds.width <= 0 || bounds.height <= 0) return {tx: centreX, ty: centreY, s: 1};
const s = Math.max(
scaleLimits.min,
Math.min(scaleLimits.max, Math.min(visibleW / bounds.width, visibleH / bounds.height)),
);
return {
tx: centreX - (bounds.x + bounds.width / 2) * s,
ty: centreY - (bounds.y + bounds.height / 2) * s,
s,
};
}
Loading