mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-19 00:10:00 +00:00
Switching a theme with several windows open flickered for seconds: the plugin theme is shared by every window, and each window put back the theme its own settings named whenever it changed, before those settings caught up. Windows now act on the plugin theme only when their own selection changes, and applying the theme already shown keeps its stylesheet instead of replacing it. Theme previews in the gear menu, Quick Switch and Settings waited 500 ms, and on themes written with hsl() or color-mix() the terminal took up to a second to read its colours: each token was resolved through a new element inserted under the root, restyling the whole page every time. Previews now show at once, and token colours resolve through one element kept for it. The built-in One Light and One Dark themes are removed in favour of the One Dark Pro plugin; settings naming them read as Islands Light and Islands Dark.
153 lines
5.5 KiB
TypeScript
153 lines
5.5 KiB
TypeScript
/**
|
||
* 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 { showContextMenu, useContextMenuStore, type MenuItem } from "@/ui";
|
||
import { themeGroups, type ThemeChoiceId } from "@/platform/themes";
|
||
import { currentThemeChoice, previewTheme, setTheme } 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");
|
||
|
||
/**
|
||
* Instant theme switch: in Quick Switch the active theme shows right away, in
|
||
* this window only; choosing one selects it, and closing the menu without a
|
||
* choice brings the selected theme back.
|
||
*/
|
||
export interface ThemePreview {
|
||
preview(id: ThemeChoiceId): void;
|
||
/** Back to the selected theme (another Quick Switch entry became active). */
|
||
restore(): void;
|
||
choose(id: ThemeChoiceId): void;
|
||
/** The menu closed. */
|
||
end(): void;
|
||
}
|
||
|
||
export function themePreview(): ThemePreview {
|
||
let shown: ThemeChoiceId | null = null;
|
||
let chosen = false;
|
||
const restore = () => {
|
||
if (shown !== null && !chosen) previewTheme(null);
|
||
shown = null;
|
||
};
|
||
return {
|
||
preview(id) {
|
||
if (shown === id) return;
|
||
shown = id;
|
||
previewTheme(id);
|
||
},
|
||
restore,
|
||
choose(id) {
|
||
chosen = true;
|
||
// Shown right away; selecting it then changes nothing visible.
|
||
if (shown !== id) previewTheme(id);
|
||
void setTheme(id);
|
||
},
|
||
end: restore,
|
||
};
|
||
}
|
||
|
||
/** The built-in themes, then each plugin's, a separator between them (as IDEA's Theme list). */
|
||
export function themeItems(themeId: string = currentThemeChoice(), preview?: ThemePreview): MenuItem[] {
|
||
return themeGroups().flatMap<MenuItem>((group, i) => [
|
||
...(i > 0 ? [{ kind: "separator" as const }] : []),
|
||
...group.themes.map<MenuItem>((c) => ({
|
||
id: `theme.${c.id}`,
|
||
label: c.label,
|
||
// Plugin themes keep their own names.
|
||
verbatim: !!c.origin,
|
||
checked: c.id === themeId,
|
||
onActive: preview && (() => preview.preview(c.id)),
|
||
run: () => (preview ? preview.choose(c.id) : 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, its themes previewed. */
|
||
export function quickSwitchItems(preview?: ThemePreview): MenuItem[] {
|
||
const { keymapId, customs } = keymapState();
|
||
const restore = preview && (() => preview.restore());
|
||
return [
|
||
{
|
||
id: "qs.scheme",
|
||
label: "Editor Color Scheme",
|
||
onActive: restore,
|
||
submenu: colorSchemeItems(useProject.getState().settings?.editor?.colorScheme),
|
||
},
|
||
{ id: "qs.keymap", label: "Keymap", onActive: restore, submenu: keymapItems(keymapId, customs) },
|
||
{ id: "qs.viewMode", label: "View Mode", onActive: restore, submenu: VIEW_MODE_ITEMS },
|
||
{ id: "qs.theme", label: "Theme", submenu: themeItems(undefined, preview) },
|
||
];
|
||
}
|
||
|
||
/** Opens Quick Switch Scheme; a theme previewed there goes away with the menu unless chosen. */
|
||
export function showQuickSwitch(at: { x: number; y: number }) {
|
||
const preview = themePreview();
|
||
showContextMenu(at, quickSwitchItems(preview));
|
||
const menu = useContextMenuStore.getState().menu;
|
||
const unsubscribe = useContextMenuStore.subscribe((s) => {
|
||
if (s.menu === menu) return;
|
||
unsubscribe();
|
||
// A chosen item runs right after the menu closes: let it choose first.
|
||
queueMicrotask(() => preview.end());
|
||
});
|
||
}
|