Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

feat(ui): zoom the app window towards the mouse pointer

rubenluetke10-beep2e123c1ac243
diff --git a/jarvis/ui/web/frontend/src/lib/zoomTransition.test.ts b/jarvis/ui/web/frontend/src/lib/zoomTransition.test.ts
index 3a480b3e1f..de7b7b854a 100644
--- a/jarvis/ui/web/frontend/src/lib/zoomTransition.test.ts
+++ b/jarvis/ui/web/frontend/src/lib/zoomTransition.test.ts
@@ -1,6 +1,11 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
-import { armZoomTransition, installZoomTransition, ZOOM_TRANSITION_MS } from "./zoomTransition";
+import {
+ armZoomTransition,
+ installZoomTransition,
+ scrollAnchorUnderPointer,
+ ZOOM_TRANSITION_MS,
+} from "./zoomTransition";
interface Played {
keyframes: Keyframe[];
@@ -53,6 +58,26 @@ describe("installZoomTransition", () => {
expect(played).toHaveLength(0);
});
+ it("grows out of the pointer when it is over the window", () => {
+ window.dispatchEvent(new MouseEvent("pointermove", { clientX: 400, clientY: 300 }));
+ armZoomTransition();
+ setViewport(1.25, 1280);
+ window.dispatchEvent(new Event("resize"));
+
+ expect(played[0].keyframes[0].transform).toBe("scale(0.8)");
+ // 400 x 300 old CSS pixels are 320 x 240 at 125 %.
+ expect(played[0].keyframes[0].transformOrigin).toBe("320px 240px");
+ });
+
+ it("falls back to the top-left corner once the pointer left the window", () => {
+ window.dispatchEvent(new MouseEvent("pointermove", { clientX: 400, clientY: 300 }));
+ document.documentElement.dispatchEvent(new MouseEvent("pointerleave"));
+ armZoomTransition();
+ setViewport(1.25, 1280);
+ window.dispatchEvent(new Event("resize"));
+ expect(played[0].keyframes[0].transformOrigin).toBe("0 0");
+ });
+
it("stays still when the window itself was resized during the zoom", () => {
armZoomTransition();
setViewport(1, 1200, 1300);
@@ -60,3 +85,28 @@ describe("installZoomTransition", () => {
expect(played).toHaveLength(0);
});
});
+
+describe("scrollAnchorUnderPointer", () => {
+ it("scrolls the container so the remembered spot is under the pointer again", () => {
+ const scroller = document.createElement("div");
+ scroller.style.overflowY = "auto";
+ const target = document.createElement("p");
+ scroller.appendChild(target);
+ document.body.appendChild(scroller);
+ Object.defineProperty(scroller, "scrollHeight", { configurable: true, value: 2000 });
+ Object.defineProperty(scroller, "clientHeight", { configurable: true, value: 500 });
+ let top = 100;
+ scroller.scrollTo = ((options: ScrollToOptions) => {
+ top = options.top ?? top;
+ }) as HTMLElement["scrollTo"];
+ Object.defineProperty(scroller, "scrollTop", { configurable: true, get: () => top });
+ // After the reflow the paragraph's middle is at y = 400 (minus what is scrolled).
+ target.getBoundingClientRect = () => new DOMRect(0, 350 - (top - 100), 200, 100);
+
+ scrollAnchorUnderPointer({ x: 100, y: 300, target, fx: 0.5, fy: 0.5 }, 100, 240);
+
+ // 400 - 240 = 160 more pixels scrolled.
+ expect(top).toBe(260);
+ scroller.remove();
+ });
+});
diff --git a/jarvis/ui/web/frontend/src/lib/zoomTransition.ts b/jarvis/ui/web/frontend/src/lib/zoomTransition.ts
index 0c8df7dbe5..b5f9d22679 100644
--- a/jarvis/ui/web/frontend/src/lib/zoomTransition.ts
+++ b/jarvis/ui/web/frontend/src/lib/zoomTransition.ts
@@ -1,6 +1,6 @@
/**
* A short, smooth scale from the old zoom level to the new one, so a zoom step
- * glides instead of jumping.
+ * glides instead of jumping — centred on the mouse pointer.
*
* ## How it stays in sync with the engine
*
@@ -16,9 +16,23 @@
*
* The step is FLIP-style: the page is already laid out at the new level and is
* scaled back to how big it looked before (old / new), then released to 1.
- * The origin is the top-left corner, which is also where the engine anchors
- * the reflow, so the sidebar and the title strip grow from where they already
- * are instead of sliding.
+ *
+ * ## Zooming towards the mouse
+ *
+ * The pointer decides where the zoom goes in or out, the way a map or a photo
+ * viewer zooms. Two things make that happen:
+ *
+ * * Before the step, the element under the pointer and the spot on it are
+ * remembered. After the engine reflowed the page, the scroll containers
+ * around that element are scrolled so the same spot sits under the pointer
+ * again. Fixed chrome (sidebar, title strip) has nowhere to scroll and simply
+ * reflows; scrolled content — a chat, a document, a list — stays put.
+ * * The glide grows out of the pointer (`transform-origin`) instead of the
+ * top-left corner, so the content under it does not wander during the step.
+ *
+ * With the pointer outside the window (or never moved over it) the step keeps
+ * the old behaviour: anchored at the top-left corner, where the engine anchors
+ * the reflow.
*
* A held-down key fires steps faster than the animation runs. Each new step
* starts from the scale currently on screen, so a run of steps reads as one
@@ -36,11 +50,54 @@ const EASING = "cubic-bezier(0.22, 1, 0.36, 1)";
/** How long after a request an engine resize still counts as its answer. */
const ARM_WINDOW_MS = 1500;
+/** Nested scroll containers walked outwards from the element under the pointer. */
+const MAX_SCROLLERS = 4;
+
+/** The spot under the pointer, taken in the layout before the step. */
+export interface ZoomAnchor {
+ /** Pointer position in the old CSS pixels. */
+ x: number;
+ y: number;
+ /** The element under the pointer, if there is one. */
+ target: Element | null;
+ /** Where on that element the pointer was, as a fraction of its box. */
+ fx: number;
+ fy: number;
+}
+
let armedUntil = 0;
+let anchor: ZoomAnchor | null = null;
+let pointer: { x: number; y: number } | null = null;
let running: Animation | null = null;
-/** Mark the next engine resize as the answer to a zoom this window asked for. */
-export function armZoomTransition(now: number = performance.now()): void {
+/** Where the pointer is over this window, or null while it is outside. */
+export function setZoomPointer(position: { x: number; y: number } | null): void {
+ pointer = position;
+}
+
+function captureAnchor(doc: Document): ZoomAnchor | null {
+ if (!pointer) return null;
+ const { x, y } = pointer;
+ const target = typeof doc.elementFromPoint === "function" ? doc.elementFromPoint(x, y) : null;
+ let fx = 0;
+ let fy = 0;
+ if (target) {
+ const box = target.getBoundingClientRect();
+ fx = box.width > 0 ? (x - box.left) / box.width : 0;
+ fy = box.height > 0 ? (y - box.top) / box.height : 0;
+ }
+ return { x, y, target, fx, fy };
+}
+
+/**
+ * Mark the next engine resize as the answer to a zoom this window asked for,
+ * and remember what the pointer is over in the layout as it is now.
+ */
+export function armZoomTransition(now: number = performance.now(), doc: Document = document): void {
+ // A queued step arms again before the first answer landed; the first
+ // anchor still describes the layout on screen. One whose answer never
+ // came (a failed request) is stale.
+ if (!anchor || now > armedUntil) anchor = captureAnchor(doc);
armedUntil = now + ARM_WINDOW_MS;
}
@@ -53,8 +110,42 @@ function currentScale(el: HTMLElement): number {
return Number.isFinite(scale) && scale > 0 ? scale : 1;
}
-/** Ease the page from `from` (a scale factor) back to its real size. */
-export function playZoomTransition(el: HTMLElement, from: number): void {
+function isScroller(el: Element, win: Window): boolean {
+ const canX = el.scrollWidth > el.clientWidth + 1;
+ const canY = el.scrollHeight > el.clientHeight + 1;
+ if (!canX && !canY) return false;
+ if (el === win.document.scrollingElement) return true;
+ const style = win.getComputedStyle(el);
+ const scrolls = (v: string) => v === "auto" || v === "scroll" || v === "overlay";
+ return (canX && scrolls(style.overflowX)) || (canY && scrolls(style.overflowY));
+}
+
+/**
+ * Scroll the containers around the anchored element so the remembered spot
+ * sits under the pointer again (`px`, `py` in the new CSS pixels). The
+ * innermost container takes what it can; the rest goes to the next one out.
+ */
+export function scrollAnchorUnderPointer(anchorAt: ZoomAnchor, px: number, py: number, win: Window = window): void {
+ const target = anchorAt.target;
+ if (!target || !target.isConnected) return;
+ let scrollers = 0;
+ for (let el: Element | null = target.parentElement; el && scrollers < MAX_SCROLLERS; el = el.parentElement) {
+ if (!isScroller(el, win)) continue;
+ scrollers += 1;
+ const box = target.getBoundingClientRect();
+ const dx = box.left + anchorAt.fx * box.width - px;
+ const dy = box.top + anchorAt.fy * box.height - py;
+ if (Math.abs(dx) < 0.5 && Math.abs(dy) < 0.5) return;
+ // "instant" so a container with `scroll-behavior: smooth` does not slide.
+ el.scrollTo({ left: el.scrollLeft + dx, top: el.scrollTop + dy, behavior: "instant" as ScrollBehavior });
+ }
+}
+
+/**
+ * Ease the page from `from` (a scale factor) back to its real size, growing
+ * out of `origin` (CSS pixels, top-left corner when omitted).
+ */
+export function playZoomTransition(el: HTMLElement, from: number, origin?: { x: number; y: number }): void {
if (typeof el.animate !== "function") return;
const start = currentScale(el) * from;
running?.cancel();
@@ -62,13 +153,14 @@ export function playZoomTransition(el: HTMLElement, from: number): void {
running = null;
return;
}
+ const transformOrigin = origin ? `${Math.round(origin.x)}px ${Math.round(origin.y)}px` : "0 0";
// A page scaled above 1 overflows the window for a moment; without this
// the viewport would flash scrollbars while it shrinks back.
el.style.overflow = "hidden";
const animation = el.animate(
[
- { transform: `scale(${start})`, transformOrigin: "0 0" },
- { transform: "scale(1)", transformOrigin: "0 0" },
+ { transform: `scale(${start})`, transformOrigin },
+ { transform: "scale(1)", transformOrigin },
],
{ duration: ZOOM_TRANSITION_MS, easing: EASING },
);
@@ -91,6 +183,9 @@ export function installZoomTransition(win: Window = window): () => void {
let lastInner = win.innerWidth;
let lastOuter = win.outerWidth;
+ const onPointerMove = (event: PointerEvent) => setZoomPointer({ x: event.clientX, y: event.clientY });
+ const onPointerLeave = () => setZoomPointer(null);
+
const onResize = () => {
const dpr = win.devicePixelRatio || 1;
const inner = win.innerWidth;
@@ -100,8 +195,9 @@ export function installZoomTransition(win: Window = window): () => void {
lastInner = inner;
lastOuter = outer;
+ const anchorAt = anchor;
+ anchor = null;
if (performance.now() > armedUntil) return;
- if (win.matchMedia?.("(prefers-reduced-motion: reduce)").matches) return;
// The device pixel ratio carries the zoom on Chromium and WebKit. Where it
// does not, the CSS viewport of an unchanged window shrank or grew by the
@@ -110,12 +206,31 @@ export function installZoomTransition(win: Window = window): () => void {
if (Math.abs(dpr - before.dpr) > 0.001) from = before.dpr / dpr;
else if (outer === before.outer && before.inner > 0 && inner !== before.inner) from = inner / before.inner;
if (from === 1) return;
- playZoomTransition(win.document.documentElement, from);
+
+ const root = win.document.documentElement;
+ // The pointer has not moved on the screen, so in CSS pixels it moved by
+ // the zoom factor.
+ const origin = anchorAt ? { x: anchorAt.x * from, y: anchorAt.y * from } : undefined;
+ if (anchorAt && origin) {
+ // Measure without the previous step's scale still applied.
+ const carried = currentScale(root);
+ running?.cancel();
+ scrollAnchorUnderPointer(anchorAt, origin.x, origin.y, win);
+ from *= carried;
+ }
+ if (win.matchMedia?.("(prefers-reduced-motion: reduce)").matches) return;
+ playZoomTransition(root, from, origin);
};
win.addEventListener("resize", onResize);
+ win.addEventListener("pointermove", onPointerMove, { passive: true });
+ win.document.documentElement.addEventListener("pointerleave", onPointerLeave);
return () => {
win.removeEventListener("resize", onResize);
+ win.removeEventListener("pointermove", onPointerMove);
+ win.document.documentElement.removeEventListener("pointerleave", onPointerLeave);
running?.cancel();
+ anchor = null;
+ pointer = null;
};
}