An open-source, self-hosted system for apps built with web technologies.
What is painted of a Window never jumps under the hand
zohayrslileh592bac0e8dc8
diff --git a/source/client/view/components/window-manager/window-geometry.ts b/source/client/view/components/window-manager/window-geometry.tsindex b25ad14..740cbe5 100644--- a/source/client/view/components/window-manager/window-geometry.ts+++ b/source/client/view/components/window-manager/window-geometry.ts@@ -255,6 +255,19 @@ export const noPaintMargins: PaintMargins = Object.freeze({ top: 0, right: 0, bo*/export function windowPaintInsets(position: Position, size: Size, surface: ViewSize, inset: number, margins: PaintMargins, current?: WindowRegion, shift: Readonly<{ x: number, y: number }> = noShift) {+ return contactInsets(windowEdgeContacts(position, size, surface, current, shift), inset, margins)+}++/** Which sides of a standard Window meet an edge of its view. */+export type WindowSides = Readonly<{ top: boolean, right: boolean, bottom: boolean, left: boolean }>++export const noWindowSides: WindowSides = Object.freeze({ top: false, right: false, bottom: false, left: false })++export const allWindowSides: WindowSides = Object.freeze({ top: true, right: true, bottom: true, left: true })++/** The sides of a Window that meet an edge of the view it lives in; see `windowPaintInsets`. */+export function windowEdgeContacts(position: Position, size: Size, surface: ViewSize, current?: WindowRegion, shift: Readonly<{ x: number, y: number }> = noShift): WindowSides {+const x = current?.x ?? surface.width / 2 + pixels(position.x, surface.width)const y = current?.y ?? surface.height / 2 + pixels(position.y, surface.height)@@ -265,16 +278,45 @@ export function windowPaintInsets(position: Position, size: Size, surface: ViewSreturn {- top: startsAtBoundary(position.y, y + shift.y, surface.height) ? margins.top : inset,+ top: startsAtBoundary(position.y, y + shift.y, surface.height),++ right: endsAtBoundary(position.x, size.width, x + shift.x, width, surface.width),++ bottom: endsAtBoundary(position.y, size.height, y + shift.y, height, surface.height),- right: endsAtBoundary(position.x, size.width, x + shift.x, width, surface.width) ? margins.right : inset,+ left: startsAtBoundary(position.x, x + shift.x, surface.width)+ }+}- bottom: endsAtBoundary(position.y, size.height, y + shift.y, height, surface.height) ? margins.bottom : inset,+/** The margins on the sides that meet an edge, the inset on the others. */+export function contactInsets(contacts: WindowSides, inset: number, margins: PaintMargins): PaintMargins {- left: startsAtBoundary(position.x, x + shift.x, surface.width) ? margins.left : inset+ return {+ top: contacts.top ? margins.top : inset,+ right: contacts.right ? margins.right : inset,+ bottom: contacts.bottom ? margins.bottom : inset,+ left: contacts.left ? margins.left : inset}}+/**+ * The box that keeps a Window's painted surface where it is while the `carried` sides let go of the+ * edges they meet: each such side moves in by what its margin kept beyond the inset. A hand carrying+ * a side takes it off the edge, and what a person sees of the Window must not jump when it does.+ */+export function letGoOfEdges(region: WindowRegion, contacts: WindowSides, carried: WindowSides, inset: number, margins: PaintMargins): WindowRegion {++ const top = carried.top && contacts.top ? margins.top - inset : 0++ const right = carried.right && contacts.right ? margins.right - inset : 0++ const bottom = carried.bottom && contacts.bottom ? margins.bottom - inset : 0++ const left = carried.left && contacts.left ? margins.left - inset : 0++ return { x: region.x + left, y: region.y + top, width: region.width - left - right, height: region.height - top - bottom }+}+function pixels(value: Value, span: number) {const resolved = relative(value)diff --git a/source/client/view/structure/desktop/windows/window-geometry-motion.ts b/source/client/view/structure/desktop/windows/window-geometry-motion.tsindex 4d2ea8d..4721ae8 100644--- a/source/client/view/structure/desktop/windows/window-geometry-motion.ts+++ b/source/client/view/structure/desktop/windows/window-geometry-motion.ts@@ -299,6 +299,16 @@ export default function useWindowGeometryMotion({ position, size, animation, traif (!chase.current.frame) chase.current.frame = requestAnimationFrame(follow)}+ /**+ * Changes the shown box at once, outside the chase that smooths a hand's motion: for a change+ * the painting makes at the same moment, such as sides letting go of the edges, which must not+ * show between its two states.+ */+ function reshapeGesture(change: (shown: WindowRegion) => WindowRegion) {++ set(change(read()))+ }+function restoreGesture(region: WindowRegion) {const shown = read()@@ -385,6 +395,7 @@ export default function useWindowGeometryMotion({ position, size, animation, trapresent,beginGesture,updateGesture,+ reshapeGesture,restoreGesture,targetGesture,settleGesture,diff --git a/source/client/view/structure/desktop/windows/window.tsx b/source/client/view/structure/desktop/windows/window.tsxindex 5d98e03..86c8bb3 100644--- a/source/client/view/structure/desktop/windows/window.tsx+++ b/source/client/view/structure/desktop/windows/window.tsx@@ -2,7 +2,7 @@ import { ComponentProps, PointerEvent as ReactPointerEvent, ReactNode, useCallbaimport { useReducedMotion } from "@libs/react-motion"import { surfaceLifecyclePose, surfacePresenceTransition } from "@client/view/appearance/surface-presence"import WindowPanel from "./window-panel"-import { absoluteWindowGeometry, constrainWindowGeometry, minimumWindowSize, noPaintMargins, planeGeometry, resolveWindowGeometry, snapPlacement, windowPaintInsets, type PaintMargins, type WindowRegion, type ViewSize } from "@client/view/components/window-manager/window-geometry"+import { absoluteWindowGeometry, allWindowSides, constrainWindowGeometry, contactInsets, letGoOfEdges, minimumWindowSize, noPaintMargins, noWindowSides, planeGeometry, resolveWindowGeometry, snapPlacement, windowEdgeContacts, windowPaintInsets, type PaintMargins, type WindowRegion, type WindowSides, type ViewSize } from "@client/view/components/window-manager/window-geometry"import { type BeginPresentationMoveGesture, type Position, type Size, type TaskbarPosition, type PresentationSurface as WindowSurfaceDefinition, type WindowLayer } from "@phreshos/core"import WindowHeader from "./window-header"import WindowSurface, { windowSurfaceRadius } from "./window-surface"@@ -407,6 +407,48 @@ export default function ({ title, header = true, surface, layer, icon, children,let current: WindowRegion = { ...origin }+ // What a person sees of the Window never jumps under the hand. The sides the gesture carries+ // let go of the edges they meet, the box moving in to keep the painted surface still; the+ // others keep the contact they had. A resize carries its edge at once, a move once it moves.+ const declared = started.region++ const contacts = windowEdgeContacts(presented.current.position, presented.current.size, paintSurfaceSize, origin, cellShift.get())++ let carried: WindowSides = edge !== null ? sidesOf(edge) : absolute ? allWindowSides : noWindowSides++ let insets = gestureInsets()++ let letGo = false++ function gestureInsets() {++ const kept = { top: contacts.top && !carried.top, right: contacts.right && !carried.right, bottom: contacts.bottom && !carried.bottom, left: contacts.left && !carried.left }++ return contactInsets(kept, paintInset, paintMargins)+ }++ function carry(sides: WindowSides) {++ carried = sides++ const box = letGoOfEdges(origin, contacts, sides, paintInset, paintMargins)++ if (box.width === origin.width && box.height === origin.height) return++ letGo = true++ origin = box++ current = { ...origin }++ insets = gestureInsets()++ // The box and its insets change in the same frame, so what is painted stays still.+ geometryMotion.reshapeGesture(shown => letGoOfEdges(shown, contacts, sides, paintInset, paintMargins))+ }++ if (carried !== noWindowSides) carry(carried)+if (geometryAnimation) onPresentationAnimationComplete?.("geometry", geometryAnimation.revision)// Pulling a Window placed by shares of the view (maximized or snapped) out of its place belongs@@ -470,7 +512,7 @@ export default function ({ title, header = true, surface, layer, icon, children,renderFrame = 0- setGesture({ origin, current, zone, shown, blocked })+ setGesture({ origin, current, zone, shown, blocked, insets })})}@@ -517,9 +559,13 @@ export default function ({ title, header = true, surface, layer, icon, children,if (restoringMaximized) {const stored = resolvePresentedGeometry(position, size, bounds)origin = { ...origin, width: stored.width, height: stored.height }+ carried = allWindowSides+ insets = contactInsets(noWindowSides, paintInset, paintMargins)request(() => onMaximize?.())}+ else carry(allWindowSides)+origin = { x: pointerX - origin.width * ratio, y: pointerY - Math.min(Math.max(pointerY - origin.y, 0), 40), width: origin.width, height: origin.height }current = { ...origin }@@ -535,7 +581,7 @@ export default function ({ title, header = true, surface, layer, icon, children,if (restoringMaximized) geometryMotion.restoreGesture(current)else geometryMotion.updateGesture(current)- setGesture({ origin, current, zone, shown, blocked })+ setGesture({ origin, current, zone, shown, blocked, insets })return}@@ -625,13 +671,14 @@ export default function ({ title, header = true, surface, layer, icon, children,// The Desktop paints from the surface's corner; the System records from its center.const placed = planeGeometry(current, bounds!)- if (edge === null) request(() => latest.current.onMove?.(placed.x, placed.y))+ // A Window whose sides let go of the edges keeps the size it is seen at.+ if (edge === null) request(() => letGo ? latest.current.onResize?.(current.width, current.height, { x: placed.x, y: placed.y }) : latest.current.onMove?.(placed.x, placed.y))// Only the west and north edges move the origin. A drag// on any other reports no position, because none was// chosen — and a position nobody chose would replace a// share with the pixels it happened to resolve to.- else request(() => latest.current.onResize?.(current.width, current.height, current.x === origin.x && current.y === origin.y ? null : { x: placed.x, y: placed.y }))+ else request(() => latest.current.onResize?.(current.width, current.height, current.x === declared.x && current.y === declared.y ? null : { x: placed.x, y: placed.y }))settle()@@ -670,7 +717,7 @@ export default function ({ title, header = true, surface, layer, icon, children,cancel() { release(pointer, false) }})- setGesture({ origin, current, zone, shown, blocked })+ setGesture({ origin, current, zone, shown, blocked, insets })}// ------------------------------------------------------------ render@@ -683,7 +730,7 @@ export default function ({ title, header = true, surface, layer, icon, children,// follows the same locally owned geometry throughout that interval so an// old boundary contact cannot flash back for one frame.// Follows where the view looks from, but redraws only when this Window's own margins change.- const insetsNow = () => windowPaintInsets(presented.current.position, presented.current.size, paintSurfaceSize, paintInset, paintMargins, gesture?.current ?? settlingGeometry ?? undefined, cellShift.get())+ const insetsNow = () => gesture?.insets ?? windowPaintInsets(presented.current.position, presented.current.size, paintSurfaceSize, paintInset, paintMargins, settlingGeometry ?? undefined, cellShift.get())const insetsKey = useSyncExternalStore(cellShift.subscribe, () => JSON.stringify(insetsNow()), () => JSON.stringify(insetsNow()))const paintedInsets = JSON.parse(insetsKey) as ReturnType<typeof windowPaintInsets>@@ -842,6 +889,12 @@ export default function ({ title, header = true, surface, layer, icon, children,type WindowEdge = "n" | "s" | "e" | "w" | "ne" | "nw" | "se" | "sw"+/** The sides a resize from an edge or corner carries. */+function sidesOf(edge: WindowEdge): WindowSides {++ return { top: edge.includes("n"), right: edge.includes("e"), bottom: edge.includes("s"), left: edge.includes("w") }+}+type Snap = SnapTargetinterface WindowProps extends Omit<ComponentProps<"div">, "onAnimationStart" | "onDrag" | "onDragEnd" | "onDragStart" | "title"> {@@ -953,6 +1006,9 @@ interface Gesture {blocked?: booleanshown: Snap | null++ /** What the Window is painted inset by while the gesture lasts. */+ insets: PaintMargins}interface ActivePointerGesture {diff --git a/tests/window-geometry.test.ts b/tests/window-geometry.test.tsindex d2341f8..b73938b 100644--- a/tests/window-geometry.test.ts+++ b/tests/window-geometry.test.ts@@ -3,7 +3,7 @@ import ClientWindow from "@client/core/link-manager/auth-manager/process-managerimport ClientProcessManager from "@client/core/link-manager/auth-manager/process-manager/process-manager"import ProcessManager from "@server/core/link-manager/auth-manager/process-manager/process-manager"import ServerWindow from "@server/core/link-manager/auth-manager/process-manager/window"-import { boundedGeometry, constrainWindowGeometry, minimumWindowSize, noPaintMargins, planeGeometry, recordedPosition, resolveWindowGeometry, shiftPosition, snapPlacement, viewOfGeometry, windowPaintInsets } from "@client/view/components/window-manager/window-geometry"+import { allWindowSides, boundedGeometry, constrainWindowGeometry, contactInsets, letGoOfEdges, minimumWindowSize, noPaintMargins, planeGeometry, recordedPosition, resolveWindowGeometry, shiftPosition, snapPlacement, viewOfGeometry, windowEdgeContacts, windowPaintInsets } from "@client/view/components/window-manager/window-geometry"import { defaultAppearance } from "@phreshos/core"import { TheLink } from "@the-link/core"import { expect, test } from "vitest"@@ -84,6 +84,31 @@ test("settling paint follows released geometry instead of its former boundary co)})+test("sides a hand carries let go of their edges without moving what is painted", () => {+ const surface = { width: 1000, height: 600 }+ const half = defaultAppearance.spacing / 2+ const margins = { top: 12, right: 12, bottom: 68, left: 12 }+ // The right half of the view: it meets the top, right, and bottom edges.+ const position = { x: 0, y: "-1/2" }, size = { width: "1/2", height: "1/1" }+ const box = resolveWindowGeometry(position, size, surface)+ const contacts = windowEdgeContacts(position, size, surface)+ const painted = (region: typeof box, insets: ReturnType<typeof contactInsets>) => ({+ x: region.x + insets.left, y: region.y + insets.top,+ width: region.width - insets.left - insets.right, height: region.height - insets.top - insets.bottom+ })++ assert.deepEqual(contacts, { top: true, right: true, bottom: true, left: false })++ // Moved, every side lets go; the box moves in, and the painted surface stays where it was.+ const carried = letGoOfEdges(box, contacts, allWindowSides, half, margins)+ assert.deepEqual(painted(carried, contactInsets({ top: false, right: false, bottom: false, left: false }, half, margins)), painted(box, contactInsets(contacts, half, margins)))++ // Pulled by its bottom edge, only that side lets go; the others keep their margins.+ const pulled = letGoOfEdges(box, contacts, { top: false, right: false, bottom: true, left: false }, half, margins)+ assert.deepEqual(painted(pulled, contactInsets({ ...contacts, bottom: false }, half, margins)), painted(box, contactInsets(contacts, half, margins)))+ assert.deepEqual({ x: pulled.x, y: pulled.y, width: pulled.width }, { x: box.x, y: box.y, width: box.width })+})+test("a tiled window keeps the margins of its own view while the Desktop looks between two", () => {const surface = { width: 1000, height: 600 }const half = defaultAppearance.spacing / 2