Agent Zero AI framework
Stabilize Browser modal switching
Alessandrof9175ed00bf3
diff --git a/plugins/_browser/extensions/webui/right-canvas-panels/browser-panel.html b/plugins/_browser/extensions/webui/right-canvas-panels/browser-panel.htmlindex c313bec78..9f03b773f 100644--- a/plugins/_browser/extensions/webui/right-canvas-panels/browser-panel.html+++ b/plugins/_browser/extensions/webui/right-canvas-panels/browser-panel.html@@ -1,8 +1,11 @@<divclass="right-canvas-surface-panel browser-canvas-surface"data-surface-id="browser"- x-show="$store.rightCanvas && $store.rightCanvas.isOpen && $store.rightCanvas.activeSurfaceId === 'browser'"- style="display: none;"+ :class="{+ 'is-active': $store.rightCanvas?.isSurfaceVisible('browser'),+ 'is-mounted': $store.rightCanvas?.isSurfaceRendered('browser')+ }"+ :aria-hidden="(!$store.rightCanvas?.isSurfaceVisible('browser')).toString()"><x-component path="/plugins/_browser/webui/browser-panel.html" mode="canvas"></x-component></div>diff --git a/plugins/_office/extensions/webui/right-canvas-panels/office-panel.html b/plugins/_office/extensions/webui/right-canvas-panels/office-panel.htmlindex 75dfb6b21..57f02918c 100644--- a/plugins/_office/extensions/webui/right-canvas-panels/office-panel.html+++ b/plugins/_office/extensions/webui/right-canvas-panels/office-panel.html@@ -1,9 +1,15 @@<divclass="right-canvas-surface-panel office-canvas-surface"data-surface-id="office"- x-show="$store.rightCanvas && $store.rightCanvas.isOpen && $store.rightCanvas.activeSurfaceId === 'office'"- x-effect="(() => { const visible = Boolean($store.rightCanvas && $store.rightCanvas.isOpen && $store.rightCanvas.activeSurfaceId === 'office'); (globalThis.queueMicrotask || ((callback) => globalThis.setTimeout(callback, 0)))(() => $store.office?.setDesktopHostVisible?.(visible)); })()"- style="display: none;"+ :class="{+ 'is-active': $store.rightCanvas?.isSurfaceVisible('office'),+ 'is-mounted': $store.rightCanvas?.isSurfaceRendered('office')+ }"+ :aria-hidden="(!$store.rightCanvas?.isSurfaceVisible('office')).toString()"+ x-effect="(() => {+ const visible = Boolean($store.rightCanvas?.isSurfaceRendered('office'));+ (globalThis.queueMicrotask || ((callback) => globalThis.setTimeout(callback, 0)))(() => $store.office?.setDesktopHostVisible?.(visible));+ })()"><x-component path="/plugins/_office/webui/office-panel.html"></x-component></div>diff --git a/plugins/_office/webui/office-store.js b/plugins/_office/webui/office-store.jsindex f2e1cec71..850d91a69 100644--- a/plugins/_office/webui/office-store.js+++ b/plugins/_office/webui/office-store.js@@ -229,6 +229,7 @@ const model = {_desktopPrimeTimer: null,_desktopPrimeAttempts: 0,_desktopKeyboardActive: false,+ _desktopFocusInProgress: false,_desktopBridgeReady: false,_desktopKeyboardCaptureState: { ready: false, active: false, capture: false, focused: false },_desktopLastState: null,@@ -1011,8 +1012,8 @@ const model = {isDesktopHostVisible() {if (this._mode === "modal") return true;- const canvas = globalThis.Alpine?.store?.("rightCanvas") || rightCanvasStore;- return Boolean(canvas?.isOpen && canvas.activeSurfaceId === "office");+ const canvas = rightCanvasStore;+ return Boolean(canvas?.isOpen && (canvas.isSurfaceMounted?.("office") ?? canvas.activeSurfaceId === "office"));},setDesktopHostVisible(visible) {@@ -1247,9 +1248,11 @@ const model = {},focusDesktopFrame(frame = null, options = {}) {+ if (this._desktopFocusInProgress) return false;const target = this.desktopFrame(frame);if (!target) return false;if (options.arm !== false) this._desktopKeyboardActive = true;+ this._desktopFocusInProgress = true;try {target.setAttribute("tabindex", "0");target.focus?.({ preventScroll: true });@@ -1261,6 +1264,8 @@ const model = {if (target.contentWindow?.client) target.contentWindow.client.capture_keyboard = true;} catch {target.focus?.({ preventScroll: true });+ } finally {+ this._desktopFocusInProgress = false;}const focused = Boolean(document.activeElement === target || target.contentDocument?.hasFocus?.());this.updateDesktopKeyboardCaptureState(target);@@ -1908,7 +1913,10 @@ const model = {frame.setAttribute("tabindex", "0");if (remoteWindow.__a0XpraDesktopKeyboardBridgeInstalled) return;- const activate = () => this.focusDesktopFrame(frame, { arm: true });+ const activate = () => {+ if (this._desktopFocusInProgress) return;+ this.focusDesktopFrame(frame, { arm: true });+ };const events = ["pointerdown", "mousedown", "touchstart", "focusin"];for (const eventName of events) {remoteDocument.addEventListener(eventName, activate, true);@@ -2339,8 +2347,8 @@ const model = {focusButton.className = "modal-dock-button office-modal-focus-button";focusButton.innerHTML = '<span class="material-symbols-outlined" aria-hidden="true">fullscreen</span>';const updateFocusButton = (active) => {- focusButton.title = active ? "Restore size" : "Focus mode";- focusButton.setAttribute("aria-label", focusButton.title);+ const label = active ? "Restore size" : "Focus mode";+ focusButton.setAttribute("aria-label", label);focusButton.querySelector(".material-symbols-outlined").textContent = active ? "fullscreen_exit" : "fullscreen";};updateFocusButton(false);diff --git a/tests/test_browser_agent_regressions.py b/tests/test_browser_agent_regressions.pyindex 39f1048ea..4a4e464f3 100644--- a/tests/test_browser_agent_regressions.py+++ b/tests/test_browser_agent_regressions.py@@ -671,10 +671,18 @@ def test_browser_and_desktop_surface_buttons_remember_latest_window_mode():)modals_js = (PROJECT_ROOT / "webui" / "js" / "modals.js").read_text(encoding="utf-8")modals_css = (PROJECT_ROOT / "webui" / "css" / "modals.css").read_text(encoding="utf-8")+ surface_button_block = modals_js[+ modals_js.index("function createModalSurfaceButton"):+ modals_js.index("function configureModalSurfaceSwitcher")+ ]assert "surfaceModes: {}" in canvas_store+ assert "mountedSurfaces: {}" in canvas_storeassert "recordSurfaceMode(surfaceId" in canvas_storeassert "latestSurfaceMode(surfaceId)" in canvas_store+ assert "markSurfaceMounted(targetId)" in canvas_store+ assert "isSurfaceRendered(id)" in canvas_store+ assert "isSurfaceVisible(id)" in canvas_storeassert "async openLatest(surfaceId" in canvas_storeassert "async openModalSurface(surfaceId" in canvas_storeassert "this.recordSurfaceMode(targetId, SURFACE_MODE_CANVAS" in canvas_store@@ -689,9 +697,16 @@ def test_browser_and_desktop_surface_buttons_remember_latest_window_mode():assert "configureModalSurfaceSwitcher" in modals_jsassert "modal-surface-switcher" in modals_jsassert "modal-surface-button" in modals_js+ assert "SINGLE_VISIBLE_MODAL_SURFACE_PATHS" in modals_js+ assert "modal-surface-parked" in modals_js+ assert "parkSiblingSurfaceModals(activeModal)" in modals_js+ assert "activateModal(modal)" in modals_js+ assert "button.title = title" not in modals_js+ assert "button.title = metadata.title" not in modals_jsassert "rightCanvasStore.panelSurfaces" in modals_jsassert 'rightCanvasStore.recordSurfaceMode?.(surface.id, "modal")' in modals_js- assert "await closeModal(modal.path)" in modals_js+ assert "const openPromise = ensureModalOpen(targetModalPath)" in surface_button_block+ assert "await closeModal(modal.path)" not in surface_button_blockassert "modalRequiresExplicitClose" in modals_jsassert '"plugins/_browser/webui/main.html"' in modals_jsassert '"plugins/_office/webui/main.html"' in modals_js@@ -700,6 +715,7 @@ def test_browser_and_desktop_surface_buttons_remember_latest_window_mode():assert ".modal-surface-switcher" in modals_cssassert ".modal-surface-button.is-active" in modals_cssassert ".modal-surface-image" in modals_css+ assert ".modal.modal-surface-parked" in modals_cssassert "grid-auto-flow: column" in modals_cssdiff --git a/tests/test_office_canvas_setup.py b/tests/test_office_canvas_setup.pyindex 7cb34def0..6cd40d42a 100644--- a/tests/test_office_canvas_setup.py+++ b/tests/test_office_canvas_setup.py@@ -59,6 +59,9 @@ def test_document_canvas_uses_markdown_editor_and_official_libreoffice_desktop_fassert "clearDesktopViewportSyncTimers" in storeassert "setDesktopHostVisible" in canvas_panelassert "queueMicrotask" in canvas_panel+ assert "isSurfaceRendered('office')" in canvas_panel+ assert "isSurfaceVisible('office')" in canvas_panel+ assert "canvas.isSurfaceMounted?.(\"office\")" in storeassert "Starting Agent Zero Desktop environment" in storeassert "handleOfficialOfficeClosed" in storeassert "ResizeObserver" in store@@ -100,6 +103,8 @@ def test_document_canvas_uses_markdown_editor_and_official_libreoffice_desktop_fassert "_desktopKeyboardCaptureState" in storeassert "installXpraDesktopKeyboardBridge" in storeassert "focusDesktopFrame" in store+ assert "_desktopFocusInProgress" in store+ assert "if (this._desktopFocusInProgress) return" in storeassert "_desktopKeyboardActive" in storeassert "isEditableInputTarget" in storeassert "reloadDesktopFrame" in store@@ -127,6 +132,7 @@ def test_document_canvas_uses_markdown_editor_and_official_libreoffice_desktop_fassert "__a0AllowScreenResize" in storeassert "_desktopHeartbeatTimer" in storeassert "office-modal-focus-button" in store+ assert "focusButton.title" not in storeassert "officialOfficeUrl" in storeassert 'parsed.searchParams.set("offscreen", secureContext ? "true" : "false")' in storeassert 'parsed.searchParams.set("clipboard_poll", secureContext ? "true" : "false")' in storediff --git a/webui/components/canvas/right-canvas-store.js b/webui/components/canvas/right-canvas-store.jsindex 5a19adbe1..88695fd8d 100644--- a/webui/components/canvas/right-canvas-store.js+++ b/webui/components/canvas/right-canvas-store.js@@ -31,6 +31,7 @@ const model = {surfaces: [],activeSurfaceId: "",surfaceModes: {},+ mountedSurfaces: {},isOpen: false,width: DEFAULT_WIDTH,isOverlayMode: false,@@ -130,6 +131,7 @@ const model = {}this.activeSurfaceId = targetId;+ this.markSurfaceMounted(targetId);this.isOpen = true;this.recordSurfaceMode(targetId, SURFACE_MODE_CANVAS, { persist: false });this._lastPayloadBySurface[targetId] = payload || {};@@ -144,6 +146,41 @@ const model = {return true;},+ markSurfaceMounted(surfaceId) {+ const targetId = String(surfaceId || "").trim();+ if (!targetId) return;+ this.mountedSurfaces = {+ ...this.mountedSurfaces,+ [targetId]: true,+ };+ },++ markSurfaceUnmounted(surfaceId) {+ const targetId = String(surfaceId || "").trim();+ if (!targetId || !this.mountedSurfaces[targetId]) return;+ const next = { ...this.mountedSurfaces };+ delete next[targetId];+ this.mountedSurfaces = next;+ },++ mountedSurfaceIds() {+ return Object.entries(this.mountedSurfaces)+ .filter(([, mounted]) => mounted)+ .map(([surfaceId]) => surfaceId);+ },++ isSurfaceMounted(id) {+ return Boolean(this.mountedSurfaces[String(id || "").trim()]);+ },++ isSurfaceRendered(id) {+ return Boolean(this.isOpen && this.isSurfaceMounted(id));+ },++ isSurfaceVisible(id) {+ return Boolean(this.isOpen && this.activeSurfaceId === id && this.isSurfaceMounted(id));+ },+recordSurfaceMode(surfaceId, mode = SURFACE_MODE_CANVAS, options = {}) {const targetId = String(surfaceId || "").trim();if (!targetId) return;@@ -169,14 +206,19 @@ const model = {},async close() {- const surface = this.currentSurface();+ const mountedIds = this.mountedSurfaceIds();this.isOpen = false;+ this.mountedSurfaces = {};this.persist();this.applyLayoutState();- try {- await surface?.close?.(this._lastPayloadBySurface[this.activeSurfaceId] || {});- } catch (error) {- console.error(`Canvas surface ${this.activeSurfaceId} failed to close`, error);++ for (const surfaceId of mountedIds) {+ const surface = this.getSurface(surfaceId);+ try {+ await surface?.close?.(this._lastPayloadBySurface[surfaceId] || {});+ } catch (error) {+ console.error(`Canvas surface ${surfaceId} failed to close`, error);+ }}},@@ -237,13 +279,19 @@ const model = {const openModal = globalThis.ensureModalOpen || globalThis.openModal;if (!openModal) return false;if (this.activeSurfaceId === targetId) {+ const mountedIds = this.mountedSurfaceIds();this.isOpen = false;+ this.mountedSurfaces = {};this.persist();this.applyLayoutState();- try {- await surface.close?.(this._lastPayloadBySurface[targetId] || {});- } catch (error) {- console.error(`Canvas surface ${targetId} failed to close while undocking`, error);++ for (const mountedId of mountedIds) {+ const mountedSurface = this.getSurface(mountedId);+ try {+ await mountedSurface?.close?.(this._lastPayloadBySurface[mountedId] || {});+ } catch (error) {+ console.error(`Canvas surface ${mountedId} failed to close while undocking`, error);+ }}}this.recordSurfaceMode(targetId, SURFACE_MODE_MODAL);@@ -263,13 +311,19 @@ const model = {if (!openModal) return false;if (this.isOpen && this.activeSurfaceId === targetId) {+ const mountedIds = this.mountedSurfaceIds();this.isOpen = false;+ this.mountedSurfaces = {};this.persist();this.applyLayoutState();- try {- await surface.close?.(this._lastPayloadBySurface[targetId] || {});- } catch (error) {- console.error(`Canvas surface ${targetId} failed to close before modal open`, error);++ for (const mountedId of mountedIds) {+ const mountedSurface = this.getSurface(mountedId);+ try {+ await mountedSurface?.close?.(this._lastPayloadBySurface[mountedId] || {});+ } catch (error) {+ console.error(`Canvas surface ${mountedId} failed to close before modal open`, error);+ }}}@@ -413,12 +467,16 @@ const model = {this.isMobileMode = width <= MOBILE_BREAKPOINT;if (this.isMobileMode) {const wasOpen = this.isOpen;- const surface = wasOpen ? this.currentSurface() : null;- const payload = this._lastPayloadBySurface[this.activeSurfaceId] || {};+ const mountedIds = this.mountedSurfaceIds();this.isOpen = false;- if (surface && wasOpen) {+ this.mountedSurfaces = {};+ if ((wasOpen || mountedIds.length > 0) && mountedIds.length > 0) {globalThis.setTimeout?.(() => {- surface.close?.({ ...payload, reason: "mobile" });+ for (const surfaceId of mountedIds) {+ const surface = this.getSurface(surfaceId);+ const payload = this._lastPayloadBySurface[surfaceId] || {};+ surface?.close?.({ ...payload, reason: "mobile" });+ }}, 0);}} else if (wasMobileMode && this.width < MIN_WIDTH) {diff --git a/webui/components/canvas/right-canvas.css b/webui/components/canvas/right-canvas.cssindex f9c161e1e..186d18402 100644--- a/webui/components/canvas/right-canvas.css+++ b/webui/components/canvas/right-canvas.css@@ -239,12 +239,24 @@ body.right-canvas-resizing {}.right-canvas-surface-panel {+ position: absolute;+ inset: 0;display: flex;- flex: 1 1 auto;+ flex: 0 0 auto;width: 100%;+ height: 100%;min-width: 0;min-height: 0;overflow: hidden;+ opacity: 0;+ pointer-events: none;+ z-index: 0;+}++.right-canvas-surface-panel.is-active {+ opacity: 1;+ pointer-events: auto;+ z-index: 2;}.right-canvas-surface-panel > x-component,diff --git a/webui/css/modals.css b/webui/css/modals.cssindex 19c738b28..fcce3e352 100644--- a/webui/css/modals.css+++ b/webui/css/modals.css@@ -18,6 +18,16 @@ the old and the new system. */display: block;}+.modal.modal-surface-parked {+ display: block;+ opacity: 0;+ pointer-events: none;+}++.modal.modal-surface-parked .modal-inner {+ pointer-events: none;+}+.modal.modal-floating {pointer-events: none;}@@ -153,9 +163,11 @@ the old and the new system. */}.modal-surface-switcher {- display: inline-flex;+ display: grid;+ grid-auto-flow: column;+ grid-auto-columns: 34px;align-items: center;- gap: 4px;+ gap: 5px;}.modal-dock-button,diff --git a/webui/js/modals.js b/webui/js/modals.jsindex 5c9c6653c..14948de41 100644--- a/webui/js/modals.js+++ b/webui/js/modals.js@@ -9,6 +9,25 @@ const EXPLICIT_CLOSE_MODAL_PATHS = new Set(["plugins/_browser/webui/main.html","plugins/_office/webui/main.html",]);+const SINGLE_VISIBLE_MODAL_SURFACE_PATHS = new Set([+ "plugins/_browser/webui/main.html",+ "plugins/_office/webui/main.html",+]);+const CANVAS_SURFACE_MODAL_GROUP = "canvas-surfaces";+const DEFAULT_MODAL_SURFACES = [+ {+ id: "browser",+ title: "Browser",+ icon: "language",+ modalPath: "/plugins/_browser/webui/main.html",+ },+ {+ id: "office",+ title: "Desktop",+ icon: "desktop_windows",+ modalPath: "/plugins/_office/webui/main.html",+ },+];function normalizeModalPath(modalPath = "") {return String(modalPath || "").replace(/^\/+/, "");@@ -26,6 +45,44 @@ function modalRequiresExplicitClose(modalOrElement) {|| element?.querySelector?.(".modal-inner")?.classList?.contains("modal-explicit-close");}+function modalSurfaceGroup(modalOrElement) {+ const element = modalOrElement?.element || modalOrElement;+ const path = normalizeModalPath(modalOrElement?.path || element?.path || "");+ return SINGLE_VISIBLE_MODAL_SURFACE_PATHS.has(path) ? CANVAS_SURFACE_MODAL_GROUP : "";+}++function setModalParked(modal, parked = false) {+ const element = modal?.element;+ if (!element) return;+ element.classList.toggle("modal-surface-parked", parked);+ if (parked) {+ element.classList.remove("show");+ element.setAttribute("aria-hidden", "true");+ } else {+ element.classList.add("show");+ element.removeAttribute("aria-hidden");+ }+}++function parkSiblingSurfaceModals(activeModal) {+ const group = modalSurfaceGroup(activeModal);+ if (!group) {+ setModalParked(activeModal, false);+ return;+ }++ for (const modal of modalStack) {+ setModalParked(modal, modal !== activeModal && modalSurfaceGroup(modal) === group);+ }+}++function activateModal(modal) {+ if (!modal) return;+ parkSiblingSurfaceModals(modal);+ updateModalZIndexes();+ restoreModalScrollSnapshot(modal);+}+function findModalIndexByPath(modalPath) {return modalStack.findIndex((modal) => sameModalPath(modal.path, modalPath));}@@ -33,10 +90,13 @@ function findModalIndexByPath(modalPath) {function focusModal(modalPath) {const modalIndex = findModalIndexByPath(modalPath);if (modalIndex === -1) return false;- if (modalIndex === modalStack.length - 1) return true;+ const currentTopModal = modalStack[modalStack.length - 1];+ if (currentTopModal) {+ currentTopModal.savedScrollSnapshot = captureModalScrollSnapshot(currentTopModal);+ }const [modal] = modalStack.splice(modalIndex, 1);modalStack.push(modal);- updateModalZIndexes();+ activateModal(modal);return true;}@@ -206,29 +266,31 @@ function getDockMetadata(doc, modalPath) {}function getModalSwitchSurfaces(metadata) {- if (!metadata) return [];+ const surfacesById = new Map(DEFAULT_MODAL_SURFACES.map((surface) => [surface.id, surface]));const surfaces = Array.isArray(rightCanvasStore.panelSurfaces)? rightCanvasStore.panelSurfaces: [];- const modalSurfaces = surfaces.filter((surface) => (- surface?.id- && surface?.modalPath- && !surface.actionOnly- ));-- if (modalSurfaces.some((surface) => surface.id === metadata.surfaceId)) {- return modalSurfaces;++ for (const surface of surfaces) {+ if (!surface?.id || !surface.modalPath || surface.actionOnly) continue;+ surfacesById.set(surface.id, {+ ...surface,+ modalPath: surface.modalPath,+ });}- return [- {+ if (metadata?.surfaceId && !surfacesById.has(metadata.surfaceId)) {+ surfacesById.set(metadata.surfaceId, {id: metadata.surfaceId,title: metadata.title,icon: metadata.icon,modalPath: metadata.modalPath,- },- ...modalSurfaces,- ];+ });+ }++ return Array.from(surfacesById.values())+ .filter((surface) => surface?.id && surface.modalPath && !surface.actionOnly)+ .sort((left, right) => (left.order ?? 100) - (right.order ?? 100));}function createModalSurfaceButton(surface, metadata, modal) {@@ -239,7 +301,6 @@ function createModalSurfaceButton(surface, metadata, modal) {button.type = "button";button.className = "modal-surface-button";button.dataset.canvasSurface = surface.id;- button.title = title;button.setAttribute("aria-label", title);button.setAttribute("aria-pressed", isActive.toString());if (isActive) button.classList.add("is-active");@@ -259,7 +320,7 @@ function createModalSurfaceButton(surface, metadata, modal) {button.appendChild(icon);}- button.addEventListener("click", async () => {+ button.addEventListener("click", () => {if (button.disabled || isActive || !targetModalPath) return;button.disabled = true;try {@@ -268,7 +329,6 @@ function createModalSurfaceButton(surface, metadata, modal) {if (openPromise?.catch) {openPromise.catch((error) => console.error(`Modal surface ${surface.id} failed to open`, error));}- await closeModal(modal.path);} finally {if (document.contains(button)) button.disabled = false;}@@ -310,7 +370,6 @@ function configureModalDockButton(modal, doc) {const button = document.createElement("button");button.type = "button";button.className = "modal-dock-button";- button.title = metadata.title;button.setAttribute("aria-label", metadata.title);button.innerHTML = `<span class="material-symbols-outlined" aria-hidden="true">${metadata.icon}</span>`;button.addEventListener("click", async () => {@@ -409,11 +468,9 @@ export async function openModal(modalPath, beforeClose = null) {// Add modal to stackmodal.path = modalPath;modalStack.push(modal);- modal.element.classList.add("show");document.body.style.overflow = "hidden";- // Update modal z-indexes- updateModalZIndexes();+ activateModal(modal);} catch (error) {console.error("Error loading modal content:", error);resolve();@@ -450,7 +507,7 @@ export async function closeModal(modalPath = null) {if (modalPath) {// Find the modal with the specified name in the stack- modalIndex = modalStack.findIndex((modal) => modal.path === modalPath);+ modalIndex = findModalIndexByPath(modalPath);if (modalIndex === -1) return; // Modal not found in stack// Get the modal from stack at the found index@@ -529,9 +586,7 @@ export async function closeModal(modalPath = null) {backdrop.style.display = "none";document.body.style.overflow = "";} else {- // Update modal z-indexes- updateModalZIndexes();- restoreModalScrollSnapshot(modalStack[modalStack.length - 1]);+ activateModal(modalStack[modalStack.length - 1]);}document.dispatchEvent(