Files
openidea/src/workbench/quickSwitch.ts
T
l0ng-ai bde19959b9 feat(settings): import and export editor color schemes (#518)
The Color Scheme page only offered built-in schemes, so a scheme from another IDE could not be used.

Import reads .icls files into named custom schemes in user settings, reports applied and skipped values, and rejects malformed files. Export writes the current scheme, replacing an existing file, and custom schemes can be deleted. Colours are validated before reaching a stylesheet, and project settings cannot define schemes. The Appearance selector and Quick Switch list custom schemes too.

Fixes #513
2026-10-31 11:41:39 +08:00

92 lines
3.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Theme / color scheme / keymap / view mode switches shared by the main
* toolbar's gear menu and View › Quick Switch Scheme… (⌃`).
*/
import { call, commands, isTauri } from "@/platform/ipc";
import { listSchemes } from "@/platform/colorScheme";
import { editorPrefs } from "@/platform/editorSettings";
import { PRESET_TITLES, useKeymap } from "@/platform/keymap";
import { useProject } from "@/platform/project";
import type { MenuItem } from "@/ui";
import { currentThemeId, setTheme, THEME_CHOICES } from "./theme";
async function setGlobal(key: string, value: unknown, what: string) {
if (!isTauri()) return;
try {
useProject.setState({ settings: await call(commands.settingsSet("global", key, JSON.stringify(value))) });
} catch (e) {
console.warn(`cannot switch ${what}`, e);
}
}
export const selectKeymap = (id: string) => setGlobal("keymap", id, "keymap");
export function themeItems(themeId: string = currentThemeId()): MenuItem[] {
return THEME_CHOICES.map<MenuItem>((c) => ({
id: `theme.${c.id}`,
label: c.label,
checked: c.id === themeId,
run: () => void setTheme(c.id),
}));
}
export function keymapItems(keymapId: string, customKeymaps: string[]): MenuItem[] {
return [...Object.keys(PRESET_TITLES), ...customKeymaps].map<MenuItem>((id) => ({
id: `keymap.${id}`,
label: PRESET_TITLES[id] ?? id,
verbatim: !PRESET_TITLES[id],
checked: id === keymapId,
run: () => void selectKeymap(id),
}));
}
export function colorSchemeItems(current: string | undefined): MenuItem[] {
const schemes = listSchemes(editorPrefs().customSchemes);
const value = current && schemes.some((s) => s.id === current) ? current : "theme-default";
return [
{
id: "scheme.theme-default",
label: "Theme default",
checked: value === "theme-default",
run: () => void setGlobal("editor.colorScheme", "theme-default", "color scheme"),
},
...schemes.map<MenuItem>((s) => ({
id: `scheme.${s.id}`,
label: s.name,
checked: s.id === value,
run: () => void setGlobal("editor.colorScheme", s.id, "color scheme"),
})),
];
}
/** The existing view modes: Distraction Free Mode and Full Screen. */
export const VIEW_MODE_ITEMS: MenuItem[] = [
{ kind: "action", action: "editor.toggleZen" },
{ kind: "action", action: "window.toggleFullScreen" },
];
/** The keymap in effect and the user's custom keymaps. */
export function keymapState(): { keymapId: string; customs: string[] } {
const settings = useProject.getState().settings;
const customs = settings?.customKeymaps;
return {
keymapId: typeof settings?.keymap === "string" ? settings.keymap : useKeymap.getState().preset,
customs: customs && typeof customs === "object" ? Object.keys(customs) : [],
};
}
/** View › Quick Switch Scheme…: IDEA's popup of the switchable schemes. */
export function quickSwitchItems(): MenuItem[] {
const { keymapId, customs } = keymapState();
return [
{
id: "qs.scheme",
label: "Editor Color Scheme",
submenu: colorSchemeItems(useProject.getState().settings?.editor?.colorScheme),
},
{ id: "qs.keymap", label: "Keymap", submenu: keymapItems(keymapId, customs) },
{ id: "qs.viewMode", label: "View Mode", submenu: VIEW_MODE_ITEMS },
{ id: "qs.theme", label: "Theme", submenu: themeItems() },
];
}