Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions apps/desktop/src/ipc/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const PREVIEW_ZOOM_IN_CHANNEL = "desktop:preview-zoom-in";
export const PREVIEW_ZOOM_OUT_CHANNEL = "desktop:preview-zoom-out";
export const PREVIEW_RESET_ZOOM_CHANNEL = "desktop:preview-reset-zoom";
export const PREVIEW_HARD_RELOAD_CHANNEL = "desktop:preview-hard-reload";
export const PREVIEW_SET_COLOR_SCHEME_CHANNEL = "desktop:preview-set-color-scheme";
export const PREVIEW_OPEN_DEVTOOLS_CHANNEL = "desktop:preview-open-devtools";
export const PREVIEW_CLEAR_COOKIES_CHANNEL = "desktop:preview-clear-cookies";
export const PREVIEW_CLEAR_CACHE_CHANNEL = "desktop:preview-clear-cache";
Expand Down
11 changes: 11 additions & 0 deletions apps/desktop/src/ipc/methods/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
DesktopPreviewRecordingSaveInputSchema,
DesktopPreviewRegisterWebviewInputSchema,
DesktopPreviewScreenshotArtifactSchema,
DesktopPreviewSetColorSchemeInputSchema,
DesktopPreviewTabInputSchema,
DesktopPreviewWebviewConfigSchema,
PreviewAnnotationPayloadSchema,
Expand Down Expand Up @@ -138,6 +139,15 @@ export const hardReload = tabMethod(
"desktop.ipc.preview.hardReload",
(manager, tabId) => manager.hardReload(tabId),
);
export const setColorScheme = DesktopIpc.makeIpcMethod({
channel: IpcChannels.PREVIEW_SET_COLOR_SCHEME_CHANNEL,
payload: DesktopPreviewSetColorSchemeInputSchema,
result: Schema.Void,
handler: Effect.fn("desktop.ipc.preview.setColorScheme")(function* ({ tabId, colorScheme }) {
const manager = yield* PreviewManager.PreviewManager;
yield* manager.setColorScheme(tabId, colorScheme);
}),
});
export const openDevTools = tabMethod(
IpcChannels.PREVIEW_OPEN_DEVTOOLS_CHANNEL,
"desktop.ipc.preview.openDevTools",
Expand Down Expand Up @@ -346,6 +356,7 @@ export const methods = [
zoomOut,
resetZoom,
hardReload,
setColorScheme,
openDevTools,
clearCookies,
clearCache,
Expand Down
2 changes: 2 additions & 0 deletions apps/desktop/src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ contextBridge.exposeInMainWorld("desktopBridge", {
zoomOut: (tabId) => ipcRenderer.invoke(IpcChannels.PREVIEW_ZOOM_OUT_CHANNEL, { tabId }),
resetZoom: (tabId) => ipcRenderer.invoke(IpcChannels.PREVIEW_RESET_ZOOM_CHANNEL, { tabId }),
hardReload: (tabId) => ipcRenderer.invoke(IpcChannels.PREVIEW_HARD_RELOAD_CHANNEL, { tabId }),
setColorScheme: (tabId, colorScheme) =>
ipcRenderer.invoke(IpcChannels.PREVIEW_SET_COLOR_SCHEME_CHANNEL, { tabId, colorScheme }),
openDevTools: (tabId) =>
ipcRenderer.invoke(IpcChannels.PREVIEW_OPEN_DEVTOOLS_CHANNEL, { tabId }),
clearCookies: () => ipcRenderer.invoke(IpcChannels.PREVIEW_CLEAR_COOKIES_CHANNEL),
Expand Down
73 changes: 73 additions & 0 deletions apps/desktop/src/preview/Manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,79 @@ describe("PreviewManager", () => {
),
);

effectIt.effect("emulates prefers-color-scheme and re-applies it across webview swaps", () =>
withManager((manager) =>
Effect.gen(function* () {
const makeWebContents = (id: number) => {
const sendCommand = vi.fn(async () => undefined);
return {
sendCommand,
wc: {
id,
isDestroyed: () => false,
isDevToolsOpened: () => false,
getType: () => "webview",
getURL: () => "https://example.com",
getTitle: () => "Example",
isLoading: () => false,
getZoomFactor: () => 1,
setZoomFactor: vi.fn(),
on: vi.fn(),
off: vi.fn(),
ipc: { on: vi.fn(), off: vi.fn() },
send: webviewSend,
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
setWindowOpenHandler: vi.fn(),
debugger: {
isAttached: () => false,
attach: vi.fn(),
sendCommand,
on: vi.fn(),
off: vi.fn(),
},
} as never,
};
};
const first = makeWebContents(42);
fromId.mockReturnValue(first.wc);
const states: PreviewManager.PreviewTabState[] = [];

yield* manager.subscribeStateChanges((_tabId, state) =>
Effect.sync(() => {
states.push(state);
}),
);
yield* manager.createTab("tab_scheme");
yield* manager.registerWebview("tab_scheme", 42);
yield* Effect.yieldNow;

yield* manager.setColorScheme("tab_scheme", "dark");

expect(first.sendCommand).toHaveBeenCalledWith("Emulation.setEmulatedMedia", {
features: [{ name: "prefers-color-scheme", value: "dark" }],
});
expect(states.at(-1)?.colorScheme).toBe("dark");

const replacement = makeWebContents(43);
fromId.mockReturnValue(replacement.wc);
yield* manager.registerWebview("tab_scheme", 43);
yield* Effect.yieldNow;

expect(replacement.sendCommand).toHaveBeenCalledWith("Emulation.setEmulatedMedia", {
features: [{ name: "prefers-color-scheme", value: "dark" }],
});
expect(states.at(-1)?.colorScheme).toBe("dark");

yield* manager.setColorScheme("tab_scheme", "system");

expect(replacement.sendCommand).toHaveBeenCalledWith("Emulation.setEmulatedMedia", {
features: [{ name: "prefers-color-scheme", value: "" }],
});
expect(states.at(-1)?.colorScheme).toBe("system");
}),
),
);

effectIt.effect("keeps a main-frame load failure visible until a retry starts", () =>
withManager((manager) =>
Effect.gen(function* () {
Expand Down
74 changes: 72 additions & 2 deletions apps/desktop/src/preview/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
import type {
DesktopPreviewAnnotationTheme,
DesktopPreviewColorScheme,
DesktopPreviewPointerEvent,
PreviewAnnotationPayload,
PreviewAnnotationRect,
Expand Down Expand Up @@ -84,6 +85,7 @@ export interface PreviewTabState {
canGoBack: boolean;
canGoForward: boolean;
zoomFactor: number;
colorScheme: DesktopPreviewColorScheme;
controller: "human" | "agent" | "none";
updatedAt: string;
}
Expand Down Expand Up @@ -1288,6 +1290,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
canGoBack: false,
canGoForward: false,
zoomFactor: DEFAULT_ZOOM_FACTOR,
colorScheme: "system",
controller: "none",
updatedAt,
};
Expand Down Expand Up @@ -1320,6 +1323,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
canGoBack: false,
canGoForward: false,
zoomFactor: DEFAULT_ZOOM_FACTOR,
colorScheme: "system",
controller: "none",
updatedAt,
};
Expand Down Expand Up @@ -1386,7 +1390,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
wc.getZoomFactor(),
);
yield* attachListeners(tabId, wc);
runFork(ensureControlSession(wc).pipe(Effect.ignore));
runFork(restoreControlSession(tabId, wc));
const registeredAt = yield* currentIso;
const registration = yield* SynchronizedRef.modify(tabsRef, (tabs) => {
const current = tabs.get(tabId);
Expand Down Expand Up @@ -1457,6 +1461,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
canGoBack: current?.canGoBack ?? false,
canGoForward: current?.canGoForward ?? false,
zoomFactor: current?.zoomFactor ?? DEFAULT_ZOOM_FACTOR,
colorScheme: current?.colorScheme ?? "system",
controller: current?.controller ?? "none",
updatedAt,
};
Expand Down Expand Up @@ -1525,7 +1530,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
yield* detachControlSession(wc.id);
yield* attempt({ operation: "openDevTools", tabId, webContentsId: wc.id }, () => {
wc.once("devtools-closed", () => {
if (!wc.isDestroyed()) runFork(ensureControlSession(wc).pipe(Effect.ignore));
if (!wc.isDestroyed()) runFork(restoreControlSession(tabId, wc));
});
wc.openDevTools({ mode: "detach" });
});
Expand Down Expand Up @@ -1684,6 +1689,65 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
yield* update(tabId, { zoomFactor: next });
});

// Emulated media lives on the CDP debugger session, not the WebContents, so
// it is lost whenever the session detaches (webview swap, DevTools
// open/close) and must be re-applied after every (re)attach.
const applyColorScheme = Effect.fn("PreviewManager.applyColorScheme")(function* (
tabId: string,
wc: Electron.WebContents,
colorScheme: DesktopPreviewColorScheme,
) {
yield* ensureControlSession(wc);
yield* attemptPromise({ operation: "applyColorScheme", tabId, webContentsId: wc.id }, () =>
wc.debugger.sendCommand("Emulation.setEmulatedMedia", {
features: [
{
name: "prefers-color-scheme",
// An empty value clears the override so the page follows the OS.
value: colorScheme === "system" ? "" : colorScheme,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Clear the emulated feature when selecting System

After a user switches from Light or Dark to System, sending a prefers-color-scheme feature with an empty value does not remove that feature override; it emulates an invalid/no-preference value, so the page may match neither its light nor dark query instead of following the OS. Clear the override by sending an empty features array for System.

Useful? React with 👍 / 👎.

},
],
}),
);
});

// Re-establish the control session after a detach, restoring any
// color-scheme override the tab carries. The scheme is read after the
// session attaches so a concurrent setColorScheme is not overwritten with
// a stale snapshot.
const restoreControlSession = (tabId: string, wc: Electron.WebContents) =>
ensureControlSession(wc).pipe(
Effect.andThen(SynchronizedRef.get(tabsRef)),
Effect.flatMap((tabs) => {
const colorScheme = tabs.get(tabId)?.colorScheme ?? "system";
return colorScheme === "system" ? Effect.void : applyColorScheme(tabId, wc, colorScheme);
}),
Effect.ignore,
);

const setColorScheme = Effect.fn("PreviewManager.setColorScheme")(function* (
tabId: string,
colorScheme: DesktopPreviewColorScheme,
) {
const tab = (yield* SynchronizedRef.get(tabsRef)).get(tabId);
if (!tab) {
return yield* new PreviewTabNotFoundError({ tabId });
}
if (tab.colorScheme !== colorScheme) {
// Record the choice even when the CDP call below can't run yet (no
// webview, DevTools holding the debugger) — it is re-applied on the
// next control-session (re)attach.
yield* update(tabId, { colorScheme });
}
// Re-read after the update: registerWebview may have swapped the guest
// in the meantime and the override must land on the current one.
const webContentsId = (yield* SynchronizedRef.get(tabsRef)).get(tabId)?.webContentsId;
if (webContentsId == null) return;
const wc = webContents.fromId(webContentsId);
if (!wc || wc.isDestroyed()) return;
yield* applyColorScheme(tabId, wc, colorScheme);
});

const captureScreenshot = Effect.fn("PreviewManager.captureScreenshot")(function* (
tabId: string,
) {
Expand Down Expand Up @@ -2526,6 +2590,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
revealArtifact,
saveRecording,
setAnnotationTheme,
setColorScheme,
setMainWindow,
startRecording,
stopRecording,
Expand Down Expand Up @@ -2830,6 +2895,10 @@ export class PreviewManager extends Context.Service<
readonly zoomOut: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
readonly resetZoom: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
readonly hardReload: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
readonly setColorScheme: (
tabId: string,
colorScheme: DesktopPreviewColorScheme,
) => Effect.Effect<void, PreviewManagerError>;
readonly openDevTools: (tabId: string) => Effect.Effect<void, PreviewManagerError>;
readonly clearCookies: () => Effect.Effect<void, PreviewManagerError>;
readonly clearCache: () => Effect.Effect<void, PreviewManagerError>;
Expand Down Expand Up @@ -2921,6 +2990,7 @@ export const make = Effect.gen(function* PreviewManagerMake() {
zoomOut: operations.zoomOut,
resetZoom: operations.resetZoom,
hardReload: operations.hardReload,
setColorScheme: operations.setColorScheme,
openDevTools: operations.openDevTools,
clearCookies: Effect.fn("PreviewManager.clearCookies")(function* () {
yield* browserSession
Expand Down
26 changes: 20 additions & 6 deletions apps/mobile/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ import { createStaticNavigation, DarkTheme, DefaultTheme } from "@react-navigati
import { RegistryContext } from "@effect/atom-react";
import { ConfirmDialogHost } from "./components/ConfirmDialogHost";
import { CloudAuthProvider } from "./features/cloud/CloudAuthProvider";
import { IncomingShareProvider } from "./features/sharing/IncomingShareProvider";
import { prepareNativeShowcaseCapture } from "./features/showcase/nativeShowcaseScene";
import { AppearancePreferencesProvider } from "./features/settings/appearance/AppearancePreferencesProvider";
import { IncomingShareProvider } from "./features/sharing/IncomingShareProvider";
import {
AppearancePreferencesProvider,
useAppearancePreferences,
} from "./features/settings/appearance/AppearancePreferencesProvider";
import { RootStack } from "./Stack";
import { appAtomRegistry } from "./state/atom-registry";
import { OverlayPortalHost } from "./components/OverlayPortal";
Expand All @@ -26,6 +29,10 @@ if (process.env.EXPO_PUBLIC_SHOWCASE === "1") {
prepareNativeShowcaseCapture();
}

void SplashScreen.preventAutoHideAsync().catch(() => {
// The native module can be unavailable in non-native test environments.
});

const appLinking = {
prefixes: [Linking.createURL("/"), "t3code://", "t3code-dev://", "t3code-preview://"],
// The Expo dev client launches the app via
Expand All @@ -40,18 +47,25 @@ const appLinking = {

const Navigation = createStaticNavigation(RootStack);

function SplashScreenCoordinator() {
const { isReady } = useAppearancePreferences();

useEffect(() => {
if (isReady) void SplashScreen.hide();
}, [isReady]);
Comment on lines +50 to +55

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Handle preference-load failures before gating splash dismissal.

AppearancePreferencesProvider exposes isReady only for successful results. If loading preferences permanently fails, this effect never hides the splash, leaving the app stuck at launch even though fallback preferences are available. Make readiness failure-tolerant or add a fallback/error path that dismisses the splash once initialization settles.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/mobile/src/App.tsx` around lines 50 - 55, Update SplashScreenCoordinator
and the appearance-preferences initialization state so splash dismissal is
failure-tolerant: hide the splash once preference loading settles successfully
or fails, using fallback preferences when loading fails. Do not gate dismissal
solely on the successful-only isReady value; preserve the existing behavior for
successful initialization.


return null;
}

export default function App() {
const colorScheme = useColorScheme();
const statusBarBg = useThemeColor("--color-status-bar");

useEffect(() => {
SplashScreen.hide();
}, []);

return (
<RegistryContext.Provider value={appAtomRegistry}>
<CloudAuthProvider>
<AppearancePreferencesProvider>
<SplashScreenCoordinator />
<GestureHandlerRootView className="flex-1">
<KeyboardProvider statusBarTranslucent>
<SafeAreaProvider>
Expand Down
Loading
Loading