Skip to content

Commit 66e22bd

Browse files
juliusmarmingecodex
andcommitted
Add preview appearance controls for color scheme
- Add system, light, and dark preview appearance options - Persist color scheme across webview swaps and DevTools sessions Co-authored-by: codex <[email protected]>
1 parent 4d83436 commit 66e22bd

12 files changed

Lines changed: 235 additions & 3 deletions

File tree

‎apps/desktop/src/ipc/channels.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export const PREVIEW_ZOOM_IN_CHANNEL = "desktop:preview-zoom-in";
5050
export const PREVIEW_ZOOM_OUT_CHANNEL = "desktop:preview-zoom-out";
5151
export const PREVIEW_RESET_ZOOM_CHANNEL = "desktop:preview-reset-zoom";
5252
export const PREVIEW_HARD_RELOAD_CHANNEL = "desktop:preview-hard-reload";
53+
export const PREVIEW_SET_COLOR_SCHEME_CHANNEL = "desktop:preview-set-color-scheme";
5354
export const PREVIEW_OPEN_DEVTOOLS_CHANNEL = "desktop:preview-open-devtools";
5455
export const PREVIEW_CLEAR_COOKIES_CHANNEL = "desktop:preview-clear-cookies";
5556
export const PREVIEW_CLEAR_CACHE_CHANNEL = "desktop:preview-clear-cache";

‎apps/desktop/src/ipc/methods/preview.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
DesktopPreviewRecordingSaveInputSchema,
1414
DesktopPreviewRegisterWebviewInputSchema,
1515
DesktopPreviewScreenshotArtifactSchema,
16+
DesktopPreviewSetColorSchemeInputSchema,
1617
DesktopPreviewTabInputSchema,
1718
DesktopPreviewWebviewConfigSchema,
1819
PreviewAnnotationPayloadSchema,
@@ -138,6 +139,15 @@ export const hardReload = tabMethod(
138139
"desktop.ipc.preview.hardReload",
139140
(manager, tabId) => manager.hardReload(tabId),
140141
);
142+
export const setColorScheme = DesktopIpc.makeIpcMethod({
143+
channel: IpcChannels.PREVIEW_SET_COLOR_SCHEME_CHANNEL,
144+
payload: DesktopPreviewSetColorSchemeInputSchema,
145+
result: Schema.Void,
146+
handler: Effect.fn("desktop.ipc.preview.setColorScheme")(function* ({ tabId, colorScheme }) {
147+
const manager = yield* PreviewManager.PreviewManager;
148+
yield* manager.setColorScheme(tabId, colorScheme);
149+
}),
150+
});
141151
export const openDevTools = tabMethod(
142152
IpcChannels.PREVIEW_OPEN_DEVTOOLS_CHANNEL,
143153
"desktop.ipc.preview.openDevTools",
@@ -346,6 +356,7 @@ export const methods = [
346356
zoomOut,
347357
resetZoom,
348358
hardReload,
359+
setColorScheme,
349360
openDevTools,
350361
clearCookies,
351362
clearCache,

‎apps/desktop/src/preload.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ contextBridge.exposeInMainWorld("desktopBridge", {
160160
zoomOut: (tabId) => ipcRenderer.invoke(IpcChannels.PREVIEW_ZOOM_OUT_CHANNEL, { tabId }),
161161
resetZoom: (tabId) => ipcRenderer.invoke(IpcChannels.PREVIEW_RESET_ZOOM_CHANNEL, { tabId }),
162162
hardReload: (tabId) => ipcRenderer.invoke(IpcChannels.PREVIEW_HARD_RELOAD_CHANNEL, { tabId }),
163+
setColorScheme: (tabId, colorScheme) =>
164+
ipcRenderer.invoke(IpcChannels.PREVIEW_SET_COLOR_SCHEME_CHANNEL, { tabId, colorScheme }),
163165
openDevTools: (tabId) =>
164166
ipcRenderer.invoke(IpcChannels.PREVIEW_OPEN_DEVTOOLS_CHANNEL, { tabId }),
165167
clearCookies: () => ipcRenderer.invoke(IpcChannels.PREVIEW_CLEAR_COOKIES_CHANNEL),

‎apps/desktop/src/preview/Manager.test.ts‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,79 @@ describe("PreviewManager", () => {
365365
),
366366
);
367367

368+
effectIt.effect("emulates prefers-color-scheme and re-applies it across webview swaps", () =>
369+
withManager((manager) =>
370+
Effect.gen(function* () {
371+
const makeWebContents = (id: number) => {
372+
const sendCommand = vi.fn(async () => undefined);
373+
return {
374+
sendCommand,
375+
wc: {
376+
id,
377+
isDestroyed: () => false,
378+
isDevToolsOpened: () => false,
379+
getType: () => "webview",
380+
getURL: () => "https://example.com",
381+
getTitle: () => "Example",
382+
isLoading: () => false,
383+
getZoomFactor: () => 1,
384+
setZoomFactor: vi.fn(),
385+
on: vi.fn(),
386+
off: vi.fn(),
387+
ipc: { on: vi.fn(), off: vi.fn() },
388+
send: webviewSend,
389+
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
390+
setWindowOpenHandler: vi.fn(),
391+
debugger: {
392+
isAttached: () => false,
393+
attach: vi.fn(),
394+
sendCommand,
395+
on: vi.fn(),
396+
off: vi.fn(),
397+
},
398+
} as never,
399+
};
400+
};
401+
const first = makeWebContents(42);
402+
fromId.mockReturnValue(first.wc);
403+
const states: PreviewManager.PreviewTabState[] = [];
404+
405+
yield* manager.subscribeStateChanges((_tabId, state) =>
406+
Effect.sync(() => {
407+
states.push(state);
408+
}),
409+
);
410+
yield* manager.createTab("tab_scheme");
411+
yield* manager.registerWebview("tab_scheme", 42);
412+
yield* Effect.yieldNow;
413+
414+
yield* manager.setColorScheme("tab_scheme", "dark");
415+
416+
expect(first.sendCommand).toHaveBeenCalledWith("Emulation.setEmulatedMedia", {
417+
features: [{ name: "prefers-color-scheme", value: "dark" }],
418+
});
419+
expect(states.at(-1)?.colorScheme).toBe("dark");
420+
421+
const replacement = makeWebContents(43);
422+
fromId.mockReturnValue(replacement.wc);
423+
yield* manager.registerWebview("tab_scheme", 43);
424+
yield* Effect.yieldNow;
425+
426+
expect(replacement.sendCommand).toHaveBeenCalledWith("Emulation.setEmulatedMedia", {
427+
features: [{ name: "prefers-color-scheme", value: "dark" }],
428+
});
429+
expect(states.at(-1)?.colorScheme).toBe("dark");
430+
431+
yield* manager.setColorScheme("tab_scheme", "system");
432+
433+
expect(replacement.sendCommand).toHaveBeenCalledWith("Emulation.setEmulatedMedia", {
434+
features: [{ name: "prefers-color-scheme", value: "" }],
435+
});
436+
expect(states.at(-1)?.colorScheme).toBe("system");
437+
}),
438+
),
439+
);
440+
368441
effectIt.effect("keeps a main-frame load failure visible until a retry starts", () =>
369442
withManager((manager) =>
370443
Effect.gen(function* () {

‎apps/desktop/src/preview/Manager.ts‎

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
import type {
99
DesktopPreviewAnnotationTheme,
10+
DesktopPreviewColorScheme,
1011
DesktopPreviewPointerEvent,
1112
PreviewAnnotationPayload,
1213
PreviewAnnotationRect,
@@ -84,6 +85,7 @@ export interface PreviewTabState {
8485
canGoBack: boolean;
8586
canGoForward: boolean;
8687
zoomFactor: number;
88+
colorScheme: DesktopPreviewColorScheme;
8789
controller: "human" | "agent" | "none";
8890
updatedAt: string;
8991
}
@@ -1288,6 +1290,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
12881290
canGoBack: false,
12891291
canGoForward: false,
12901292
zoomFactor: DEFAULT_ZOOM_FACTOR,
1293+
colorScheme: "system",
12911294
controller: "none",
12921295
updatedAt,
12931296
};
@@ -1320,6 +1323,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
13201323
canGoBack: false,
13211324
canGoForward: false,
13221325
zoomFactor: DEFAULT_ZOOM_FACTOR,
1326+
colorScheme: "system",
13231327
controller: "none",
13241328
updatedAt,
13251329
};
@@ -1386,7 +1390,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
13861390
wc.getZoomFactor(),
13871391
);
13881392
yield* attachListeners(tabId, wc);
1389-
runFork(ensureControlSession(wc).pipe(Effect.ignore));
1393+
runFork(restoreControlSession(tabId, wc, tab.colorScheme));
13901394
const registeredAt = yield* currentIso;
13911395
const registration = yield* SynchronizedRef.modify(tabsRef, (tabs) => {
13921396
const current = tabs.get(tabId);
@@ -1457,6 +1461,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
14571461
canGoBack: current?.canGoBack ?? false,
14581462
canGoForward: current?.canGoForward ?? false,
14591463
zoomFactor: current?.zoomFactor ?? DEFAULT_ZOOM_FACTOR,
1464+
colorScheme: current?.colorScheme ?? "system",
14601465
controller: current?.controller ?? "none",
14611466
updatedAt,
14621467
};
@@ -1525,7 +1530,14 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
15251530
yield* detachControlSession(wc.id);
15261531
yield* attempt({ operation: "openDevTools", tabId, webContentsId: wc.id }, () => {
15271532
wc.once("devtools-closed", () => {
1528-
if (!wc.isDestroyed()) runFork(ensureControlSession(wc).pipe(Effect.ignore));
1533+
if (wc.isDestroyed()) return;
1534+
runFork(
1535+
SynchronizedRef.get(tabsRef).pipe(
1536+
Effect.flatMap((tabs) =>
1537+
restoreControlSession(tabId, wc, tabs.get(tabId)?.colorScheme ?? "system"),
1538+
),
1539+
),
1540+
);
15291541
});
15301542
wc.openDevTools({ mode: "detach" });
15311543
});
@@ -1684,6 +1696,60 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
16841696
yield* update(tabId, { zoomFactor: next });
16851697
});
16861698

1699+
// Emulated media lives on the CDP debugger session, not the WebContents, so
1700+
// it is lost whenever the session detaches (webview swap, DevTools
1701+
// open/close) and must be re-applied after every (re)attach.
1702+
const applyColorScheme = Effect.fn("PreviewManager.applyColorScheme")(function* (
1703+
tabId: string,
1704+
wc: Electron.WebContents,
1705+
colorScheme: DesktopPreviewColorScheme,
1706+
) {
1707+
yield* ensureControlSession(wc);
1708+
yield* attemptPromise({ operation: "applyColorScheme", tabId, webContentsId: wc.id }, () =>
1709+
wc.debugger.sendCommand("Emulation.setEmulatedMedia", {
1710+
features: [
1711+
{
1712+
name: "prefers-color-scheme",
1713+
// An empty value clears the override so the page follows the OS.
1714+
value: colorScheme === "system" ? "" : colorScheme,
1715+
},
1716+
],
1717+
}),
1718+
);
1719+
});
1720+
1721+
// Re-establish the control session after a detach, restoring any
1722+
// color-scheme override the tab carries.
1723+
const restoreControlSession = (
1724+
tabId: string,
1725+
wc: Electron.WebContents,
1726+
colorScheme: DesktopPreviewColorScheme,
1727+
) =>
1728+
(colorScheme === "system"
1729+
? ensureControlSession(wc)
1730+
: applyColorScheme(tabId, wc, colorScheme)
1731+
).pipe(Effect.ignore);
1732+
1733+
const setColorScheme = Effect.fn("PreviewManager.setColorScheme")(function* (
1734+
tabId: string,
1735+
colorScheme: DesktopPreviewColorScheme,
1736+
) {
1737+
const tab = (yield* SynchronizedRef.get(tabsRef)).get(tabId);
1738+
if (!tab) {
1739+
return yield* new PreviewTabNotFoundError({ tabId });
1740+
}
1741+
if (tab.colorScheme !== colorScheme) {
1742+
// Record the choice even when the CDP call below can't run yet (no
1743+
// webview, DevTools holding the debugger) — it is re-applied on the
1744+
// next control-session (re)attach.
1745+
yield* update(tabId, { colorScheme });
1746+
}
1747+
if (tab.webContentsId == null) return;
1748+
const wc = webContents.fromId(tab.webContentsId);
1749+
if (!wc || wc.isDestroyed()) return;
1750+
yield* applyColorScheme(tabId, wc, colorScheme);
1751+
});
1752+
16871753
const captureScreenshot = Effect.fn("PreviewManager.captureScreenshot")(function* (
16881754
tabId: string,
16891755
) {
@@ -2526,6 +2592,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
25262592
revealArtifact,
25272593
saveRecording,
25282594
setAnnotationTheme,
2595+
setColorScheme,
25292596
setMainWindow,
25302597
startRecording,
25312598
stopRecording,
@@ -2830,6 +2897,10 @@ export class PreviewManager extends Context.Service<
28302897
readonly zoomOut: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
28312898
readonly resetZoom: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
28322899
readonly hardReload: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
2900+
readonly setColorScheme: (
2901+
tabId: string,
2902+
colorScheme: DesktopPreviewColorScheme,
2903+
) => Effect.Effect<void, PreviewManagerError>;
28332904
readonly openDevTools: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
28342905
readonly clearCookies: () => Effect.Effect<void, PreviewManagerError>;
28352906
readonly clearCache: () => Effect.Effect<void, PreviewManagerError>;
@@ -2921,6 +2992,7 @@ export const make = Effect.gen(function* PreviewManagerMake() {
29212992
zoomOut: operations.zoomOut,
29222993
resetZoom: operations.resetZoom,
29232994
hardReload: operations.hardReload,
2995+
setColorScheme: operations.setColorScheme,
29242996
openDevTools: operations.openDevTools,
29252997
clearCookies: Effect.fn("PreviewManager.clearCookies")(function* () {
29262998
yield* browserSession

‎apps/web/src/components/preview/PreviewMoreMenu.tsx‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
11
"use client";
22

3+
import type { DesktopPreviewColorScheme } from "@t3tools/contracts";
34
import { Minus, MoreVertical, Plus as PlusIcon, RotateCcw } from "lucide-react";
45

56
import { Button } from "~/components/ui/button";
6-
import { Menu, MenuItem, MenuPopup, MenuSeparator, MenuTrigger } from "~/components/ui/menu";
7+
import {
8+
Menu,
9+
MenuItem,
10+
MenuPopup,
11+
MenuRadioGroup,
12+
MenuRadioItem,
13+
MenuSeparator,
14+
MenuSub,
15+
MenuSubPopup,
16+
MenuSubTrigger,
17+
MenuTrigger,
18+
} from "~/components/ui/menu";
719
import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
820

921
import { previewBridge } from "./previewBridge";
1022

23+
const COLOR_SCHEME_OPTIONS: ReadonlyArray<{
24+
value: DesktopPreviewColorScheme;
25+
label: string;
26+
}> = [
27+
{ value: "system", label: "System" },
28+
{ value: "light", label: "Light" },
29+
{ value: "dark", label: "Dark" },
30+
];
31+
1132
interface Props {
1233
/** Active preview tab id. Tab-targeting actions are disabled without it. */
1334
tabId: string | null;
@@ -19,6 +40,8 @@ interface Props {
1940
hasWebContents: boolean;
2041
/** Current zoom factor as a number (1.0 = 100%). */
2142
zoomFactor: number;
43+
/** Emulated `prefers-color-scheme` for the guest page. */
44+
colorScheme: DesktopPreviewColorScheme;
2245
/** Fixed viewport modes expose the device toolbar and resize rails. */
2346
deviceToolbarVisible: boolean;
2447
/** Switches between fill-panel mode and a fixed responsive viewport. */
@@ -34,6 +57,7 @@ export function PreviewMoreMenu({
3457
tabId,
3558
hasWebContents,
3659
zoomFactor,
60+
colorScheme,
3761
deviceToolbarVisible,
3862
onToggleDeviceToolbar,
3963
}: Props) {
@@ -72,6 +96,26 @@ export function PreviewMoreMenu({
7296
<MenuItem onClick={onToggleDeviceToolbar} disabled={tabDisabled}>
7397
{deviceToolbarVisible ? "Hide device toolbar" : "Show device toolbar"}
7498
</MenuItem>
99+
<MenuSub>
100+
<MenuSubTrigger disabled={tabDisabled}>Appearance</MenuSubTrigger>
101+
<MenuSubPopup className="min-w-32">
102+
<MenuRadioGroup
103+
value={colorScheme}
104+
onValueChange={(value) => {
105+
if (!tabId) return;
106+
void bridge
107+
.setColorScheme(tabId, value as DesktopPreviewColorScheme)
108+
.catch(() => undefined);
109+
}}
110+
>
111+
{COLOR_SCHEME_OPTIONS.map((option) => (
112+
<MenuRadioItem key={option.value} value={option.value}>
113+
{option.label}
114+
</MenuRadioItem>
115+
))}
116+
</MenuRadioGroup>
117+
</MenuSubPopup>
118+
</MenuSub>
75119
<MenuSeparator />
76120
{/*
77121
Zoom row: label + inline control cluster. `closeOnClick=false`

‎apps/web/src/components/preview/PreviewView.test.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ vi.mock("~/previewStateStore", () => ({
4040
canGoForward: false,
4141
loading: false,
4242
zoomFactor: 1,
43+
colorScheme: "system",
4344
controller: "none",
4445
},
4546
},

‎apps/web/src/components/preview/PreviewView.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ export function PreviewView({ threadRef, tabId: requestedTabId, configuredUrls,
610610
tabId={tabId}
611611
hasWebContents={desktopOverlay !== null}
612612
zoomFactor={desktopOverlay?.zoomFactor ?? 1}
613+
colorScheme={desktopOverlay?.colorScheme ?? "system"}
613614
deviceToolbarVisible={viewport._tag !== "fill"}
614615
onToggleDeviceToolbar={handleToggleDeviceToolbar}
615616
/>

‎apps/web/src/components/preview/usePreviewBridge.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ function projectDesktopState(state: DesktopPreviewTabState): DesktopPreviewOverl
7777
canGoForward: state.canGoForward,
7878
loading: state.navStatus.kind === "Loading",
7979
zoomFactor: state.zoomFactor,
80+
colorScheme: state.colorScheme,
8081
controller: state.controller,
8182
};
8283
}

0 commit comments

Comments
 (0)