Файли
openidea/src/features/settings/settingsWindow.ts
T
Deng Liuтаdengliu 98f163da75 fix(agents): open SKILL.md from Settings window in parent editor (#6639)
Open SKILL.md in Settings > Agents > Skills did nothing in the native Settings window, which hosts no editor service. The Settings window now forwards editor opens to its parent window over openidea://settings/open-file; the parent opens the file in its project or LightEdit editor (or through file.openPaths from the Welcome screen), raises itself and reports failures. In the in-page Settings dialog, a clean dialog closes so the opened tab is visible, and trailing slashes on skill paths are normalized.

Co-authored-by: dengliu <[email protected]>
2026-11-28 20:10:49 +08:00

222 рядки
8.7 KiB
TypeScript

/**
* Standalone native Settings window (`settings-<parent>`): settings pages only,
* working on the parent's project without importing workbench status bar widgets
* or CodeMirror.
*/
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow";
import { registerActions } from "@/platform/actions";
import { logError } from "@/platform/errorLog";
import { isImeKey } from "@/platform/ime";
import { commands } from "@/platform/ipc";
import {
bindingsFor,
installMouseShortcuts,
isMouseBinding,
keyboardDispatchSuspended,
normalizeBinding,
strokeOf,
} from "@/platform/keymap";
import { useProject } from "@/platform/project";
import { enableRemote } from "@/platform/remote";
import { isPopupOpen } from "@/ui";
import { NEW_PROJECTS_PAGE, settingsActions } from "./actions";
import { loadBuiltinPages, preloadBuiltinRegistry, unloadBuiltinPages } from "./dialog/LazySettingsDialog";
import { trackIconThemePages } from "./iconThemePages";
import { validateSettings } from "./model/schema";
import {
closeSettings,
openSettings,
rebaseSettings,
selectNextPage,
selectPage,
selectPreviousPage,
setNewProjects,
setQuery,
settingsTitle,
useSettingsDialog,
} from "./model/staging";
import { trackRuntimeSettings } from "./runtime";
import { installActionProxies, installEditorProxy } from "./window/bridge";
/** Same event as `windows::LAUNCH_EVENT`: a pending launch (page) is waiting. */
const LAUNCH_EVENT = "openidea://launch";
export interface SettingsLaunch {
page?: string;
newProjects: boolean;
}
function navigate(page: string | undefined) {
if (!page) return;
setQuery("");
selectPage(page);
}
async function takeLaunch(): Promise<SettingsLaunch | null> {
const launch = await commands.windowTakeLaunch().catch(() => null);
return launch && { page: launch.settingsPage ?? undefined, newProjects: launch.newProjects };
}
/** A launch for the open window: its page, and the mode when it asks for new projects. */
export function relaunchSettings(launch: SettingsLaunch | null) {
if (!launch) return;
if (launch.newProjects) setNewProjects(true);
navigate(launch.page);
}
function listenLaunch(afterInit: Promise<unknown>): () => void {
let disposed = false;
let off: (() => void) | undefined;
void getCurrentWebviewWindow()
.listen(LAUNCH_EVENT, () => void takeLaunch().then(relaunchSettings))
.then(async (u) => {
if (disposed) return u();
off = u;
// A page requested after `window_take_settings_init` read the slot but before `listen`
// attached: its event is gone, the launch is not. Apply it after the initial launch settles.
await afterInit.catch(() => {});
if (!disposed) void takeLaunch().then(relaunchSettings);
});
return () => {
disposed = true;
off?.();
};
}
/** Esc and ⌘W cancel, like closing the window; ⌘, stays here. */
function installWindowKeys(): () => void {
const onKey = (e: KeyboardEvent) => {
if (e.defaultPrevented || isImeKey(e)) return;
const mod = e.metaKey || e.ctrlKey;
if ((e.key === "Escape" && !isPopupOpen()) || (mod && !e.altKey && !e.shiftKey && e.key.toLowerCase() === "w")) {
e.preventDefault();
closeSettings();
} else if (mod && e.key === ",") {
e.preventDefault();
}
};
window.addEventListener("keydown", onKey);
return () => window.removeEventListener("keydown", onKey);
}
/**
* Back / forward between the pages visited this session: the workbench keymap's
* navigate.back / navigate.forward (⌘[ / ⌘], mouse side buttons, two-finger
* swipes) from the standalone window. Runs the keymap's own bindings so a
* user's re-mapped shortcut keeps working; the dispatchers are window-local
* (the workbench one would fight the IME-free input handling of the pages).
*/
function installHistoryShortcuts(): () => void {
const onKey = (e: KeyboardEvent) => {
if (e.defaultPrevented || isImeKey(e) || keyboardDispatchSuspended()) return;
const stroke = strokeOf(e);
if (!stroke) return;
// Plain text fields keep their unmodified editing keys, as in the workbench
// dispatcher; a shortcut with a command modifier still reaches the keymap.
const el = e.target instanceof Element ? (e.target.closest(".cm-editor") ? null : (e.target as HTMLElement)) : null;
if (
el &&
(el.tagName === "INPUT" || el.tagName === "TEXTAREA" || el.isContentEditable) &&
!(e.metaKey || e.ctrlKey || e.altKey)
)
return;
const go = (run: () => boolean, action: string) => {
// Mouse / swipe bindings are the mouse shortcuts' business.
const bound = bindingsFor(action)
.filter((b) => !isMouseBinding(b) && !b.startsWith("Swipe"))
.map(normalizeBinding);
if (!bound.includes(normalizeBinding(stroke))) return false;
e.preventDefault();
e.stopPropagation();
return run();
};
if (go(selectPreviousPage, "navigate.back")) return;
go(selectNextPage, "navigate.forward");
};
// Capture phase, like the workbench dispatcher: the window's browser guards cancel ⌘[ / ⌘]
// in the bubble phase, before a bubble listener added later would see them.
window.addEventListener("keydown", onKey, true);
return () => window.removeEventListener("keydown", onKey, true);
}
/**
* The settings window (`settings-<parent>`): settings pages only, working on
* the parent's project; Cancel / OK / Esc close the window.
*/
export function activateSettingsWindow(parent: string): () => void {
const bootstrap = (async () => {
useSettingsDialog.setState({ pagesLoading: true });
openSettings(undefined, { globalBase: useSettingsDialog.getState().globalBase });
try {
const [init] = await Promise.all([commands.windowTakeSettingsInit(), preloadBuiltinRegistry().catch(() => null)]);
if (init.remote) enableRemote(init.remote);
const globalBase = validateSettings(init.globalSettings);
if (init.project) {
useProject.setState({ project: init.project, settings: init.project.settings });
} else {
useProject.setState({ project: null, settings: init.globalSettings });
}
const launch = init.launch && {
page: init.launch.settingsPage ?? undefined,
newProjects: init.launch.newProjects,
};
const targetPage =
launch?.page ?? (launch?.newProjects ? NEW_PROJECTS_PAGE : useSettingsDialog.getState().pageId);
await loadBuiltinPages({ pageId: targetPage, updates: init.updates }).catch((e) =>
logError("cannot register settings pages", e),
);
relaunchSettings(launch);
rebaseSettings(globalBase);
if (init.remote) {
// Reconcile remote-host settings asynchronously without blocking first paint on SSH.
// `rebaseSettings` updates only `base` / `globalBase` and leaves any staged edits intact.
const remoteGlobalPromise = init.remote.commands.includes("settings_get_global")
? commands.settingsGetGlobal().catch(() => null)
: Promise.resolve(null);
void Promise.all([commands.projectCurrent(), remoteGlobalPromise])
.then(([fresh, remoteGlobal]) => {
if (!useSettingsDialog.getState().open) return;
const nextGlobal = remoteGlobal ? validateSettings(remoteGlobal) : globalBase;
if (fresh) {
useProject.setState({ project: fresh, settings: fresh.settings });
}
if (fresh || remoteGlobal) {
rebaseSettings(nextGlobal);
}
})
.catch(() => {});
}
} finally {
useSettingsDialog.setState({ pagesLoading: false });
}
})();
bootstrap.catch((e) => logError("settings window startup failed", e));
const disposers = [
trackRuntimeSettings(),
trackIconThemePages(),
unloadBuiltinPages,
registerActions(settingsActions((page, newProjects) => relaunchSettings({ page, newProjects: !!newProjects }))),
// Mouse side buttons and the keymap's back / forward keys navigate the page
// history (the workbench dispatcher does not run in this window).
registerActions([
{ id: "navigate.back", title: "Back", category: "Navigate", run: () => void selectPreviousPage() },
{ id: "navigate.forward", title: "Forward", category: "Navigate", run: () => void selectNextPage() },
]),
installMouseShortcuts(),
installHistoryShortcuts(),
installActionProxies(parent),
installEditorProxy(parent),
listenLaunch(bootstrap),
installWindowKeys(),
useSettingsDialog.subscribe((s, prev) => {
if (prev.open && !s.open) void getCurrentWebviewWindow().close();
if (s.newProjects !== prev.newProjects) {
void getCurrentWebviewWindow()
.setTitle(settingsTitle(s.newProjects))
.catch(() => {});
}
}),
];
return () => disposers.reverse().forEach((d) => d());
}