Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions apps/backend/test/unit/shared/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ import {
FS_CHANGED,
PTY_DATA,
PTY_EXIT,
BROWSER_PAGE_LOAD,
BROWSER_TITLE_CHANGED,
BROWSER_URL_CHANGE,
BROWSER_WORKSPACE_CHANGE,
BROWSER_DETACHED_CLOSED,
BROWSER_NEW_TAB_REQUESTED,
CHAT_INSERT,
GIT_CLONE_PROGRESS,
Expand All @@ -23,7 +18,6 @@ import {
PtyDataSchema,
ChatInsertSchema,
GitCloneProgressSchema,
BrowserWorkspaceChangeSchema,
// Domain constants
QUERY_RESOURCES,
REQUEST_RESOURCES,
Expand All @@ -40,11 +34,6 @@ describe("shared/events", () => {
FS_CHANGED,
PTY_DATA,
PTY_EXIT,
BROWSER_PAGE_LOAD,
BROWSER_TITLE_CHANGED,
BROWSER_URL_CHANGE,
BROWSER_WORKSPACE_CHANGE,
BROWSER_DETACHED_CLOSED,
BROWSER_NEW_TAB_REQUESTED,
CHAT_INSERT,
GIT_CLONE_PROGRESS,
Expand Down Expand Up @@ -129,15 +118,6 @@ describe("shared/events", () => {
});
expect(result.success).toBe(true);
});

it("BrowserWorkspaceChangeSchema accepts nullish fields", () => {
const result = BrowserWorkspaceChangeSchema.safeParse({
workspaceId: "ws-1",
directoryName: null,
repoName: undefined,
});
expect(result.success).toBe(true);
});
});

describe("schema validation — rejects invalid payloads", () => {
Expand Down
136 changes: 136 additions & 0 deletions apps/desktop/main/browser-emulation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/**
* Main-process helpers for the <webview>-based browser path.
*
* 1. CDP viewport emulation (requires debugger attach — cannot be done
* from executeJavaScript, which runs in the guest page context).
* 2. DevTools open/close. Routing through `webContents` pairs our toolbar
* toggle with an explicit close. Guest DevTools always open detached —
* docked modes (`bottom`/`right`) silently fail because the guest has
* no BrowserWindow to dock into.
*
* Both identify the target by `webContentsId`, which the renderer gets from
* `webview.getWebContentsId()` after the guest page attaches.
*/

import { ipcMain, webContents } from "electron";

const emulatedIds = new Set<number>();

export function registerBrowserEmulationHandlers(): void {
ipcMain.handle(
"browser_webview_emulation_set",
async (
_e,
{
webContentsId,
width,
height,
deviceScaleFactor,
mobile,
scale,
}: {
webContentsId: number;
width: number;
height: number;
deviceScaleFactor: number;
mobile: boolean;
scale?: number;
}
): Promise<{ success: boolean; error?: string }> => {
const wc = webContents.fromId(webContentsId);
if (!wc || wc.isDestroyed()) return { success: false, error: "webContents not found" };

try {
if (!wc.debugger.isAttached()) wc.debugger.attach("1.3");

// Always apply device-metrics override so the page reflows for the
// emulated device (mobile UA + breakpoints kick in from the `mobile`
// flag + width). Separately, a sub-unity `scale` shrinks the rendered
// output so oversized viewports (Desktop 1920×1080 on a narrow panel)
// still fit — that's exactly what webContents.setZoomFactor does.
await wc.debugger.sendCommand("Emulation.setDeviceMetricsOverride", {
width,
height,
deviceScaleFactor,
mobile,
});
wc.setZoomFactor(scale !== undefined && scale < 1 ? scale : 1);

await wc.debugger.sendCommand("Emulation.setTouchEmulationEnabled", {
enabled: mobile,
...(mobile ? { maxTouchPoints: 5 } : {}),
});

emulatedIds.add(webContentsId);
return { success: true };
} catch (err) {
return { success: false, error: String(err) };
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
);

ipcMain.handle(
"browser_webview_emulation_clear",
async (
_e,
{ webContentsId }: { webContentsId: number }
): Promise<{ success: boolean; error?: string }> => {
const wc = webContents.fromId(webContentsId);
if (!wc || wc.isDestroyed()) return { success: false, error: "webContents not found" };

try {
if (wc.debugger.isAttached()) {
await wc.debugger.sendCommand("Emulation.clearDeviceMetricsOverride", {});
await wc.debugger.sendCommand("Emulation.setTouchEmulationEnabled", { enabled: false });
// Detach the debugger so Chromium fully releases emulation state and
// re-runs layout against the webview element's real dimensions. Just
// calling clearDeviceMetricsOverride leaves an active CDP session
// that can retain stale viewport state — the page stays laid-out at
// the previous mobile dims until something else (navigation, zoom
// change) invalidates layout. Detaching is the cleanest signal.
// Next setEmulation call re-attaches (it checks isAttached()).
wc.debugger.detach();
}
wc.setZoomFactor(1);
emulatedIds.delete(webContentsId);
return { success: true };
} catch (err) {
return { success: false, error: String(err) };
}
}
);

ipcMain.handle(
"browser_webview_devtools_open",
(
_e,
{
webContentsId,
mode = "detach",
}: { webContentsId: number; mode?: "right" | "bottom" | "undocked" | "detach" }
): { success: boolean; error?: string } => {
const wc = webContents.fromId(webContentsId);
if (!wc || wc.isDestroyed()) return { success: false, error: "webContents not found" };
try {
wc.openDevTools({ mode });
return { success: true };
} catch (err) {
return { success: false, error: String(err) };
}
}
);

ipcMain.handle(
"browser_webview_devtools_close",
(_e, { webContentsId }: { webContentsId: number }): { success: boolean; error?: string } => {
const wc = webContents.fromId(webContentsId);
if (!wc || wc.isDestroyed()) return { success: false, error: "webContents not found" };
try {
wc.closeDevTools();
return { success: true };
} catch (err) {
return { success: false, error: String(err) };
}
}
);
}
Loading
Loading