Skip to content

An open-source, self-hosted system for apps built with web technologies.

System 0.8.2: a dragged Window snaps only once held at an edge; the Map shows its crossing

zohayrslilehe35e48af6a44
diff --git a/package.json b/package.json
index 70516b9..e10084e 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "phreshos",
- "version": "0.8.1",
+ "version": "0.8.2",
"description": "The PhreshOS server, desktop, and authoritative system runtime.",
"type": "module",
"private": true,
diff --git a/source/client/view/structure/desktop/edge-hold.ts b/source/client/view/structure/desktop/edge-hold.ts
index 74f778c..360f79f 100644
--- a/source/client/view/structure/desktop/edge-hold.ts
+++ b/source/client/view/structure/desktop/edge-hold.ts
@@ -45,6 +45,10 @@ export function against(x: number, y: number, width: number, height: number): Di
* Something held against an edge of the screen takes the view that way: after 700 milliseconds, one
* whole view; then, once the view has arrived and a half second has passed, another, for as long as
* it stays there. Where the plane goes no further, it says so, so the one holding can show it.
+ *
+ * A hold that can also settle there, as a Window that snaps to the edge, first waits 300 milliseconds
+ * to settle and only then 700 more to travel, so a hand that only carries it near the edge does
+ * neither. Once settled, it stays so along the edges until it leaves them.
*/
export class EdgeHold {
@@ -54,9 +58,16 @@ export class EdgeHold {
private blocked = false
+ /** The edges it has settled against, or null while it has not. */
+ public settled: Direction | null = null
+
public constructor(
private readonly travel: (direction: Direction) => number | null | undefined,
- private readonly onBlocked?: (blocked: boolean) => void
+ private readonly events: Readonly<{
+ onBlocked?: (blocked: boolean) => void
+ /** Given, the hold settles before it travels, and says when it settles or leaves. */
+ onSettled?: (settled: Direction | null) => void
+ }> = {}
) {}
/** Where it is held now; a new edge starts the wait again, and no edge stops it. */
@@ -64,29 +75,66 @@ export class EdgeHold {
if (direction.x === this.direction.x && direction.y === this.direction.y) return
- this.stop()
+ if (!direction.x && !direction.y) return this.stop()
+
+ this.pause()
this.direction = direction
- if (direction.x || direction.y) this.timer = window.setTimeout(() => this.hold(), 700)
+ if (!this.events.onSettled) this.timer = window.setTimeout(() => this.hold(), 700)
+
+ else if (this.settled) {
+
+ this.settle()
+
+ this.timer = window.setTimeout(() => this.hold(), 700)
+ }
+
+ else this.timer = window.setTimeout(() => {
+
+ this.settle()
+
+ this.timer = window.setTimeout(() => this.hold(), 700)
+
+ }, 300)
}
public stop() {
+ this.pause()
+
+ this.direction = { x: 0, y: 0 }
+
+ if (this.settled) {
+
+ this.settled = null
+
+ this.events.onSettled?.(null)
+ }
+ }
+
+ /** Stops waiting and shows the plane's end no more, still settled. */
+ private pause() {
+
if (this.timer) clearTimeout(this.timer)
this.timer = 0
- this.direction = { x: 0, y: 0 }
-
if (this.blocked) {
this.blocked = false
- this.onBlocked?.(false)
+ this.events.onBlocked?.(false)
}
}
+ private settle() {
+
+ this.settled = this.direction
+
+ this.events.onSettled?.(this.settled)
+ }
+
private hold() {
const took = this.travel(this.direction)
@@ -97,7 +145,7 @@ export class EdgeHold {
this.blocked = true
- this.onBlocked?.(true)
+ this.events.onBlocked?.(true)
return
}
diff --git a/source/client/view/structure/desktop/layers/shell/taskbar/map/crossing-sign.tsx b/source/client/view/structure/desktop/layers/shell/taskbar/map/crossing-sign.tsx
new file mode 100644
index 0000000..22af6cd
--- /dev/null
+++ b/source/client/view/structure/desktop/layers/shell/taskbar/map/crossing-sign.tsx
@@ -0,0 +1,70 @@
+import { type CSSProperties } from "react"
+import { motion } from "motion/react"
+import { Surface, useTiming } from "@phreshos/react-ui"
+import { ArrowDown, ArrowDownLeft, ArrowDownRight, ArrowLeft, ArrowRight, ArrowUp, ArrowUpLeft, ArrowUpRight } from "@phreshos/react-ui/icons"
+import { motionTransition } from "@client/view/appearance/motion"
+import { type PaintMargins } from "@client/view/components/window-manager/window-geometry"
+import { type Direction } from "../../../../edge-hold"
+
+/** The arrow for each way, by row (up, level, down) and column (left, level, right). */
+const arrows = [
+ [ArrowUpLeft, ArrowUp, ArrowUpRight],
+ [ArrowLeft, null, ArrowRight],
+ [ArrowDownLeft, ArrowDown, ArrowDownRight]
+]
+
+/** Where each way places the sign along an axis. */
+const sides = ["flex-start", "center", "flex-end"] as const
+
+/**
+ * Where the Map is held against an edge of the screen: a sign at that edge, pointing where the view
+ * goes, with the place of the view shown now. Unlike a Window's, the Map's hold places nothing, so it
+ * shows a way, not a placement: the arrow, then the place, side by side. Each time the
+ * view crosses, its new place comes in from the way it went and the sign leans that way, so a
+ * crossing shows even where no Window moves. Where the plane goes no further, it turns to danger.
+ */
+export default function CrossingSign({ direction, place, visible, blocked, margins, spacing, reducedMotion, zIndex }: CrossingSignProps) {
+
+ const transaction = useTiming()("change")
+
+ const Arrow = arrows[direction.y + 1][direction.x + 1]
+
+ const lean = { x: direction.x * spacing / 2, y: direction.y * spacing / 2 }
+
+ return <div aria-hidden="true" className="pointer-events-none absolute flex" style={{
+ top: margins.top + spacing, right: margins.right + spacing, bottom: margins.bottom + spacing, left: margins.left + spacing,
+ justifyContent: sides[direction.x + 1], alignItems: sides[direction.y + 1], zIndex
+ }}>
+ <motion.div
+ initial={false}
+ animate={{ opacity: visible ? 1 : 0, x: visible ? 0 : -direction.x * spacing, y: visible ? 0 : -direction.y * spacing }}
+ transition={motionTransition(transaction, reducedMotion)}
+ >
+ {/* Drawn anew at each place, so each crossing leans it once. */}
+ <motion.div key={place} initial={reducedMotion ? false : lean} animate={{ x: 0, y: 0 }} transition={motionTransition(transaction, reducedMotion)}>
+ <Surface color={blocked ? "danger:soft" : "primary:soft"} radius="full" className="flex items-center justify-center"
+ style={{ gap: spacing / 2, padding: spacing, minWidth: spacing * 8 }}>
+ {Arrow && <Arrow />}
+ <motion.span className="tabular-nums" initial={reducedMotion ? false : { opacity: 0, ...lean }} animate={{ opacity: 1, x: 0, y: 0 }} transition={motionTransition(transaction, reducedMotion)}>
+ {place}
+ </motion.span>
+ </Surface>
+ </motion.div>
+ </motion.div>
+ </div>
+}
+
+interface CrossingSignProps {
+ /** The edges it is held against; kept while it fades away. */
+ direction: Direction
+ /** The place of the view shown now, as the Map names it. */
+ place: string
+ visible: boolean
+ /** The hand holds it against the plane's end. */
+ blocked: boolean
+ /** The room the Taskbar leaves, which the sign stays inside. */
+ margins: PaintMargins
+ spacing: number
+ reducedMotion: boolean
+ zIndex: CSSProperties["zIndex"]
+}
diff --git a/source/client/view/structure/desktop/layers/shell/taskbar/map/map-control.tsx b/source/client/view/structure/desktop/layers/shell/taskbar/map/map-control.tsx
index aba6133..57b8b47 100644
--- a/source/client/view/structure/desktop/layers/shell/taskbar/map/map-control.tsx
+++ b/source/client/view/structure/desktop/layers/shell/taskbar/map/map-control.tsx
@@ -3,14 +3,14 @@ import { Map as MapIcon, Maximize2 } from "@phreshos/react-ui/icons"
import WindowMenu from "../../window-menu"
import TaskbarTooltip from "../taskbar-tooltip"
import { useEffect, useId, useLayoutEffect, useRef, useState, type CSSProperties, type PointerEvent as ReactPointerEvent } from "react"
-import { planeReach, snapPlacement, type WindowRegion } from "@client/view/components/window-manager/window-geometry"
+import { planeReach, type WindowRegion } from "@client/view/components/window-manager/window-geometry"
import { type AppearanceTaskbar } from "@phreshos/core"
import { useReducedMotion } from "@libs/react-motion"
import { motion } from "motion/react"
import { motionTransition } from "@client/view/appearance/motion"
-import { EdgeHold, against, useViewTravel } from "../../../../edge-hold"
+import { EdgeHold, against, useViewTravel, type Direction } from "../../../../edge-hold"
import { createPortal } from "react-dom"
-import SnapPreview, { type SnapTarget } from "../../../../windows/snap-preview"
+import CrossingSign from "./crossing-sign"
import { desktopMargins } from "../../../desktop-layers"
import { surfacePresenceTransition } from "@client/view/appearance/surface-presence"
import { useShellPopover } from "../../shell-popover"
@@ -129,10 +129,9 @@ export default function MapControl({ viewport, windows, taskbar, spacing, onOpen
// A carry that ended over the button is not also a press.
const carried = useRef(false)
- // Where the ghost is held against the Desktop's edges: the placement a Window would take there,
- // shown as a Window's is, but only as a sign; nothing is placed when the hand lets go. It turns to
- // danger where the plane goes no further.
- const [edge, setEdge] = useState<{ zone: SnapTarget | null, shown: SnapTarget | null, blocked: boolean, layer: HTMLElement | null }>({ zone: null, shown: null, blocked: false, layer: null })
+ // Where the ghost is held against the Desktop's edges, shown as the way the view goes; see
+ // CrossingSign. `shown` stays while the sign fades away.
+ const [edge, setEdge] = useState<{ held: boolean, shown: Direction | null, blocked: boolean, layer: HTMLElement | null }>({ held: false, shown: null, blocked: false, layer: null })
function carry(event: ReactPointerEvent<HTMLElement>) {
@@ -144,7 +143,7 @@ export default function MapControl({ viewport, windows, taskbar, spacing, onOpen
const held = event.target as Element
- const edges = new EdgeHold(direction => travelNow.current(direction), blocked => setEdge(current => ({ ...current, blocked })))
+ const edges = new EdgeHold(direction => travelNow.current(direction), { onBlocked: blocked => setEdge(current => ({ ...current, blocked })) })
const windowLayer = document.querySelector<HTMLElement>("[data-desktop-layer=\"window\"]")
@@ -190,9 +189,9 @@ export default function MapControl({ viewport, windows, taskbar, spacing, onOpen
edges.update(direction)
- const zone = direction.x || direction.y ? snapPlacement(direction, desktopMargins(spacing, taskbar)) : null
+ const atEdge = direction.x !== 0 || direction.y !== 0
- setEdge(current => ({ ...current, zone, shown: zone ?? current.shown, layer: windowLayer }))
+ setEdge(current => ({ ...current, held: atEdge, shown: atEdge ? direction : current.shown, layer: windowLayer }))
}
function end(motion: PointerEvent) {
@@ -203,7 +202,7 @@ export default function MapControl({ viewport, windows, taskbar, spacing, onOpen
setGhost(null)
- setEdge(current => ({ ...current, zone: null, blocked: false }))
+ setEdge(current => ({ ...current, held: false, blocked: false }))
removeEventListener("pointermove", move)
@@ -273,13 +272,13 @@ export default function MapControl({ viewport, windows, taskbar, spacing, onOpen
</TaskbarTooltip>
</span>
- {edge.shown && edge.layer && createPortal(<SnapPreview
- shown={edge.shown}
- visible={edge.zone !== null}
+ {edge.shown && edge.layer && createPortal(<CrossingSign
+ direction={edge.shown}
+ place={place}
+ visible={edge.held}
blocked={edge.blocked}
- paintSurfaceSize={{ width: edge.layer.clientWidth, height: edge.layer.clientHeight }}
- paintInset={spacing / 2}
- paintMargins={desktopMargins(spacing, taskbar)}
+ margins={desktopMargins(spacing, taskbar)}
+ spacing={spacing}
reducedMotion={reducedMotion}
zIndex={2147483646}
/>, edge.layer)}
@@ -582,7 +581,7 @@ function ViewMap({ labelId, viewport, windows, centered, menus, room }: Readonly
<div ref={footer} className="flex items-center justify-between" style={{ gap: space.medium }}>
- <Text size="xsmall" tone="secondary">Choose a view or a Window, drag the frame, or drag a Window to move it.</Text>
+ <Text size="xsmall" tone="secondary">Drag the Map button to an edge of the screen to go to the next view.</Text>
<Button size="small" disabled={centered} onPress={viewport.home}>Center</Button>
diff --git a/source/client/view/structure/desktop/windows/snap-preview.tsx b/source/client/view/structure/desktop/windows/snap-preview.tsx
index 6f134f1..2780095 100644
--- a/source/client/view/structure/desktop/windows/snap-preview.tsx
+++ b/source/client/view/structure/desktop/windows/snap-preview.tsx
@@ -25,8 +25,9 @@ export default function SnapPreview({ shown, visible, blocked = false, minimumSi
className="pointer-events-none absolute"
style={{ left: 0, top: 0, zIndex, ...geometry.style }}
>
+ {/* It is drawn when it is first shown, so it comes in from hidden, as it does every later time. */}
<motion.div
- initial={false}
+ initial={{ scale: 0.98, opacity: 0 }}
animate={{ scale: visible ? 1 : 0.98, opacity: visible ? 1 : 0 }}
transition={motionTransition(transaction, reducedMotion)}
style={{ position: "absolute", inset: 0, transformOrigin: "center" }}
diff --git a/source/client/view/structure/desktop/windows/window.tsx b/source/client/view/structure/desktop/windows/window.tsx
index e9e47e7..cf32786 100644
--- a/source/client/view/structure/desktop/windows/window.tsx
+++ b/source/client/view/structure/desktop/windows/window.tsx
@@ -484,15 +484,28 @@ export default function ({ title, header = true, surface, layer, icon, children,
let renderFrame = 0
- // A hand holding a standard Window against an edge of the screen takes the view that way; see
- // EdgeHold. Where the plane goes no further, the preview of the placement turns to danger.
+ // A hand holding a standard Window against an edge of the screen settles there, which offers
+ // the half or quarter that edge names, then takes the view that way; see EdgeHold. Where the
+ // plane goes no further, the preview of the placement turns to danger.
let blocked = false
- const edges = new EdgeHold(direction => latest.current.onEdgeHold?.(direction), held => {
+ const edges = new EdgeHold(direction => latest.current.onEdgeHold?.(direction), {
- blocked = held
+ onBlocked(held) {
- renderGesture()
+ blocked = held
+
+ renderGesture()
+ },
+
+ onSettled(settled) {
+
+ zone = snapZone(settled)
+
+ if (zone) shown = zone
+
+ renderGesture()
+ }
})
function holdAgainst(motion: DesktopMovePoint) {
@@ -536,13 +549,11 @@ export default function ({ title, header = true, surface, layer, icon, children,
})
}
- // A zone is where the pointer is, within 16 pixels of an edge, and names a half or a quarter of
- // the room the view leaves for Windows, which each Desktop resolves in its own space.
- function snapTerm(motion: DesktopMovePoint): Snap | null {
+ // A zone is the edges the hold settled against, and names a half or a quarter of the room the
+ // view leaves for Windows, which each Desktop resolves in its own space.
+ function snapZone(settled: Readonly<{ x: number, y: number }> | null): Snap | null {
- const direction = against(physicalToDesktopPixels(motion.x - bounds!.left, desktopScale), physicalToDesktopPixels(motion.y - bounds!.top, desktopScale), bounds!.width, bounds!.height)
-
- return direction.x || direction.y ? snapPlacement(direction, paintMargins) : null
+ return settled ? snapPlacement(settled, paintMargins) : null
}
function move(motion: DesktopMovePoint) {
@@ -608,8 +619,6 @@ export default function ({ title, header = true, surface, layer, icon, children,
current = { ...origin, x: origin.x + dx, y: origin.y + dy }
// Only the Desktop's own Windows snap; a raw drawing goes where it is taken.
- zone = moved && standard ? snapTerm(motion) : null
-
if (moved && standard) {
held.set(1)
@@ -617,6 +626,8 @@ export default function ({ title, header = true, surface, layer, icon, children,
holdAgainst(motion)
}
+ zone = moved && standard ? snapZone(edges.settled) : null
+
if (zone) shown = zone
}
@@ -649,7 +660,10 @@ export default function ({ title, header = true, surface, layer, icon, children,
renderGesture()
}
- function release(motion: DesktopMovePoint, committed: boolean) {
+ function release(committed: boolean) {
+
+ // Only a hold that settled snaps; one let go before then leaves the Window where it is.
+ const term = moved && edge === null && committed && standard ? zone : null
edges.stop()
@@ -668,8 +682,6 @@ export default function ({ title, header = true, surface, layer, icon, children,
return
}
- const term = moved && edge === null && committed && standard ? snapTerm(motion) : null
-
// Pointer input has ended, but visible gesture ownership remains
// until every authoritative mutation below has settled.
if (term) {
@@ -722,7 +734,7 @@ export default function ({ title, header = true, surface, layer, icon, children,
handle.removeEventListener("pointermove", movePointer)
handle.removeEventListener("pointerup", releasePointer)
handle.removeEventListener("pointercancel", releasePointer)
- release({ x: motion.clientX, y: motion.clientY }, motion.type === "pointerup")
+ release(motion.type === "pointerup")
}
handle.addEventListener("pointermove", movePointer)
handle.addEventListener("pointerup", releasePointer)
@@ -730,8 +742,8 @@ export default function ({ title, header = true, surface, layer, icon, children,
}
else receive?.({
update: move,
- end(point) { release(point, true) },
- cancel() { release(pointer, false) }
+ end() { release(true) },
+ cancel() { release(false) }
})
setGesture({ origin, current, zone, shown, blocked, insets })
diff --git a/tests/edge-hold.test.ts b/tests/edge-hold.test.ts
new file mode 100644
index 0000000..5649513
--- /dev/null
+++ b/tests/edge-hold.test.ts
@@ -0,0 +1,95 @@
+import { EdgeHold, type Direction } from "@client/view/structure/desktop/edge-hold"
+import { afterEach, beforeEach, expect, test, vi } from "vitest"
+
+const right: Direction = { x: 1, y: 0 }
+const corner: Direction = { x: 1, y: 1 }
+const away: Direction = { x: 0, y: 0 }
+
+beforeEach(() => {
+ vi.useFakeTimers()
+ vi.stubGlobal("window", globalThis)
+})
+
+afterEach(() => {
+ vi.useRealTimers()
+ vi.unstubAllGlobals()
+})
+
+test("a hold that settles waits 300 milliseconds to settle, then 700 more to take the view", () => {
+ const travels: Direction[] = []
+ const settled: (Direction | null)[] = []
+ const hold = new EdgeHold(direction => { travels.push(direction); return 300 }, { onSettled: value => settled.push(value) })
+
+ hold.update(right)
+ vi.advanceTimersByTime(299)
+ expect(settled).toEqual([])
+
+ vi.advanceTimersByTime(1)
+ expect(settled).toEqual([right])
+ expect(hold.settled).toEqual(right)
+
+ vi.advanceTimersByTime(699)
+ expect(travels).toEqual([])
+
+ vi.advanceTimersByTime(1)
+ expect(travels).toEqual([right])
+
+ // Once the view arrived, another after a half second.
+ vi.advanceTimersByTime(800)
+ expect(travels).toEqual([right, right])
+})
+
+test("a hand that leaves before 300 milliseconds neither settles nor takes the view", () => {
+ const travel = vi.fn(() => 300)
+ const onSettled = vi.fn()
+ const hold = new EdgeHold(travel, { onSettled })
+
+ hold.update(right)
+ vi.advanceTimersByTime(250)
+ hold.update(away)
+ vi.advanceTimersByTime(5000)
+
+ expect(onSettled).not.toHaveBeenCalled()
+ expect(travel).not.toHaveBeenCalled()
+ expect(hold.settled).toBeNull()
+})
+
+test("once settled, another edge settles at once and waits 700 milliseconds to take the view", () => {
+ const travels: Direction[] = []
+ const settled: (Direction | null)[] = []
+ const hold = new EdgeHold(direction => { travels.push(direction); return 300 }, { onSettled: value => settled.push(value) })
+
+ hold.update(right)
+ vi.advanceTimersByTime(300)
+ hold.update(corner)
+ expect(settled).toEqual([right, corner])
+
+ vi.advanceTimersByTime(700)
+ expect(travels).toEqual([corner])
+
+ hold.update(away)
+ expect(settled).toEqual([right, corner, null])
+})
+
+test("a hold that only travels, as the Map's, takes the view after 700 milliseconds", () => {
+ const travel = vi.fn(() => 300)
+ const hold = new EdgeHold(travel)
+
+ hold.update(right)
+ vi.advanceTimersByTime(700)
+
+ expect(travel).toHaveBeenCalledWith(right)
+ expect(hold.settled).toBeNull()
+})
+
+test("where the plane goes no further, it says so until the hand leaves", () => {
+ const blocked: boolean[] = []
+ const hold = new EdgeHold(() => null, { onBlocked: value => blocked.push(value) })
+
+ hold.update(right)
+ vi.advanceTimersByTime(700)
+ expect(blocked).toEqual([true])
+
+ hold.update(away)
+ expect(blocked).toEqual([true, false])
+})