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
133 changes: 133 additions & 0 deletions apps/desktop/src/lib/trpc/routers/hotkeys/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { readFile, writeFile } from "node:fs/promises";
import { type BrowserWindow, dialog } from "electron";
import { appState } from "main/lib/app-state";
import {
buildHotkeysStateFromExport,
createHotkeysExport,
getCurrentPlatform,
getHotkeysSummary,
type HotkeysExportFile,
type HotkeysState,
normalizeBindingsWithDefaults,
} from "shared/hotkeys";
import { z } from "zod";
import { publicProcedure, router } from "../..";

const hotkeysExportSchema = z.object({
schemaVersion: z.number(),
exportedAt: z.string(),
app: z.string(),
hotkeys: z
.object({
darwin: z.record(z.string(), z.string().nullable()).optional(),
win32: z.record(z.string(), z.string().nullable()).optional(),
linux: z.record(z.string(), z.string().nullable()).optional(),
})
.optional(),
});

export type HotkeysImportResult =
| { canceled: true }
| {
canceled: false;
path: string;
state: HotkeysState;
summary: { assigned: number; disabled: number };
raw: HotkeysExportFile;
}
| { canceled: false; error: string };

type HotkeysExportResult =
| { canceled: true }
| { canceled: false; path: string }
| { canceled: false; error: string };

export const createHotkeysRouter = (getWindow: () => BrowserWindow | null) => {
return router({
export: publicProcedure.mutation(async (): Promise<HotkeysExportResult> => {
const window = getWindow();
if (!window) {
return { canceled: false, error: "No window available" };
}

const result = await dialog.showSaveDialog(window, {
title: "Export Keyboard Shortcuts",
defaultPath: "superset-hotkeys.json",
filters: [{ name: "JSON", extensions: ["json"] }],
});

if (result.canceled || !result.filePath) {
return { canceled: true };
}

const exportFile = createHotkeysExport(appState.data.hotkeysState);
try {
await writeFile(
result.filePath,
JSON.stringify(exportFile, null, 2),
"utf-8",
);
} catch (error) {
const message =
error instanceof Error ? error.message : "Failed to write file";
return { canceled: false, error: message };
}

return { canceled: false, path: result.filePath };
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}),

import: publicProcedure.mutation(async (): Promise<HotkeysImportResult> => {
const window = getWindow();
if (!window) {
return { canceled: false, error: "No window available" };
}

const result = await dialog.showOpenDialog(window, {
title: "Import Keyboard Shortcuts",
properties: ["openFile"],
filters: [{ name: "JSON", extensions: ["json"] }],
});

if (result.canceled || result.filePaths.length === 0) {
return { canceled: true };
}

const filePath = result.filePaths[0];

try {
const raw = await readFile(filePath, "utf-8");
const parsed = hotkeysExportSchema.parse(JSON.parse(raw));
const exportFile: HotkeysExportFile = {
schemaVersion: parsed.schemaVersion,
exportedAt: parsed.exportedAt,
app: parsed.app,
hotkeys: {
darwin: parsed.hotkeys?.darwin ?? {},
win32: parsed.hotkeys?.win32 ?? {},
linux: parsed.hotkeys?.linux ?? {},
},
};

const state = buildHotkeysStateFromExport(exportFile);
const platform = getCurrentPlatform();
const bindings = normalizeBindingsWithDefaults(
exportFile.hotkeys?.[platform] ?? {},
platform,
);
const summary = getHotkeysSummary(bindings);

return {
canceled: false,
path: filePath,
state,
summary,
raw: exportFile,
};
} catch (error) {
const message =
error instanceof Error ? error.message : "Invalid hotkeys file";
return { canceled: false, error: message };
}
}),
});
};
2 changes: 2 additions & 0 deletions apps/desktop/src/lib/trpc/routers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createAutoUpdateRouter } from "./auto-update";
import { createChangesRouter } from "./changes";
import { createConfigRouter } from "./config";
import { createExternalRouter } from "./external";
import { createHotkeysRouter } from "./hotkeys";
import { createMenuRouter } from "./menu";
import { createNotificationsRouter } from "./notifications";
import { createPortsRouter } from "./ports";
Expand Down Expand Up @@ -33,6 +34,7 @@ export const createAppRouter = (getWindow: () => BrowserWindow | null) => {
notifications: createNotificationsRouter(),
ports: createPortsRouter(),
menu: createMenuRouter(),
hotkeys: createHotkeysRouter(getWindow),
external: createExternalRouter(),
settings: createSettingsRouter(),
config: createConfigRouter(),
Expand Down
70 changes: 70 additions & 0 deletions apps/desktop/src/lib/trpc/routers/ui-state/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { observable } from "@trpc/server/observable";
import { appState } from "main/lib/app-state";
import type { TabsState, ThemeState } from "main/lib/app-state/schemas";
import { hotkeysEmitter } from "main/lib/hotkeys-events";
import {
buildOverridesFromBindings,
HOTKEYS_STATE_VERSION,
type HotkeysState,
} from "shared/hotkeys";
import { z } from "zod";
import { publicProcedure, router } from "../..";

Expand Down Expand Up @@ -156,6 +163,15 @@ const themeStateSchema = z.object({
customThemes: z.array(themeSchema),
});

const hotkeysStateSchema = z.object({
version: z.number(),
byPlatform: z.object({
darwin: z.record(z.string(), z.string().nullable()).default({}),
win32: z.record(z.string(), z.string().nullable()).default({}),
linux: z.record(z.string(), z.string().nullable()).default({}),
}),
});

/**
* UI State router - manages tabs and theme persistence via lowdb
*/
Expand Down Expand Up @@ -190,5 +206,59 @@ export const createUiStateRouter = () => {
return { success: true };
}),
}),

// Hotkeys state procedures
hotkeys: router({
get: publicProcedure.query((): HotkeysState => {
return appState.data.hotkeysState;
}),

set: publicProcedure
.input(hotkeysStateSchema)
.mutation(async ({ input }) => {
const version =
input.version === HOTKEYS_STATE_VERSION
? input.version
: HOTKEYS_STATE_VERSION;

const normalized: HotkeysState = {
version,
byPlatform: {
darwin: buildOverridesFromBindings(
input.byPlatform.darwin ?? {},
"darwin",
),
win32: buildOverridesFromBindings(
input.byPlatform.win32 ?? {},
"win32",
),
linux: buildOverridesFromBindings(
input.byPlatform.linux ?? {},
"linux",
),
},
};

appState.data.hotkeysState = normalized;
await appState.write();
hotkeysEmitter.emit("change", {
version: normalized.version,
updatedAt: new Date().toISOString(),
});
return { success: true };
}),

subscribe: publicProcedure.subscription(() => {
return observable<{ version: number; updatedAt: string }>((emit) => {
const onChange = (data: { version: number; updatedAt: string }) => {
emit.next(data);
};
hotkeysEmitter.on("change", onChange);
return () => {
hotkeysEmitter.off("change", onChange);
};
});
}),
}),
});
};
8 changes: 8 additions & 0 deletions apps/desktop/src/main/lib/app-state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ function ensureValidShape(data: Partial<AppState>): AppState {
...defaultAppState.themeState,
...(data.themeState ?? {}),
},
hotkeysState: {
...defaultAppState.hotkeysState,
...(data.hotkeysState ?? {}),
byPlatform: {
...defaultAppState.hotkeysState.byPlatform,
...(data.hotkeysState?.byPlatform ?? {}),
},
},
};
}

Expand Down
3 changes: 3 additions & 0 deletions apps/desktop/src/main/lib/app-state/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* UI state schemas (persisted from renderer zustand stores)
*/
import { createDefaultHotkeysState, type HotkeysState } from "shared/hotkeys";
import type { BaseTabsState } from "shared/tabs-types";
import type { Theme } from "shared/themes";

Expand All @@ -15,6 +16,7 @@ export interface ThemeState {
export interface AppState {
tabsState: BaseTabsState;
themeState: ThemeState;
hotkeysState: HotkeysState;
}

export const defaultAppState: AppState = {
Expand All @@ -29,4 +31,5 @@ export const defaultAppState: AppState = {
activeThemeId: "dark",
customThemes: [],
},
hotkeysState: createDefaultHotkeysState(),
};
8 changes: 8 additions & 0 deletions apps/desktop/src/main/lib/hotkeys-events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { EventEmitter } from "node:events";

export interface HotkeysStateChangedEvent {
version: number;
updatedAt: string;
}

export const hotkeysEmitter = new EventEmitter();
32 changes: 31 additions & 1 deletion apps/desktop/src/main/lib/menu.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { COMPANY } from "@superset/shared/constants";
import { app, Menu, shell } from "electron";
import { env } from "main/env.main";
import { appState } from "main/lib/app-state";
import { hotkeysEmitter } from "main/lib/hotkeys-events";
import {
getCurrentPlatform,
getEffectiveHotkey,
type HotkeyId,
toElectronAccelerator,
} from "shared/hotkeys";
import {
checkForUpdatesInteractive,
simulateDownloading,
Expand All @@ -9,7 +17,28 @@ import {
} from "./auto-updater";
import { menuEmitter } from "./menu-events";

let isHotkeyListenerRegistered = false;

function getMenuAccelerator(id: HotkeyId): string | undefined {
const platform = getCurrentPlatform();
const overrides = appState.data.hotkeysState.byPlatform[platform];
const keys = getEffectiveHotkey(id, overrides, platform);
const accelerator = toElectronAccelerator(keys, platform);
return accelerator ?? undefined;
}

export function registerMenuHotkeyUpdates() {
if (isHotkeyListenerRegistered) return;
isHotkeyListenerRegistered = true;
hotkeysEmitter.on("change", () => {
createApplicationMenu();
});
}

export function createApplicationMenu() {
const closeAccelerator = getMenuAccelerator("CLOSE_WINDOW");
const showHotkeysAccelerator = getMenuAccelerator("SHOW_HOTKEYS");

const template: Electron.MenuItemConstructorOptions[] = [
{
label: "Edit",
Expand Down Expand Up @@ -43,7 +72,7 @@ export function createApplicationMenu() {
{ role: "minimize" },
{ role: "zoom" },
{ type: "separator" },
{ role: "close", accelerator: "CmdOrCtrl+Shift+W" },
{ role: "close", accelerator: closeAccelerator },
],
},
{
Expand All @@ -70,6 +99,7 @@ export function createApplicationMenu() {
{ type: "separator" },
{
label: "Keyboard Shortcuts",
accelerator: showHotkeysAccelerator,
click: () => {
menuEmitter.emit("open-settings", "keyboard");
},
Expand Down
3 changes: 2 additions & 1 deletion apps/desktop/src/main/windows/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { NOTIFICATION_EVENTS, PORTS } from "shared/constants";
import { createIPCHandler } from "trpc-electron/main";
import { productName } from "~/package.json";
import { appState } from "../lib/app-state";
import { createApplicationMenu } from "../lib/menu";
import { createApplicationMenu, registerMenuHotkeyUpdates } from "../lib/menu";
import { playNotificationSound } from "../lib/notification-sound";
import {
type AgentCompleteEvent,
Expand Down Expand Up @@ -54,6 +54,7 @@ export async function MainWindow() {
});

createApplicationMenu();
registerMenuHotkeyUpdates();

currentWindow = window;

Expand Down
Loading
Loading