дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 17:21:13 +02:00
Settings pages, parent action proxies and template actions registered one by one, cloning the store map and notifying subscribers per item (~1,650 action and ~172 page updates per Settings open). registerActions now disposes in one update too, and the new registerSettingsPages batches page registration and disposal; both keep later per-id overrides. Disabled font pickers no longer enumerate system fonts; enabled ones defer it to idle and load at once on interaction, and the icon-font probe waits past startup. Co-authored-by: dengliu <[email protected]>
84 рядки
3.3 KiB
TypeScript
84 рядки
3.3 KiB
TypeScript
/** Shared suggestions only: editor, UI and terminal preferences remain independent. */
|
|
import { useEffect } from "react";
|
|
import { detectCandidateFonts } from "./candidateFonts";
|
|
import { BUNDLED_MONO_FAMILY, BUNDLED_UI_FAMILY, discoverNerdFonts } from "./fonts";
|
|
import { create } from "zustand";
|
|
import { isTauri } from "@tauri-apps/api/core";
|
|
import { call, commands, type FontFamily } from "@/platform/ipc";
|
|
|
|
export type FontStatus = "idle" | "loading" | "ready" | "empty" | "error" | "browser";
|
|
export const bundledFonts: FontFamily[] = [
|
|
{ name: BUNDLED_MONO_FAMILY, monospaced: true },
|
|
{ name: BUNDLED_UI_FAMILY, monospaced: false },
|
|
];
|
|
export function mergeFontFamilies(fonts: FontFamily[]): FontFamily[] {
|
|
const unique = new Map<string, FontFamily>();
|
|
for (const font of [...fonts, ...bundledFonts]) {
|
|
if (font.name.trim()) unique.set(font.name.toLocaleLowerCase(), font);
|
|
}
|
|
return [...unique.values()].sort((a, b) => a.name.localeCompare(b.name));
|
|
}
|
|
export type FontSource = "bundled" | "limited" | "native";
|
|
export const useSystemFontStore = create<{ fonts: FontFamily[]; status: FontStatus; source: FontSource }>(() => ({
|
|
fonts: bundledFonts,
|
|
status: "idle",
|
|
source: "bundled",
|
|
}));
|
|
let pending: Promise<void> | null = null;
|
|
function fallback(status: "error" | "browser") {
|
|
// Even an empty successful native catalog is authoritative. A failed refresh
|
|
// must never replace it with the old, necessarily incomplete candidate list.
|
|
if (useSystemFontStore.getState().source === "native") {
|
|
useSystemFontStore.setState({ status });
|
|
return;
|
|
}
|
|
let candidates: FontFamily[] = [];
|
|
try {
|
|
candidates = detectCandidateFonts();
|
|
} catch {
|
|
// A failing detector must not break settings or discard manual preferences.
|
|
}
|
|
useSystemFontStore.setState({ fonts: mergeFontFamilies(candidates), source: "limited", status });
|
|
}
|
|
export function loadSystemFonts(refresh = false): Promise<void> {
|
|
if (pending) return pending;
|
|
if (!refresh && useSystemFontStore.getState().status !== "idle") return Promise.resolve();
|
|
if (!isTauri()) {
|
|
fallback("browser");
|
|
return Promise.resolve();
|
|
}
|
|
useSystemFontStore.setState({ status: "loading" });
|
|
pending = Promise.resolve()
|
|
.then(() => call(commands.systemFontFamilies(refresh)))
|
|
.then((fonts) => {
|
|
const merged = mergeFontFamilies(fonts);
|
|
// Before subscribers (open terminals) re-apply their font stacks with it.
|
|
discoverNerdFonts(merged.map((font) => font.name));
|
|
useSystemFontStore.setState({ fonts: merged, status: fonts.length ? "ready" : "empty", source: "native" });
|
|
})
|
|
.catch(() => fallback("error"))
|
|
.finally(() => {
|
|
pending = null;
|
|
});
|
|
return pending;
|
|
}
|
|
export function useSystemFonts(enabled = true) {
|
|
const state = useSystemFontStore();
|
|
useEffect(() => {
|
|
if (!enabled || useSystemFontStore.getState().status !== "idle") return;
|
|
let idleId: number | undefined;
|
|
const timer = setTimeout(() => {
|
|
if (typeof requestIdleCallback === "function") {
|
|
idleId = requestIdleCallback(() => void loadSystemFonts(), { timeout: 1000 });
|
|
} else {
|
|
void loadSystemFonts();
|
|
}
|
|
}, 250);
|
|
return () => {
|
|
clearTimeout(timer);
|
|
if (idleId !== undefined && typeof cancelIdleCallback === "function") cancelIdleCallback(idleId);
|
|
};
|
|
}, [enabled]);
|
|
return state;
|
|
}
|