diff --git a/capsules/browser/browser/browser.js b/capsules/browser/browser/browser.js index cfa904a61..d138c5bda 100644 --- a/capsules/browser/browser/browser.js +++ b/capsules/browser/browser/browser.js @@ -65,6 +65,7 @@ const BROWSER_AUTHORITY_RENEWAL_RETRY_DELAYS_MS = Object.freeze([ 30_000, ]); const HOME_GUI_OPAQUE_ORIGIN = "null"; +const WINDOW_DRAG_MESSAGE_TYPE = "elastos.home.window-drag/v1"; const params = new URLSearchParams(window.location.search); const browserInstanceId = params.get("browser_instance") || ""; const launchToken = new URLSearchParams(window.location.hash.replace(/^#/, "")).get("home_token") || ""; @@ -842,6 +843,58 @@ function postHomeBrowserWindowCloseResult(message, owner, outcome, error = null) ); } +/** Home draws only the window dots over this bar, so a press on an empty part + * of it moves the window the way a title bar does. */ +function installChromeWindowDrag(chrome) { + // Home moves the window only for the launch token it gave this frame. + if (!chrome || window.parent === window || !launchToken) { + return; + } + let pointerId = null; + const send = (phase, event) => { + window.parent.postMessage( + { + type: WINDOW_DRAG_MESSAGE_TYPE, + homeToken: launchToken, + phase, + clientX: event.clientX, + clientY: event.clientY, + }, + "*", + ); + }; + chrome.addEventListener("pointerdown", (event) => { + if ( + event.button !== 0 || + pointerId !== null || + event.target.closest("button, input, select, textarea, a, label, form, [role='button']") + ) { + return; + } + event.preventDefault(); + pointerId = event.pointerId; + chrome.setPointerCapture(pointerId); + send("start", event); + }); + chrome.addEventListener("pointermove", (event) => { + if (event.pointerId === pointerId) { + send("move", event); + } + }); + const stop = (event) => { + if (event.pointerId !== pointerId) { + return; + } + pointerId = null; + send("end", event); + }; + chrome.addEventListener("pointerup", stop); + chrome.addEventListener("pointercancel", stop); + chrome.addEventListener("lostpointercapture", stop); +} + +installChromeWindowDrag(document.querySelector(".browser-chrome")); + function deliverPendingHomeBrowserWindowClose(owner, outcome) { const delivery = pendingHomeWindowCloseDelivery; const currentOwner = currentRuntimePageOwner(); diff --git a/capsules/browser/browser/index.html b/capsules/browser/browser/index.html index d0a9dd5b2..c7ff1cd10 100644 --- a/capsules/browser/browser/index.html +++ b/capsules/browser/browser/index.html @@ -6,8 +6,8 @@ ElastOS Browser - - + + @@ -110,6 +110,6 @@ - + diff --git a/capsules/browser/browser/style.css b/capsules/browser/browser/style.css index a8ab68903..9fd83f3c5 100644 --- a/capsules/browser/browser/style.css +++ b/capsules/browser/browser/style.css @@ -1,4 +1,5 @@ -/* Browser chrome — Safari-inspired toolbar under the standard Home title bar. +/* Browser chrome — Safari-inspired unified toolbar. + Shell owns traffic lights (unified-toolbar); menubar owns the app name. Page stage stays white — content ground, not chrome. */ :root { --bg: var(--el-bg); @@ -61,14 +62,14 @@ select { overflow: hidden; } -/* One tool band under the Home title bar. */ +/* One tool band under shell lights — pad leading for traffic lights. */ .browser-chrome { display: grid; grid-template-columns: auto auto minmax(0, 1fr) auto; align-items: center; gap: 8px; min-height: 52px; - padding: 8px 10px; + padding: 8px 10px 8px var(--window-chrome-safe-leading, 96px); border-bottom: 0; background: var(--bg); } @@ -452,7 +453,7 @@ body[data-loading="true"] .browser-address { .browser-chrome { min-height: 48px; gap: 6px; - padding: 6px 8px; + padding: 6px 8px 6px var(--window-chrome-safe-leading, 96px); } .browser-address { diff --git a/capsules/home-gui/browser/shell-core.js b/capsules/home-gui/browser/shell-core.js index 099441231..54f018151 100644 --- a/capsules/home-gui/browser/shell-core.js +++ b/capsules/home-gui/browser/shell-core.js @@ -419,9 +419,12 @@ export const HOME_GUI_HOST_SELECTORS = Object.freeze([ ]); /* Window chrome modes are presentation-only. Fail closed to standard. - unified-sidebar: split-view apps — lights over the leading column. */ + unified-sidebar: split-view apps — lights over the leading column. + unified-toolbar: Browser-style — lights over the tool row; menubar owns the + focused app name. */ export const WINDOW_CHROME_STANDARD = "standard"; export const WINDOW_CHROME_UNIFIED_SIDEBAR = "unified-sidebar"; +export const WINDOW_CHROME_UNIFIED_TOOLBAR = "unified-toolbar"; const WINDOW_CHROME_BY_TARGET = { marketplace: WINDOW_CHROME_UNIFIED_SIDEBAR, @@ -433,6 +436,7 @@ const WINDOW_CHROME_BY_TARGET = { inbox: WINDOW_CHROME_UNIFIED_SIDEBAR, chat: WINDOW_CHROME_UNIFIED_SIDEBAR, "chat-room": WINDOW_CHROME_UNIFIED_SIDEBAR, + browser: WINDOW_CHROME_UNIFIED_TOOLBAR, }; const WINDOW_CHROME_CONTINUOUS_TARGETS = new Set([ @@ -448,6 +452,9 @@ export function parseWindowChromeMode(value) { if (value === WINDOW_CHROME_UNIFIED_SIDEBAR) { return WINDOW_CHROME_UNIFIED_SIDEBAR; } + if (value === WINDOW_CHROME_UNIFIED_TOOLBAR) { + return WINDOW_CHROME_UNIFIED_TOOLBAR; + } return WINDOW_CHROME_STANDARD; } @@ -463,11 +470,14 @@ export function applyWindowChrome(windowNode, targetId) { } windowNode.classList.remove( "window-chrome-unified-sidebar", + "window-chrome-unified-toolbar", "window-chrome-continuous", ); const mode = windowChromeModeForTarget(targetId); if (mode === WINDOW_CHROME_UNIFIED_SIDEBAR) { windowNode.classList.add("window-chrome-unified-sidebar"); + } else if (mode === WINDOW_CHROME_UNIFIED_TOOLBAR) { + windowNode.classList.add("window-chrome-unified-toolbar"); } else if (WINDOW_CHROME_CONTINUOUS_TARGETS.has(normalizeText(targetId).toLowerCase())) { windowNode.classList.add("window-chrome-continuous"); } diff --git a/capsules/home-gui/browser/shell-window-geometry.js b/capsules/home-gui/browser/shell-window-geometry.js index 845b2431e..6412d0bfd 100644 --- a/capsules/home-gui/browser/shell-window-geometry.js +++ b/capsules/home-gui/browser/shell-window-geometry.js @@ -10,6 +10,7 @@ import { beginShellInteraction, clamp, endShellInteraction, + WINDOW_CHROME_UNIFIED_TOOLBAR, } from "./shell-core.js?v=home-20260813a"; import { isPhone } from "./shell-form-factor.js?v=home-20260813a"; import { registerStageGesture } from "./shell-popovers.js?v=home-20260813a"; @@ -415,6 +416,140 @@ export function attachWindowDrag(windowNode, handle, focusWindow, onWindowGeomet handle.addEventListener("pointercancel", stopDrag); } +export const WINDOW_DRAG_MESSAGE_TYPE = "elastos.home.window-drag/v1"; + +/** A unified-toolbar capsule draws its own top bar, so a press on an empty + * part of that bar arrives as messages. Only this window's frame can move it: + * the message must come from that frame's WindowProxy with the opaque origin + * and carry the launch token Home gave that frame (`frameHomeToken`), in the + * exact `elastos.home.window-drag/v1` shape. Anything else is ignored. + * Positions are frame-relative and are added to the frame's current place, + * because some engines report screen positions offset or flipped. + * Like the title bar, a drag restores a maximized or snapped window and can + * snap it to a screen edge on release. */ +export function attachForwardedWindowDrag( + windowNode, + focusWindow, + onWindowGeometryChange = null, + frameHomeToken = null, +) { + let dragging = null; + const finish = (snapOnRelease = false) => { + if (!dragging) { + return; + } + const snapTarget = snapOnRelease ? dragging.snapTarget : null; + dragging = null; + endShellInteraction(); + if (snapTarget) { + applyWindowSnap(windowNode, snapTarget); + focusWindow(windowNode.dataset.windowId); + } else { + hideWindowSnapPreview(); + } + if (typeof onWindowGeometryChange === "function") { + onWindowGeometryChange(); + } + }; + const listener = (event) => { + const data = event.data; + // Capsule frames are sandboxed without allow-same-origin: origin "null". + if ( + event.origin !== "null" || + !data || + typeof data !== "object" || + data.type !== WINDOW_DRAG_MESSAGE_TYPE || + Object.keys(data).sort().join(",") !== "clientX,clientY,homeToken,phase,type" || + !["start", "move", "end"].includes(data.phase) + ) { + return; + } + const frame = windowNode.querySelector(".window-frame"); + const homeToken = typeof frameHomeToken === "function" ? frameHomeToken() : ""; + if ( + windowNode.dataset.windowChromeMode !== WINDOW_CHROME_UNIFIED_TOOLBAR || + !windowNode.isConnected || + !frame?.contentWindow || + event.source !== frame.contentWindow || + typeof homeToken !== "string" || + !homeToken || + data.homeToken !== homeToken + ) { + return; + } + if (data.phase === "end") { + finish(true); + return; + } + // A hidden window keeps its place. A window that is maximized, snapped or + // hidden by something else in the middle of a drag stops following. + const special = + windowNode.dataset.maximized === "true" || + windowNode.dataset.browserMaximized === "true" || + Boolean(windowNode.dataset.snap); + if (windowNode.classList.contains("hidden") || (dragging && special)) { + finish(); + return; + } + if (!Number.isFinite(data.clientX) || !Number.isFinite(data.clientY)) { + return; + } + const frameRect = frame.getBoundingClientRect(); + const workspaceRect = desktop.getBoundingClientRect(); + const clientX = frameRect.left + data.clientX; + const clientY = frameRect.top + data.clientY; + const pointerX = clientX - workspaceRect.left; + const pointerY = clientY - workspaceRect.top; + if (data.phase === "start") { + hideWindowSnapPreview(); + focusWindow(windowNode.dataset.windowId); + // Phone windows fill the stage, as with the title bar. + if (isPhone()) { + return; + } + const bounds = special + ? restoreWindowForDrag(windowNode, clientX, clientY) + : normalWindowBounds(windowNode); + if (!dragging) { + beginShellInteraction(); + } + dragging = { + offsetX: pointerX - bounds.x, + offsetY: pointerY - bounds.y, + startClientX: clientX, + startClientY: clientY, + snapTarget: null, + }; + return; + } + if (data.phase !== "move" || !dragging) { + return; + } + const limits = partialDragBoundsForWindow(windowNode.offsetWidth); + windowNode.style.left = `${safeClamp(pointerX - dragging.offsetX, limits.minX, limits.maxX)}px`; + windowNode.style.top = `${safeClamp(pointerY - dragging.offsetY, limits.minY, limits.maxY)}px`; + const movedDistance = Math.hypot(clientX - dragging.startClientX, clientY - dragging.startClientY); + dragging.snapTarget = movedDistance >= WINDOW_SNAP_ACTIVATION_DISTANCE + ? snapTargetForPointer(clientX, clientY) + : null; + showWindowSnapPreview(dragging.snapTarget); + }; + // A frame that reloads, navigates or is relaunched mid-drag never sends its + // end, and its old token no longer matches. + const frameLoaded = (event) => { + if (event.target?.classList?.contains("window-frame")) { + finish(); + } + }; + window.addEventListener("message", listener); + windowNode.addEventListener("load", frameLoaded, true); + return () => { + window.removeEventListener("message", listener); + windowNode.removeEventListener("load", frameLoaded, true); + finish(); + }; +} + export function attachWindowResize(windowNode, focusWindow, onWindowGeometryChange = null) { for (const handle of windowNode.querySelectorAll(".window-resize-handle")) { let resizing = null; diff --git a/capsules/home-gui/browser/shell-windows.js b/capsules/home-gui/browser/shell-windows.js index cd13ac903..46b0c67f3 100644 --- a/capsules/home-gui/browser/shell-windows.js +++ b/capsules/home-gui/browser/shell-windows.js @@ -31,6 +31,7 @@ import { restoreWindowFromSpecialState, hideWindowSnapPreview, attachWindowDrag, + attachForwardedWindowDrag, attachWindowResize, } from "./shell-window-geometry.js?v=home-20260813a"; import { @@ -969,6 +970,7 @@ function tearDownWindowEntry(entry) { if (entry.selectionRequest) finishSelectionWindowRequest(entry.selectionRequest, false); entry.homeNavigation = null; cleanupFrameAutoFit(entry.node); + entry.node.detachForwardedWindowDrag?.(); shellState.windows.delete(entry.id); entry.node.remove(); if (entry.fullscreenStage) { @@ -1594,6 +1596,12 @@ function createWindow({ toggleWindowMaximize(id); }); attachWindowDrag(node, handle, focusWindow, persistBrowserSession); + node.detachForwardedWindowDrag = attachForwardedWindowDrag( + node, + focusWindow, + persistBrowserSession, + () => browserLaunchAuthority(node.querySelector(".window-frame")?.dataset.route || "")?.homeToken || "", + ); attachWindowResize(node, focusWindow, persistBrowserSession); if (maximizeByDefault && shouldOpenMaximizedByDefault()) { diff --git a/capsules/home-gui/browser/style.css b/capsules/home-gui/browser/style.css index 0afe7d373..4e5e41d8a 100644 --- a/capsules/home-gui/browser/style.css +++ b/capsules/home-gui/browser/style.css @@ -5580,6 +5580,52 @@ body.assistant-space-active #inbox-rail { height: 100% !important; } +/* Unified-toolbar chrome (Browser): no window title — menubar owns the name. + Traffic lights sit in the tool-row band; capsule pads + --window-chrome-safe-leading. */ +.window.window-chrome-unified-toolbar { + --window-chrome-safe-leading: 96px; +} + +.window.window-chrome-unified-toolbar .window-head { + position: absolute; + top: 0; + left: 0; + z-index: 6; + width: 78px; + height: 52px; + min-height: 52px; + margin-bottom: 0; + padding: 0 8px 0 12px; + align-items: center; + border-radius: 12px 0 0 0; + background: transparent; + box-shadow: none; + filter: none; +} + +.window.window-chrome-unified-toolbar.window-active .window-head { + filter: none; +} + +.window.window-chrome-unified-toolbar .window-head-icon, +.window.window-chrome-unified-toolbar .window-head-title, +.window.window-chrome-unified-toolbar .window-head-balance { + display: none; +} + +.window.window-chrome-unified-toolbar .window-head-draggable { + flex: 1 1 auto; + min-width: 28px; + min-height: 28px; + justify-content: flex-start; + align-self: stretch; +} + +.window.window-chrome-unified-toolbar .window-body { + height: 100%; +} + /* Continuous standard: titlebar fill matches frame (Wallet / Archive / GBA / connectors). Soft hairline only — no frosted second band. */ .window.window-chrome-continuous .window-head { diff --git a/docs/HOME_SHELL_HOST_CONTRACT.md b/docs/HOME_SHELL_HOST_CONTRACT.md index a96a3a640..ae1f551f4 100644 --- a/docs/HOME_SHELL_HOST_CONTRACT.md +++ b/docs/HOME_SHELL_HOST_CONTRACT.md @@ -162,6 +162,40 @@ window. It does not receive another capsule's authority token. App-to-app work remains a Runtime-gated intent and, for off-box effects, follows the provider and Carrier path described in [Capsule Interface Contract](CAPSULE_INTERFACE_CONTRACT.md). +### Browser toolbar window drag + +A window with `unified-toolbar` chrome (today only Browser) has no Home title +bar. Its capsule draws the top bar, so `home-gui` cannot see a press on it. The +Browser frame sends `elastos.home.window-drag/v1` to its parent `home-gui` +frame when the person presses an empty part of `.browser-chrome`: + +```json +{ "type": "elastos.home.window-drag/v1", "homeToken": "", + "phase": "start" | "move" | "end", "clientX": 0, "clientY": 0 } +``` + +This message is a presentation request only. It can move that one window and +nothing else. `home-gui` (`attachForwardedWindowDrag` in +`capsules/home-gui/browser/shell-window-geometry.js`) ignores the message +unless all of these are true: + +- the source is the `WindowProxy` of that window's own frame; +- the origin is the opaque `null` origin of a sandboxed capsule frame; +- `homeToken` is not empty and is the same as the `home_token` in the launch + route that Home gave that frame; +- the message has exactly these five keys and `phase` is `start`, `move` or + `end`; +- the window uses `unified-toolbar` chrome and is connected. + +The drag behaves like a Home title bar. The window must be visible. A start +on a maximized, Browser-maximized or snapped window restores it under the +pointer. If the window is maximized, snapped or hidden by something else +during a drag, the drag stops. Dragging to a screen edge shows the snap +preview, and releasing there snaps the window. On a phone the press only +focuses the window. A reload or close during a drag never snaps. Home removes +the listener when it closes the window. The Browser does not send the message when +it has no launch token. Tests: `scripts/browser-toolbar-window-drag.test.mjs`. + ### First-party Clipboard edge First-party capsule Clipboard access terminates in the trusted top-level Home @@ -323,6 +357,7 @@ node scripts/home-shell-switchback-recovery-smoke.mjs node scripts/home-shell-unavailable-shell-fallback-smoke.mjs node scripts/home-cli-browser-smoke.mjs node scripts/wallet-connector-transaction-smoke.mjs +node --test scripts/browser-toolbar-window-drag.test.mjs node scripts/check-product-data.mjs node scripts/home-shell-objective-audit.mjs (cd elastos && cargo test -p elastos-server test_home_launch -- --nocapture) diff --git a/scripts/browser-toolbar-window-drag.test.mjs b/scripts/browser-toolbar-window-drag.test.mjs new file mode 100644 index 000000000..7c68249fc --- /dev/null +++ b/scripts/browser-toolbar-window-drag.test.mjs @@ -0,0 +1,373 @@ +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; +import test from "node:test"; +import vm from "node:vm"; + +const browserSource = readFileSync(new URL("../capsules/browser/browser/browser.js", import.meta.url), "utf8"); +const geometrySource = readFileSync(new URL("../capsules/home-gui/browser/shell-window-geometry.js", import.meta.url), "utf8"); +const coreSource = readFileSync(new URL("../capsules/home-gui/browser/shell-core.js", import.meta.url), "utf8"); + +function declaration(source, name) { + const start = source.search(new RegExp(`(?:export )?function ${name}\\(`)); + assert.ok(start >= 0, name); + const end = source.indexOf("\n}\n", start); + assert.ok(end > start, name); + return source.slice(start, end + 2).replace(/^export /, ""); +} + +function stringConstant(source, name) { + const match = source.match(new RegExp(`(?:export )?const ${name} = "([^"]+)";`)); + assert.ok(match, name); + return match[1]; +} + +function numberConstant(source, name) { + const match = source.match(new RegExp(`const ${name} = (\\d+);`)); + assert.ok(match, name); + return Number(match[1]); +} + +const MESSAGE_TYPE = stringConstant(geometrySource, "WINDOW_DRAG_MESSAGE_TYPE"); +const SNAP_ACTIVATION_DISTANCE = numberConstant(geometrySource, "WINDOW_SNAP_ACTIVATION_DISTANCE"); + +test("Browser and Home name the same window-drag message", () => { + assert.equal(stringConstant(browserSource, "WINDOW_DRAG_MESSAGE_TYPE"), MESSAGE_TYPE); +}); + +function browserToolbar({ launchToken = "frame-token" } = {}) { + const posted = []; + const listeners = {}; + const captured = []; + const chrome = { + addEventListener: (type, listener) => { listeners[type] = listener; }, + setPointerCapture: (id) => captured.push(id), + }; + const context = vm.createContext({ + WINDOW_DRAG_MESSAGE_TYPE: MESSAGE_TYPE, + launchToken, + window: { parent: { postMessage: (message, origin) => posted.push({ message, origin }) } }, + }); + vm.runInContext(`${declaration(browserSource, "installChromeWindowDrag")}\nthis.install = installChromeWindowDrag;`, context); + context.install(chrome); + const press = (type, fields) => { + let prevented = false; + listeners[type]({ + button: 0, pointerId: 1, clientX: 0, clientY: 0, + target: { closest: () => null }, + preventDefault: () => { prevented = true; }, + ...fields, + }); + return prevented; + }; + return { posted, captured, press, listeners }; +} + +test("an empty part of the Browser toolbar sends start, move and end to Home", () => { + const toolbar = browserToolbar(); + assert.equal(toolbar.press("pointerdown", { clientX: 300, clientY: 20 }), true); + toolbar.press("pointermove", { clientX: 180, clientY: 100 }); + toolbar.press("pointerup", { clientX: 180, clientY: 100 }); + toolbar.press("lostpointercapture", {}); + assert.deepEqual(toolbar.captured, [1]); + assert.deepEqual( + toolbar.posted.map(({ message, origin }) => [message.type, message.phase, message.clientX, message.clientY, origin]), + [ + [MESSAGE_TYPE, "start", 300, 20, "*"], + [MESSAGE_TYPE, "move", 180, 100, "*"], + [MESSAGE_TYPE, "end", 180, 100, "*"], + ], + ); + assert.ok(toolbar.posted.every(({ message }) => message.homeToken === "frame-token"), "each message carries the frame's launch token"); +}); + +test("a Browser frame without a launch token sends no drag messages", () => { + const toolbar = browserToolbar({ launchToken: "" }); + assert.deepEqual(Object.keys(toolbar.listeners), []); + assert.deepEqual(toolbar.posted, []); +}); + +test("toolbar controls, other buttons and a second pointer keep their own behavior", () => { + const toolbar = browserToolbar(); + const control = { closest: (selector) => (selector.includes("input") ? {} : null) }; + assert.equal(toolbar.press("pointerdown", { target: control }), false); + assert.equal(toolbar.press("pointerdown", { button: 2 }), false); + toolbar.press("pointermove", {}); + assert.deepEqual(toolbar.posted, []); + + toolbar.press("pointerdown", {}); + assert.equal(toolbar.press("pointerdown", { pointerId: 2 }), false); + toolbar.press("pointermove", { pointerId: 2 }); + toolbar.press("pointerup", { pointerId: 2 }); + assert.deepEqual(toolbar.posted.map(({ message }) => message.phase), ["start"]); +}); + +const SNAP_EDGE = 8; + +function homeWindow({ + mode = "unified-toolbar", + maximized = false, + browserMaximized = false, + snap = "", + hidden = false, + phone = false, + frameToken = "frame-token", +} = {}) { + const listeners = new Set(); + const interactions = { begun: 0, ended: 0, saved: 0, focused: [] }; + const restores = []; + const previews = []; + const snaps = []; + const frameWindow = {}; + const classes = new Set(hidden ? ["window", "hidden"] : ["window"]); + const nodeListeners = new Set(); + const node = { + isConnected: true, + addEventListener: (type, listener, capture) => type === "load" && capture === true && nodeListeners.add(listener), + removeEventListener: (type, listener, capture) => type === "load" && capture === true && nodeListeners.delete(listener), + offsetWidth: 800, + classList: { contains: (name) => classes.has(name), add: (name) => classes.add(name) }, + dataset: { + windowId: "browser-1", + windowChromeMode: mode, + ...(maximized ? { maximized: "true" } : {}), + ...(browserMaximized ? { browserMaximized: "true" } : {}), + ...(snap ? { snap } : {}), + }, + style: { left: "100px", top: "50px" }, + }; + const frame = { + classList: { contains: (name) => name === "window-frame" }, + contentWindow: frameWindow, + getBoundingClientRect: () => ({ left: parseFloat(node.style.left) + 1, top: parseFloat(node.style.top) + 1 }), + }; + node.querySelector = (selector) => (selector === ".window-frame" ? frame : null); + const context = vm.createContext({ + WINDOW_DRAG_MESSAGE_TYPE: MESSAGE_TYPE, + WINDOW_CHROME_UNIFIED_TOOLBAR: stringConstant(coreSource, "WINDOW_CHROME_UNIFIED_TOOLBAR"), + window: { + addEventListener: (type, listener) => type === "message" && listeners.add(listener), + removeEventListener: (type, listener) => listeners.delete(listener), + }, + desktop: { getBoundingClientRect: () => ({ left: 0, top: 30 }) }, + clamp: (value, min, max) => Math.min(Math.max(value, min), max), + normalWindowBounds: (windowNode) => ({ x: parseFloat(windowNode.style.left), y: parseFloat(windowNode.style.top) }), + partialDragBoundsForWindow: () => ({ minX: -1000, maxX: 5000, minY: 0, maxY: 5000 }), + hideWindowSnapPreview: () => previews.push("hidden"), + showWindowSnapPreview: (target) => previews.push(target ? target.state : "hidden"), + beginShellInteraction: () => { interactions.begun += 1; }, + endShellInteraction: () => { interactions.ended += 1; }, + isPhone: () => phone, + WINDOW_SNAP_ACTIVATION_DISTANCE: SNAP_ACTIVATION_DISTANCE, + // Restored windows come back 400px wide with the pointer at their middle. + restoreWindowForDrag: (windowNode, clientX, clientY) => { + restores.push([clientX, clientY]); + windowNode.dataset.maximized = "false"; + windowNode.dataset.browserMaximized = "false"; + windowNode.dataset.snap = ""; + const bounds = { x: clientX - 200, y: clientY - 30 - 20 }; + windowNode.style.left = `${bounds.x}px`; + windowNode.style.top = `${bounds.y}px`; + return bounds; + }, + snapTargetForPointer: (clientX) => (clientX <= SNAP_EDGE ? { state: "left", bounds: {} } : null), + applyWindowSnap: (windowNode, target) => { + snaps.push(target.state); + windowNode.dataset.snap = target.state; + }, + }); + vm.runInContext( + `${declaration(geometrySource, "safeClamp")}\n${declaration(geometrySource, "attachForwardedWindowDrag")}\nthis.attach = attachForwardedWindowDrag;`, + context, + ); + const detach = context.attach( + node, + (id) => interactions.focused.push(id), + () => { interactions.saved += 1; }, + () => frameToken, + ); + // The Browser reports positions inside its own frame, which moves with the window. + const send = (phase, screenX, screenY, source = frameWindow, { origin = "null", homeToken = "frame-token", extra = {} } = {}) => { + const rect = frame.getBoundingClientRect(); + for (const listener of [...listeners]) { + listener({ + source, + origin, + data: { type: MESSAGE_TYPE, homeToken, phase, clientX: screenX - rect.left, clientY: screenY - rect.top, ...extra }, + }); + } + }; + const position = () => [parseFloat(node.style.left), parseFloat(node.style.top)]; + const loadFrame = (target = frame) => { + for (const listener of [...nodeListeners]) { + listener({ target }); + } + }; + return { node, interactions, restores, previews, snaps, send, position, detach, listeners, nodeListeners, loadFrame }; +} + +test("Home moves the Browser window with the pointer and saves the place on release", () => { + const home = homeWindow(); + home.send("start", 400, 80); + home.send("move", 340, 120); + home.send("move", 280, 160); + home.send("end", 280, 160); + assert.deepEqual(home.position(), [-20, 130]); + assert.deepEqual(home.interactions, { begun: 1, ended: 1, saved: 1, focused: ["browser-1"] }); +}); + +test("Home ignores drag messages from other frames, other chrome modes and hidden windows", () => { + const stranger = homeWindow(); + stranger.send("start", 400, 80, {}); + stranger.send("move", 280, 160, {}); + assert.deepEqual(stranger.position(), [100, 50]); + + const standard = homeWindow({ mode: "standard" }); + standard.send("start", 400, 80); + standard.send("move", 280, 160); + assert.deepEqual(standard.position(), [100, 50]); + + const hidden = homeWindow({ hidden: true }); + hidden.send("start", 400, 80); + hidden.send("move", 280, 160); + assert.deepEqual(hidden.position(), [100, 50]); + assert.deepEqual(hidden.interactions, { begun: 0, ended: 0, saved: 0, focused: [] }); +}); + +test("dragging a maximized or snapped Browser restores it under the pointer and moves it", () => { + for (const state of [{ maximized: true }, { browserMaximized: true }, { snap: "left" }, { snap: "ne" }]) { + const name = JSON.stringify(state); + const home = homeWindow(state); + home.send("start", 400, 80); + assert.deepEqual(home.restores, [[400, 80]], name); + assert.deepEqual(home.position(), [200, 30], name); + home.send("move", 280, 160); + home.send("end", 280, 160); + assert.deepEqual(home.position(), [80, 110], name); + assert.deepEqual(home.interactions, { begun: 1, ended: 1, saved: 1, focused: ["browser-1"] }, name); + assert.deepEqual(home.snaps, [], name); + } +}); + +test("dragging the Browser to a screen edge previews and applies the snap on release", () => { + const home = homeWindow(); + home.send("start", 400, 80); + home.send("move", 300, 100); + home.send("move", 4, 100); + assert.equal(home.previews.at(-1), "left"); + home.send("end", 4, 100); + assert.deepEqual(home.snaps, ["left"]); + assert.equal(home.node.dataset.snap, "left"); + assert.deepEqual(home.interactions, { begun: 1, ended: 1, saved: 1, focused: ["browser-1", "browser-1"] }); +}); + +test("an edge snap needs a real drag and is dropped when the pointer leaves the edge or the drag is cut", () => { + const tiny = homeWindow(); + tiny.send("start", SNAP_EDGE, 100); + tiny.send("move", SNAP_EDGE - 1, 100); + tiny.send("end", SNAP_EDGE - 1, 100); + assert.deepEqual(tiny.snaps, [], "a press that barely moves at the edge does not snap"); + + const away = homeWindow(); + away.send("start", 400, 80); + away.send("move", 4, 100); + away.send("move", 200, 100); + away.send("end", 200, 100); + assert.deepEqual(away.snaps, []); + assert.equal(away.previews.at(-1), "hidden"); + + const reloaded = homeWindow(); + reloaded.send("start", 400, 80); + reloaded.send("move", 4, 100); + reloaded.loadFrame(); + assert.deepEqual(reloaded.snaps, [], "a reload mid-drag never snaps"); + assert.equal(reloaded.previews.at(-1), "hidden"); + + const closed = homeWindow(); + closed.send("start", 400, 80); + closed.send("move", 4, 100); + closed.detach(); + assert.deepEqual(closed.snaps, [], "closing mid-drag never snaps"); + assert.equal(closed.previews.at(-1), "hidden"); +}); + +test("on a phone the Browser toolbar focuses the window but never drags, restores or snaps it", () => { + const home = homeWindow({ phone: true, maximized: true }); + home.send("start", 400, 80); + home.send("move", 4, 160); + home.send("end", 4, 160); + assert.deepEqual(home.position(), [100, 50]); + assert.deepEqual(home.restores, []); + assert.deepEqual(home.snaps, []); + assert.equal(home.node.dataset.maximized, "true"); + assert.deepEqual(home.interactions, { begun: 0, ended: 0, saved: 0, focused: ["browser-1"] }); +}); + +test("a window that is snapped, maximized or hidden during a drag stops following the pointer", () => { + for (const change of [ + (node) => { node.dataset.snap = "left"; }, + (node) => { node.dataset.maximized = "true"; }, + (node) => { node.classList.add("hidden"); }, + ]) { + const home = homeWindow(); + home.send("start", 400, 80); + home.send("move", 340, 120); + const placed = home.position(); + change(home.node); + home.send("move", 280, 160); + home.send("end", 280, 160); + assert.deepEqual(home.position(), placed); + assert.deepEqual([home.interactions.begun, home.interactions.ended, home.interactions.saved], [1, 1, 1]); + } +}); + +test("closing the window removes its drag listener and ends an active drag", () => { + const home = homeWindow(); + home.send("start", 400, 80); + home.detach(); + assert.equal(home.listeners.size, 0); + assert.equal(home.nodeListeners.size, 0); + assert.equal(home.interactions.ended, 1); +}); + +test("a frame that loads again mid-drag ends the drag", () => { + const home = homeWindow(); + home.send("start", 400, 80); + home.send("move", 340, 120); + home.loadFrame({ classList: { contains: () => false } }); + assert.equal(home.interactions.ended, 0, "a load inside another element is not the frame"); + home.loadFrame(); + const placed = home.position(); + assert.deepEqual([home.interactions.begun, home.interactions.ended, home.interactions.saved], [1, 1, 1]); + home.send("move", 280, 160); + assert.deepEqual(home.position(), placed, "moves after the reload are ignored until a new start"); +}); + +test("Home ignores drag messages that are not bound to this frame's launch", () => { + const cases = [ + ["another origin", { origin: "https://evil.example" }], + ["the Home origin", { origin: "http://127.0.0.1:3000" }], + ["another launch token", { homeToken: "other-token" }], + ["no launch token", { homeToken: "" }], + ["an extra field", { extra: { windowId: "browser-2" } }], + ]; + for (const [name, fields] of cases) { + const home = homeWindow(); + home.send("start", 400, 80, undefined, fields); + home.send("move", 280, 160, undefined, fields); + home.send("end", 280, 160, undefined, fields); + assert.deepEqual(home.position(), [100, 50], name); + assert.deepEqual(home.interactions, { begun: 0, ended: 0, saved: 0, focused: [] }, name); + } + + // A window whose frame Home holds no launch token for cannot be moved at all. + const unlaunched = homeWindow({ frameToken: "" }); + unlaunched.send("start", 400, 80, undefined, { homeToken: "" }); + unlaunched.send("move", 280, 160, undefined, { homeToken: "" }); + assert.deepEqual(unlaunched.position(), [100, 50]); + + const badPhase = homeWindow(); + badPhase.send("start", 400, 80); + badPhase.send("teleport", 0, 0); + assert.deepEqual(badPhase.position(), [100, 50], "an unknown phase is ignored, not treated as a move"); + assert.equal(badPhase.interactions.ended, 0, "and does not end the drag"); +}); diff --git a/scripts/home-shell-regression-smoke.mjs b/scripts/home-shell-regression-smoke.mjs index ee65a09c6..fb97db496 100644 --- a/scripts/home-shell-regression-smoke.mjs +++ b/scripts/home-shell-regression-smoke.mjs @@ -323,6 +323,10 @@ globalThis.window = { listeners.push(listener); windowEventListeners.set(type, listeners); }, + removeEventListener(type, listener) { + const listeners = windowEventListeners.get(type) || []; + windowEventListeners.set(type, listeners.filter((candidate) => candidate !== listener)); + }, setTimeout, clearTimeout, clearInterval: () => {}, @@ -970,10 +974,9 @@ assert( "People, Chat, or Inbox reintroduced capsule-owned outer window chrome", ); assert( - browserStyle.includes("padding: 8px 10px;") && - !browserStyle.includes("--window-chrome-safe-leading") && - !homeGuiStyle.includes("window-chrome-unified-toolbar"), - "Browser must sit under the standard draggable Home title bar, without a traffic-light inset", + browserStyle.includes("padding: 8px 10px 8px var(--window-chrome-safe-leading, 96px);") && + homeGuiStyle.includes(".window.window-chrome-unified-toolbar .window-head"), + "Browser no longer applies the Home-owned toolbar content inset", ); assert( !homeGuiScript.includes("(label || toolbarFullscreenButton).textContent"), @@ -1433,7 +1436,8 @@ const expectedWindowChrome = { className: "window-chrome-continuous", }, browser: { - mode: "standard", + mode: "unified-toolbar", + className: "window-chrome-unified-toolbar", }, }; @@ -1466,11 +1470,10 @@ const browserEntry = [...shellCore.shellState.windows.values()].find((entry) => browserHead.dispatch("pointermove", { pointerId: 41, clientX: 460, clientY: 180, target: browserHead }); browserHead.dispatch("pointerup", { pointerId: 41, clientX: 460, clientY: 180, target: browserHead }); assert( - browserEntry.node.dataset.windowChromeMode === "standard" && - !browserEntry.node.classList.contains("window-chrome-unified-toolbar") && + browserEntry.node.dataset.windowChromeMode === "unified-toolbar" && browserEntry.node.style.left === "260px" && browserEntry.node.style.top === "160px", - "Browser window title bar must be a standard Home title bar that drags the window", + "Browser window-button area must drag the window by the pointer delta", { left: [leftBefore, browserEntry.node.style.left], top: [topBefore, browserEntry.node.style.top] }, ); }