From fb79807c16910b46ec021f49adfe3bcbdc0cd802 Mon Sep 17 00:00:00 2001 From: SashaMIT Date: Fri, 16 Oct 2026 01:19:55 +0700 Subject: [PATCH 1/7] browser: drag the window from empty parts of the toolbar The Browser draws a Safari-style single bar, and Home's own drag area in that unified-toolbar style is the 78 px window-button box. A press on an empty part of the Browser toolbar now posts frame-relative pointer positions to Home, which moves only that Browser's window, only in unified-toolbar mode, and saves the place on release. Buttons, the address field and other controls keep their clicks. Co-authored-by: Cursor --- capsules/browser/browser/browser.js | 46 +++++ .../home-gui/browser/shell-window-geometry.js | 75 ++++++++ capsules/home-gui/browser/shell-windows.js | 3 + scripts/browser-toolbar-window-drag.test.mjs | 169 ++++++++++++++++++ 4 files changed, 293 insertions(+) create mode 100644 scripts/browser-toolbar-window-drag.test.mjs diff --git a/capsules/browser/browser/browser.js b/capsules/browser/browser/browser.js index c5a3228fa..0a1063218 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,51 @@ 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) { + if (!chrome || window.parent === window) { + return; + } + let pointerId = null; + const send = (phase, event) => { + window.parent.postMessage( + { type: WINDOW_DRAG_MESSAGE_TYPE, 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/home-gui/browser/shell-window-geometry.js b/capsules/home-gui/browser/shell-window-geometry.js index 7ec5dae5b..884ee25f6 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"; const WINDOW_MIN_VISIBLE_DRAG_WIDTH = 96; @@ -404,6 +405,80 @@ 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. + * Positions are frame-relative and are added to the frame's current place, + * because some engines report screen positions offset or flipped. */ +export function attachForwardedWindowDrag(windowNode, focusWindow, onWindowGeometryChange = null) { + let dragging = null; + const finish = () => { + if (!dragging) { + return; + } + dragging = null; + endShellInteraction(); + if (typeof onWindowGeometryChange === "function") { + onWindowGeometryChange(); + } + }; + const listener = (event) => { + const data = event.data; + if (!data || data.type !== WINDOW_DRAG_MESSAGE_TYPE) { + return; + } + const frame = windowNode.querySelector(".window-frame"); + if ( + windowNode.dataset.windowChromeMode !== WINDOW_CHROME_UNIFIED_TOOLBAR || + !windowNode.isConnected || + !frame?.contentWindow || + event.source !== frame.contentWindow + ) { + return; + } + if (data.phase === "end") { + finish(); + return; + } + if (!Number.isFinite(data.clientX) || !Number.isFinite(data.clientY)) { + return; + } + const frameRect = frame.getBoundingClientRect(); + const workspaceRect = desktop.getBoundingClientRect(); + const pointerX = frameRect.left + data.clientX - workspaceRect.left; + const pointerY = frameRect.top + data.clientY - workspaceRect.top; + if (data.phase === "start") { + if ( + windowNode.dataset.maximized === "true" || + windowNode.dataset.browserMaximized === "true" || + windowNode.dataset.snap + ) { + return; + } + hideWindowSnapPreview(); + focusWindow(windowNode.dataset.windowId); + const bounds = normalWindowBounds(windowNode); + if (!dragging) { + beginShellInteraction(); + } + dragging = { offsetX: pointerX - bounds.x, offsetY: pointerY - bounds.y }; + 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`; + }; + window.addEventListener("message", listener); + return () => { + window.removeEventListener("message", listener); + 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 6e9b21190..237d07d81 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 { @@ -965,6 +966,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) { @@ -1590,6 +1592,7 @@ function createWindow({ toggleWindowMaximize(id); }); attachWindowDrag(node, handle, focusWindow, persistBrowserSession); + node.detachForwardedWindowDrag = attachForwardedWindowDrag(node, focusWindow, persistBrowserSession); attachWindowResize(node, focusWindow, persistBrowserSession); if (maximizeByDefault && shouldOpenMaximizedByDefault()) { diff --git a/scripts/browser-toolbar-window-drag.test.mjs b/scripts/browser-toolbar-window-drag.test.mjs new file mode 100644 index 000000000..55829e2f1 --- /dev/null +++ b/scripts/browser-toolbar-window-drag.test.mjs @@ -0,0 +1,169 @@ +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]; +} + +const MESSAGE_TYPE = stringConstant(geometrySource, "WINDOW_DRAG_MESSAGE_TYPE"); + +test("Browser and Home name the same window-drag message", () => { + assert.equal(stringConstant(browserSource, "WINDOW_DRAG_MESSAGE_TYPE"), MESSAGE_TYPE); +}); + +function browserToolbar() { + 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, + 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 }; +} + +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, "*"], + ], + ); +}); + +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"]); +}); + +function homeWindow({ mode = "unified-toolbar", maximized = false } = {}) { + const listeners = new Set(); + const interactions = { begun: 0, ended: 0, saved: 0, focused: [] }; + const frameWindow = {}; + const node = { + isConnected: true, + offsetWidth: 800, + dataset: { windowId: "browser-1", windowChromeMode: mode, ...(maximized ? { maximized: "true" } : {}) }, + style: { left: "100px", top: "50px" }, + }; + const 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: () => {}, + beginShellInteraction: () => { interactions.begun += 1; }, + endShellInteraction: () => { interactions.ended += 1; }, + }); + 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; }); + // The Browser reports positions inside its own frame, which moves with the window. + const send = (phase, screenX, screenY, source = frameWindow) => { + const rect = frame.getBoundingClientRect(); + for (const listener of [...listeners]) { + listener({ source, data: { type: MESSAGE_TYPE, phase, clientX: screenX - rect.left, clientY: screenY - rect.top } }); + } + }; + const position = () => [parseFloat(node.style.left), parseFloat(node.style.top)]; + return { node, interactions, send, position, detach, listeners }; +} + +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 maximized 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 maximized = homeWindow({ maximized: true }); + maximized.send("start", 400, 80); + maximized.send("move", 280, 160); + assert.deepEqual(maximized.position(), [100, 50]); + assert.equal(maximized.interactions.begun, 0); +}); + +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.interactions.ended, 1); +}); From c7b4978926722df7411e69091f795ca648970347 Mon Sep 17 00:00:00 2001 From: SashaMIT Date: Fri, 16 Oct 2026 01:25:08 +0700 Subject: [PATCH 2/7] browser: toolbar drag moves only a visible, free window A forwarded drag starts only on a visible window that is neither maximized nor snapped, and it ends when the window is hidden, snapped or maximized mid-drag. The Browser page version moves to browser-20261016a so Home loads the new toolbar code. Co-authored-by: Cursor --- capsules/browser/browser/index.html | 4 +-- .../home-gui/browser/shell-window-geometry.js | 16 +++++----- scripts/browser-toolbar-window-drag.test.mjs | 30 +++++++++++++++++-- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/capsules/browser/browser/index.html b/capsules/browser/browser/index.html index f8cdc9e1a..fa2802531 100644 --- a/capsules/browser/browser/index.html +++ b/capsules/browser/browser/index.html @@ -7,7 +7,7 @@ - + @@ -110,6 +110,6 @@ - + diff --git a/capsules/home-gui/browser/shell-window-geometry.js b/capsules/home-gui/browser/shell-window-geometry.js index 884ee25f6..bc24ffc8d 100644 --- a/capsules/home-gui/browser/shell-window-geometry.js +++ b/capsules/home-gui/browser/shell-window-geometry.js @@ -437,7 +437,14 @@ export function attachForwardedWindowDrag(windowNode, focusWindow, onWindowGeome ) { return; } - if (data.phase === "end") { + // A hidden, maximized or snapped window keeps its place, including when it + // changes state in the middle of a drag. + const movable = + !windowNode.classList.contains("hidden") && + windowNode.dataset.maximized !== "true" && + windowNode.dataset.browserMaximized !== "true" && + !windowNode.dataset.snap; + if (data.phase === "end" || !movable) { finish(); return; } @@ -449,13 +456,6 @@ export function attachForwardedWindowDrag(windowNode, focusWindow, onWindowGeome const pointerX = frameRect.left + data.clientX - workspaceRect.left; const pointerY = frameRect.top + data.clientY - workspaceRect.top; if (data.phase === "start") { - if ( - windowNode.dataset.maximized === "true" || - windowNode.dataset.browserMaximized === "true" || - windowNode.dataset.snap - ) { - return; - } hideWindowSnapPreview(); focusWindow(windowNode.dataset.windowId); const bounds = normalWindowBounds(windowNode); diff --git a/scripts/browser-toolbar-window-drag.test.mjs b/scripts/browser-toolbar-window-drag.test.mjs index 55829e2f1..872feb949 100644 --- a/scripts/browser-toolbar-window-drag.test.mjs +++ b/scripts/browser-toolbar-window-drag.test.mjs @@ -86,13 +86,15 @@ test("toolbar controls, other buttons and a second pointer keep their own behavi assert.deepEqual(toolbar.posted.map(({ message }) => message.phase), ["start"]); }); -function homeWindow({ mode = "unified-toolbar", maximized = false } = {}) { +function homeWindow({ mode = "unified-toolbar", maximized = false, hidden = false } = {}) { const listeners = new Set(); const interactions = { begun: 0, ended: 0, saved: 0, focused: [] }; const frameWindow = {}; + const classes = new Set(hidden ? ["window", "hidden"] : ["window"]); const node = { isConnected: true, offsetWidth: 800, + classList: { contains: (name) => classes.has(name), add: (name) => classes.add(name) }, dataset: { windowId: "browser-1", windowChromeMode: mode, ...(maximized ? { maximized: "true" } : {}) }, style: { left: "100px", top: "50px" }, }; @@ -142,7 +144,7 @@ test("Home moves the Browser window with the pointer and saves the place on rele 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 maximized windows", () => { +test("Home ignores drag messages from other frames, other chrome modes, maximized and hidden windows", () => { const stranger = homeWindow(); stranger.send("start", 400, 80, {}); stranger.send("move", 280, 160, {}); @@ -158,6 +160,30 @@ test("Home ignores drag messages from other frames, other chrome modes and maxim maximized.send("move", 280, 160); assert.deepEqual(maximized.position(), [100, 50]); assert.equal(maximized.interactions.begun, 0); + + 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("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", () => { From ba414024f8e6934f999b4a956d21d7fb6b4e4a12 Mon Sep 17 00:00:00 2001 From: SashaMIT Date: Fri, 16 Oct 2026 01:30:41 +0700 Subject: [PATCH 3/7] browser: keep the Safari-style single bar, draggable from the toolbar Sasha chose the single toolbar with the window buttons in its leading corner. This restores the unified-toolbar chrome mode, its Home styles and the Browser toolbar inset that the standard title bar replaced, now that empty parts of the toolbar drag the window. The Home regression smoke expects unified-toolbar for the Browser and keeps the head drag check; its window fixture gains removeEventListener. Co-authored-by: Cursor --- capsules/browser/browser/index.html | 2 +- capsules/browser/browser/style.css | 9 ++--- capsules/home-gui/browser/shell-core.js | 12 ++++++- capsules/home-gui/browser/style.css | 46 +++++++++++++++++++++++++ scripts/home-shell-regression-smoke.mjs | 19 +++++----- 5 files changed, 74 insertions(+), 14 deletions(-) diff --git a/capsules/browser/browser/index.html b/capsules/browser/browser/index.html index de5a46f82..a0f5d5272 100644 --- a/capsules/browser/browser/index.html +++ b/capsules/browser/browser/index.html @@ -6,7 +6,7 @@ ElastOS Browser - + diff --git a/capsules/browser/browser/style.css b/capsules/browser/browser/style.css index e14b0de1e..7d99462d4 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 3779c2dc1..ce9c438b5 100644 --- a/capsules/home-gui/browser/shell-core.js +++ b/capsules/home-gui/browser/shell-core.js @@ -392,9 +392,12 @@ export function canonicalTargetTitle(targetId, title) { } /* 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, @@ -406,6 +409,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([ @@ -421,6 +425,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; } @@ -436,11 +443,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/style.css b/capsules/home-gui/browser/style.css index 460dd1dc5..c9e1ee266 100644 --- a/capsules/home-gui/browser/style.css +++ b/capsules/home-gui/browser/style.css @@ -5410,6 +5410,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/scripts/home-shell-regression-smoke.mjs b/scripts/home-shell-regression-smoke.mjs index 23e9acb40..2690c6b70 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"), @@ -1390,7 +1393,8 @@ const expectedWindowChrome = { className: "window-chrome-continuous", }, browser: { - mode: "standard", + mode: "unified-toolbar", + className: "window-chrome-unified-toolbar", }, }; @@ -1423,11 +1427,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] }, ); } From a136a72c963678948ff4f2863fd10345f6525cd3 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 17 Oct 2026 06:00:29 +0000 Subject: [PATCH 4/7] browser: Home moves a window only for drag messages bound to its frame's launch Home accepted elastos.home.window-drag/v1 from the window's own WindowProxy, but did not check the origin, the shape, or the launch capability. PRINCIPLES.md 16 says Home must bind a browser-frame message to the launched frame and its capsule-scoped capability before it acts, as the window-close and navigation messages already do. The Browser now sends its home_token with each drag message and sends nothing without one. Home ignores a drag message unless the origin is the opaque "null" origin, the keys are exactly type/homeToken/phase/clientX/ clientY, the phase is start, move or end, and homeToken equals the home_token in the route Home gave that frame. browser.js is bumped to ?v=browser-20261017a so a cached copy without the token is not reused. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01W6Qn9RX7LDqrwiAfNC2F2K --- capsules/browser/browser/browser.js | 11 +++- capsules/browser/browser/index.html | 4 +- .../home-gui/browser/shell-window-geometry.js | 28 +++++++-- capsules/home-gui/browser/shell-windows.js | 7 ++- scripts/browser-toolbar-window-drag.test.mjs | 59 +++++++++++++++++-- 5 files changed, 94 insertions(+), 15 deletions(-) diff --git a/capsules/browser/browser/browser.js b/capsules/browser/browser/browser.js index d90f9a909..d138c5bda 100644 --- a/capsules/browser/browser/browser.js +++ b/capsules/browser/browser/browser.js @@ -846,13 +846,20 @@ 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) { - if (!chrome || window.parent === window) { + // 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, phase, clientX: event.clientX, clientY: event.clientY }, + { + type: WINDOW_DRAG_MESSAGE_TYPE, + homeToken: launchToken, + phase, + clientX: event.clientX, + clientY: event.clientY, + }, "*", ); }; diff --git a/capsules/browser/browser/index.html b/capsules/browser/browser/index.html index a0f5d5272..c7ff1cd10 100644 --- a/capsules/browser/browser/index.html +++ b/capsules/browser/browser/index.html @@ -7,7 +7,7 @@ - + @@ -110,6 +110,6 @@ - + diff --git a/capsules/home-gui/browser/shell-window-geometry.js b/capsules/home-gui/browser/shell-window-geometry.js index 5b2dea2d9..f29bec971 100644 --- a/capsules/home-gui/browser/shell-window-geometry.js +++ b/capsules/home-gui/browser/shell-window-geometry.js @@ -419,10 +419,18 @@ export function attachWindowDrag(windowNode, handle, focusWindow, onWindowGeomet 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. + * 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. */ -export function attachForwardedWindowDrag(windowNode, focusWindow, onWindowGeometryChange = null) { +export function attachForwardedWindowDrag( + windowNode, + focusWindow, + onWindowGeometryChange = null, + frameHomeToken = null, +) { let dragging = null; const finish = () => { if (!dragging) { @@ -436,15 +444,27 @@ export function attachForwardedWindowDrag(windowNode, focusWindow, onWindowGeome }; const listener = (event) => { const data = event.data; - if (!data || data.type !== WINDOW_DRAG_MESSAGE_TYPE) { + // 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 + event.source !== frame.contentWindow || + typeof homeToken !== "string" || + !homeToken || + data.homeToken !== homeToken ) { return; } diff --git a/capsules/home-gui/browser/shell-windows.js b/capsules/home-gui/browser/shell-windows.js index 90de3a7d8..46b0c67f3 100644 --- a/capsules/home-gui/browser/shell-windows.js +++ b/capsules/home-gui/browser/shell-windows.js @@ -1596,7 +1596,12 @@ function createWindow({ toggleWindowMaximize(id); }); attachWindowDrag(node, handle, focusWindow, persistBrowserSession); - node.detachForwardedWindowDrag = attachForwardedWindowDrag(node, 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/scripts/browser-toolbar-window-drag.test.mjs b/scripts/browser-toolbar-window-drag.test.mjs index 872feb949..7568dbdaa 100644 --- a/scripts/browser-toolbar-window-drag.test.mjs +++ b/scripts/browser-toolbar-window-drag.test.mjs @@ -27,7 +27,7 @@ test("Browser and Home name the same window-drag message", () => { assert.equal(stringConstant(browserSource, "WINDOW_DRAG_MESSAGE_TYPE"), MESSAGE_TYPE); }); -function browserToolbar() { +function browserToolbar({ launchToken = "frame-token" } = {}) { const posted = []; const listeners = {}; const captured = []; @@ -37,6 +37,7 @@ function browserToolbar() { }; 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); @@ -51,7 +52,7 @@ function browserToolbar() { }); return prevented; }; - return { posted, captured, press }; + return { posted, captured, press, listeners }; } test("an empty part of the Browser toolbar sends start, move and end to Home", () => { @@ -69,6 +70,13 @@ test("an empty part of the Browser toolbar sends start, move and end to Home", ( [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", () => { @@ -86,7 +94,7 @@ test("toolbar controls, other buttons and a second pointer keep their own behavi assert.deepEqual(toolbar.posted.map(({ message }) => message.phase), ["start"]); }); -function homeWindow({ mode = "unified-toolbar", maximized = false, hidden = false } = {}) { +function homeWindow({ mode = "unified-toolbar", maximized = false, hidden = false, frameToken = "frame-token" } = {}) { const listeners = new Set(); const interactions = { begun: 0, ended: 0, saved: 0, focused: [] }; const frameWindow = {}; @@ -122,12 +130,21 @@ function homeWindow({ mode = "unified-toolbar", maximized = false, hidden = fals `${declaration(geometrySource, "safeClamp")}\n${declaration(geometrySource, "attachForwardedWindowDrag")}\nthis.attach = attachForwardedWindowDrag;`, context, ); - const detach = context.attach(node, (id) => interactions.focused.push(id), () => { interactions.saved += 1; }); + 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) => { + const send = (phase, screenX, screenY, source = frameWindow, { origin = "null", homeToken = "frame-token", extra = {} } = {}) => { const rect = frame.getBoundingClientRect(); for (const listener of [...listeners]) { - listener({ source, data: { type: MESSAGE_TYPE, phase, clientX: screenX - rect.left, clientY: screenY - rect.top } }); + 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)]; @@ -193,3 +210,33 @@ test("closing the window removes its drag listener and ends an active drag", () assert.equal(home.listeners.size, 0); assert.equal(home.interactions.ended, 1); }); + +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"); +}); From ea575224276758fbbeb7a9aa37deb66a6edf0cce Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 17 Oct 2026 06:00:29 +0000 Subject: [PATCH 5/7] docs: describe the Browser toolbar window-drag message in the Home host contract Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01W6Qn9RX7LDqrwiAfNC2F2K --- docs/HOME_SHELL_HOST_CONTRACT.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/docs/HOME_SHELL_HOST_CONTRACT.md b/docs/HOME_SHELL_HOST_CONTRACT.md index a96a3a640..7e5d1b1fb 100644 --- a/docs/HOME_SHELL_HOST_CONTRACT.md +++ b/docs/HOME_SHELL_HOST_CONTRACT.md @@ -162,6 +162,36 @@ 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. + +On each message the window must also be visible, not maximized, not +Browser-maximized and not snapped. If not, the drag stops. 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 +353,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) From 76fc08afecd21811fb79aa7eb9ed6dcf4f7e3f3b Mon Sep 17 00:00:00 2001 From: SashaMIT Date: Sat, 17 Oct 2026 18:32:54 +0800 Subject: [PATCH 6/7] home: a Browser frame that loads again mid-drag ends the forwarded drag A reload, navigation or relaunch never sends the drag's end, and the old launch token no longer matches, so Home's interaction state stayed active until another drag ended or the window closed. Co-authored-by: Cursor --- .../home-gui/browser/shell-window-geometry.js | 9 +++++++ scripts/browser-toolbar-window-drag.test.mjs | 25 ++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/capsules/home-gui/browser/shell-window-geometry.js b/capsules/home-gui/browser/shell-window-geometry.js index f29bec971..9c1ddf142 100644 --- a/capsules/home-gui/browser/shell-window-geometry.js +++ b/capsules/home-gui/browser/shell-window-geometry.js @@ -503,9 +503,18 @@ export function attachForwardedWindowDrag( windowNode.style.left = `${safeClamp(pointerX - dragging.offsetX, limits.minX, limits.maxX)}px`; windowNode.style.top = `${safeClamp(pointerY - dragging.offsetY, limits.minY, limits.maxY)}px`; }; + // 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(); }; } diff --git a/scripts/browser-toolbar-window-drag.test.mjs b/scripts/browser-toolbar-window-drag.test.mjs index 7568dbdaa..505f406a0 100644 --- a/scripts/browser-toolbar-window-drag.test.mjs +++ b/scripts/browser-toolbar-window-drag.test.mjs @@ -99,14 +99,18 @@ function homeWindow({ mode = "unified-toolbar", maximized = false, hidden = fals const interactions = { begun: 0, ended: 0, saved: 0, focused: [] }; 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" } : {}) }, 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 }), }; @@ -148,7 +152,12 @@ function homeWindow({ mode = "unified-toolbar", maximized = false, hidden = fals } }; const position = () => [parseFloat(node.style.left), parseFloat(node.style.top)]; - return { node, interactions, send, position, detach, listeners }; + const loadFrame = (target = frame) => { + for (const listener of [...nodeListeners]) { + listener({ target }); + } + }; + return { node, interactions, send, position, detach, listeners, nodeListeners, loadFrame }; } test("Home moves the Browser window with the pointer and saves the place on release", () => { @@ -208,9 +217,23 @@ test("closing the window removes its drag listener and ends an active drag", () 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" }], From 13764e74dd724322ed4fa4d5788a3f79dbbea1c7 Mon Sep 17 00:00:00 2001 From: SashaMIT Date: Sat, 17 Oct 2026 23:14:19 +0800 Subject: [PATCH 7/7] home: dragging the Browser toolbar restores a maximized or snapped window and snaps at screen edges Co-authored-by: Cursor --- .../home-gui/browser/shell-window-geometry.js | 59 ++++++-- docs/HOME_SHELL_HOST_CONTRACT.md | 10 +- scripts/browser-toolbar-window-drag.test.mjs | 130 ++++++++++++++++-- 3 files changed, 171 insertions(+), 28 deletions(-) diff --git a/capsules/home-gui/browser/shell-window-geometry.js b/capsules/home-gui/browser/shell-window-geometry.js index 9c1ddf142..6412d0bfd 100644 --- a/capsules/home-gui/browser/shell-window-geometry.js +++ b/capsules/home-gui/browser/shell-window-geometry.js @@ -424,7 +424,9 @@ export const WINDOW_DRAG_MESSAGE_TYPE = "elastos.home.window-drag/v1"; * 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. */ + * 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, @@ -432,12 +434,19 @@ export function attachForwardedWindowDrag( frameHomeToken = null, ) { let dragging = null; - const finish = () => { + 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(); } @@ -468,14 +477,17 @@ export function attachForwardedWindowDrag( ) { return; } - // A hidden, maximized or snapped window keeps its place, including when it - // changes state in the middle of a drag. - const movable = - !windowNode.classList.contains("hidden") && - windowNode.dataset.maximized !== "true" && - windowNode.dataset.browserMaximized !== "true" && - !windowNode.dataset.snap; - if (data.phase === "end" || !movable) { + 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; } @@ -484,16 +496,30 @@ export function attachForwardedWindowDrag( } const frameRect = frame.getBoundingClientRect(); const workspaceRect = desktop.getBoundingClientRect(); - const pointerX = frameRect.left + data.clientX - workspaceRect.left; - const pointerY = frameRect.top + data.clientY - workspaceRect.top; + 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); - const bounds = normalWindowBounds(windowNode); + // 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 }; + dragging = { + offsetX: pointerX - bounds.x, + offsetY: pointerY - bounds.y, + startClientX: clientX, + startClientY: clientY, + snapTarget: null, + }; return; } if (data.phase !== "move" || !dragging) { @@ -502,6 +528,11 @@ export function attachForwardedWindowDrag( 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. diff --git a/docs/HOME_SHELL_HOST_CONTRACT.md b/docs/HOME_SHELL_HOST_CONTRACT.md index 7e5d1b1fb..ae1f551f4 100644 --- a/docs/HOME_SHELL_HOST_CONTRACT.md +++ b/docs/HOME_SHELL_HOST_CONTRACT.md @@ -187,9 +187,13 @@ unless all of these are true: `end`; - the window uses `unified-toolbar` chrome and is connected. -On each message the window must also be visible, not maximized, not -Browser-maximized and not snapped. If not, the drag stops. Home removes the -listener when it closes the window. The Browser does not send the message when +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 diff --git a/scripts/browser-toolbar-window-drag.test.mjs b/scripts/browser-toolbar-window-drag.test.mjs index 505f406a0..7c68249fc 100644 --- a/scripts/browser-toolbar-window-drag.test.mjs +++ b/scripts/browser-toolbar-window-drag.test.mjs @@ -21,7 +21,14 @@ function stringConstant(source, 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); @@ -94,9 +101,22 @@ test("toolbar controls, other buttons and a second pointer keep their own behavi assert.deepEqual(toolbar.posted.map(({ message }) => message.phase), ["start"]); }); -function homeWindow({ mode = "unified-toolbar", maximized = false, hidden = false, frameToken = "frame-token" } = {}) { +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(); @@ -106,7 +126,13 @@ function homeWindow({ mode = "unified-toolbar", maximized = false, hidden = fals 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" } : {}) }, + dataset: { + windowId: "browser-1", + windowChromeMode: mode, + ...(maximized ? { maximized: "true" } : {}), + ...(browserMaximized ? { browserMaximized: "true" } : {}), + ...(snap ? { snap } : {}), + }, style: { left: "100px", top: "50px" }, }; const frame = { @@ -126,9 +152,28 @@ function homeWindow({ mode = "unified-toolbar", maximized = false, hidden = fals 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: () => {}, + 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;`, @@ -157,7 +202,7 @@ function homeWindow({ mode = "unified-toolbar", maximized = false, hidden = fals listener({ target }); } }; - return { node, interactions, send, position, detach, listeners, nodeListeners, loadFrame }; + 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", () => { @@ -170,7 +215,7 @@ test("Home moves the Browser window with the pointer and saves the place on rele assert.deepEqual(home.interactions, { begun: 1, ended: 1, saved: 1, focused: ["browser-1"] }); }); -test("Home ignores drag messages from other frames, other chrome modes, maximized and hidden windows", () => { +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, {}); @@ -181,12 +226,6 @@ test("Home ignores drag messages from other frames, other chrome modes, maximize standard.send("move", 280, 160); assert.deepEqual(standard.position(), [100, 50]); - const maximized = homeWindow({ maximized: true }); - maximized.send("start", 400, 80); - maximized.send("move", 280, 160); - assert.deepEqual(maximized.position(), [100, 50]); - assert.equal(maximized.interactions.begun, 0); - const hidden = homeWindow({ hidden: true }); hidden.send("start", 400, 80); hidden.send("move", 280, 160); @@ -194,6 +233,75 @@ test("Home ignores drag messages from other frames, other chrome modes, maximize 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"; },