Files
NetMesh/domain/terminalAppearanceRuntime.ts
zhaolei 3c72efcb7f
Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
[Init] Initial commit - NetMesh terminal manager
2026-09-13 18:24:01 +08:00

193 lines
6.1 KiB
TypeScript

import type { Host, TerminalTheme } from './models';
import {
applyCustomAccentToTerminalTheme,
getFollowAppTerminalThemeSelectionUpdate,
isFollowAppTerminalThemeId,
resolveFollowedTerminalThemeId,
resolveHostTerminalThemeId,
resolveManualTerminalThemeId,
type FollowAppTerminalThemeSelection,
} from './terminalAppearance';
import { TERMINAL_THEMES, getBuiltinTerminalThemeById } from '../infrastructure/config/terminalThemes';
export type ThemeUserIntent =
| { kind: 'idle' }
| { kind: 'picking'; themeId: string; startedAt: number; scopeHostId?: string | null };
export type AppearanceSource = 'intent' | 'follow-app' | 'host-override' | 'manual-global';
export type ResolvedAppearance = {
themeId: string;
theme: TerminalTheme;
source: AppearanceSource;
appThemeUpdate: FollowAppTerminalThemeSelection | null;
};
export type TerminalAppearanceSettings = {
terminalThemeId: string;
terminalThemeDarkId: string;
terminalThemeLightId: string;
followAppTerminalTheme: boolean;
resolvedTheme: 'light' | 'dark';
lightUiThemeId: string;
darkUiThemeId: string;
accentMode: 'theme' | 'custom';
customAccent: string;
};
export type TerminalAppearanceHostScope = {
host: Host | null;
isEphemeral: boolean;
};
export type ResolveTerminalAppearanceInput = {
userIntent: ThemeUserIntent;
settings: TerminalAppearanceSettings;
hostScope: TerminalAppearanceHostScope;
customThemes: TerminalTheme[];
};
export const idleThemeUserIntent = (): ThemeUserIntent => ({ kind: 'idle' });
export const pickingThemeUserIntent = (
themeId: string,
options?: { startedAt?: number; scopeHostId?: string | null },
): ThemeUserIntent => ({
kind: 'picking',
themeId,
startedAt: options?.startedAt ?? Date.now(),
...(options?.scopeHostId !== undefined ? { scopeHostId: options.scopeHostId } : {}),
});
export function isFollowAppIntentSettled(
themeId: string,
settings: Pick<
TerminalAppearanceSettings,
'resolvedTheme' | 'lightUiThemeId' | 'darkUiThemeId' | 'terminalThemeId'
>,
): boolean {
const selection = getFollowAppTerminalThemeSelectionUpdate(themeId);
if (!selection) return true;
if (settings.resolvedTheme !== selection.appTheme) return false;
const activeUiThemeId = selection.appTheme === 'dark'
? settings.darkUiThemeId
: settings.lightUiThemeId;
if (activeUiThemeId !== selection.uiThemeId) return false;
return resolveFollowedTerminalThemeId({
resolvedTheme: settings.resolvedTheme,
lightUiThemeId: settings.lightUiThemeId,
darkUiThemeId: settings.darkUiThemeId,
fallbackThemeId: settings.terminalThemeId,
}) === themeId;
}
function resolveFollowAppThemeId(
userIntent: ThemeUserIntent,
settings: TerminalAppearanceSettings,
): { themeId: string; source: AppearanceSource } {
if (userIntent.kind === 'picking' && isFollowAppTerminalThemeId(userIntent.themeId)) {
return { themeId: userIntent.themeId, source: 'intent' };
}
const themeId = resolveFollowedTerminalThemeId({
resolvedTheme: settings.resolvedTheme,
lightUiThemeId: settings.lightUiThemeId,
darkUiThemeId: settings.darkUiThemeId,
fallbackThemeId: settings.terminalThemeId,
});
return { themeId, source: 'follow-app' };
}
function resolveManualThemeId(
userIntent: ThemeUserIntent,
settings: TerminalAppearanceSettings,
hostScope: TerminalAppearanceHostScope,
): { themeId: string; source: AppearanceSource } {
if (userIntent.kind === 'picking') {
const scopeHostId = userIntent.scopeHostId ?? null;
const hostId = hostScope.host?.id ?? null;
if (scopeHostId === null) {
if (hostScope.isEphemeral || !hostScope.host) {
return { themeId: userIntent.themeId, source: 'intent' };
}
} else if (hostId === scopeHostId) {
return { themeId: userIntent.themeId, source: 'intent' };
}
}
const globalThemeId = resolveManualTerminalThemeId({
resolvedTheme: settings.resolvedTheme,
terminalThemeDarkId: settings.terminalThemeDarkId,
terminalThemeLightId: settings.terminalThemeLightId,
lightUiThemeId: settings.lightUiThemeId,
darkUiThemeId: settings.darkUiThemeId,
fallbackThemeId: settings.terminalThemeId,
});
if (hostScope.isEphemeral || !hostScope.host) {
return { themeId: globalThemeId, source: 'manual-global' };
}
const hostThemeId = resolveHostTerminalThemeId(hostScope.host, globalThemeId);
if (hostThemeId !== globalThemeId) {
return { themeId: hostThemeId, source: 'host-override' };
}
return { themeId: globalThemeId, source: 'manual-global' };
}
function lookupTheme(themeId: string, customThemes: TerminalTheme[]): TerminalTheme {
return getBuiltinTerminalThemeById(themeId)
|| customThemes.find((theme) => theme.id === themeId)
|| getBuiltinTerminalThemeById(TERMINAL_THEMES[0].id)
|| TERMINAL_THEMES[0];
}
export function resolveTerminalAppearance({
userIntent,
settings,
hostScope,
customThemes,
}: ResolveTerminalAppearanceInput): ResolvedAppearance {
const { themeId, source } = settings.followAppTerminalTheme
? resolveFollowAppThemeId(userIntent, settings)
: resolveManualThemeId(userIntent, settings, hostScope);
const baseTheme = lookupTheme(themeId, customThemes);
const theme = applyCustomAccentToTerminalTheme(
baseTheme,
settings.accentMode,
settings.customAccent,
);
const appThemeUpdate = settings.followAppTerminalTheme && userIntent.kind === 'picking'
? getFollowAppTerminalThemeSelectionUpdate(userIntent.themeId)
: null;
return {
themeId,
theme,
source,
appThemeUpdate,
};
}
export function resolveGlobalTerminalAppearance(
input: Omit<ResolveTerminalAppearanceInput, 'hostScope'> & {
hostScope?: TerminalAppearanceHostScope;
},
): ResolvedAppearance {
return resolveTerminalAppearance({
...input,
hostScope: input.hostScope ?? { host: null, isEphemeral: true },
});
}
/** Pane-level theme id for workspace sessions (follow-app ignores host overrides). */
export function resolveSessionAppearanceThemeId(
followAppTerminalTheme: boolean,
globalThemeId: string,
host: Host | null,
): string {
if (followAppTerminalTheme) return globalThemeId;
return resolveHostTerminalThemeId(host, globalThemeId);
}