Skip to content

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

A Window opened by another opens where the asker says, not over it by itself

zohayrslilehffbc2708938f
diff --git a/source/client/core/link-manager/auth-manager/auth-manager.ts b/source/client/core/link-manager/auth-manager/auth-manager.ts
index bcf9d9c..97ef723 100644
--- a/source/client/core/link-manager/auth-manager/auth-manager.ts
+++ b/source/client/core/link-manager/auth-manager/auth-manager.ts
@@ -149,9 +149,9 @@ export default class AuthManager extends TheLink {
return await this.$outbound.publishFirst("/icon", size)
}
- public async open(process: string, target: unknown) {
+ public async open(process: string, target: unknown, options: unknown) {
- await this.$outbound.publishFirst("/opening/open", process, target)
+ await this.$outbound.publishFirst("/opening/open", process, target, options)
}
public disconnect() {
diff --git a/source/client/view/components/desktop-host/host.ts b/source/client/view/components/desktop-host/host.ts
index a62c6f1..6135e50 100644
--- a/source/client/view/components/desktop-host/host.ts
+++ b/source/client/view/components/desktop-host/host.ts
@@ -764,7 +764,7 @@ export default function host(authManager: AuthManager, pane: string, viewport: D
if (word === "open") {
- await authManager.open(pane, args[0])
+ await authManager.open(pane, args[0], args[1])
return []
}
diff --git a/source/server/core/link-manager/auth-manager/auth-manager.ts b/source/server/core/link-manager/auth-manager/auth-manager.ts
index 78f934f..455087c 100644
--- a/source/server/core/link-manager/auth-manager/auth-manager.ts
+++ b/source/server/core/link-manager/auth-manager/auth-manager.ts
@@ -243,7 +243,7 @@ export default class AuthManager extends TheLink {
/** A Client's request to open something, forwarded by the Desktop showing it. */
@Subscribe("/opening/open")
- protected async openForClient(process: unknown, target: unknown) {
+ protected async openForClient(process: unknown, target: unknown, options: unknown) {
if (typeof process !== "string") throw new Error("An open request is invalid")
@@ -251,14 +251,14 @@ export default class AuthManager extends TheLink {
if (!held) throw new Error("The Process that asked has exited")
- await this.openingManager.open(target, { process: held, endpoint: "client" })
+ await this.openingManager.open(target, { process: held, endpoint: "client" }, options)
}
/** The owner opens something from outside, such as a Node script. */
@Subscribe("/open")
- protected async openForOwner(target: unknown) {
+ protected async openForOwner(target: unknown, options: unknown) {
- await this.openingManager.open(target, null, this.linkManager.connection().signal)
+ await this.openingManager.open(target, null, options, this.linkManager.connection().signal)
}
@Subscribe("/appearance/update")
diff --git a/source/server/core/link-manager/auth-manager/process-manager/process-manager.ts b/source/server/core/link-manager/auth-manager/process-manager/process-manager.ts
index 9554e25..904b9b4 100644
--- a/source/server/core/link-manager/auth-manager/process-manager/process-manager.ts
+++ b/source/server/core/link-manager/auth-manager/process-manager/process-manager.ts
@@ -1968,7 +1968,7 @@ export default class ProcessManager extends TheLink {
if (word === "open") {
- await this.authManager.openingManager.open(rest[0], { process, endpoint: "server" })
+ await this.authManager.openingManager.open(rest[0], { process, endpoint: "server" }, rest[1])
return []
}
diff --git a/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts b/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts
index 191865c..2d8ea52 100644
--- a/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts
+++ b/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts
@@ -10,10 +10,9 @@ import { dirname, isAbsolute, join } from "node:path"
import { isDeepStrictEqual } from "node:util"
import Logs, { type LogSource } from "./logs"
import { isValue, layers } from "./config"
-import { parseLaunch, withProcessDefaults, parseProgramDefinition, parseProgramInstallOptions, parseProgramUninstallOptions, type ProgramInstallOptions, type ProgramUninstallOptions, type ClientLaunch, type Launch, type Position, type ProgramCommandChunk, type ProgramDefinition, type Size, type Value } from "@phreshos/core"
+import { parseLaunch, withProcessDefaults, parseProgramDefinition, parseProgramInstallOptions, parseProgramUninstallOptions, parseRelativeValue, type ProgramInstallOptions, type ProgramUninstallOptions, type ClientLaunch, type Launch, type Position, type ProgramCommandChunk, type ProgramDefinition, type Size, type Value } from "@phreshos/core"
import type { OpenTarget } from "@phreshos/core"
import { type default as Process, type ProcessLaunch, type Stream } from "../process-manager/process"
-import { overWindow, weighted, type PlacedWindow } from "./window-placement"
import { type StandardShape } from "../process-manager/process-manager"
import Program, { type CommandOutput, type InstallOutput } from "./program"
import Entry, { type ProgramRecord } from "./entry"
@@ -1437,7 +1436,7 @@ export default class ProgramManager extends TheLink {
// One interpretation of a Process launch, used both when it is created now
// and when a future startup launch is persisted. Runtime-only facts such as
// name occupancy and capacity remain in `start`.
- private resolveLaunch(program: Program, value: unknown, near: PlacedWindow | null = null) {
+ private resolveLaunch(program: Program, value: unknown, fallbackPosition: Position | null = null) {
const launch = this.launchOf(program, value)
@@ -1463,7 +1462,7 @@ export default class ProgramManager extends TheLink {
// Resolving the Window here validates the same launch grammar even when
// the launch is being stored for a later boot. Only the original launch
// is persisted; system defaults are derived again when it actually runs.
- const windowShape = program.client ? this.clientShape(program, askedClient, near) : null
+ const windowShape = program.client ? this.clientShape(program, askedClient, fallbackPosition) : null
const shape = client ? windowShape : null
@@ -1488,7 +1487,7 @@ export default class ProgramManager extends TheLink {
return { options, server, client, shape, windowShape, intent }
}
- private async start(program: Program, launch: Launch = {}, watching?: Watching, parent: Process | null = null, transitionOwnsIdentity = false, prepared?: ReturnType<ProgramManager["resolveLaunch"]>, opened: OpenTarget | null = null, near: PlacedWindow | null = null) {
+ private async start(program: Program, launch: Launch = {}, watching?: Watching, parent: Process | null = null, transitionOwnsIdentity = false, prepared?: ReturnType<ProgramManager["resolveLaunch"]>, opened: OpenTarget | null = null, fallbackPosition: Position | null = null) {
if (!transitionOwnsIdentity && this.changing.has(program.identity)) throw new Error("This program is changing and cannot create a process")
@@ -1500,7 +1499,7 @@ export default class ProgramManager extends TheLink {
// Every launch takes the Program's Process defaults first: its name, replacement, and options.
launch = this.launchOf(program, launch)
- const resolved = prepared ?? this.resolveLaunch(program, launch, near)
+ const resolved = prepared ?? this.resolveLaunch(program, launch, fallbackPosition)
const { options, server, client } = resolved
@@ -1565,14 +1564,10 @@ export default class ProgramManager extends TheLink {
}
/** Starts one Process of a Program with its default launch, to open one target. */
- /** Opens a target in a new Process; the window that asked, if one did, is where it opens from. */
- public async open(program: Program, target: OpenTarget, asker: Process | null = null) {
+ /** Opens a target in a new Process; `position` is where its window opens unless its Program says. */
+ public async open(program: Program, target: OpenTarget, position: Position | null = null) {
- const window = asker?.client ? asker.clientEndpoint?.window : null
-
- const near = window?.layer === "window" ? { position: window.position, size: window.size } : null
-
- return await this.start(program, {}, undefined, null, false, undefined, target, near)
+ return await this.start(program, {}, undefined, null, false, undefined, target, position)
}
/** Creates one fresh execution runtime for a Server endpoint. */
@@ -1588,10 +1583,10 @@ export default class ProgramManager extends TheLink {
/** Resolves and validates one client endpoint incarnation. */
/**
* The window a client launch opens: each setting as the launch says, else as the Program says,
- * else as the System chooses by the situation. `near` is a window this one opens from, as one that
- * asked to open something.
+ * else as the System chooses. `fallbackPosition` is where whoever opened it would have it open,
+ * which the Program's own declaration still overrides.
*/
- public clientShape(program: Program, asked: ClientLaunch = {}, near: PlacedWindow | null = null): StandardShape {
+ public clientShape(program: Program, asked: ClientLaunch = {}, fallbackPosition: Position | null = null): StandardShape {
const client = program.client
@@ -1625,8 +1620,8 @@ export default class ProgramManager extends TheLink {
const layer = asked.layer ?? client.layer ?? "window"
// What neither the launch nor the Program says, the System fills the same way in every layer,
- // knowing nothing of any screen: a square of fixed pixels, centered over the window it opens
- // from, or else on the plane's zero.
+ // knowing nothing of any screen: a square of fixed pixels, centered on the plane's zero, or
+ // where whoever opened it would have it.
const size = asked.size ?? client.size ?? defaultWindowSize
return {
@@ -1635,7 +1630,7 @@ export default class ProgramManager extends TheLink {
header: asked.header ?? client.header ?? true,
- position: asked.position ?? client.position ?? (near ? overWindow(near, size) : centered(size)),
+ position: asked.position ?? client.position ?? fallbackPosition ?? centered(size),
size,
@@ -1670,7 +1665,20 @@ function centered(size: Size): Position {
/** Minus half of a value, pixels and share alike: `500` gives `-250`, `"1/2"` gives `"-25%"`. */
function half(value: Value): Value {
- return weighted([[value, -0.5]])
+ const parsed = parseRelativeValue(value)
+
+ if (!parsed) return 0
+
+ // Adding zero turns a halved zero's sign away: -0 is still no distance.
+ const share = -parsed.relative / 2 + 0
+
+ const pixels = -parsed.pixels / 2 + 0
+
+ if (share === 0) return pixels
+
+ if (pixels === 0) return `${share * 100}%`
+
+ return `${share * 100}% ${pixels < 0 ? "-" : "+"} ${Math.abs(pixels)}`
}
// What a launcher may say at the start. Named rather than ordered,
diff --git a/source/server/core/link-manager/auth-manager/program-manager/window-placement.ts b/source/server/core/link-manager/auth-manager/program-manager/window-placement.ts
deleted file mode 100644
index 8aafef7..0000000
--- a/source/server/core/link-manager/auth-manager/program-manager/window-placement.ts
+++ /dev/null
@@ -1,45 +0,0 @@
-import { parseRelativeValue, type Position, type Size, type Value } from "@phreshos/core"
-
-/** A standard window as placement reads it: where its corner is, how large it is. */
-export type PlacedWindow = Readonly<{ position: Position, size: Size }>
-
-/** Where a window of this size stands centered over another standard window. */
-export function overWindow(window: PlacedWindow, size: Size): Position {
-
- // Its corner, moved by half the asker's size and back by half its own: pixels and shares alike.
- return {
- x: weighted([[window.position.x, 1], [window.size.width, 0.5], [size.width, -0.5]]),
- y: weighted([[window.position.y, 1], [window.size.height, 0.5], [size.height, -0.5]])
- }
-}
-
-/** The sum of values, each scaled by its weight, as one value: pixels, a share, or both. */
-export function weighted(terms: readonly (readonly [Value, number])[]): Value {
-
- let share = 0
-
- let pixels = 0
-
- for (const [value, weight] of terms) {
-
- const parsed = parseRelativeValue(value)
-
- if (!parsed) continue
-
- share += parsed.relative * weight
-
- pixels += parsed.pixels * weight
- }
-
- // Rounded past what a screen can show, so sums of halves leave no float dust; adding zero turns a
- // zero's sign away: -0 is still no distance.
- share = Math.round(share * 1e6) / 1e6 + 0
-
- pixels = Math.round(pixels * 1e3) / 1e3 + 0
-
- if (share === 0) return pixels
-
- if (pixels === 0) return `${share * 100}%`
-
- return `${share * 100}% ${pixels < 0 ? "-" : "+"} ${Math.abs(pixels)}`
-}
diff --git a/source/server/core/opening-manager.ts b/source/server/core/opening-manager.ts
index edcf058..59825f2 100644
--- a/source/server/core/opening-manager.ts
+++ b/source/server/core/opening-manager.ts
@@ -1,6 +1,6 @@
import { TheLink } from "@the-link/core"
import { Subscribe } from "@the-link/core/decorators"
-import { opensType, parseOpenTarget, parseOpenType, type OpenRequestSnapshot, type OpenTarget } from "@phreshos/core"
+import { opensType, parseOpenOptions, parseOpenTarget, parseOpenType, type OpenRequestSnapshot, type OpenTarget, type Position } from "@phreshos/core"
import shortIdentity from "@libs/short-identity"
import type AuthManager from "./link-manager/auth-manager/auth-manager"
import type Process from "./link-manager/auth-manager/process-manager/process"
@@ -51,9 +51,11 @@ export default class OpeningManager extends TheLink {
* Process is started for it, and rejects when no Program opens it or nothing is chosen. A request
* from outside ends with the connection that asked, through `signal`.
*/
- public async open(value: unknown, from: Readonly<{ process: Process, endpoint: "server" | "client" }> | null, signal?: AbortSignal) {
+ public async open(value: unknown, from: Readonly<{ process: Process, endpoint: "server" | "client" }> | null, options?: unknown, signal?: AbortSignal) {
const target = parseOpenTarget(value)
+ // Where the asker would have the window open, unless the Program that opens it says.
+ const position = parseOpenOptions(options).position ?? null
const programs = this.programsFor(target.type)
if (!programs.length) throw new Error(`No Program opens ${target.type}`)
@@ -63,11 +65,8 @@ export default class OpeningManager extends TheLink {
const chosen = [defaults[target.type], defaults[`${target.type.slice(0, target.type.indexOf("/"))}/*`]]
.find(program => program !== undefined && programs.includes(program))
- // A window that asks has the new window open over it.
- const asker = from?.endpoint === "client" ? from.process : null
-
if (chosen) {
- await this.launch(chosen, target, asker)
+ await this.launch(chosen, target, position)
return
}
@@ -87,7 +86,7 @@ export default class OpeningManager extends TheLink {
const cancelEndpoint = from ? from.endpoint === "server" ? from.process.onServerStop(end) : from.process.onClientStop(end)
: signal ? (signal.addEventListener("abort", end), () => signal.removeEventListener("abort", end)) : () => undefined
- this.pendingRequests.set(identity, { snapshot, asker, settle, cancelEndpoint, timer: null, resolving: false })
+ this.pendingRequests.set(identity, { snapshot, position, settle, cancelEndpoint, timer: null, resolving: false })
await Promise.all([
this.authManager.processManager.announceHost("opening", null, "request", "system", snapshot).catch(() => undefined),
@@ -128,7 +127,7 @@ export default class OpeningManager extends TheLink {
if ((options as { always?: unknown } | undefined)?.always === true) await this.setDefault(request.snapshot.target.type, program)
- await this.launch(program, request.snapshot.target, request.asker)
+ await this.launch(program, request.snapshot.target, request.position)
await this.finish(request, program)
}
catch (error) {
@@ -224,13 +223,13 @@ export default class OpeningManager extends TheLink {
return this.authManager.linkManager.application.store
}
- private async launch(program: string, target: OpenTarget, asker: Process | null) {
+ private async launch(program: string, target: OpenTarget, position: Position | null) {
const entry = this.authManager.programManager.programs.get(program)
if (!entry?.installed) throw new Error("That Program is not installed")
- await this.authManager.programManager.open(entry.program, target, asker)
+ await this.authManager.programManager.open(entry.program, target, position)
}
private claim(identity: unknown) {
@@ -270,8 +269,8 @@ export default class OpeningManager extends TheLink {
interface PendingOpenRequest {
readonly snapshot: OpenRequestSnapshot
- /** The Process whose window asked, which the new window opens over. */
- readonly asker: Process | null
+ /** Where the asker would have the window open, unless its Program says. */
+ readonly position: Position | null
readonly settle: (program: string | null) => void
readonly cancelEndpoint: () => void
timer: ReturnType<typeof setTimeout> | null
diff --git a/tests/opening.test.ts b/tests/opening.test.ts
index 11d0290..a1d0f85 100644
--- a/tests/opening.test.ts
+++ b/tests/opening.test.ts
@@ -97,23 +97,26 @@ test("without a default, a request waits; choosing always opens it and makes the
expect(manager.pending(request!.identity)).toBe(false)
})
-test("a window that asks is where the new one opens from, even once a request is decided; a Server is not", async () => {
+test("the asker's position goes with the opening, even once a request is decided; without one, none", async () => {
const { manager, open, stored } = fixture()
const asker = { record: () => ({ identity: "asker" }), program: { record: () => ({ identity: "files" }) }, onClientStop: () => () => undefined, onServerStop: () => () => undefined } as never
+ const beside = { x: 400, y: "-25% + 10" }
stored.set("opening-defaults", { "image/png": "paint" })
- await manager.open(png, { process: asker, endpoint: "client" })
- expect(open).toHaveBeenLastCalledWith(expect.objectContaining({ identity: "paint" }), png, asker)
+ await manager.open(png, { process: asker, endpoint: "client" }, { position: beside })
+ expect(open).toHaveBeenLastCalledWith(expect.objectContaining({ identity: "paint" }), png, beside)
- await manager.open(png, { process: asker, endpoint: "server" })
+ await manager.open(png, { process: asker, endpoint: "client" })
expect(open).toHaveBeenLastCalledWith(expect.objectContaining({ identity: "paint" }), png, null)
+ await expect(manager.open(png, null, { position: { x: "beside", y: 0 } })).rejects.toThrow(/relative expression/)
+
stored.clear()
- const opening = manager.open(png, { process: asker, endpoint: "client" })
+ const opening = manager.open(png, { process: asker, endpoint: "server" }, { position: beside })
await vi.waitFor(() => expect(manager.requests()).toHaveLength(1))
await manager.choose(manager.requests()[0]!.identity, "preview", {})
await opening
- expect(open).toHaveBeenLastCalledWith(expect.objectContaining({ identity: "preview" }), png, asker)
+ expect(open).toHaveBeenLastCalledWith(expect.objectContaining({ identity: "preview" }), png, beside)
})
test("a cancelled request opens nothing and tells the one who asked", async () => {
@@ -147,7 +150,7 @@ test("a request from outside ends when the connection that asked closes", async
const { manager, open } = fixture()
const connection = new AbortController()
- const opening = manager.open(png, null, connection.signal)
+ const opening = manager.open(png, null, undefined, connection.signal)
await vi.waitFor(() => expect(manager.requests()).toHaveLength(1))
connection.abort()
diff --git a/tests/window-placement.test.ts b/tests/window-placement.test.ts
deleted file mode 100644
index aa793c1..0000000
--- a/tests/window-placement.test.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-import { expect, test } from "vitest"
-import { overWindow, weighted } from "@server/core/link-manager/auth-manager/program-manager/window-placement"
-
-test("a window opens centered over another, in pixels", () => {
- expect(overWindow({ position: { x: -400, y: -300 }, size: { width: 800, height: 600 } }, { width: 400, height: 200 })).toEqual({ x: -200, y: -100 })
-})
-
-test("a window opens centered over one placed by shares of the view, keeping them shares", () => {
- // The asker fills the left half: its center is a quarter in from the left.
- expect(overWindow({ position: { x: "-50%", y: "-50%" }, size: { width: "1/2", height: "1/1" } }, { width: 400, height: 300 }))
- .toEqual({ x: "-25% - 200", y: -150 })
- expect(overWindow({ position: { x: -300, y: 0 }, size: { width: 600, height: 400 } }, { width: "1/2", height: "1/2" }))
- .toEqual({ x: "-25%", y: "-25% + 200" })
-})
-
-test("a weighted sum stays one value, with no float dust and no negative zero", () => {
- expect(weighted([["1/3", 0.5], ["1/3", 0.5]])).toBe("33.3333%")
- expect(weighted([[0, -0.5]])).toBe(0)
- expect(Object.is(weighted([[0, -0.5]]), -0)).toBe(false)
-})