Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(desktop): keep Settings menus above the Settings dialog
vastsa476fb5bdd30a
diff --git a/apps/desktop/src/components/ProjectInstructionsDialog.tsx b/apps/desktop/src/components/ProjectInstructionsDialog.tsxindex d458396b0..3f3ceb2da 100644--- a/apps/desktop/src/components/ProjectInstructionsDialog.tsx+++ b/apps/desktop/src/components/ProjectInstructionsDialog.tsx@@ -1,5 +1,5 @@import { useEffect, useState } from "react";-import { createPortal } from "react-dom";+import { portalToBody } from "../lib/portal-visibility";import { useTranslation } from "react-i18next";import { useBlockingOverlay } from "../lib/blocking-overlay";import type { AgentInstructionFile } from "@pi-desktop/shared";@@ -128,7 +128,5 @@ export function ProjectInstructionsDialog({</OverlayBackdrop>);- return typeof document === "undefined"- ? dialog- : createPortal(dialog, document.body);+ return portalToBody(dialog);}diff --git a/apps/desktop/src/components/ProjectMemoryDialog.tsx b/apps/desktop/src/components/ProjectMemoryDialog.tsxindex fc1248bb4..9f3293bc7 100644--- a/apps/desktop/src/components/ProjectMemoryDialog.tsx+++ b/apps/desktop/src/components/ProjectMemoryDialog.tsx@@ -1,5 +1,5 @@import { useEffect, useState } from "react";-import { createPortal } from "react-dom";+import { portalToBody } from "../lib/portal-visibility";import { useTranslation } from "react-i18next";import { useBlockingOverlay } from "../lib/blocking-overlay";import type { ProjectMemory, ProjectMemoryEntry } from "@pi-desktop/shared";@@ -224,5 +224,5 @@ export function ProjectMemoryDialog({</OverlayBackdrop>);- return typeof document === "undefined" ? dialog : createPortal(dialog, document.body);+ return portalToBody(dialog);}diff --git a/apps/desktop/src/components/settings/AnchoredMenu.tsx b/apps/desktop/src/components/settings/AnchoredMenu.tsxindex 9797c4be6..d0d6b63b2 100644--- a/apps/desktop/src/components/settings/AnchoredMenu.tsx+++ b/apps/desktop/src/components/settings/AnchoredMenu.tsx@@ -3,7 +3,8 @@** Settings cards clip their overflow (`.settings-panel` draws the frame with* `overflow: hidden`), so an anchored surface cannot be an absolutely- * positioned child: it portals to `document.body` as a fixed layer and is+ * positioned child: it portals to body level (or the enclosing modal's layer+ * host, see `portalToBody`) as a fixed layer and is* placed from the trigger's rect. The placement, close and reposition rules* follow the font picker, which solved the same problem first.*@@ -20,7 +21,7 @@ import {type KeyboardEvent as ReactKeyboardEvent,type ReactNode,} from "react";-import { createPortal } from "react-dom";+import { portalToBody } from "../../lib/portal-visibility";const MARGIN = 8;const GAP = 6;@@ -231,8 +232,8 @@ export function AnchoredMenu({return (<div className={className} ref={rootRef}>{trigger(triggerRef)}- {open && typeof document !== "undefined"- ? createPortal(+ {open+ ? portalToBody(<divref={menuRef}className={`${menuClassName}${position ? " is-open" : ""}`}@@ -253,7 +254,6 @@ export function AnchoredMenu({>{children}</div>,- document.body,): null}</div>diff --git a/apps/desktop/src/components/settings/FontFamilyRow.tsx b/apps/desktop/src/components/settings/FontFamilyRow.tsxindex 38b55dc7e..479aa532a 100644--- a/apps/desktop/src/components/settings/FontFamilyRow.tsx+++ b/apps/desktop/src/components/settings/FontFamilyRow.tsx@@ -6,7 +6,7 @@ import {useRef,useState,} from "react";-import { createPortal } from "react-dom";+import { portalToBody } from "../../lib/portal-visibility";import { useTranslation } from "react-i18next";import type { AppSettings } from "@pi-desktop/shared";import {@@ -325,8 +325,7 @@ export function FontFamilyRow({<IconChevronDown size={14} /></button>{open &&- typeof document !== "undefined" &&- createPortal(+ portalToBody(<divref={menuRef}className={`settings-font-menu${@@ -427,7 +426,6 @@ export function FontFamilyRow({</div>)}</div>,- document.body,)}</div></div>diff --git a/apps/desktop/src/components/settings/McpMarketPanel.tsx b/apps/desktop/src/components/settings/McpMarketPanel.tsxindex f60e0e5d2..eaff7702e 100644--- a/apps/desktop/src/components/settings/McpMarketPanel.tsx+++ b/apps/desktop/src/components/settings/McpMarketPanel.tsx@@ -1,5 +1,5 @@import { useEffect, useMemo, useRef, useState, type CSSProperties } from "react";-import { createPortal } from "react-dom";+import { portalToBody } from "../../lib/portal-visibility";import { useTranslation } from "react-i18next";import {allowInsecureUserEndpoints,@@ -837,8 +837,8 @@ export function McpMarketPanel({{/* The settings shell carries a transform, which would turn theoverlay's `position: fixed` into shell-relative positioning — portalboth sheets to <body> so they always cover the window. */}- {installSheet && createPortal(installSheet, document.body)}- {sourcesSheet && createPortal(sourcesSheet, document.body)}+ {installSheet && portalToBody(installSheet)}+ {sourcesSheet && portalToBody(sourcesSheet)}</div>);}diff --git a/apps/desktop/src/components/settings/SkillMarketPanel.tsx b/apps/desktop/src/components/settings/SkillMarketPanel.tsxindex 699a2ebad..350f27dec 100644--- a/apps/desktop/src/components/settings/SkillMarketPanel.tsx+++ b/apps/desktop/src/components/settings/SkillMarketPanel.tsx@@ -1,5 +1,5 @@import { useEffect, useMemo, useRef, useState, type CSSProperties } from "react";-import { createPortal } from "react-dom";+import { portalToBody } from "../../lib/portal-visibility";import { useTranslation } from "react-i18next";import {allowInsecureUserEndpoints,@@ -922,8 +922,8 @@ export function SkillMarketPanel({{/* The settings shell carries a transform, which would turn theoverlay's `position: fixed` into shell-relative positioning — portalboth sheets to <body> so they always cover the window. */}- {installSheet && createPortal(installSheet, document.body)}- {sourcesSheet && createPortal(sourcesSheet, document.body)}+ {installSheet && portalToBody(installSheet)}+ {sourcesSheet && portalToBody(sourcesSheet)}</div>);diff --git a/apps/desktop/src/features/settings/SettingsDialog.tsx b/apps/desktop/src/features/settings/SettingsDialog.tsxindex 9fcb5fd67..c0f7e10a7 100644--- a/apps/desktop/src/features/settings/SettingsDialog.tsx+++ b/apps/desktop/src/features/settings/SettingsDialog.tsx@@ -2,6 +2,7 @@ import { useRef } from "react";import { useTranslation } from "react-i18next";import { IconClose } from "../../components/icons";import { OverlayBackdrop, portalOverlay, TooltipButton } from "../../components/ui";+import { ModalLayer } from "../../lib/portal-visibility";import { useModalDialog } from "../../lib/use-modal-dialog";import { SettingsPage } from "./SettingsPage";@@ -15,33 +16,35 @@ export function SettingsDialog({ onClose }: { onClose: () => void }) {useModalDialog(dialogRef, { onClose });return portalOverlay(- <OverlayBackdrop- className="overlay settings-dialog-overlay"- role="presentation"- onClick={(event) => {- if (event.target === event.currentTarget) onClose();- }}- >- <div- ref={dialogRef}- className="dialog settings-dialog"- role="dialog"- aria-modal="true"- aria-label={t("settings.title")}- data-surface="settings-dialog"+ <ModalLayer>+ <OverlayBackdrop+ className="overlay settings-dialog-overlay"+ role="presentation"+ onClick={(event) => {+ if (event.target === event.currentTarget) onClose();+ }}>- <SettingsPage />- <TooltipButton- type="button"- className="icon-btn settings-dialog-close"- data-nav="close-settings"- tooltip={t("settings.close")}- ariaLabel={t("settings.close")}- onClick={onClose}+ <div+ ref={dialogRef}+ className="dialog settings-dialog"+ role="dialog"+ aria-modal="true"+ aria-label={t("settings.title")}+ data-surface="settings-dialog">- <IconClose size={16} />- </TooltipButton>- </div>- </OverlayBackdrop>,+ <SettingsPage />+ <TooltipButton+ type="button"+ className="icon-btn settings-dialog-close"+ data-nav="close-settings"+ tooltip={t("settings.close")}+ ariaLabel={t("settings.close")}+ onClick={onClose}+ >+ <IconClose size={16} />+ </TooltipButton>+ </div>+ </OverlayBackdrop>+ </ModalLayer>,);}diff --git a/apps/desktop/src/features/startup/StartupGuideDialog.tsx b/apps/desktop/src/features/startup/StartupGuideDialog.tsxindex 145801307..8ba830f9f 100644--- a/apps/desktop/src/features/startup/StartupGuideDialog.tsx+++ b/apps/desktop/src/features/startup/StartupGuideDialog.tsx@@ -10,6 +10,7 @@ import {import { IconCheck, IconClose, IconSearch } from "../../components/icons";import { Badge, Button, cx, OverlayBackdrop, portalOverlay, TooltipButton } from "../../components/ui";import { api } from "../../lib/api";+import { ModalLayer } from "../../lib/portal-visibility";import { useModalDialog } from "../../lib/use-modal-dialog";import { useAppStore } from "../../stores/app-store";import { SettingsDestinationContent } from "../settings/SettingsDestinationContent";@@ -44,74 +45,76 @@ export function StartupGuideDialog({};return portalOverlay(- <OverlayBackdrop className="overlay startup-guide-overlay" role="presentation">- <div- ref={dialogRef}- className="dialog startup-guide-dialog"- role="dialog"- aria-modal="true"- aria-labelledby="startup-guide-title"- aria-describedby="startup-guide-description"- data-surface="startup-guide"- data-step={step}- >- <header className="startup-guide-header">- <div className="startup-guide-heading">- {plan.steps.length > 1 ? (- <span className="startup-guide-progress">- {t("startupGuide.progress", { current: index + 1, total: plan.steps.length })}- </span>- ) : null}- <h2 id="startup-guide-title">{t(`startupGuide.${step}.title`)}</h2>- <p id="startup-guide-description">{t(`startupGuide.${step}.description`)}</p>- </div>- <TooltipButton- type="button"- className="icon-btn"- tooltip={t("startupGuide.skip")}- ariaLabel={t("startupGuide.skip")}- onClick={() => onExit("skipped")}- >- <IconClose size={16} />- </TooltipButton>- </header>-- <div className="startup-guide-body">- {step === "model" ? (- <div className="startup-guide-settings settings-content-inner">- <SettingsDestinationContent tab="agent" />+ <ModalLayer>+ <OverlayBackdrop className="overlay startup-guide-overlay" role="presentation">+ <div+ ref={dialogRef}+ className="dialog startup-guide-dialog"+ role="dialog"+ aria-modal="true"+ aria-labelledby="startup-guide-title"+ aria-describedby="startup-guide-description"+ data-surface="startup-guide"+ data-step={step}+ >+ <header className="startup-guide-header">+ <div className="startup-guide-heading">+ {plan.steps.length > 1 ? (+ <span className="startup-guide-progress">+ {t("startupGuide.progress", { current: index + 1, total: plan.steps.length })}+ </span>+ ) : null}+ <h2 id="startup-guide-title">{t(`startupGuide.${step}.title`)}</h2>+ <p id="startup-guide-description">{t(`startupGuide.${step}.description`)}</p></div>- ) : (- <LauncherShortcutStep onOpenShortcuts={() => onExit("openShortcuts")} />- )}- </div>+ <TooltipButton+ type="button"+ className="icon-btn"+ tooltip={t("startupGuide.skip")}+ ariaLabel={t("startupGuide.skip")}+ onClick={() => onExit("skipped")}+ >+ <IconClose size={16} />+ </TooltipButton>+ </header>- <footer className="startup-guide-footer">- {step === "model" ? (- hasUsableModel ? (- <Badge tone="success" className="startup-guide-status">- <IconCheck size={12} aria-hidden />- {t("startupGuide.model.ready")}- </Badge>+ <div className="startup-guide-body">+ {step === "model" ? (+ <div className="startup-guide-settings settings-content-inner">+ <SettingsDestinationContent tab="agent" />+ </div>) : (- <span className="startup-guide-hint">{t("startupGuide.model.pending")}</span>- )- ) : null}- <div className="startup-guide-actions">- <Button variant="ghost" onClick={() => (last ? onExit("skipped") : next())}>- {t("startupGuide.skip")}- </Button>- <Button variant="primary" onClick={next}>- {last- ? t("startupGuide.finish")- : step === "model" && !hasUsableModel- ? t("startupGuide.later")- : t("startupGuide.next")}- </Button>+ <LauncherShortcutStep onOpenShortcuts={() => onExit("openShortcuts")} />+ )}</div>- </footer>- </div>- </OverlayBackdrop>,++ <footer className="startup-guide-footer">+ {step === "model" ? (+ hasUsableModel ? (+ <Badge tone="success" className="startup-guide-status">+ <IconCheck size={12} aria-hidden />+ {t("startupGuide.model.ready")}+ </Badge>+ ) : (+ <span className="startup-guide-hint">{t("startupGuide.model.pending")}</span>+ )+ ) : null}+ <div className="startup-guide-actions">+ <Button variant="ghost" onClick={() => (last ? onExit("skipped") : next())}>+ {t("startupGuide.skip")}+ </Button>+ <Button variant="primary" onClick={next}>+ {last+ ? t("startupGuide.finish")+ : step === "model" && !hasUsableModel+ ? t("startupGuide.later")+ : t("startupGuide.next")}+ </Button>+ </div>+ </footer>+ </div>+ </OverlayBackdrop>+ </ModalLayer>,);}diff --git a/apps/desktop/src/lib/portal-visibility.tsx b/apps/desktop/src/lib/portal-visibility.tsxindex c296e4969..04fda81f0 100644--- a/apps/desktop/src/lib/portal-visibility.tsx+++ b/apps/desktop/src/lib/portal-visibility.tsx@@ -1,8 +1,16 @@-import { createContext, type ReactNode } from "react";+import { createContext, type ReactNode, useState } from "react";import { createPortal } from "react-dom";export const PortalVisibilityContext = createContext(true);+/**+ * Where body-level layers (menus, popovers, sheets) mount. Outside a modal it+ * is `document.body`. Inside a modal on the overlay host (Settings, startup+ * guide) the modal supplies a host stacked above itself; a layer portaled to+ * `document.body` would otherwise paint under the modal and swallow no clicks.+ */+export const PortalLayerContext = createContext<HTMLElement | null>(null);+export function PortalVisibilityProvider({visible,children,@@ -25,8 +33,30 @@ export function visiblePortalContent(node: ReactNode) {);}+/**+ * Mount a floating layer at body level — or, inside a modal, on the modal's+ * layer host so it stays above that modal.+ */export function portalToBody(node: ReactNode) {- return typeof document === "undefined"- ? node- : createPortal(visiblePortalContent(node), document.body);+ if (typeof document === "undefined") return node;+ return (+ <PortalLayerContext.Consumer>+ {(host) => createPortal(visiblePortalContent(node), host ?? document.body)}+ </PortalLayerContext.Consumer>+ );+}++/**+ * Wrap a modal's overlay content. Renders the layer host right after the+ * modal (`.modal-layer-host`, one step above the dialog layer) and points+ * every `portalToBody` call inside the modal at it.+ */+export function ModalLayer({ children }: { children: ReactNode }) {+ const [host, setHost] = useState<HTMLDivElement | null>(null);+ return (+ <>+ <PortalLayerContext.Provider value={host}>{children}</PortalLayerContext.Provider>+ <div ref={setHost} className="modal-layer-host" data-modal-layer-host="" />+ </>+ );}diff --git a/apps/desktop/src/styles/ui-kit.css b/apps/desktop/src/styles/ui-kit.cssindex ac7d85d15..a4ea6f5b3 100644--- a/apps/desktop/src/styles/ui-kit.css+++ b/apps/desktop/src/styles/ui-kit.css@@ -237,6 +237,22 @@animation: overlay-in var(--motion-duration-normal) var(--motion-ease-out) both;}+/*+ Layer host a modal (Settings, startup guide) renders right after itself.+ Menus, popovers and sheets opened inside the modal mount here so they stack+ above it; the empty host never takes pointer input.+*/+.modal-layer-host {+ position: fixed;+ inset: 0;+ z-index: 41;+ pointer-events: none;+}++.modal-layer-host > * {+ pointer-events: auto;+}+.dialog {width: min(100%, 440px);/* Overlay is a flex box; auto min-width would let wide content overflow it. */diff --git a/apps/desktop/test/e2e/trusted-extensions/drive.mjs b/apps/desktop/test/e2e/trusted-extensions/drive.mjsindex 732567bd5..632e1c51d 100644--- a/apps/desktop/test/e2e/trusted-extensions/drive.mjs+++ b/apps/desktop/test/e2e/trusted-extensions/drive.mjs@@ -268,6 +268,17 @@ await ui.until("OAuth account row before sign-out",);await ui.click(`[data-provider-id="${pluginOAuthId}"] button[aria-haspopup="menu"]`);+// The row menu must be on top of the Settings dialog, not painted beneath it:+// a person's pointer has to land on the item itself.+const menuOnTop = await ui.until(() => {+ const item = [...document.querySelectorAll('[role="menuitem"]')].find((entry) => entry.textContent?.includes("Sign out"));+ if (!item) return null;+ const box = item.getBoundingClientRect();+ if (box.width === 0 || box.height === 0) return null;+ const hit = document.elementFromPoint(box.left + box.width / 2, box.top + box.height / 2);+ return { onTop: Boolean(hit && item.contains(hit)), by: hit?.className ?? "nothing" };+}, null, "OAuth row menu item");+check("Settings row menu is clickable above the Settings dialog", menuOnTop.onTop, JSON.stringify(menuOnTop));const armedSignOut = await ui.run(() => {const item = [...document.querySelectorAll('[role="menuitem"]')].find((entry) => entry.textContent?.includes("Sign out"));item?.click();diff --git a/apps/desktop/test/fixed-dropdown-surfaces.test.mjs b/apps/desktop/test/fixed-dropdown-surfaces.test.mjsindex 7f2e8cdc7..d7be7cc09 100644--- a/apps/desktop/test/fixed-dropdown-surfaces.test.mjs+++ b/apps/desktop/test/fixed-dropdown-surfaces.test.mjs@@ -68,8 +68,8 @@ test("renderer-owned dropdown surfaces are fixed and out of layout flow", () =>});test("anchored menus portal, measure, and reposition without reflowing parents", () => {- assert.match(anchoredMenuSource, /createPortal\(/);- assert.match(anchoredMenuSource, /document\.body/);+ // Body level, or the enclosing modal's layer host (portalToBody).+ assert.match(anchoredMenuSource, /portalToBody\(/);assert.match(anchoredMenuSource, /ResizeObserver/);assert.match(anchoredMenuSource, /addEventListener\("scroll", onViewportChange, true\)/);assert.match(anchoredMenuSource, /visibility: hidden/);diff --git a/apps/desktop/test/settings-dialog-layers.test.mjs b/apps/desktop/test/settings-dialog-layers.test.mjsnew file mode 100644index 000000000..74fc99ac2--- /dev/null+++ b/apps/desktop/test/settings-dialog-layers.test.mjs@@ -0,0 +1,52 @@+import assert from "node:assert/strict";+import { readdir, readFile } from "node:fs/promises";+import test from "node:test";+import { loadStyles } from "./helpers/styles.mjs";++const read = (path) => readFile(new URL(path, import.meta.url), "utf8");+const zIndex = (styles, selector) => {+ // The selector may lead or sit inside a selector list.+ const escaped = selector.replace(/\./g, "\\.");+ const block = styles.match(new RegExp(`\\n${escaped}(?:,[^{]*)?\\s*\\{([^}]*)\\}`))?.[1] ?? "";+ return Number(block.match(/z-index:\s*(\d+)/)?.[1]);+};++test("menus opened inside the Settings dialog mount above it", async () => {+ const [portal, settingsDialog, guide, anchoredMenu, styles] = await Promise.all([+ read("../src/lib/portal-visibility.tsx"),+ read("../src/features/settings/SettingsDialog.tsx"),+ read("../src/features/startup/StartupGuideDialog.tsx"),+ read("../src/components/settings/AnchoredMenu.tsx"),+ loadStyles(),+ ]);+ // Body-level layers follow the enclosing modal's layer host.+ assert.match(portal, /createPortal\(visiblePortalContent\(node\), host \?\? document\.body\)/);+ assert.match(portal, /className="modal-layer-host"/);+ // Both modals that embed Settings destinations provide that host.+ for (const dialog of [settingsDialog, guide]) {+ assert.match(dialog, /portalOverlay\(\s*<ModalLayer>/);+ }+ assert.match(anchoredMenu, /portalToBody\(/);+ // The host stacks above the dialog layer and never eats clicks itself.+ const host = zIndex(styles, ".modal-layer-host");+ assert.ok(host > zIndex(styles, ".settings-dialog-overlay"), `host z ${host}`);+ assert.ok(host > zIndex(styles, ".startup-guide-overlay"), `host z ${host}`);+ assert.match(styles, /\.modal-layer-host\s*\{[^}]*pointer-events:\s*none;/);+ assert.match(styles, /\.modal-layer-host > \*\s*\{\s*pointer-events:\s*auto;/);+});++test("nothing reachable from Settings portals straight to document.body", async () => {+ const roots = ["../src/components/settings/", "../src/features/settings/"];+ const files = [+ "../src/components/ProjectInstructionsDialog.tsx",+ "../src/components/ProjectMemoryDialog.tsx",+ ];+ for (const root of roots) {+ for (const entry of await readdir(new URL(root, import.meta.url), { recursive: true })) {+ if (/\.tsx?$/.test(entry)) files.push(root + entry);+ }+ }+ for (const file of files) {+ assert.doesNotMatch(await read(file), /createPortal\([\s\S]*?document\.body\)/, file);+ }+});diff --git a/apps/desktop/test/settings-font-picker.test.mjs b/apps/desktop/test/settings-font-picker.test.mjsindex ed2724830..68be06402 100644--- a/apps/desktop/test/settings-font-picker.test.mjs+++ b/apps/desktop/test/settings-font-picker.test.mjs@@ -9,9 +9,9 @@ const rowSource = await readFile();const styles = await loadStyles();-test("font picker menu portals to the body so the settings card cannot clip it", () => {- assert.match(rowSource, /createPortal\(/);- assert.match(rowSource, /document\.body/);+test("font picker menu portals out of the settings card so it cannot be clipped", () => {+ // Body level, or the enclosing modal's layer host (portalToBody).+ assert.match(rowSource, /portalToBody\(/);assert.match(styles, /\.settings-font-menu\s*\{[^}]*position:\s*fixed;/s);assert.match(styles, /\.settings-font-menu\.is-open\s*\{/);assert.doesNotMatch(diff --git a/docs/spec/04-ux/06-settings-ia.md b/docs/spec/04-ux/06-settings-ia.mdindex f5b8842d2..90f905998 100644--- a/docs/spec/04-ux/06-settings-ia.md+++ b/docs/spec/04-ux/06-settings-ia.md@@ -39,8 +39,12 @@ the page underneath keeps its state.`sidebar-surface` material as the main sidebar. Only a nested settingscontent enter wrapper plays a route animation; the scrolling inner pane,rail, and backing never fade or translate. That entrance is opacity-only.- Dialogs and sheets opened from Settings portal to the same overlay host and- stack above it.+ Dialogs opened from Settings portal to the same overlay host and stack+ above it. Menus, dropdowns, popovers and sheets that would portal to+ `document.body` (`portalToBody`) instead mount on the dialog's layer host+ (`.modal-layer-host`, one step above the dialog layer), so row "…" menus,+ pickers and market sheets stay clickable inside the dialog. The startup+ guide provides the same host for its embedded Models page.- Top of rail: the pill **Search settings…**. The rail has no footer action;the dialog's close button sits at its top-right corner.- The dialog is not a window drag region.