Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: draw browser menus, tooltips and toasts over a live native page (#13122)

Michael Hanf7f81ffc69d2
diff --git a/studio/frontend/src/app/provider.tsx b/studio/frontend/src/app/provider.tsx
index 1a6ba13fc90..04b50dbe64c 100644
--- a/studio/frontend/src/app/provider.tsx
+++ b/studio/frontend/src/app/provider.tsx
@@ -515,6 +515,8 @@ function TauriUpdateLayer({
className="pointer-events-none fixed bottom-0 right-0 flex max-h-[calc(100dvh-var(--studio-window-chrome-top,0px))] flex-col items-end gap-2 overflow-y-auto overflow-x-hidden overscroll-contain"
// Measured from the outside, per card, by tests/studio/playwright_update_banner_layout.py.
data-testid="overlay-rail"
+ // Its cards stay clickable over a macOS browser page (native-view.ts).
+ data-native-rail=""
// Gutters in px, never a spacing utility: those are rem, and the cards would drift off the corner.
style={{
paddingTop: STACK_SHADOW_GUTTER_TOP,
diff --git a/studio/frontend/src/components/floating-monitor.tsx b/studio/frontend/src/components/floating-monitor.tsx
index 77cb2a42f3c..00171993674 100644
--- a/studio/frontend/src/components/floating-monitor.tsx
+++ b/studio/frontend/src/components/floating-monitor.tsx
@@ -198,6 +198,8 @@ function FloatingMonitorPanel({
layout ? "top-0 left-0 resize" : "right-0 bottom-0",
)}
data-testid="floating-monitor"
+ // Takes its own clicks over a macOS browser page (native-view.ts).
+ data-native-clickable=""
style={
layout
? {
diff --git a/studio/frontend/src/features/api-monitor/api-monitor-overlay.tsx b/studio/frontend/src/features/api-monitor/api-monitor-overlay.tsx
index dac3e3beb92..6f9c248a789 100644
--- a/studio/frontend/src/features/api-monitor/api-monitor-overlay.tsx
+++ b/studio/frontend/src/features/api-monitor/api-monitor-overlay.tsx
@@ -329,6 +329,8 @@ function ApiMonitorPanel({
{...{ [FIND_PORTAL_ATTRIBUTE]: "" }}
ref={monitorRef}
data-testid="api-monitor-panel"
+ // Takes its own clicks over a macOS browser page (native-view.ts).
+ data-native-clickable=""
onPointerDownCapture={() => raisePanel("api-monitor")}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
diff --git a/studio/frontend/src/features/browser/find.ts b/studio/frontend/src/features/browser/find.ts
index 13e6e6ddf0f..9866a3cec55 100644
--- a/studio/frontend/src/features/browser/find.ts
+++ b/studio/frontend/src/features/browser/find.ts
@@ -9,7 +9,7 @@ import {
notifyFindTargets,
registerFindTarget,
} from "@/features/find-in-page";
-import { hasNativeView, nativeFind } from "./native-view";
+import { focusPage, focusPanel, hasNativeView, nativeFind, refreshCoveredPage } from "./native-view";
import { sendFrameCommand } from "./page-frame";
import { type BrowserTab, currentEntry, useBrowserStore } from "./store";
@@ -40,6 +40,7 @@ function nativeStep(tabId: string, backwards: boolean): void {
void nativeFind(tabId, asked, backwards).then((found) => {
// The native view walks matches without counting them: null keeps the walk open.
if (asked === query && searchedTabId === tabId) setResult({ count: found ? null : 0, active: found ? 0 : -1 });
+ refreshCoveredPage(tabId);
});
}
@@ -80,6 +81,17 @@ export function registerBrowserFind(contains: (node: Node) => boolean): () => vo
else sendFrameCommand(searchedTabId, { command: "findStep", delta });
},
result: () => result,
+ // The app menu's chord while the native page has the keys.
+ takeFocus: () => {
+ const tab = activeTab();
+ if (!searchable(tab) || !hasNativeView(tab.id) || document.hasFocus()) return false;
+ void focusPanel(tab.id);
+ return true;
+ },
+ returnFocus: () => {
+ const tab = activeTab();
+ if (tab) focusPage(tab.id);
+ },
});
// Another tab, or a page turning into a document, moves or drops the search.
let tabId = activeTab()?.id;
diff --git a/studio/frontend/src/features/browser/native-view.ts b/studio/frontend/src/features/browser/native-view.ts
index 0adf9fc989d..7ae4680e9b5 100644
--- a/studio/frontend/src/features/browser/native-view.ts
+++ b/studio/frontend/src/features/browser/native-view.ts
@@ -1,7 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-/** desktop web pages use per-tab native views for bot checks; native views cover the DOM, so overlays use snapshots. */
+/** desktop web pages use per-tab native views for bot checks. macOS puts each page under the app's webview, so
+ * overlays draw over the live page; elsewhere native views cover the DOM, so overlays use snapshots. */
import { useChatRuntimeStore } from "@/features/chat";
import { getLocale, translate } from "@/i18n";
@@ -113,6 +114,7 @@ function closeView(tabId: string): void {
pageEntries.delete(tabId);
loadingPages.delete(tabId);
recency = recency.filter((id) => id !== tabId);
+ if (parkedView === tabId) parkedView = null;
void call("browser_view_close", { tabId }).catch(() => undefined);
}
@@ -215,6 +217,8 @@ function onNativeEvent(event: NativeEvent): void {
page(tab.id).url = event.url;
remember(tab.id, event.url);
if (!event.loading) history.recordVisit(event.url, tab.title, temporary);
+ // A page that loads while parked would otherwise show its first (blank) frame.
+ if (!event.loading) refreshCoveredPage(tab.id);
break;
case "title":
store.updateTab(tab.id, { title: event.title });
@@ -327,6 +331,11 @@ export function nativeAction(tabId: string, action: "back" | "forward" | "reload
void call("browser_view_action", { tabId, action }).catch(() => undefined);
}
+export function focusPage(tabId: string): void {
+ if (!views.has(tabId)) return;
+ void call("browser_view_action", { tabId, action: "focus" }).catch(() => undefined);
+}
+
/** Gives key focus back to the panel's webview. */
export function focusPanel(tabId: string): Promise<void> {
if (!views.has(tabId)) return Promise.resolve();
@@ -359,23 +368,187 @@ export async function nativeFind(tabId: string, query: string, backwards: boolea
return call<boolean>("browser_view_find", { tabId, query, backwards }).catch(() => false);
}
-// exclude upward-opening tooltips to avoid hiding the native page on every hover; overlapping toasts still cover it.
+// Snapshot mode: any of these over the page covers it.
// `data-native-cover`: panel UI over the page, e.g. an annotation comment.
const OVERLAY_SELECTOR =
- '[data-radix-popper-content-wrapper], [role="dialog"], [role="alertdialog"], [data-slot$="-overlay"], [data-sonner-toast], [data-native-cover]';
+ '[data-radix-popper-content-wrapper], [role="dialog"], [role="alertdialog"], [data-slot$="-overlay"], [data-sonner-toast], [data-native-cover], .find-bar-surface';
+// Layered mode: menus and dialogs take all page input, so an outside click closes them.
+const BLOCKING_SELECTOR =
+ '[data-radix-popper-content-wrapper], [role="dialog"], [role="alertdialog"], [data-slot$="-overlay"], [data-native-cover]';
+const MENU_SELECTOR = "[data-radix-popper-content-wrapper]";
+// Layered mode: these take input only within their own rect. Lasting panels and rail cards stay out of
+// snapshot mode, which would freeze the page while they show.
+const CLICKABLE_SELECTOR =
+ "[data-sonner-toast], .find-bar-surface, [data-native-clickable], [data-native-rail] > *";
+
+// Whether pages sit under the app's webview (macOS); null until the backend answers.
+let layered: boolean | null = null;
+let layeredAsked: Promise<boolean> | null = null;
+
+function askLayered(): Promise<boolean> {
+ layeredAsked ??= call<boolean>("browser_view_layered")
+ .catch(() => false)
+ .then((answer) => (layered = answer === true));
+ return layeredAsked;
+}
function intersects(a: DOMRect, b: DOMRect): boolean {
return a.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom;
}
-function covered(rect: DOMRect): boolean {
- for (const element of document.querySelectorAll<HTMLElement>(OVERLAY_SELECTOR)) {
+const TOOLTIP = '[role="tooltip"]';
+// Hover cards close on their own once the pointer leaves, like tooltips.
+const HOVER_ONLY = '[role="tooltip"], [data-slot="hover-card-content"]';
+
+/** Boxes of `selector` over `rect` (anywhere when null), minus those holding `skip`. */
+function overlays(selector: string, rect: DOMRect | null, skip: string | null = TOOLTIP): DOMRect[] {
+ const boxes: DOMRect[] = [];
+ for (const element of document.querySelectorAll<HTMLElement>(selector)) {
if (element.closest("[data-native-page]")) continue;
- if (element.querySelector('[role="tooltip"]')) continue;
+ if (skip && element.querySelector(skip)) continue;
const box = element.getBoundingClientRect();
- if (box.width > 0 && box.height > 0 && intersects(box, rect)) return true;
+ if (box.width > 0 && box.height > 0 && (!rect || intersects(box, rect))) boxes.push(box);
}
- return false;
+ return boxes;
+}
+
+function covering(rect: DOMRect): "none" | "tooltip" | "other" {
+ if (overlays(OVERLAY_SELECTOR, rect).length > 0) return "other";
+ return overlays(OVERLAY_SELECTOR, rect, null).length > 0 ? "tooltip" : "none";
+}
+
+// A page covered only by a tooltip stays parked this long after it closes (or until the pointer
+// reaches the page), so hovering along the toolbar captures once, not per button.
+const TOOLTIP_HOLD_MS = 250;
+let tooltipHold = 0;
+
+function coveredNow(rect: DOMRect): boolean {
+ const cover = covering(rect);
+ if (cover === "tooltip") tooltipHold = performance.now() + TOOLTIP_HOLD_MS;
+ else if (cover === "other") tooltipHold = 0;
+ return cover !== "none" || performance.now() < tooltipHold;
+}
+
+type Input = { blocked: boolean; exclude: Bounds[] };
+
+const NO_INPUT: Input = { blocked: false, exclude: [] };
+
+function panelInput(rect: DOMRect): Input {
+ // A menu away from the page owns it too, so a click on the page closes the menu, as anywhere else.
+ if (overlays(BLOCKING_SELECTOR, rect).length > 0 || overlays(MENU_SELECTOR, null, HOVER_ONLY).length > 0) {
+ return { blocked: true, exclude: [] };
+ }
+ const exclude = overlays(CLICKABLE_SELECTOR, rect).map((box) => ({
+ x: Math.floor(box.left),
+ y: Math.floor(box.top),
+ width: Math.ceil(box.width) + 1,
+ height: Math.ceil(box.height) + 1,
+ viewportWidth: window.innerWidth,
+ }));
+ return { blocked: false, exclude };
+}
+
+// Empty, so the first sync after a reload clears a menu's block the backend kept.
+let sentInput = "";
+
+// Sent directly, not queued, so a menu owns the page as soon as it opens.
+function sendInput(input: Input): void {
+ const key = JSON.stringify(input);
+ if (key === sentInput) return;
+ const wasBlocked = sentInput !== "" && (JSON.parse(sentInput) as Input).blocked;
+ sentInput = key;
+ void call("browser_view_input", input).catch(() => undefined);
+ if (input.blocked !== wasBlocked) lendKeys(input.blocked);
+}
+
+// A menu or dialog opened while the page holds the keys takes them, and returns them on close.
+let lentFrom: { tabId: string; active: Element | null } | null = null;
+
+function lendKeys(blocked: boolean): void {
+ if (blocked) {
+ if (!shownView || document.hasFocus()) return;
+ lentFrom = { tabId: shownView, active: document.activeElement };
+ void focusPanel(shownView);
+ return;
+ }
+ const lent = lentFrom;
+ lentFrom = null;
+ if (!lent) return;
+ requestAnimationFrame(() => {
+ const active = document.activeElement;
+ // Not to a tab switched away from meanwhile: its view is hidden.
+ if (lent.tabId !== shownView) return;
+ if (active === null || active === document.body || active === lent.active) focusPage(lent.tabId);
+ });
+}
+
+const HOLE_ATTRIBUTE = "data-native-hole";
+const HOLE_VARS = ["--native-hole-x", "--native-hole-y", "--native-hole-w", "--native-hole-h"] as const;
+let holed: HTMLElement[] = [];
+let holeSignature = "";
+let holeTab: string | null = null;
+let holeBounds: Bounds | null = null;
+
+function opaque(color: string): boolean {
+ if (color === "transparent") return false;
+ const alpha = /\/\s*([\d.]+)%?\s*\)$/.exec(color) ?? /^rgba\([^)]*,\s*([\d.]+)\s*\)$/.exec(color);
+ return !alpha || Number.parseFloat(alpha[1]) > 0;
+}
+
+function openHole(tabId: string, bounds: Bounds): void {
+ const moved = JSON.stringify(bounds) !== JSON.stringify(holeBounds);
+ holeTab = tabId;
+ holeBounds = bounds;
+ if (!markHole() && moved) placeHole();
+}
+
+// Per holed node, not the root: the vars don't inherit, so a move restyles only those nodes.
+function placeHole(): void {
+ if (!holeBounds) return;
+ const values = [holeBounds.x, holeBounds.y, holeBounds.width, holeBounds.height];
+ for (const node of holed) HOLE_VARS.forEach((name, index) => node.style.setProperty(name, `${values[index]}px`));
+}
+
+/** Re-reads background colors when the chain, its classes or the theme change; true if it did. */
+function markHole(): boolean {
+ const element = holeTab ? placeholder(holeTab) : null;
+ if (!element) return false;
+ const chain: HTMLElement[] = [];
+ for (let node: HTMLElement | null = element; node; node = node.parentElement) chain.push(node);
+ // Theme: html classes, palette and inline color vars, minus our own.
+ const root = document.documentElement;
+ const theme = (root.style.cssText ?? "").replace(/--native-hole-[^;]*;?/g, "");
+ const signature = `${root.dataset.palette}|${theme}|${chain.map((node) => node.className).join("|")}`;
+ if (signature === holeSignature && holed.every((node) => node.isConnected)) return false;
+ unmarkHole();
+ holeSignature = signature;
+ const colors = chain.map((node) => getComputedStyle(node));
+ chain.forEach((node, index) => {
+ const computed = colors[index];
+ if (computed.backgroundImage === "none" && !opaque(computed.backgroundColor)) return;
+ node.style.setProperty("--native-hole-bg", computed.backgroundColor);
+ holed.push(node);
+ });
+ placeHole();
+ for (const node of holed) node.setAttribute(HOLE_ATTRIBUTE, "");
+ // Mid theme switch everything can read transparent: retry next sync.
+ if (holed.length === 0) holeSignature = "";
+ return true;
+}
+
+function unmarkHole(): void {
+ for (const node of holed) {
+ node.removeAttribute(HOLE_ATTRIBUTE);
+ for (const name of ["--native-hole-bg", ...HOLE_VARS]) node.style.removeProperty(name);
+ }
+ holed = [];
+ holeSignature = "";
+}
+
+function closeHole(): void {
+ holeTab = null;
+ holeBounds = null;
+ unmarkHole();
}
function visibleRect(element: HTMLElement): DOMRect | null {
@@ -399,10 +572,8 @@ function visibleRect(element: HTMLElement): DOMRect | null {
return new DOMRect(rect.left, rect.top, rect.width, bottom - rect.top);
}
-type Desired =
- | { tabId: string; url: string; entry: number; zoom: number; bounds: Bounds }
- | { tabId: string; covered: true }
- | null;
+type Shown = { tabId: string; url: string; entry: number; zoom: number; bounds: Bounds };
+type Desired = Shown | (Shown & { covered: true }) | null;
function placeholder(tabId: string): HTMLElement | null {
return document.querySelector<HTMLElement>(`[data-native-page="${CSS.escape(tabId)}"]`);
@@ -425,11 +596,11 @@ function insetToasts(rect: DOMRect | null): void {
function desiredView(): Desired {
const page = pageRect();
- insetToasts(page?.rect ?? null);
+ if (layered) sendInput(page ? panelInput(page.rect) : NO_INPUT);
+ else insetToasts(page?.rect ?? null);
if (!page) return null;
const { tab, entry, rect } = page;
- if (covered(rect)) return { tabId: tab.id, covered: true };
- return {
+ const shown: Shown = {
tabId: tab.id,
url: entry.url,
entry: entryKey(entry),
@@ -442,6 +613,7 @@ function desiredView(): Desired {
viewportWidth: window.innerWidth,
},
};
+ return !layered && coveredNow(rect) ? { ...shown, covered: true } : shown;
}
function pageRect(): { tab: BrowserTab; entry: Extract<BrowserEntry, { kind: "web" }>; rect: DOMRect } | null {
@@ -508,31 +680,122 @@ function clearSnapshot(): void {
snapshot = null;
}
-async function paintSnapshot(tabId: string): Promise<void> {
+let paintToken = 0;
+
+/** True when the placeholder shows a snapshot of `tabId` (a fresh one if `fresh`); only the newest call paints. */
+async function paintSnapshot(tabId: string, fresh = false): Promise<boolean> {
+ const shows = () => !fresh && snapshot?.tabId === tabId;
const bounds = shownBounds;
const element = placeholder(tabId);
- if (shownView !== tabId || !bounds || !element) return;
+ if ((shownView !== tabId && parkedView !== tabId) || !bounds || !element) return shows();
const started = generation;
+ const token = ++paintToken;
+ const current = () => element.isConnected && started === generation && token === paintToken;
const png = await Promise.race([
call<ArrayBuffer>("browser_capture", { tabId }).catch(() => null),
new Promise<null>((resolve) => setTimeout(() => resolve(null), SNAPSHOT_WAIT_MS)),
]);
- if (!png?.byteLength || !element.isConnected || started !== generation) return;
+ if (!png?.byteLength || !current()) return shows();
const url = URL.createObjectURL(new Blob([png], { type: "image/png" }));
+ // Decode before hiding the view, or the placeholder flashes blank.
+ const image = new Image();
+ image.src = url;
+ if (!(await image.decode().then(() => true, () => false)) || !current()) {
+ URL.revokeObjectURL(url);
+ return shows();
+ }
// align snapshots to the visible native bounds because the chat dock can shorten the view.
const box = element.getBoundingClientRect();
element.style.backgroundImage = `url(${url})`;
element.style.backgroundPosition = `${bounds.x - box.left}px ${bounds.y - box.top}px`;
element.style.backgroundSize = `${bounds.width}px ${bounds.height}px`;
element.style.backgroundRepeat = "no-repeat";
+ if (snapshot && snapshot.url !== url) URL.revokeObjectURL(snapshot.url);
snapshot = { tabId, element, url };
+ return true;
+}
+
+// Covered views are parked off-window instead of hidden: a hidden view stops painting and returns blank.
+let parkedView: string | null = null;
+
+function parkable(tabId: string): boolean {
+ return views.has(tabId) && shownBounds !== null && (shownView === tabId || parkedView === tabId);
+}
+
+// A failed capture keeps the page on screen and asks the next sync to try again, a few times.
+const COVER_TRIES = 3;
+let coverFails = 0;
+let coverRetry = 0;
+
+// A parked page whose snapshot missed a change (a zoom), until a capture lands.
+let staleSnapshot: string | null = null;
+
+function retryCover(): void {
+ if (coverFails >= COVER_TRIES) return;
+ coverFails += 1;
+ coverRetry += 1;
+}
+
+async function coverView(tabId: string, zoom: number, bounds: Bounds): Promise<void> {
+ if (parkedView !== tabId) {
+ // Parked without a snapshot, the placeholder would show blank.
+ if (!(await paintSnapshot(tabId))) {
+ retryCover();
+ return;
+ }
+ await call("browser_view_show", { tabId, bounds: shownBounds, parked: true });
+ parkedView = tabId;
+ staleSnapshot = null;
+ coverFails = 0;
+ setShownView(null);
+ }
+ // A resize while covered: park at the new size and capture again, or the snapshot keeps the old frame.
+ if (JSON.stringify(bounds) !== JSON.stringify(shownBounds)) {
+ shownBounds = bounds;
+ viewBounds.set(tabId, bounds);
+ await call("browser_view_show", { tabId, bounds, parked: true });
+ staleSnapshot = tabId;
+ }
+ if ((zooms.get(tabId) ?? 1) !== zoom) {
+ zooms.set(tabId, zoom);
+ await call("browser_view_zoom", { tabId, zoom });
+ staleSnapshot = tabId;
+ }
+ if (staleSnapshot !== tabId) return;
+ if (await paintSnapshot(tabId, true)) {
+ staleSnapshot = null;
+ coverFails = 0;
+ } else retryCover();
+}
+
+/** Re-snapshots a covered page after it changes, e.g. a find step. */
+export function refreshCoveredPage(tabId: string): void {
+ if (parkedView !== tabId) return;
+ staleSnapshot = tabId;
+ void paintSnapshot(tabId, true).then((painted) => {
+ if (!painted) retryCover();
+ else if (staleSnapshot === tabId) {
+ staleSnapshot = null;
+ coverFails = 0;
+ }
+ });
}
async function applyView(desired: Desired): Promise<void> {
if (!desired || "covered" in desired) {
+ closeHole();
// keep the snapshot if an overlay reopens during capture.
if (snapshot?.tabId !== desired?.tabId) clearSnapshot();
- if (desired) await paintSnapshot(desired.tabId);
+ // Another tab under a lasting cover (the find bar): show it first so it can be captured, not left blank.
+ if (desired && !parkable(desired.tabId)) {
+ const { tabId, url, entry, zoom, bounds } = desired;
+ await applyView({ tabId, url, entry, zoom, bounds });
+ }
+ if (desired && parkable(desired.tabId)) {
+ await coverView(desired.tabId, desired.zoom, desired.bounds);
+ return;
+ }
+ parkedView = null;
setShownView(null);
await call("browser_view_show", { tabId: null });
return;
@@ -551,7 +814,12 @@ async function applyView(desired: Desired): Promise<void> {
openedTabs.add(tabId);
await call("browser_view_show", { tabId, url: resumed?.entry === entry ? resumed.url : url, bounds });
if (stale()) return;
+ parkedView = null;
+ staleSnapshot = null;
+ coverFails = 0;
clearSnapshot();
+ // After the move, so hole and page update together.
+ if (layered) openHole(tabId, bounds);
shownBounds = bounds;
viewBounds.set(tabId, bounds);
setShownView(tabId);
@@ -603,6 +871,7 @@ function apply(desired: Desired): void {
let epoch = 0;
onNativeViewsClosed(() => {
for (const tabId of [...views.keys()]) keepReachedPage(tabId);
+ parkedView = null;
setShownView(null);
views.clear();
viewBounds.clear();
@@ -618,6 +887,11 @@ onNativeViewsClosed(() => {
pump();
});
+const MOTION_EVENTS = ["transitionrun", "transitionend", "animationstart", "animationend"] as const;
+// Past Sonner's 400 ms slide.
+const MOTION_MS = 500;
+let motionUntil = 0;
+
export function startNativeViews(): () => void {
listenOnce();
let frame = 0;
@@ -627,6 +901,8 @@ export function startNativeViews(): () => void {
const sync = () => {
frame = 0;
+ if (layered === null) return;
+ markHole();
const desired = desiredView();
pruneViews(desired?.tabId ?? null);
const element = desired ? placeholder(desired.tabId) : null;
@@ -635,7 +911,8 @@ export function startNativeViews(): () => void {
if (element) resizeObserver.observe(element);
resized = element;
}
- const key = JSON.stringify([epoch, desired]);
+ if (performance.now() < motionUntil) schedule();
+ const key = JSON.stringify([epoch, coverRetry, desired]);
if (key === sent) return;
sent = key;
apply(desired);
@@ -647,8 +924,26 @@ export function startNativeViews(): () => void {
const unsubscribe = useBrowserStore.subscribe(schedule);
const overlays = new MutationObserver(schedule);
overlays.observe(document.body, { childList: true });
+ overlays.observe(document.documentElement, { attributes: true, attributeFilter: ["class", "style", "data-palette"] });
window.addEventListener("resize", schedule);
+ // Clickable overlays slide in without a body mutation: measure them every frame while they move.
+ const moved = (event: Event) => {
+ if (!(event.target as Element | null)?.closest?.(CLICKABLE_SELECTOR)) return;
+ motionUntil = performance.now() + MOTION_MS;
+ schedule();
+ };
+ for (const type of MOTION_EVENTS) document.addEventListener(type, moved, true);
+ const reachedPage = (event: Event) => {
+ if (tooltipHold && (event.target as Element | null)?.closest?.("[data-native-page]")) {
+ tooltipHold = 0;
+ schedule();
+ }
+ };
+ document.addEventListener("pointerover", reachedPage, true);
+ // A dragged or resized panel ends where the pointer lets go.
+ document.addEventListener("pointerup", schedule, true);
const interval = window.setInterval(schedule, RECHECK_MS);
+ void askLayered().then(schedule);
schedule();
return () => {
@@ -657,12 +952,19 @@ export function startNativeViews(): () => void {
overlays.disconnect();
resizeObserver.disconnect();
window.removeEventListener("resize", schedule);
+ for (const type of MOTION_EVENTS) document.removeEventListener(type, moved, true);
+ document.removeEventListener("pointerover", reachedPage, true);
+ document.removeEventListener("pointerup", schedule, true);
window.clearInterval(interval);
+ tooltipHold = 0;
// close pages because hidden native views keep scripts and media running.
generation += 1;
pending = null;
+ parkedView = null;
setShownView(null);
clearSnapshot();
+ closeHole();
+ sendInput(NO_INPUT);
insetToasts(null);
for (const tabId of [...views.keys()]) closeView(tabId);
};
diff --git a/studio/frontend/src/features/find-in-page/components/find-in-page.tsx b/studio/frontend/src/features/find-in-page/components/find-in-page.tsx
index ed31b5f07c0..24cc2c18886 100644
--- a/studio/frontend/src/features/find-in-page/components/find-in-page.tsx
+++ b/studio/frontend/src/features/find-in-page/components/find-in-page.tsx
@@ -29,7 +29,7 @@ import {
import { createPortal } from "react-dom";
import { FIND_SCOPE_ATTRIBUTE } from "../lib/find-attributes.ts";
import { isFindScopeBackgrounded } from "../lib/find-backgrounded.ts";
-import { findTarget, findTargetHolding, onFindRequest } from "../lib/find-targets.ts";
+import { type FindTarget, findTarget, findTargetHolding, onFindRequest, takeFindFocus } from "../lib/find-targets.ts";
const DISMISSIBLE_SURFACE_SELECTOR =
'[data-slot="popover-content"], [role="menu"], [role="listbox"]';
@@ -202,15 +202,23 @@ export function FindInPage({ enabled = true }: { enabled?: boolean }) {
[],
);
const originRef = useRef<HTMLElement | null>(null);
+ // A native page lent its keys to the bar: they go back to it, not to the stale origin.
+ const outsideRef = useRef<FindTarget | null>(null);
const requestFocus = useCallback((targetId?: string | null) => {
const active = document.activeElement;
+ // Keys in a native page leave `activeElement` stale: that page is where the reader is.
+ const outside = targetId === undefined ? takeFindFocus() : undefined;
// Searches where the reader is: the page while focus is in the browser, else the chat. Pressed
// again from the bar's own field, it keeps searching what it was.
+ if (outside) outsideRef.current = outside;
+ else if (!(active instanceof Element && active.closest('[role="search"]'))) outsideRef.current = null;
if (targetId !== undefined) setScope(targetId);
+ else if (outside) setScope(outside.id);
else if (!(active instanceof Element && active.closest('[role="search"]')))
setScope(findTargetHolding(active)?.id ?? null);
if (
originRef.current === null &&
+ outsideRef.current === null &&
active instanceof HTMLElement &&
active.closest('[role="search"]') === null
) {
@@ -250,10 +258,17 @@ export function FindInPage({ enabled = true }: { enabled?: boolean }) {
}, []);
const close = useCallback(() => {
const origin = originRef.current;
+ const outside = outsideRef.current;
originRef.current = null;
+ outsideRef.current = null;
setOpen(false);
requestAnimationFrame(() => {
const active = document.activeElement;
+ // The bar's own cleanup may already have focused the stale origin; the page had the keys.
+ if (outside) {
+ outside.returnFocus?.();
+ return;
+ }
if (
origin?.isConnected &&
typeof origin.focus === "function" &&
diff --git a/studio/frontend/src/features/find-in-page/lib/find-targets.ts b/studio/frontend/src/features/find-in-page/lib/find-targets.ts
index dd199b17d7c..893fdf2e623 100644
--- a/studio/frontend/src/features/find-in-page/lib/find-targets.ts
+++ b/studio/frontend/src/features/find-in-page/lib/find-targets.ts
@@ -15,6 +15,10 @@ export type FindTarget = {
search: (query: string) => void;
step: (delta: -1 | 1) => void;
result: () => FindTargetResult;
+ /** When keys are inside it but outside this document (a native page), moves them here: true. */
+ takeFocus?: () => boolean;
+ /** Gives keys back after `takeFocus`, as the bar closes. */
+ returnFocus?: () => void;
};
export const EMPTY_FIND_RESULT: FindTargetResult = { count: 0, active: -1 };
@@ -61,6 +65,11 @@ export function findTargetHolding(node: Node | null): FindTarget | undefined {
return availableFindTargets().find((target) => target.contains(node));
}
+/** The target holding keyboard focus outside this document, which hands it back for the bar. */
+export function takeFindFocus(): FindTarget | undefined {
+ return availableFindTargets().find((target) => target.takeFocus?.() === true);
+}
+
const requests = new Set<(targetId: string | null) => void>();
export function requestFind(targetId: string | null): void {
diff --git a/studio/frontend/src/index.css b/studio/frontend/src/index.css
index 7acfe0cc7ee..ac11cb8a168 100644
--- a/studio/frontend/src/index.css
+++ b/studio/frontend/src/index.css
@@ -6080,6 +6080,52 @@ html:has([data-blocking-screen]) .interface-zoom-position {
cursor: none !important;
}
+/* macOS: backgrounds around a browser page skip its rect (`--native-hole-*`, viewport px), so the
+ native page under the app's webview shows through. Not inherited: moving the hole restyles only
+ the holed nodes. */
+@property --native-hole-x {
+ syntax: "<length>";
+ inherits: false;
+ initial-value: 0px;
+}
+@property --native-hole-y {
+ syntax: "<length>";
+ inherits: false;
+ initial-value: 0px;
+}
+@property --native-hole-w {
+ syntax: "<length>";
+ inherits: false;
+ initial-value: 0px;
+}
+@property --native-hole-h {
+ syntax: "<length>";
+ inherits: false;
+ initial-value: 0px;
+}
+@property --native-hole-bg {
+ syntax: "*";
+ inherits: false;
+}
+
+[data-native-hole] {
+ --native-hole-fill: linear-gradient(var(--native-hole-bg), var(--native-hole-bg));
+ background-color: transparent !important;
+ background-image: var(--native-hole-fill), var(--native-hole-fill), var(--native-hole-fill), var(--native-hole-fill) !important;
+ background-attachment: fixed !important;
+ background-repeat: no-repeat !important;
+ background-position:
+ 0 0,
+ 0 calc(var(--native-hole-y) + var(--native-hole-h)),
+ 0 var(--native-hole-y),
+ calc(var(--native-hole-x) + var(--native-hole-w)) var(--native-hole-y) !important;
+ background-size:
+ 100% var(--native-hole-y),
+ 100% max(0px, calc(100% - var(--native-hole-y) - var(--native-hole-h))),
+ var(--native-hole-x) var(--native-hole-h),
+ max(0px, calc(100% - var(--native-hole-x) - var(--native-hole-w))) var(--native-hole-h) !important;
+}
+
.unsloth-files-panel {
@apply relative mb-2 flex w-full flex-col gap-5 overflow-hidden rounded-[24px] px-4 pt-4 pb-6;
container: files-panel / inline-size;
diff --git a/studio/frontend/tests/browser-native-view-layered.test.ts b/studio/frontend/tests/browser-native-view-layered.test.ts
new file mode 100644
index 00000000000..94d7cd02fbe
--- /dev/null
+++ b/studio/frontend/tests/browser-native-view-layered.test.ts
@@ -0,0 +1,218 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import { register } from "node:module";
+import { test } from "node:test";
+
+// macOS: the page sits under the app's webview, so panel UI draws over it without a snapshot.
+const frames: (() => void)[] = [];
+const mutations: (() => void)[] = [];
+const rect = (x: number, y: number, width: number, height: number) =>
+ ({ left: x, top: y, right: x + width, bottom: y + height, width, height }) as DOMRect;
+type Overlay = { getBoundingClientRect: () => DOMRect; closest: () => null; querySelector: () => object | null };
+const overlay = (box: DOMRect): Overlay => ({
+ getBoundingClientRect: () => box,
+ closest: () => null,
+ querySelector: () => null,
+});
+// overlays by selector: menus and dialogs block, toasts and the find bar take only their rects
+const blocking: Overlay[] = [];
+const clickable: Overlay[] = [];
+
+function node(name: string, background: string, parent: object | null) {
+ const vars = new Map<string, string>();
+ const attributes = new Set<string>();
+ return {
+ name,
+ background,
+ parentElement: parent,
+ className: name,
+ isConnected: true,
+ offsetParent: {},
+ attributes,
+ vars,
+ style: {
+ setProperty: (key: string, value: string) => vars.set(key, value),
+ removeProperty: (key: string) => vars.delete(key),
+ getPropertyValue: (key: string) => vars.get(key) ?? "",
+ },
+ setAttribute: (key: string) => attributes.add(key),
+ removeAttribute: (key: string) => attributes.delete(key),
+ getBoundingClientRect: () => rect(500, 100, 500, 600),
+ closest: () => null,
+ };
+}
+type FakeNode = ReturnType<typeof node>;
+const html = node("html", "rgb(255, 255, 255)", null);
+const section = node("section", "oklch(0.97 0 0)", html);
+const wrapper = node("wrapper", "rgba(0, 0, 0, 0)", section);
+const placeholder = node("placeholder", "oklch(1 0 0 / 1)", wrapper);
+
+const noop = () => undefined;
+const listeners = new Map<string, (event: { target: unknown }) => void>();
+let panelFocused = true;
+Object.assign(globalThis, {
+ window: Object.assign(globalThis, { innerWidth: 1000, addEventListener: noop, removeEventListener: noop }),
+ document: {
+ documentElement: Object.assign(html, { dataset: {} }),
+ body: {},
+ activeElement: null,
+ hasFocus: () => panelFocused,
+ addEventListener: (type: string, listener: (event: { target: unknown }) => void) => listeners.set(type, listener),
+ removeEventListener: (type: string) => listeners.delete(type),
+ querySelector: (selector: string) => (selector.startsWith("[data-native-page") ? placeholder : null),
+ querySelectorAll: (selector: string) =>
+ selector.startsWith("[data-sonner-toast]") ? clickable : selector.startsWith("[data-radix") ? blocking : [],
+ },
+ getComputedStyle: (element: FakeNode) => ({ backgroundColor: element.background, backgroundImage: "none" }),
+ CSS: { escape: (value: string) => value },
+ DOMRect: class {
+ constructor(x: number, y: number, width: number, height: number) {
+ // biome-ignore lint/correctness/noConstructorReturn: a plain rect stands in for DOMRect
+ return rect(x, y, width, height);
+ }
+ },
+ requestAnimationFrame: (callback: () => void) => frames.push(callback),
+ cancelAnimationFrame: noop,
+ ResizeObserver: class {
+ observe() {}
+ unobserve() {}
+ disconnect() {}
+ },
+ MutationObserver: class {
+ constructor(callback: () => void) {
+ mutations.push(callback);
+ }
+ observe() {}
+ disconnect() {}
+ },
+});
+
+type Call = { command: string; args?: Record<string, unknown> };
+const calls: Call[] = [];
+(globalThis as { nativeViewCall?: unknown }).nativeViewCall = (command: string, args?: Record<string, unknown>) => {
+ calls.push({ command, args });
+ return Promise.resolve(command === "browser_view_layered" ? true : undefined);
+};
+
+register("./helpers/browser-store-resolver.mjs", import.meta.url);
+register("./helpers/native-view-resolver.mjs", import.meta.url);
+const { useBrowserStore } = await import("../src/features/browser/store.ts");
+const { startNativeViews } = await import("../src/features/browser/native-view.ts");
+
+const settle = () => new Promise((resolve) => setTimeout(resolve, 5));
+async function frame(): Promise<void> {
+ for (const callback of mutations) callback();
+ for (const callback of frames.splice(0)) callback();
+ await settle();
+}
+const lastInput = () => calls.filter(({ command }) => command === "browser_view_input").at(-1)?.args;
+
+test("a menu over a layered page neither captures nor hides it, and owns its input", async () => {
+ useBrowserStore.getState().openUrl("https://example.com/");
+ const stop = startNativeViews();
+ try {
+ await frame();
+ await frame();
+ const tabId = useBrowserStore.getState().activeTabId;
+ assert.ok(calls.some(({ command, args }) => command === "browser_view_show" && args?.tabId === tabId));
+
+ // every opaque background around the page skips its rect; transparent ones are left alone
+ assert.equal(html.vars.get("--native-hole-x"), "500px");
+ assert.equal(html.vars.get("--native-hole-h"), "600px");
+ assert.deepEqual(
+ [html, section, wrapper, placeholder].map((element) => element.attributes.has("data-native-hole")),
+ [true, true, false, true],
+ );
+ assert.equal(section.vars.get("--native-hole-bg"), "oklch(0.97 0 0)");
+ // the rect goes on each holed node (the vars don't inherit), so moving it restyles only those
+ assert.equal(placeholder.vars.get("--native-hole-y"), "100px");
+ assert.equal(wrapper.vars.has("--native-hole-x"), false);
+
+ // a theme switch recolors them, though the page didn't move; read mid-switch, it tries again
+ html.className = "html dark";
+ for (const element of [html, section, placeholder]) element.background = "rgba(0, 0, 0, 0)";
+ await frame();
+ assert.equal(section.attributes.has("data-native-hole"), false);
+ section.background = "oklch(0.2 0 0)";
+ await frame();
+ assert.equal(section.vars.get("--native-hole-bg"), "oklch(0.2 0 0)");
+
+ const shows = calls.filter(({ command }) => command === "browser_view_show").length;
+ blocking.push(overlay(rect(900, 100, 100, 200)));
+ await frame();
+ assert.deepEqual(lastInput(), { blocked: true, exclude: [] });
+ assert.equal(calls.filter(({ command }) => command === "browser_view_show").length, shows, "the page stays put");
+ assert.ok(!calls.some(({ command }) => command === "browser_capture"), "nothing is captured");
+
+ // zoom from the menu reaches the live page at once
+ if (tabId) useBrowserStore.getState().setZoom(tabId, 1.5);
+ await frame();
+ assert.ok(calls.some(({ command, args }) => command === "browser_view_zoom" && args?.zoom === 1.5));
+
+ // a menu away from the page owns it too, so a click there closes the menu; a hover card doesn't
+ blocking.length = 0;
+ blocking.push(overlay(rect(0, 600, 200, 100)));
+ await frame();
+ assert.deepEqual(lastInput(), { blocked: true, exclude: [] });
+ blocking.length = 0;
+ blocking.push({ ...overlay(rect(0, 600, 200, 100)), querySelector: () => ({}) });
+ await frame();
+ assert.deepEqual(lastInput(), { blocked: false, exclude: [] });
+
+ // a dialog the site raised while its page had the keys takes them, and gives them back on close
+ blocking.length = 0;
+ await frame();
+ panelFocused = false;
+ blocking.push(overlay(rect(600, 200, 300, 200)));
+ await frame();
+ const actions = () => calls.filter(({ command }) => command === "browser_view_action").map(({ args }) => args?.action);
+ assert.deepEqual(actions().slice(-1), ["blur"]);
+ panelFocused = true;
+ blocking.length = 0;
+ await frame();
+ await frame();
+ assert.deepEqual(actions().slice(-1), ["focus"]);
+
+ // switched to another tab meanwhile: the hidden page doesn't get the keys back
+ panelFocused = false;
+ blocking.push(overlay(rect(600, 200, 300, 200)));
+ await frame();
+ useBrowserStore.getState().openUrl("https://example.org/other", { newTab: true });
+ await frame();
+ await frame();
+ panelFocused = true;
+ blocking.length = 0;
+ await frame();
+ await frame();
+ assert.equal(
+ calls.filter(({ command, args }) => command === "browser_view_action" && args?.action === "focus" && args?.tabId === tabId).length,
+ 1,
+ "only the earlier hand-back focused the first tab",
+ );
+
+ // a toast over the page takes input only where it is
+ blocking.length = 0;
+ clickable.push(overlay(rect(600, 600, 200.5, 50)));
+ await frame();
+ assert.deepEqual(lastInput(), {
+ blocked: false,
+ exclude: [{ x: 600, y: 600, width: 202, height: 51, viewportWidth: 1000 }],
+ });
+ // a toast sliding in is measured as it moves, not on the next recheck
+ clickable[0] = overlay(rect(600, 560, 200, 50));
+ listeners.get("transitionrun")?.({ target: { closest: () => ({}) } });
+ for (const callback of frames.splice(0)) callback();
+ await settle();
+ assert.equal((lastInput()?.exclude as { y: number }[])[0].y, 560);
+ assert.ok(frames.length > 0, "and again next frame while it moves");
+ clickable.length = 0;
+ await frame();
+ assert.deepEqual(lastInput(), { blocked: false, exclude: [] });
+ } finally {
+ stop();
+ }
+ assert.equal(placeholder.attributes.has("data-native-hole"), false, "closing the panel fills the hole");
+ assert.equal(html.vars.has("--native-hole-x"), false);
+});
diff --git a/studio/frontend/tests/browser-native-view-snapshot.test.ts b/studio/frontend/tests/browser-native-view-snapshot.test.ts
index 32429bf4637..fe78aac9226 100644
--- a/studio/frontend/tests/browser-native-view-snapshot.test.ts
+++ b/studio/frontend/tests/browser-native-view-snapshot.test.ts
@@ -11,8 +11,9 @@ const mutations: (() => void)[] = [];
const overlays: {
getBoundingClientRect: () => DOMRect;
closest: () => null;
- querySelector: () => null;
+ querySelector: (selector: string) => object | null;
}[] = [];
+const documentListeners = new Map<string, (event: { target: unknown }) => void>();
const rect = (x: number, y: number, width: number, height: number) =>
({
left: x,
@@ -53,6 +54,9 @@ Object.assign(globalThis, {
document: {
documentElement: { style: rootStyle },
body: {},
+ addEventListener: (type: string, listener: (event: { target: unknown }) => void) =>
+ documentListeners.set(type, listener),
+ removeEventListener: (type: string) => documentListeners.delete(type),
querySelector: (selector: string) =>
selector.startsWith("[data-native-page") ? placeholder : null,
querySelectorAll: (selector: string) =>
@@ -65,6 +69,12 @@ Object.assign(globalThis, {
return rect(x, y, width, height);
}
},
+ Image: class {
+ src = "";
+ decode() {
+ return Promise.resolve();
+ }
+ },
requestAnimationFrame: (callback: () => void) => frames.push(callback),
cancelAnimationFrame: noop,
ResizeObserver: class {
@@ -97,7 +107,7 @@ let captureDone: ((bytes: ArrayBuffer) => void) | null = null;
register("./helpers/browser-store-resolver.mjs", import.meta.url);
register("./helpers/native-view-resolver.mjs", import.meta.url);
const { currentEntry, useBrowserStore } = await import("../src/features/browser/store.ts");
-const { startNativeViews } = await import(
+const { refreshCoveredPage, startNativeViews } = await import(
"../src/features/browser/native-view.ts"
);
@@ -118,6 +128,8 @@ test("a menu that closes and reopens while the page is captured keeps the snapsh
useBrowserStore.getState().openUrl("https://example.com/");
const stop = startNativeViews();
try {
+ // the first frame waits for whether pages layer under the app (not here)
+ await frame();
await frame();
const tabId = useBrowserStore.getState().activeTabId;
assert.ok(
@@ -143,10 +155,27 @@ test("a menu that closes and reopens while the page is captured keeps the snapsh
const shows = calls.filter(
({ command }) => command === "browser_view_show",
);
+ assert.deepEqual(
+ [shows.at(-1)?.args?.tabId, shows.at(-1)?.args?.parked],
+ [tabId, true],
+ "the page is parked off the window under the menu, not hidden",
+ );
+
+ // zoom from the menu reaches the parked page, and the snapshot follows it
+ const captures = calls.filter(({ command }) => command === "browser_capture").length;
+ const tab = useBrowserStore.getState().tabs.find((candidate) => candidate.id === tabId);
+ if (tab) useBrowserStore.getState().setZoom(tab.id, 1.25);
+ await frame();
+ assert.ok(
+ calls.some(({ command, args }) => command === "browser_view_zoom" && args?.zoom === 1.25),
+ "the zoom applies while the menu is open",
+ );
+ captureDone?.(new Uint8Array([137, 80, 78, 71]).buffer);
+ await settle();
assert.equal(
- shows.at(-1)?.args?.tabId,
- null,
- "the page stays hidden under the menu",
+ calls.filter(({ command }) => command === "browser_capture").length,
+ captures + 1,
+ "the covered page is captured again at its new zoom",
);
overlays.length = 0;
@@ -161,6 +190,157 @@ test("a menu that closes and reopens while the page is captured keeps the snapsh
}
});
+test("tooltips along the toolbar capture the page once, and it returns when the pointer reaches it", async () => {
+ const stop = startNativeViews();
+ try {
+ useBrowserStore.getState().openUrl("https://example.net/", { newTab: true });
+ await frame();
+ await frame();
+ const tabId = useBrowserStore.getState().activeTabId;
+ const tooltip = (x: number) => ({
+ getBoundingClientRect: () => rect(x, 90, 120, 30),
+ closest: () => null,
+ querySelector: (selector: string) => (selector.includes("tooltip") ? {} : null),
+ });
+ const captures = () => calls.filter(({ command }) => command === "browser_capture").length;
+ const lastShow = () => calls.filter(({ command }) => command === "browser_view_show").at(-1)?.args;
+ const before = captures();
+ overlays.push(tooltip(600));
+ await frame();
+ captureDone?.(new Uint8Array([137, 80, 78, 71]).buffer);
+ await settle();
+ assert.equal(captures(), before + 1);
+ assert.deepEqual([lastShow()?.tabId, lastShow()?.parked], [tabId, true]);
+
+ // the next button's tooltip reuses the snapshot
+ overlays.length = 0;
+ await frame();
+ overlays.push(tooltip(700));
+ await frame();
+ overlays.length = 0;
+ await frame();
+ assert.equal(captures(), before + 1, "no capture per button");
+ assert.equal(lastShow()?.parked, true, "still parked just after the tooltip closes");
+
+ documentListeners.get("pointerover")?.({
+ target: { closest: (selector: string) => (selector === "[data-native-page]" ? {} : null) },
+ });
+ await frame();
+ assert.deepEqual([lastShow()?.tabId, lastShow()?.parked], [tabId, undefined], "the page is back");
+ } finally {
+ stop();
+ overlays.length = 0;
+ }
+});
+
+test("a capture that never lands keeps the page on screen instead of parking it blank, and tries again", async () => {
+ const stop = startNativeViews();
+ try {
+ useBrowserStore.getState().openUrl("https://example.edu/", { newTab: true });
+ await frame();
+ await frame();
+ const tabId = useBrowserStore.getState().activeTabId;
+ const captures = () => calls.filter(({ command }) => command === "browser_capture").length;
+ const parked = () =>
+ calls.some(({ command, args }) => command === "browser_view_show" && args?.tabId === tabId && args?.parked);
+ const before = captures();
+ overlays.push(menu);
+ await frame();
+ await new Promise((resolve) => setTimeout(resolve, 300));
+ assert.equal(parked(), false, "no snapshot, so the page stays where it is");
+ await frame();
+ assert.equal(captures(), before + 2, "the next sync captures again");
+ captureDone?.(new Uint8Array([137, 80, 78, 71]).buffer);
+ await settle();
+ assert.equal(parked(), true, "parked once a snapshot shows");
+
+ // a zoom whose capture misses is captured again, though nothing else changes
+ const zoomed = captures();
+ if (tabId) useBrowserStore.getState().setZoom(tabId, 1.5);
+ await frame();
+ assert.equal(captures(), zoomed + 1);
+ await new Promise((resolve) => setTimeout(resolve, 300));
+ await frame();
+ const retried = captures();
+ assert.ok(retried >= zoomed + 2, "the stale snapshot is retried");
+ await new Promise((resolve) => setTimeout(resolve, 300));
+ await frame();
+ assert.equal(captures(), retried + 1, "and again while captures keep missing");
+ captureDone?.(new Uint8Array([137, 80, 78, 71]).buffer);
+ await settle();
+ await frame();
+ assert.equal(captures(), retried + 1, "and left alone once it lands");
+ } finally {
+ stop();
+ overlays.length = 0;
+ }
+});
+
+test("a tab opened under a lasting cover is shown and captured, not left blank, and find misses retry", async () => {
+ const stop = startNativeViews();
+ try {
+ useBrowserStore.getState().openUrl("https://example.org/a", { newTab: true });
+ await frame();
+ await frame();
+ overlays.push(menu);
+ await frame();
+ captureDone?.(new Uint8Array([137, 80, 78, 71]).buffer);
+ await settle();
+
+ // switch tabs while the cover stays
+ useBrowserStore.getState().openUrl("https://example.org/b", { newTab: true });
+ const next = useBrowserStore.getState().activeTabId;
+ await frame();
+ const shows = () => calls.filter(({ command, args }) => command === "browser_view_show" && args?.tabId === next);
+ assert.ok(shows().some(({ args }) => args?.url && !args?.parked), "the new tab is shown first");
+ captureDone?.(new Uint8Array([137, 80, 78, 71]).buffer);
+ await settle();
+ assert.equal(shows().at(-1)?.args?.parked, true, "then parked under its snapshot");
+ assert.notEqual(calls.filter(({ command }) => command === "browser_view_show").at(-1)?.args?.tabId, null);
+
+ // a find step whose capture misses is captured again
+ const captures = () => calls.filter(({ command }) => command === "browser_capture").length;
+ const before = captures();
+ if (next) refreshCoveredPage(next);
+ assert.equal(captures(), before + 1);
+ await new Promise((resolve) => setTimeout(resolve, 300));
+ await frame();
+ assert.equal(captures(), before + 2, "the missed refresh is retried");
+ captureDone?.(new Uint8Array([137, 80, 78, 71]).buffer);
+ await settle();
+ } finally {
+ stop();
+ overlays.length = 0;
+ }
+});
+
+test("a resize under a lasting cover parks the page at its new size and captures it again", async () => {
+ const stop = startNativeViews();
+ try {
+ useBrowserStore.getState().openUrl("https://example.org/resize", { newTab: true });
+ await frame();
+ await frame();
+ const tabId = useBrowserStore.getState().activeTabId;
+ overlays.push(menu);
+ await frame();
+ captureDone?.(new Uint8Array([137, 80, 78, 71]).buffer);
+ await settle();
+ const captures = () => calls.filter(({ command }) => command === "browser_capture").length;
+ const before = captures();
+ pageBox = rect(500, 100, 450, 600);
+ await frame();
+ const parked = calls.filter(({ command, args }) => command === "browser_view_show" && args?.parked).at(-1)?.args;
+ assert.deepEqual([parked?.tabId, (parked?.bounds as { width: number }).width], [tabId, 450]);
+ assert.equal(captures(), before + 1, "the snapshot is taken again at the new size");
+ captureDone?.(new Uint8Array([137, 80, 78, 71]).buffer);
+ await settle();
+ } finally {
+ stop();
+ overlays.length = 0;
+ pageBox = rect(500, 100, 500, 600);
+ }
+});
+
test("toasts move left of a page that sits beside the Run settings panel", async () => {
const stop = startNativeViews();
try {
diff --git a/studio/frontend/tests/find-targets.test.ts b/studio/frontend/tests/find-targets.test.ts
index d14dbef8ff5..a883c7ded7d 100644
--- a/studio/frontend/tests/find-targets.test.ts
+++ b/studio/frontend/tests/find-targets.test.ts
@@ -13,6 +13,7 @@ import {
registerFindTarget,
requestFind,
subscribeFindTargets,
+ takeFindFocus,
} from "../src/features/find-in-page/lib/find-targets.ts";
const target = (id: string, available: () => boolean, holds: unknown = null): FindTarget => ({
@@ -64,3 +65,22 @@ test("requests reach the bar until it stops listening", () => {
requestFind("browser");
assert.deepEqual(asked, ["browser", null]);
});
+
+test("a target holding keys outside the document hands them back and is searched", () => {
+ let outside = false;
+ let taken = 0;
+ const unregister = registerFindTarget({
+ ...target("page", () => true),
+ takeFocus: () => {
+ if (outside) taken += 1;
+ return outside;
+ },
+ });
+ const chat = registerFindTarget(target("other", () => true));
+ assert.equal(takeFindFocus(), undefined);
+ outside = true;
+ assert.equal(takeFindFocus()?.id, "page");
+ assert.equal(taken, 1);
+ chat();
+ unregister();
+});
diff --git a/studio/src-tauri/Cargo.toml b/studio/src-tauri/Cargo.toml
index 2900ae3f19d..093321ce8ae 100644
--- a/studio/src-tauri/Cargo.toml
+++ b/studio/src-tauri/Cargo.toml
@@ -61,7 +61,7 @@ tokio = { version = "1", features = ["test-util"] }
objc2 = { version = "0.6", features = ["exception"] }
objc2-app-kit = { version = "0.3.2", default-features = false, features = ["std", "NSAppearance", "NSButton", "NSControl", "NSResponder", "NSStatusBarButton", "NSStatusItem", "NSView", "NSBitmapImageRep", "NSImage", "NSImageRep", "NSPrintInfo", "NSPrintOperation", "NSWindow", "NSWorkspace", "NSGraphics"] }
-objc2-foundation = { version = "0.3.2", default-features = false, features = ["std", "NSArray", "NSError", "NSDictionary", "NSKeyValueCoding", "NSKeyValueObserving", "NSObject", "NSProcessInfo", "NSString", "NSURL", "NSURLRequest", "NSURLResponse", "NSData", "NSGeometry"] }
+objc2-foundation = { version = "0.3.2", default-features = false, features = ["std", "NSArray", "NSError", "NSDictionary", "NSKeyValueCoding", "NSValue", "NSKeyValueObserving", "NSObject", "NSProcessInfo", "NSString", "NSURL", "NSURLRequest", "NSURLResponse", "NSData", "NSGeometry"] }
# The browser panel's content rules (browser_webview.rs), snapshots and printing
# (browser_capture.rs) and context menu downloads (browser_context_downloads.rs); wry already
# builds this crate.
diff --git a/studio/src-tauri/src/browser_layer.rs b/studio/src-tauri/src/browser_layer.rs
new file mode 100644
index 00000000000..4c427734dd5
--- /dev/null
+++ b/studio/src-tauri/src/browser_layer.rs
@@ -0,0 +1,318 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+//! macOS: browser pages sit under the app's webview, which is transparent over them, so panel
+//! overlays draw on top of the live page. The app's webview returns nil from `hitTest:` and
+//! `_hitTest:dragTypes:` over the page unless panel UI covers that point (`browser_view_input`).
+
+use crate::browser_webview::ViewBounds;
+use block2::RcBlock;
+use objc2::rc::Retained;
+use objc2::runtime::{AnyClass, AnyObject, Bool, Imp, Sel};
+use objc2::{class, msg_send, sel};
+use objc2_app_kit::{NSEvent, NSView, NSWindowOrderingMode};
+use objc2_foundation::{ns_string, NSNumber, NSObjectNSKeyValueCoding, NSPoint, NSRect, NSSize};
+use std::ptr::null_mut;
+use std::sync::atomic::{AtomicBool, AtomicPtr, AtomicU8, Ordering};
+use std::sync::{Mutex, OnceLock};
+use tauri::{Runtime, Webview};
+
+type HitTest = unsafe extern "C-unwind" fn(*mut AnyObject, Sel, NSPoint) -> *mut AnyObject;
+/// AppKit's private drop target lookup; WKWebView claims its whole frame without `hitTest:`.
+type DragHitTest = unsafe extern "C-unwind" fn(
+ *mut AnyObject,
+ Sel,
+ *mut NSPoint,
+ *mut AnyObject,
+) -> *mut AnyObject;
+
+/// The app's webview, set once on the main thread; it lives as long as the app.
+static MAIN: AtomicPtr<AnyObject> = AtomicPtr::new(null_mut());
+static INSTALLING: AtomicBool = AtomicBool::new(false);
+static HIT_TEST: OnceLock<HitTest> = OnceLock::new();
+static DRAG_HIT_TEST: OnceLock<DragHitTest> = OnceLock::new();
+/// Which webview takes mouse moves (`APP` or `PAGE`, 0 before the first). WebKit's tracking
+/// areas hand every move in the window to both views whatever is on top, so both would set the
+/// cursor and hover in turn, a flicker over links, text and annotate.
+static MOVES_TO: AtomicU8 = AtomicU8::new(0);
+/// The view that just lost moves, cut off on the next one so it sees the pointer leave first.
+static LAGGING: AtomicU8 = AtomicU8::new(0);
+const APP: u8 = 1;
+const PAGE: u8 = 2;
+static INPUT: Mutex<Input> = Mutex::new(Input {
+ blocked: false,
+ exclude: Vec::new(),
+});
+
+/// Panel UI over the page, in the app's CSS pixels.
+struct Input {
+ /// A menu or dialog is open, so the panel takes all page input.
+ blocked: bool,
+ /// `(x, y, width, height, viewport_width)` of clickable UI over the page (toasts, find bar).
+ exclude: Vec<(f64, f64, f64, f64, f64)>,
+}
+
+/// Sets which parts of the page the panel's UI covers; true when `blocked` changed.
+pub fn set_input(blocked: bool, exclude: &[ViewBounds]) -> bool {
+ let mut input = INPUT.lock().unwrap();
+ let changed = input.blocked != blocked;
+ input.blocked = blocked;
+ input.exclude = exclude.iter().map(ViewBounds::parts).collect();
+ changed
+}
+
+/// Gives mouse moves to whichever view owns the pointer now, e.g. after a menu opened. Main thread.
+pub fn reroute_moves() {
+ let Some(main) = main_view() else {
+ return;
+ };
+ let Some(window) = main.window() else {
+ return;
+ };
+ // Safety: AppKit class and window methods, on the main thread.
+ let point: NSPoint = unsafe {
+ let screen: NSPoint = msg_send![class!(NSEvent), mouseLocation];
+ msg_send![&*window, convertPointFromScreen: screen]
+ };
+ route_moves(main, main.convertPoint_fromView(point, None));
+}
+
+fn route_moves(main: &NSView, point: NSPoint) {
+ let to = if over_page(main, point) { PAGE } else { APP };
+ let lagging = LAGGING.swap(0, Ordering::Relaxed);
+ if lagging != 0 && lagging != to {
+ ignore_moves(main, lagging, true);
+ }
+ let from = MOVES_TO.swap(to, Ordering::Relaxed);
+ if from == to {
+ return;
+ }
+ ignore_moves(main, to, false);
+ match from {
+ 0 => ignore_moves(main, APP + PAGE - to, true),
+ _ => LAGGING.store(from, Ordering::Relaxed),
+ }
+}
+
+/// `_setIgnoresMouseMoveEvents:` (private, macOS 13+, checked first) on the app or every page.
+fn ignore_moves(main: &NSView, which: u8, ignore: bool) {
+ let set = |view: &NSView| {
+ // Safety: a live view of wry's class, on the main thread.
+ unsafe {
+ let responds: bool =
+ msg_send![view, respondsToSelector: sel!(_setIgnoresMouseMoveEvents:)];
+ if responds {
+ let _: () = msg_send![view, _setIgnoresMouseMoveEvents: Bool::new(ignore)];
+ }
+ }
+ };
+ if which == APP {
+ set(main);
+ return;
+ }
+ // Safety: the main thread's view tree.
+ let Some(parent) = (unsafe { main.superview() }) else {
+ return;
+ };
+ for view in parent.subviews().iter() {
+ if !std::ptr::eq(&*view, main) && std::ptr::eq(view.class(), main.class()) {
+ set(&view);
+ }
+ }
+}
+
+/// Makes the app's webview transparent and routes input. Runs once, before any page exists.
+pub fn install<R: Runtime>(main: &Webview<R>) {
+ if INSTALLING.swap(true, Ordering::SeqCst) {
+ return;
+ }
+ let _ = main.with_webview(|platform| {
+ let view = platform.inner() as *mut AnyObject;
+ // Safety: wry's live WKWebView, on the main thread.
+ unsafe { install_on(view) };
+ });
+}
+
+pub fn place<R: Runtime>(page: &Webview<R>) {
+ let _ = page.with_webview(|platform| {
+ let main = MAIN.load(Ordering::Acquire);
+ if main.is_null() {
+ return;
+ }
+ // Safety: both are live NSViews, used on the main thread.
+ unsafe {
+ let main = &*(main as *const NSView);
+ let page = &*(platform.inner() as *const NSView);
+ if let Some(parent) = main.superview() {
+ if page.superview().as_deref() == Some(&*parent) {
+ parent.addSubview_positioned_relativeTo(
+ page,
+ NSWindowOrderingMode::Below,
+ Some(main),
+ );
+ MOVES_TO.store(0, Ordering::Relaxed);
+ reroute_moves();
+ }
+ }
+ }
+ });
+}
+
+unsafe fn install_on(view: *mut AnyObject) {
+ let Some(class) = (unsafe { view.as_ref() }).map(AnyObject::class) else {
+ return;
+ };
+ // Pages share wry's class, so the overrides check for the main view by pointer.
+ let (Some(hit_test), Some(drag_hit_test)) = (
+ class.instance_method(sel!(hitTest:)),
+ class.instance_method(sel!(_hitTest:dragTypes:)),
+ ) else {
+ log::warn!("browser pages stay over the panel: the webview class can't be routed");
+ return;
+ };
+ // Safety: same signatures; the originals are stored before the swap can call them.
+ unsafe {
+ let _ = HIT_TEST.set(std::mem::transmute::<Imp, HitTest>(
+ hit_test.implementation(),
+ ));
+ let _ = DRAG_HIT_TEST.set(std::mem::transmute::<Imp, DragHitTest>(
+ drag_hit_test.implementation(),
+ ));
+ let class = (class as *const AnyClass).cast_mut();
+ objc2::ffi::class_replaceMethod(
+ class,
+ sel!(hitTest:),
+ std::mem::transmute::<HitTest, Imp>(routed_hit_test),
+ objc2::ffi::method_getTypeEncoding(hit_test),
+ );
+ objc2::ffi::class_replaceMethod(
+ class,
+ sel!(_hitTest:dragTypes:),
+ std::mem::transmute::<DragHitTest, Imp>(routed_drag_hit_test),
+ objc2::ffi::method_getTypeEncoding(drag_hit_test),
+ );
+ // Private KVC key, as used by wry's `transparent` option.
+ let main = &*(view as *const NSView);
+ main.setValue_forKey(
+ Some(&NSNumber::numberWithBool(false)),
+ ns_string!("drawsBackground"),
+ );
+ }
+ MAIN.store(view, Ordering::Release);
+ // Before dispatch, so the view losing moves is cut off at the move that leaves it.
+ let route = RcBlock::new(|event: *mut AnyObject| -> *mut AnyObject {
+ // Safety: AppKit passes a live NSEvent to the monitor, on the main thread.
+ if let (Some(main), Some(moved)) =
+ (main_view(), unsafe { (event as *const NSEvent).as_ref() })
+ {
+ if main.window().map(|window| window.windowNumber()) == Some(moved.windowNumber()) {
+ route_moves(
+ main,
+ main.convertPoint_fromView(moved.locationInWindow(), None),
+ );
+ }
+ }
+ event
+ });
+ // NSEventMaskMouseMoved. Kept for the app's lifetime, like the main webview.
+ let mask: u64 = 1 << 5;
+ // Safety: a class method taking a mask and an `NSEvent *(^)(NSEvent *)` block.
+ let monitor: Option<Retained<AnyObject>> = unsafe {
+ msg_send![class!(NSEvent), addLocalMonitorForEventsMatchingMask: mask, handler: &*route]
+ };
+ std::mem::forget(monitor);
+}
+
+/// Whether `point` (main view coordinates) belongs to the page.
+fn over_page(main: &NSView, point: NSPoint) -> bool {
+ let input = INPUT.lock().unwrap();
+ if input.blocked {
+ return false;
+ }
+ // Safety: the main thread's view tree.
+ let Some(parent) = (unsafe { main.superview() }) else {
+ return false;
+ };
+ let class = main.class();
+ let page = parent.subviews().iter().any(|view| {
+ !std::ptr::eq(&*view, main)
+ && std::ptr::eq(view.class(), class)
+ && !view.isHidden()
+ && point_in(
+ main.convertRect_fromView(view.frame(), Some(&parent)),
+ point,
+ )
+ });
+ if !page {
+ return false;
+ }
+ // The main view is flipped: points are CSS px times the app zoom.
+ let width = main.frame().size.width;
+ !input.exclude.iter().any(|&(x, y, w, h, viewport)| {
+ let scale = if viewport > 0.0 {
+ width / viewport
+ } else {
+ 1.0
+ };
+ point_in(
+ NSRect::new(
+ NSPoint::new(x * scale, y * scale),
+ NSSize::new(w * scale, h * scale),
+ ),
+ point,
+ )
+ })
+}
+
+fn point_in(rect: NSRect, point: NSPoint) -> bool {
+ point.x >= rect.origin.x
+ && point.y >= rect.origin.y
+ && point.x < rect.origin.x + rect.size.width
+ && point.y < rect.origin.y + rect.size.height
+}
+
+fn main_view() -> Option<&'static NSView> {
+ // Safety: set once to the app's webview, which outlives every event.
+ unsafe { (MAIN.load(Ordering::Acquire) as *const NSView).as_ref() }
+}
+
+unsafe extern "C-unwind" fn routed_hit_test(
+ this: *mut AnyObject,
+ cmd: Sel,
+ point: NSPoint,
+) -> *mut AnyObject {
+ if let Some(main) = main_view().filter(|main| std::ptr::eq(*main as *const NSView, this.cast()))
+ {
+ // `point` is in the superview's coordinates.
+ // Safety: the main thread's view tree.
+ let local = main.convertPoint_fromView(point, unsafe { main.superview() }.as_deref());
+ if over_page(main, local) {
+ return null_mut();
+ }
+ }
+ let original = HIT_TEST.get().expect("installed before routing");
+ // Safety: AppKit's arguments, forwarded to the original implementation.
+ unsafe { original(this, cmd, point) }
+}
+
+/// Files dragged over the page drop on it, as before it sat under the app's webview.
+unsafe extern "C-unwind" fn routed_drag_hit_test(
+ this: *mut AnyObject,
+ cmd: Sel,
+ point: *mut NSPoint,
+ types: *mut AnyObject,
+) -> *mut AnyObject {
+ if let Some(main) = main_view().filter(|main| std::ptr::eq(*main as *const NSView, this.cast()))
+ {
+ // Safety: AppKit passes a live point in the superview's coordinates, on the main thread.
+ if let Some(at) = unsafe { point.as_ref() } {
+ let local = main.convertPoint_fromView(*at, unsafe { main.superview() }.as_deref());
+ if over_page(main, local) {
+ return null_mut();
+ }
+ }
+ }
+ let original = DRAG_HIT_TEST.get().expect("installed before routing");
+ // Safety: AppKit's arguments, forwarded to the original implementation.
+ unsafe { original(this, cmd, point, types) }
+}
diff --git a/studio/src-tauri/src/browser_webview.rs b/studio/src-tauri/src/browser_webview.rs
index ed5f9634fa8..2e06882ee3c 100644
--- a/studio/src-tauri/src/browser_webview.rs
+++ b/studio/src-tauri/src/browser_webview.rs
@@ -26,6 +26,8 @@ const EVENT: &str = "unsloth-browser";
const MAIN_WEBVIEW: &str = "main";
const MAIN_WINDOW: &str = "main";
const URL_POLL: Duration = Duration::from_millis(800);
+/// How far left of the window a parked page sits, in logical pixels.
+const PARK_GAP: f64 = 64.0;
/// macOS 14+ data store for pages (fixed, so it persists).
#[cfg(target_os = "macos")]
const PAGE_DATA_STORE: [u8; 16] = *b"unsloth-browser1";
@@ -1214,6 +1216,8 @@ fn create_view<R: Runtime>(
.add_child(builder, position, size)
.map_err(|error| error.to_string())?;
#[cfg(target_os = "macos")]
+ crate::browser_layer::place(&webview);
+ #[cfg(target_os = "macos")]
crate::browser_context_downloads::watch(&webview, &tab);
// A muted tab whose view closed (four at most stay open) opens muted again.
if app.state::<BrowserViews>().inner.lock().unwrap().muted.contains(&tab) {
@@ -1362,7 +1366,41 @@ pub fn browser_view_supported() -> bool {
false
}
+/// Whether pages sit under the app's webview (`browser_layer`). Elsewhere covered pages are snapshotted.
+/// Asked once per load of the app, so it also drops input state a reload left behind.
+#[tauri::command]
+pub fn browser_view_layered<R: Runtime>(webview: Webview<R>) -> Result<bool, String> {
+ require_main(&webview)?;
+ #[cfg(target_os = "macos")]
+ {
+ if crate::browser_layer::set_input(false, &[]) {
+ let _ = webview.run_on_main_thread(crate::browser_layer::reroute_moves);
+ }
+ Ok(browser_view_supported())
+ }
+ #[cfg(not(target_os = "macos"))]
+ Ok(false)
+}
+
+/// The panel UI over the page: all of it while a menu or dialog is open, else clickable rects.
+#[tauri::command]
+pub fn browser_view_input<R: Runtime>(
+ webview: Webview<R>,
+ blocked: bool,
+ exclude: Vec<ViewBounds>,
+) -> Result<(), String> {
+ require_main(&webview)?;
+ #[cfg(target_os = "macos")]
+ if crate::browser_layer::set_input(blocked, &exclude) {
+ let _ = webview.run_on_main_thread(crate::browser_layer::reroute_moves);
+ }
+ #[cfg(not(target_os = "macos"))]
+ let _ = (blocked, exclude);
+ Ok(())
+}
+
/// Show a tab's page at `bounds` (created at `url` first time), hide the rest; `None` hides all.
+/// `parked` moves the page just off-window instead of hiding it, so it keeps painting under its snapshot.
/// Async: creating a webview from a sync command deadlocks on Windows (Tauri known issue).
#[tauri::command]
pub async fn browser_view_show<R: Runtime>(
@@ -1371,13 +1409,18 @@ pub async fn browser_view_show<R: Runtime>(
tab_id: Option<String>,
url: Option<String>,
bounds: Option<ViewBounds>,
+ parked: Option<bool>,
) -> Result<(), String> {
require_main(&webview)?;
let app = webview.app_handle().clone();
+ let parked = parked.unwrap_or(false);
let target = match (&tab_id, &bounds) {
(Some(tab_id), Some(bounds)) => Some(match app.get_webview(&label_for(tab_id)?) {
Some(view) => {
- let (position, size) = logical_rect(&webview, bounds);
+ let (mut position, size) = logical_rect(&webview, bounds);
+ if parked {
+ position.x = -size.width - PARK_GAP;
+ }
view.set_bounds(Rect {
position: position.into(),
size: size.into(),
@@ -1385,8 +1428,11 @@ pub async fn browser_view_show<R: Runtime>(
.map_err(|error| error.to_string())?;
view
}
+ None if parked => return Err("no page to park".into()),
None => {
let url = parse_page_url(url.as_deref().ok_or("no address to open")?)?;
+ #[cfg(target_os = "macos")]
+ crate::browser_layer::install(&webview);
create_view(&webview, tab_id, url, bounds)?
}
}),
diff --git a/studio/src-tauri/src/main.rs b/studio/src-tauri/src/main.rs
index 3781ed36452..53ea34effbc 100644
--- a/studio/src-tauri/src/main.rs
+++ b/studio/src-tauri/src/main.rs
@@ -6,6 +6,8 @@ mod browser_capture;
#[cfg(target_os = "macos")]
mod browser_context_downloads;
mod browser_downloads;
+#[cfg(target_os = "macos")]
+mod browser_layer;
mod browser_open_with;
mod browser_proxy;
mod browser_webview;
@@ -2258,6 +2260,8 @@ fn main() {
browser_webview::browser_view_close,
browser_webview::browser_view_clear_data,
browser_webview::browser_view_mute,
+ browser_webview::browser_view_layered,
+ browser_webview::browser_view_input,
browser_capture::browser_capture,
browser_downloads::browser_download_save,
browser_downloads::browser_download_reveal,