Skip to content

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

Halves held against the edges divide the room left for Windows equally

zohayrslileha0a1c4ee818e
diff --git a/source/client/view/components/window-manager/window-geometry.ts b/source/client/view/components/window-manager/window-geometry.ts
index bab4ed0..b25ad14 100644
--- a/source/client/view/components/window-manager/window-geometry.ts
+++ b/source/client/view/components/window-manager/window-geometry.ts
@@ -193,6 +193,46 @@ export function constrainWindowGeometry(region: WindowRegion, surface: ViewSize,
}
}
+/**
+ * Where a standard Window goes when it is held against edges of the view: a half, or a quarter at a
+ * corner, of the room the view leaves for Windows. That room is the view less the margins painted at
+ * its edges, which are larger on the Taskbar's side, so the line between two halves is at the room's
+ * middle, not the view's: the view's middle moved by half the difference of the margins. It is written
+ * as a share of the view and pixels, so every Desktop resolves it in its own size.
+ *
+ * `direction` is -1 toward the start (left, top), 1 toward the end, or 0 for the whole room, per axis.
+ */
+export function snapPlacement(direction: Readonly<{ x: number, y: number }>, margins: PaintMargins): { position: Position, size: Size } {
+
+ const across = snapSpan(direction.x, margins.left, margins.right)
+
+ const down = snapSpan(direction.y, margins.top, margins.bottom)
+
+ return { position: { x: across.position, y: down.position }, size: { width: across.size, height: down.size } }
+}
+
+function snapSpan(direction: number, start: number, end: number): { position: Value, size: Value } {
+
+ if (!direction) return { position: "-1/2", size: "1/1" }
+
+ // Where the middle of the room is, in pixels from the view's middle.
+ const middle = Math.round((start - end) / 2)
+
+ return direction < 0
+ ? { position: "-1/2", size: shareAndPixels(0.5, middle) }
+ : { position: shareAndPixels(0, middle), size: shareAndPixels(0.5, -middle) }
+}
+
+/** A share of the view and pixels, in the form a Window's values take. */
+function shareAndPixels(share: number, pixels: number): Value {
+
+ if (share === 0) return pixels
+
+ if (pixels === 0) return `${share * 100}%`
+
+ return `${share * 100}% ${pixels < 0 ? "-" : "+"} ${Math.abs(pixels)}`
+}
+
/** Space kept on each side of the Desktop when painting what touches it. */
export interface PaintMargins {
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 a8aa2c7..9a6d7ea 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
@@ -4,7 +4,7 @@ import { Map as MapIcon, Maximize2 } from "@phreshos/react-ui/icons"
import WindowMenu from "../../window-menu"
import TaskbarTooltip from "../taskbar-tooltip"
import { useCallback, useEffect, useId, useLayoutEffect, useRef, useState, type CSSProperties, type PointerEvent as ReactPointerEvent } from "react"
-import { planeReach, type WindowRegion } from "@client/view/components/window-manager/window-geometry"
+import { planeReach, snapPlacement, 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"
@@ -204,7 +204,7 @@ export default function MapControl({ viewport, windows, taskbar, spacing, onOpen
edges.update(direction)
- const zone = direction.x || direction.y ? placementAt(direction) : null
+ const zone = direction.x || direction.y ? snapPlacement(direction, desktopMargins(spacing, taskbar)) : null
setEdge(current => ({ ...current, zone, shown: zone ?? current.shown, layer: windowLayer }))
}
@@ -794,10 +794,3 @@ function clamp(value: number, minimum: number, maximum: number) {
}
/** The placement a Window takes against edges of the Desktop: a half against a side, a quarter in a corner. */
-function placementAt(direction: Readonly<{ x: number, y: number }>): SnapTarget {
-
- return {
- position: { x: direction.x > 0 ? "0/1" : "-1/2", y: direction.y > 0 ? "0/1" : "-1/2" },
- size: { width: direction.x ? "1/2" : "1/1", height: direction.y ? "1/2" : "1/1" }
- }
-}
diff --git a/source/client/view/structure/desktop/windows/window.tsx b/source/client/view/structure/desktop/windows/window.tsx
index 7f33453..5d98e03 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, useCallba
import { 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, windowPaintInsets, type PaintMargins, type WindowRegion, type ViewSize } from "@client/view/components/window-manager/window-geometry"
+import { absoluteWindowGeometry, constrainWindowGeometry, minimumWindowSize, noPaintMargins, planeGeometry, resolveWindowGeometry, snapPlacement, windowPaintInsets, type PaintMargins, type WindowRegion, 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"
@@ -474,31 +474,13 @@ export default function ({ title, header = true, surface, layer, icon, children,
})
}
- // Zones are where the pointer is — within 16px of an edge — and
- // they name shares of the surface, which each client resolves in
- // its own space.
+ // 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 {
- const pointerX = physicalToDesktopPixels(motion.x - bounds!.left, desktopScale)
+ const direction = against(physicalToDesktopPixels(motion.x - bounds!.left, desktopScale), physicalToDesktopPixels(motion.y - bounds!.top, desktopScale), bounds!.width, bounds!.height)
- const pointerY = physicalToDesktopPixels(motion.y - bounds!.top, desktopScale)
-
- const west = pointerX <= 16
-
- const east = pointerX >= bounds!.width - 16
-
- const north = pointerY <= 16
-
- const south = pointerY >= bounds!.height - 16
-
- if (!west && !east && !north && !south) return null
-
- return {
-
- position: { x: east ? "0/1" : "-1/2", y: south ? "0/1" : "-1/2" },
-
- size: { width: west || east ? "1/2" : "1/1", height: north || south ? "1/2" : "1/1" }
- }
+ return direction.x || direction.y ? snapPlacement(direction, paintMargins) : null
}
function move(motion: DesktopMovePoint) {
diff --git a/tests/window-geometry.test.ts b/tests/window-geometry.test.ts
index fc5c418..d2341f8 100644
--- a/tests/window-geometry.test.ts
+++ b/tests/window-geometry.test.ts
@@ -3,10 +3,10 @@ import ClientWindow from "@client/core/link-manager/auth-manager/process-manager
import 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, viewOfGeometry, windowPaintInsets } from "@client/view/components/window-manager/window-geometry"
+import { boundedGeometry, constrainWindowGeometry, minimumWindowSize, noPaintMargins, planeGeometry, recordedPosition, resolveWindowGeometry, shiftPosition, snapPlacement, viewOfGeometry, windowPaintInsets } from "@client/view/components/window-manager/window-geometry"
import { defaultAppearance } from "@phreshos/core"
import { TheLink } from "@the-link/core"
-import { test } from "vitest"
+import { expect, test } from "vitest"
test("a position is the top-left corner on a plane whose zero is the surface's center", () => {
const surface = { width: 1200, height: 800 }
@@ -229,3 +229,24 @@ test("the Desktop shows a standard Window on its plane, two views from the cente
assert.deepEqual(boundedGeometry({ x: 9000, y: -4000 }, { width: 500, height: 300 }, surface), { position: { x: 2000, y: -1500 }, size: { width: 500, height: 300 } })
assert.deepEqual(boundedGeometry({ x: 1000, y: 0 }, { width: "3/1", height: 5000 }, surface), { position: { x: 500, y: 0 }, size: { width: 2000, height: 1200 } })
})
+
+test.each([
+ ["bottom", { top: 12, right: 12, bottom: 68, left: 12 }],
+ ["right", { top: 12, right: 68, bottom: 12, left: 12 }]
+] as const)("halves held against the edges divide the room equally with the Taskbar at the %s", (_, margins) => {
+ const view = { width: 1440, height: 900 }
+ const inset = 6
+ const painted = (direction: { x: number, y: number }) => {
+ const { position, size } = snapPlacement(direction, margins)
+ const region = resolveWindowGeometry(position, size, view)
+ const insets = windowPaintInsets(position, size, view, inset, margins)
+ return { width: region.width - insets.left - insets.right, height: region.height - insets.top - insets.bottom, x: region.x + insets.left, y: region.y + insets.top }
+ }
+ const [top, bottom] = [painted({ x: 0, y: -1 }), painted({ x: 0, y: 1 })]
+ const [left, right] = [painted({ x: -1, y: 0 }), painted({ x: 1, y: 0 })]
+ expect(top.height).toBe(bottom.height)
+ expect(left.width).toBe(right.width)
+ // The gap between the halves is one spacing, as between any two Windows.
+ expect(bottom.y - (top.y + top.height)).toBe(inset * 2)
+ expect(right.x - (left.x + left.width)).toBe(inset * 2)
+})