Skip to content

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

3.3 KB
Raw Blame
import { useCallback } from "react"
import { useReducedMotion } from "@libs/react-motion"
import { planeReach } from "@client/view/components/window-manager/window-geometry"
import { timing, useAppearance } from "@phreshos/react-ui"
import { type Viewport } from "./viewport-offset"

export type Direction = Readonly<{ x: number, y: number }>

/**
 * Takes the view one whole view in a direction, when the plane goes on there. It answers how long
 * the view takes to get there, so whoever holds it there waits for it, or null when there is nowhere
 * further to go.
 */
export function useViewTravel(viewport: Viewport) {

    const reducedMotion = useReducedMotion()

    const tempo = useAppearance().tempo

    const { view, moveTo, size } = viewport

    return useCallback(function (direction: Direction) {

        const next = { x: view.x + direction.x, y: view.y + direction.y }

        if (Math.abs(next.x) > planeReach || Math.abs(next.y) > planeReach || (next.x === view.x && next.y === view.y)) return null

        moveTo(next)

        return reducedMotion ? 0 : timing("view", { distance: Math.hypot(direction.x * size.width, direction.y * size.height), tempo }).duration

    }, [view.x, view.y, moveTo, reducedMotion, tempo, size.width, size.height])
}

/**
 * How close to an edge, in pixels, a point is held against it. Only the edge itself, or beyond it:
 * a hand that only carries something near the edge does not hold it there.
 */
const reach = 3

/** Which edges of a box a point is held against, within `reach` or beyond: -1, 0 or 1 in each direction. */
export function against(x: number, y: number, width: number, height: number): Direction {

    return {
        x: x <= reach ? -1 : x >= width - reach ? 1 : 0,
        y: y <= reach ? -1 : y >= height - reach ? 1 : 0
    }
}

/**
 * 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.
 */
export class EdgeHold {

    private direction: Direction = { x: 0, y: 0 }

    private timer = 0

    private blocked = false

    public constructor(
        private readonly travel: (direction: Direction) => number | null | undefined,
        private readonly onBlocked?: (blocked: boolean) => void
    ) {}

    /** Where it is held now; a new edge starts the wait again, and no edge stops it. */
    public update(direction: Direction) {

        if (direction.x === this.direction.x && direction.y === this.direction.y) return

        this.stop()

        this.direction = direction

        if (direction.x || direction.y) this.timer = window.setTimeout(() => this.hold(), 700)
    }

    public stop() {

        if (this.timer) clearTimeout(this.timer)

        this.timer = 0

        this.direction = { x: 0, y: 0 }

        if (this.blocked) {

            this.blocked = false

            this.onBlocked?.(false)
        }
    }

    private hold() {

        const took = this.travel(this.direction)

        if (took === null || took === undefined) {

            this.timer = 0

            this.blocked = true

            this.onBlocked?.(true)

            return
        }

        this.timer = window.setTimeout(() => this.hold(), took + 500)
    }
}