Repository navigation
feat(desktop): apply theme files from command line #8080
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,17 +2,23 @@ import { createClerkBridge } from "@clerk/electron"; | |
| import { storage } from "@clerk/electron/storage"; | ||
| import * as Context from "effect/Context"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as FileSystem from "effect/FileSystem"; | ||
| import * as Layer from "effect/Layer"; | ||
| import * as Option from "effect/Option"; | ||
| import * as Path from "effect/Path"; | ||
| import * as Schema from "effect/Schema"; | ||
| import * as Scope from "effect/Scope"; | ||
| import type * as Electron from "electron"; | ||
|
|
||
| import { clerkFrontendApiHostnameFromPublishableKey } from "@t3tools/shared/relayAuth"; | ||
| import * as IpcChannels from "../ipc/channels.ts"; | ||
| import * as ElectronApp from "../electron/ElectronApp.ts"; | ||
| import * as ElectronProtocol from "../electron/ElectronProtocol.ts"; | ||
| import * as ElectronWindow from "../electron/ElectronWindow.ts"; | ||
| import * as DesktopAppIdentity from "./DesktopAppIdentity.ts"; | ||
| import * as DesktopEnvironment from "./DesktopEnvironment.ts"; | ||
| import { makeComponentLogger } from "./DesktopObservability.ts"; | ||
| import { readThemeFile, themeFilePathFromArguments } from "./DesktopThemeFileCommand.ts"; | ||
|
|
||
| declare const __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__: string | undefined; | ||
|
|
||
|
|
@@ -72,6 +78,8 @@ export const desktopClerkFrontendApiHostname = resolveDesktopClerkFrontendApiHos | |
| : __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__, | ||
| ); | ||
|
|
||
| const { logInfo, logWarning } = makeComponentLogger("desktop-theme-file-command"); | ||
|
|
||
| export function createDesktopClerkBridge(stateDir: string, isDevelopment: boolean) { | ||
| return createClerkBridge({ | ||
| storage: storage({ path: stateDir }), | ||
|
|
@@ -86,6 +94,8 @@ export function createDesktopClerkBridge(stateDir: string, isDevelopment: boolea | |
| export const make = Effect.gen(function* () { | ||
| const environment = yield* DesktopEnvironment.DesktopEnvironment; | ||
| const electronApp = yield* ElectronApp.ElectronApp; | ||
| const fileSystem = yield* FileSystem.FileSystem; | ||
| const path = yield* Path.Path; | ||
|
|
||
| // Electron scopes the single-instance lock to the userData directory and | ||
| // creates that directory when the lock is acquired. The SDK bridge takes | ||
|
|
@@ -124,6 +134,32 @@ export const make = Effect.gen(function* () { | |
| const electronWindow = yield* ElectronWindow.ElectronWindow; | ||
| const context = yield* Effect.context<ElectronWindow.ElectronWindow>(); | ||
| const runPromise = Effect.runPromiseWith(context); | ||
| const pendingThemePaths: string[] = []; | ||
|
|
||
| const readRequestedTheme = (filePath: string) => | ||
| readThemeFile(filePath).pipe( | ||
| Effect.provideService(FileSystem.FileSystem, fileSystem), | ||
| Effect.provideService(Path.Path, path), | ||
| Effect.catchCause((cause) => | ||
| logWarning("failed to read requested theme file", { filePath, cause }).pipe( | ||
| Effect.as(null), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| const sendThemePath = (filePath: string) => | ||
| Effect.gen(function* () { | ||
| const file = yield* readRequestedTheme(filePath); | ||
| if (file === null) return; | ||
| yield* logInfo("applying requested theme file", { | ||
| fileName: file.name, | ||
| fileSize: file.size, | ||
| }); | ||
| yield* electronWindow.sendAll(IpcChannels.APPLY_THEME_FILE_CHANNEL, file); | ||
| }); | ||
|
|
||
| const initialThemePath = themeFilePathFromArguments(process.argv); | ||
| if (initialThemePath) pendingThemePaths.push(initialThemePath); | ||
|
|
||
| // The SDK bridge holds Electron's single-instance lock (acquired at | ||
| // bridge creation) so OAuth deep-link callbacks on Windows/Linux are | ||
|
|
@@ -135,9 +171,42 @@ export const make = Effect.gen(function* () { | |
| return yield* Effect.interrupt; | ||
| } | ||
|
|
||
| yield* electronApp.on("second-instance", () => { | ||
| yield* electronApp.on( | ||
| "browser-window-created", | ||
| (_event: Electron.Event, window: Electron.BrowserWindow) => { | ||
| window.webContents.once("did-finish-load", () => { | ||
| const paths = pendingThemePaths.splice(0); | ||
| for (const filePath of paths) { | ||
| void runPromise( | ||
| Effect.gen(function* () { | ||
| const file = yield* readRequestedTheme(filePath); | ||
| if (file !== null && !window.isDestroyed()) { | ||
| yield* logInfo("applying requested startup theme file", { | ||
| fileName: file.name, | ||
| fileSize: file.size, | ||
| }); | ||
| window.webContents.send(IpcChannels.APPLY_THEME_FILE_CHANNEL, file); | ||
| } | ||
| }), | ||
| ); | ||
| } | ||
| }); | ||
| }, | ||
| ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Splash drains startup themesHigh Severity
Additional Locations (1)Reviewed by Cursor Bugbot for commit 51668e9. Configure here. |
||
|
|
||
| yield* electronApp.on("second-instance", (_event: unknown, commandLine: unknown) => { | ||
| void runPromise( | ||
| Effect.gen(function* () { | ||
| if (Array.isArray(commandLine)) { | ||
| const themePath = themeFilePathFromArguments( | ||
| commandLine.filter((argument): argument is string => typeof argument === "string"), | ||
| ); | ||
| if (themePath) { | ||
| const currentWindow = yield* electronWindow.currentMainOrFirst; | ||
| if (Option.isNone(currentWindow)) pendingThemePaths.push(themePath); | ||
| else yield* sendThemePath(themePath); | ||
| } | ||
| } | ||
| const mainWindow = yield* electronWindow.currentMainOrFirst; | ||
| if (Option.isSome(mainWindow)) { | ||
| yield* electronWindow.reveal(mainWindow.value); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { assert, describe, it } from "@effect/vitest"; | ||
|
|
||
| import { themeFilePathFromArguments } from "./DesktopThemeFileCommand.ts"; | ||
|
|
||
| describe("DesktopThemeFileCommand", () => { | ||
| it("reads a separated theme file argument", () => { | ||
| assert.equal( | ||
| themeFilePathFromArguments(["t3code", "--theme-file", "/tmp/omarchy theme.json"]), | ||
| "/tmp/omarchy theme.json", | ||
| ); | ||
| }); | ||
|
|
||
| it("reads an inline theme file argument", () => { | ||
| assert.equal( | ||
| themeFilePathFromArguments(["t3code", "--theme-file=/tmp/theme.json"]), | ||
| "/tmp/theme.json", | ||
| ); | ||
| }); | ||
|
|
||
| it("ignores missing and empty values", () => { | ||
| assert.isNull(themeFilePathFromArguments(["t3code", "--theme-file"])); | ||
| assert.isNull(themeFilePathFromArguments(["t3code", "--theme-file="])); | ||
| assert.isNull(themeFilePathFromArguments(["t3code", "--theme-file", "--inspect"])); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import type { PickedThemeFile } from "@t3tools/contracts"; | ||
| import * as FileSystem from "effect/FileSystem"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Path from "effect/Path"; | ||
|
|
||
| export const THEME_FILE_ARGUMENT = "--theme-file"; | ||
| export const THEME_FILE_MAX_BYTES = 256 * 1024; | ||
|
|
||
| export function themeFilePathFromArguments(args: readonly string[]): string | null { | ||
| for (let index = 0; index < args.length; index += 1) { | ||
| const argument = args[index]!; | ||
| if (argument === THEME_FILE_ARGUMENT) { | ||
| const value = args[index + 1]; | ||
| return value && !value.startsWith("--") ? value : null; | ||
| } | ||
| if (argument.startsWith(`${THEME_FILE_ARGUMENT}=`)) { | ||
| const value = argument.slice(THEME_FILE_ARGUMENT.length + 1); | ||
| return value.length > 0 ? value : null; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| export const readThemeFile = Effect.fn("desktop.themeFileCommand.read")(function* ( | ||
| filePath: string, | ||
| ) { | ||
| const fileSystem = yield* FileSystem.FileSystem; | ||
| const path = yield* Path.Path; | ||
| const info = yield* fileSystem.stat(filePath); | ||
| const size = Number(info.size); | ||
| const text = size > THEME_FILE_MAX_BYTES ? "" : yield* fileSystem.readFileString(filePath); | ||
| return { name: path.basename(filePath), size, text } satisfies PickedThemeFile; | ||
|
Comment on lines
+31
to
+32
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 High When the file grows or is replaced after - const text = size > THEME_FILE_MAX_BYTES ? "" : yield* fileSystem.readFileString(filePath);
- return { name: path.basename(filePath), size, text } satisfies PickedThemeFile;
+ const text = size > THEME_FILE_MAX_BYTES ? "" : yield* fileSystem.readFileString(filePath);
+ const actualSize = size > THEME_FILE_MAX_BYTES ? size : new TextEncoder().encode(text).byteLength;
+ return {
+ name: path.basename(filePath),
+ size: actualSize,
+ text: actualSize > THEME_FILE_MAX_BYTES ? "" : text
+ } satisfies PickedThemeFile;🤖 Copy this AI Prompt to have your agent fix this: |
||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ import type { | |
| DesktopPreviewPointerEvent, | ||
| DesktopPreviewRecordingFrame, | ||
| DesktopPreviewTabState, | ||
| PickedThemeFile, | ||
| } from "@t3tools/contracts"; | ||
| import { exposeClerkBridge } from "@clerk/electron/preload"; | ||
| import { contextBridge, ipcRenderer } from "electron"; | ||
|
|
@@ -11,6 +12,27 @@ import * as IpcChannels from "./ipc/channels.ts"; | |
|
|
||
| exposeClerkBridge({ passkeys: true }); | ||
|
|
||
| let pendingThemeFile: PickedThemeFile | null = null; | ||
| const themeFileListeners = new Set<(file: PickedThemeFile) => void>(); | ||
|
|
||
| ipcRenderer.on(IpcChannels.APPLY_THEME_FILE_CHANNEL, (_event, file: unknown) => { | ||
| if (typeof file !== "object" || file === null) return; | ||
| const candidate = file as Partial<PickedThemeFile>; | ||
| if ( | ||
| typeof candidate.name !== "string" || | ||
| typeof candidate.size !== "number" || | ||
| typeof candidate.text !== "string" | ||
| ) { | ||
| return; | ||
| } | ||
| const picked = candidate as PickedThemeFile; | ||
| if (themeFileListeners.size === 0) { | ||
| pendingThemeFile = picked; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 High Multiple 🤖 Copy this AI Prompt to have your agent fix this: |
||
| return; | ||
| } | ||
| for (const listener of themeFileListeners) listener(picked); | ||
| }); | ||
|
|
||
| function unwrapEnsureSshEnvironmentResult(result: unknown) { | ||
| if ( | ||
| typeof result === "object" && | ||
|
|
@@ -104,6 +126,14 @@ contextBridge.exposeInMainWorld("desktopBridge", { | |
| pickProjectFavicon: (initialPath) => | ||
| ipcRenderer.invoke(IpcChannels.PICK_PROJECT_FAVICON_CHANNEL, initialPath), | ||
| pickThemeFiles: () => ipcRenderer.invoke(IpcChannels.PICK_THEME_FILES_CHANNEL, undefined), | ||
| onThemeFileApply: (listener) => { | ||
| themeFileListeners.add(listener); | ||
| if (pendingThemeFile) { | ||
| listener(pendingThemeFile); | ||
| pendingThemeFile = null; | ||
| } | ||
| return () => themeFileListeners.delete(listener); | ||
| }, | ||
| setTheme: (theme) => ipcRenderer.invoke(IpcChannels.SET_THEME_CHANNEL, theme), | ||
| showContextMenu: (items, position) => | ||
| ipcRenderer.invoke(IpcChannels.CONTEXT_MENU_CHANNEL, { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { assert } from "@effect/vitest"; | ||
| import { afterEach, beforeEach, describe, it, vi } from "vite-plus/test"; | ||
|
|
||
| import { getCustomThemes, invalidateCustomThemes } from "../themePalette"; | ||
| import { importDesktopThemeFile } from "./DesktopThemeFileCommands"; | ||
|
|
||
| const vscodeTheme = (background: string) => ({ | ||
| name: "Omarchy Current", | ||
| type: "dark", | ||
| colors: { | ||
| "editor.background": background, | ||
| "editor.foreground": "#cacccc", | ||
| }, | ||
| }); | ||
|
|
||
| describe("DesktopThemeFileCommands", () => { | ||
| beforeEach(() => { | ||
| const stored = new Map<string, string>(); | ||
| vi.stubGlobal("window", { | ||
| localStorage: { | ||
| clear: () => stored.clear(), | ||
| getItem: (key: string) => stored.get(key) ?? null, | ||
| removeItem: (key: string) => stored.delete(key), | ||
| setItem: (key: string, value: string) => stored.set(key, value), | ||
| }, | ||
| }); | ||
| invalidateCustomThemes(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| invalidateCustomThemes(); | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| it("installs and updates a requested VS Code theme", () => { | ||
| const firstId = importDesktopThemeFile({ | ||
| name: "vscode.json", | ||
| size: 100, | ||
| text: JSON.stringify(vscodeTheme("#101315")), | ||
| }); | ||
|
|
||
| assert.equal(getCustomThemes().length, 1); | ||
| assert.equal(getCustomThemes()[0]?.id, firstId); | ||
| const firstCanvas = getCustomThemes()[0]?.colors.canvas; | ||
|
|
||
| const secondId = importDesktopThemeFile({ | ||
| name: "vscode.json", | ||
| size: 100, | ||
| text: JSON.stringify(vscodeTheme("#202426")), | ||
| }); | ||
|
|
||
| assert.equal(secondId, firstId); | ||
| assert.equal(getCustomThemes().length, 1); | ||
| assert.notEqual(getCustomThemes()[0]?.colors.canvas, firstCanvas); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,42 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { PickedThemeFile } from "@t3tools/contracts"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useEffect } from "react"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useTheme } from "../hooks/useTheme"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| getCustomThemes, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| installCustomTheme, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| parseThemeFile, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| updateCustomTheme, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } from "../themePalette"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { isVsCodeThemeFile, parseVsCodeThemeFile } from "../vscodeThemeImport"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function importDesktopThemeFile(file: PickedThemeFile): string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!file.text) throw new Error(`Could not read theme file "${file.name}".`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const value: unknown = JSON.parse(file.text); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const theme = isVsCodeThemeFile(value) ? parseVsCodeThemeFile(value) : parseThemeFile(value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (getCustomThemes().some((installed) => installed.id === theme.id)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| updateCustomTheme(theme); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| installCustomTheme(theme); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return theme.id; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function DesktopThemeFileCommands() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { setTheme } = useTheme(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const bridge = window.desktopBridge; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!bridge?.onThemeFileApply) return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return bridge.onThemeFileApply((file) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const themeId = importDesktopThemeFile(file); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!setTheme(themeId)) throw new Error(`Could not activate theme "${themeId}".`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (cause) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.error("Failed to apply desktop theme file.", { file: file.name, cause }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, [setTheme]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+26
to
+39
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the requested theme id is already installed and active, this update never reaches the DOM: The existing update path handles this by re-applying after an update (
Suggested change
Posted via Macroscope — UI Consistency |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟠 High
app/DesktopClerk.ts:175A
--theme-filerequest during WSL cold boot is dropped instead of being applied to the real main window.currentMainOrFirstreturns the connecting splash, sosendThemePathsends the file to that window, and thebrowser-window-createdhandler then drains pending paths when the splash finishes loading. Check the actual main window before sending or draining, and keep the path queued until it exists.🤖 Copy this AI Prompt to have your agent fix this: