Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

perf: make platformUtils synchronous via preload

Etienne Lescot593be5eb4a40
diff --git a/electron/electron-env.d.ts b/electron/electron-env.d.ts
index 24b9a5bbb..0cad8d4c1 100644
--- a/electron/electron-env.d.ts
+++ b/electron/electron-env.d.ts
@@ -317,7 +317,7 @@ interface Window {
onMenuSaveProjectAs: (callback: () => void) => () => void;
quitApp: () => void;
setTitleBarOverlay: (color: string, symbolColor: string) => void;
- getPlatform: () => Promise<string>;
+ getPlatform: () => string;
revealInFolder: (
filePath: string,
) => Promise<{ success: boolean; error?: string; message?: string }>;
diff --git a/electron/ipc/handlers.ts b/electron/ipc/handlers.ts
index 64504872b..207ef5e2c 100644
--- a/electron/ipc/handlers.ts
+++ b/electron/ipc/handlers.ts
@@ -3282,10 +3282,6 @@ export function registerIpcHandlers(
return { success: true };
}
- ipcMain.handle("get-platform", () => {
- return process.platform;
- });
-
// Keep the native Windows/Linux window-control overlay in the app's theme
// colours. The renderer sends the resolved CSS values so the palette stays in
// one place. No-op on macOS (traffic lights aren't tintable) and on any window
diff --git a/electron/preload.ts b/electron/preload.ts
index 381723d60..937607cbe 100644
--- a/electron/preload.ts
+++ b/electron/preload.ts
@@ -19,6 +19,10 @@ const ASSET_BASE_URL_ARG_PREFIX = "--asset-base-url=";
const assetBaseUrlArg = process.argv.find((arg) => arg.startsWith(ASSET_BASE_URL_ARG_PREFIX));
const assetBaseUrl = assetBaseUrlArg ? assetBaseUrlArg.slice(ASSET_BASE_URL_ARG_PREFIX.length) : "";
+// Renderer side: process.platform is the same Node global as in the main process,
+// so a synchronous read here saves the renderer's every-call IPC round-trip.
+const PLATFORM = process.platform;
+
contextBridge.exposeInMainWorld("electronAPI", {
assetBaseUrl,
@@ -314,9 +318,7 @@ contextBridge.exposeInMainWorld("electronAPI", {
setTitleBarOverlay: (color: string, symbolColor: string) => {
ipcRenderer.send("set-titlebar-overlay", color, symbolColor);
},
- getPlatform: () => {
- return ipcRenderer.invoke("get-platform");
- },
+ getPlatform: () => PLATFORM,
revealInFolder: (filePath: string) => {
return ipcRenderer.invoke("reveal-in-folder", filePath);
},
diff --git a/src/components/launch/LaunchWindow.test.tsx b/src/components/launch/LaunchWindow.test.tsx
index ce4f7e93e..3e108ff17 100644
--- a/src/components/launch/LaunchWindow.test.tsx
+++ b/src/components/launch/LaunchWindow.test.tsx
@@ -193,7 +193,7 @@ function stubElectronAPI(getSelectedSource: Window["electronAPI"]["getSelectedSo
granted: true,
status: "granted",
})),
- getPlatform: vi.fn(async () => "darwin"),
+ getPlatform: vi.fn(() => "darwin"),
setHudOverlaySize: vi.fn(),
setHudOverlayIgnoreMouseEvents: vi.fn(),
beginHudOverlayDrag: vi.fn(),
diff --git a/src/contexts/ShortcutsContext.tsx b/src/contexts/ShortcutsContext.tsx
index aab5f45cf..a1d895b40 100644
--- a/src/contexts/ShortcutsContext.tsx
+++ b/src/contexts/ShortcutsContext.tsx
@@ -30,15 +30,14 @@ export function useShortcuts(): ShortcutsContextValue {
export function ShortcutsProvider({ children }: { children: ReactNode }) {
const [shortcuts, setShortcuts] = useState<ShortcutsConfig>(DEFAULT_SHORTCUTS);
+ // `getIsMac()` is synchronous; the state still needs an effect so the value
+ // doesn't snapshot a wrong value during SSR/hydration (the module-level
+ // `process.platform` read happens once on first import).
const [isMac, setIsMac] = useState(false);
const [isConfigOpen, setIsConfigOpen] = useState(false);
useEffect(() => {
- getIsMac()
- .then(setIsMac)
- .catch(() => {
- // Keep default non-mac fallback if detection fails.
- });
+ setIsMac(getIsMac());
window.electronAPI
.getShortcuts?.()
diff --git a/src/hooks/useScreenRecorder.ts b/src/hooks/useScreenRecorder.ts
index 185dc3249..578ad5e78 100644
--- a/src/hooks/useScreenRecorder.ts
+++ b/src/hooks/useScreenRecorder.ts
@@ -799,7 +799,7 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
countdownRunToken?: number,
) => {
try {
- const platform = await window.electronAPI.getPlatform();
+ const platform = window.electronAPI.getPlatform();
if (platform !== "win32") {
return false;
}
@@ -911,7 +911,7 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
countdownRunToken?: number,
) => {
try {
- const platform = await window.electronAPI.getPlatform();
+ const platform = window.electronAPI.getPlatform();
if (platform !== "darwin") {
return false;
}
@@ -1086,7 +1086,7 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
}
try {
- const platform = await window.electronAPI.getPlatform();
+ const platform = window.electronAPI.getPlatform();
if (platform === "darwin" && cursorCaptureMode === "editable-overlay") {
// The main process shows a native dialog that deep-links to the
// Accessibility settings pane when access is missing, so we just stop
@@ -1176,7 +1176,7 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
// `getUserMedia` calls is the dominant source of the mic-vs-video lag at the
// start of the recording (issue #57).
const screenCapture = (async (): Promise<MediaStream> => {
- const platform = await window.electronAPI.getPlatform();
+ const platform = window.electronAPI.getPlatform();
if (platform === "win32") {
// getDisplayMedia + setDisplayMediaRequestHandler (main.ts) supplies the
diff --git a/src/lib/exporter/gifExporter.ts b/src/lib/exporter/gifExporter.ts
index dbfb02179..60f57bc50 100644
--- a/src/lib/exporter/gifExporter.ts
+++ b/src/lib/exporter/gifExporter.ts
@@ -138,7 +138,7 @@ export class GifExporter {
const onWarning = (message: string) => warnings.push(message);
try {
- const platform = await getPlatform();
+ const platform = getPlatform();
this.cleanup();
this.cancelled = false;
diff --git a/src/utils/platformUtils.ts b/src/utils/platformUtils.ts
index e41145ee4..0c0e7f211 100644
--- a/src/utils/platformUtils.ts
+++ b/src/utils/platformUtils.ts
@@ -1,60 +1,33 @@
-let cachedPlatform: string | null = null;
-
/**
- * Gets the current platform from Electron
+ * Gets the current platform from Electron. `process.platform` is a synchronous
+ * Node global available in both the main and renderer processes, so we read it
+ * directly here instead of bouncing through an IPC round-trip.
*/
-export const getPlatform = async (): Promise<string> => {
- if (cachedPlatform) return cachedPlatform;
-
- try {
- const platform = await window.electronAPI.getPlatform();
- cachedPlatform = platform;
- return platform;
- } catch (error) {
- console.warn("Failed to get platform from Electron, falling back to navigator:", error);
- // Fallback for dev/testing
- let fallbackPlatform = "win32";
- if (typeof navigator !== "undefined") {
- if (/Mac|iPhone|iPad|iPod/.test(navigator.platform)) {
- fallbackPlatform = "darwin";
- } else if (/Linux/.test(navigator.platform)) {
- fallbackPlatform = "linux";
- }
- }
-
- cachedPlatform = fallbackPlatform;
- return fallbackPlatform;
- }
-};
+export function getPlatform(): NodeJS.Platform {
+ return process.platform;
+}
/**
- * Detects if the current platform is macOS
+ * Detects if the current platform is macOS.
*/
-export const isMac = async (): Promise<boolean> => {
- const platform = await getPlatform();
- return platform === "darwin";
-};
+export const isMac = (): boolean => getPlatform() === "darwin";
/**
- * Gets the modifier key symbol based on the platform
+ * Gets the modifier key symbol based on the platform.
*/
-export const getModifierKey = async (): Promise<string> => {
- return (await isMac()) ? "⌘" : "Ctrl";
-};
+export const getModifierKey = (): "⌘" | "Ctrl" => (isMac() ? "⌘" : "Ctrl");
/**
- * Gets the shift key symbol based on the platform
+ * Gets the shift key symbol based on the platform.
*/
-export const getShiftKey = async (): Promise<string> => {
- return (await isMac()) ? "⇧" : "Shift";
-};
+export const getShiftKey = (): "⇧" | "Shift" => (isMac() ? "⇧" : "Shift");
/**
- * Formats a keyboard shortcut for display based on the platform
+ * Formats a keyboard shortcut for display based on the platform.
* @param keys Array of key combinations (e.g., ['mod', 'D'] or ['shift', 'mod', 'Scroll'])
*/
-export const formatShortcut = async (keys: string[]): Promise<string> => {
- const isMacPlatform = await isMac();
+export function formatShortcut(keys: string[]): string {
+ const isMacPlatform = isMac();
return keys
.map((key) => {
if (key.toLowerCase() === "mod") return isMacPlatform ? "⌘" : "Ctrl";
@@ -62,4 +35,4 @@ export const formatShortcut = async (keys: string[]): Promise<string> => {
return key;
})
.join(" + ");
-};
+}