Файли
openidea/src/platform/systemFonts.ts
T
Luis Felipeтаluisfelipe18 9f643af069 feat(web): openidea-server --web serves the IDE to a browser (#5134)
`openidea-server --web` serves the built frontend and the IDE backend over one HTTP + WebSocket port, directly or behind a reverse proxy that strips or keeps its path prefix.

- The rpc dispatcher gains an async `invoke` with a channel sink, so project-host commands, streaming channels and IDE events run over WebSockets as they do over `--stdio`.
- Token auth is on by default (a random token unless `--token` or `--no-auth`), binds 127.0.0.1 by default, and WebSocket upgrades must be same-origin.
- The browser serves the desktop shell commands with the trust classes the desktop declares (a test keeps them in step), and the desktop menu spec, now in `ide_host::menu`. Recent-project recording and project rename are shared ide-host ops.
- WebSocket output is queued without dropping channel data or events. A reloaded page gets back the project its window still has open; after a server restart the page reloads. One tab per window, with Take Over.
- Browser fallbacks for the title bar menu, path chooser, clipboard, dialogs, Settings and external links; a reconnecting banner and an unsaved-changes guard.
- `scripts/start-web.sh` and the guide (English and Chinese).

Co-authored-by: luisfelipe18 <[email protected]>
2026-11-26 23:00:28 +08:00

86 рядки
3.4 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, noteInstalledFonts } from "./fonts";
import { create } from "zustand";
import { isTauri } from "@tauri-apps/api/core";
import { call, commands, type FontFamily } from "@/platform/ipc";
import { isWebMode } from "./webBridge";
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() || isWebMode()) {
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));
noteInstalledFonts(fonts.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;
}