|
| 1 | +import { stdin as input, stdout as output } from "node:process"; |
| 2 | +import type { |
| 3 | + DashboardAccentColor, |
| 4 | + DashboardDisplaySettings, |
| 5 | + DashboardThemePreset, |
| 6 | +} from "../dashboard-settings.js"; |
| 7 | +import type { UI_COPY } from "../ui/copy.js"; |
| 8 | +import { getUiRuntimeOptions } from "../ui/runtime.js"; |
| 9 | +import { type MenuItem, select } from "../ui/select.js"; |
| 10 | + |
| 11 | +export type ThemeConfigAction = |
| 12 | + | { type: "set-palette"; palette: DashboardThemePreset } |
| 13 | + | { type: "set-accent"; accent: DashboardAccentColor } |
| 14 | + | { type: "reset" } |
| 15 | + | { type: "save" } |
| 16 | + | { type: "cancel" }; |
| 17 | + |
| 18 | +export interface ThemeSettingsPanelDeps { |
| 19 | + cloneDashboardSettings: ( |
| 20 | + settings: DashboardDisplaySettings, |
| 21 | + ) => DashboardDisplaySettings; |
| 22 | + applyDashboardDefaultsForKeys: ( |
| 23 | + draft: DashboardDisplaySettings, |
| 24 | + keys: readonly (keyof DashboardDisplaySettings)[], |
| 25 | + ) => DashboardDisplaySettings; |
| 26 | + applyUiThemeFromDashboardSettings: ( |
| 27 | + settings: DashboardDisplaySettings, |
| 28 | + ) => void; |
| 29 | + THEME_PRESET_OPTIONS: readonly DashboardThemePreset[]; |
| 30 | + ACCENT_COLOR_OPTIONS: readonly DashboardAccentColor[]; |
| 31 | + THEME_PANEL_KEYS: readonly (keyof DashboardDisplaySettings)[]; |
| 32 | + UI_COPY: typeof UI_COPY; |
| 33 | +} |
| 34 | + |
| 35 | +export async function promptThemeSettingsPanel( |
| 36 | + initial: DashboardDisplaySettings, |
| 37 | + deps: ThemeSettingsPanelDeps, |
| 38 | +): Promise<DashboardDisplaySettings | null> { |
| 39 | + if (!input.isTTY || !output.isTTY) return null; |
| 40 | + const baseline = deps.cloneDashboardSettings(initial); |
| 41 | + let draft = deps.cloneDashboardSettings(initial); |
| 42 | + let focus: ThemeConfigAction = { |
| 43 | + type: "set-palette", |
| 44 | + palette: draft.uiThemePreset ?? "green", |
| 45 | + }; |
| 46 | + |
| 47 | + while (true) { |
| 48 | + const ui = getUiRuntimeOptions(); |
| 49 | + const palette = draft.uiThemePreset ?? "green"; |
| 50 | + const accent = draft.uiAccentColor ?? "green"; |
| 51 | + const paletteItems: MenuItem<ThemeConfigAction>[] = |
| 52 | + deps.THEME_PRESET_OPTIONS.map((candidate, index) => { |
| 53 | + const color: MenuItem<ThemeConfigAction>["color"] = |
| 54 | + palette === candidate ? "green" : "yellow"; |
| 55 | + return { |
| 56 | + label: `${palette === candidate ? "[x]" : "[ ]"} ${index + 1}. ${candidate === "green" ? "Green base" : "Blue base"}`, |
| 57 | + hint: |
| 58 | + candidate === "green" |
| 59 | + ? "High-contrast default." |
| 60 | + : "Codex-style blue look.", |
| 61 | + value: { type: "set-palette", palette: candidate }, |
| 62 | + color, |
| 63 | + }; |
| 64 | + }); |
| 65 | + const accentItems: MenuItem<ThemeConfigAction>[] = |
| 66 | + deps.ACCENT_COLOR_OPTIONS.map((candidate) => { |
| 67 | + const color: MenuItem<ThemeConfigAction>["color"] = |
| 68 | + accent === candidate ? "green" : "yellow"; |
| 69 | + return { |
| 70 | + label: `${accent === candidate ? "[x]" : "[ ]"} ${candidate}`, |
| 71 | + value: { type: "set-accent", accent: candidate }, |
| 72 | + color, |
| 73 | + }; |
| 74 | + }); |
| 75 | + |
| 76 | + const items: MenuItem<ThemeConfigAction>[] = [ |
| 77 | + { |
| 78 | + label: deps.UI_COPY.settings.baseTheme, |
| 79 | + value: { type: "cancel" }, |
| 80 | + kind: "heading", |
| 81 | + }, |
| 82 | + ...paletteItems, |
| 83 | + { label: "", value: { type: "cancel" }, separator: true }, |
| 84 | + { |
| 85 | + label: deps.UI_COPY.settings.accentColor, |
| 86 | + value: { type: "cancel" }, |
| 87 | + kind: "heading", |
| 88 | + }, |
| 89 | + ...accentItems, |
| 90 | + { label: "", value: { type: "cancel" }, separator: true }, |
| 91 | + { |
| 92 | + label: deps.UI_COPY.settings.resetDefault, |
| 93 | + value: { type: "reset" }, |
| 94 | + color: "yellow", |
| 95 | + }, |
| 96 | + { |
| 97 | + label: deps.UI_COPY.settings.saveAndBack, |
| 98 | + value: { type: "save" }, |
| 99 | + color: "green", |
| 100 | + }, |
| 101 | + { |
| 102 | + label: deps.UI_COPY.settings.backNoSave, |
| 103 | + value: { type: "cancel" }, |
| 104 | + color: "red", |
| 105 | + }, |
| 106 | + ]; |
| 107 | + |
| 108 | + const initialCursor = items.findIndex((item) => { |
| 109 | + const value = item.value; |
| 110 | + if (value.type !== focus.type) return false; |
| 111 | + if (value.type === "set-palette" && focus.type === "set-palette") { |
| 112 | + return value.palette === focus.palette; |
| 113 | + } |
| 114 | + if (value.type === "set-accent" && focus.type === "set-accent") { |
| 115 | + return value.accent === focus.accent; |
| 116 | + } |
| 117 | + return true; |
| 118 | + }); |
| 119 | + |
| 120 | + const result = await select<ThemeConfigAction>(items, { |
| 121 | + message: deps.UI_COPY.settings.themeTitle, |
| 122 | + subtitle: deps.UI_COPY.settings.themeSubtitle, |
| 123 | + help: deps.UI_COPY.settings.themeHelp, |
| 124 | + clearScreen: true, |
| 125 | + theme: ui.theme, |
| 126 | + selectedEmphasis: "minimal", |
| 127 | + initialCursor: initialCursor >= 0 ? initialCursor : undefined, |
| 128 | + onCursorChange: ({ cursor }) => { |
| 129 | + const item = items[cursor]; |
| 130 | + if (item && !item.separator && item.kind !== "heading") { |
| 131 | + focus = item.value; |
| 132 | + } |
| 133 | + }, |
| 134 | + onInput: (raw) => { |
| 135 | + const lower = raw.toLowerCase(); |
| 136 | + if (lower === "q") return { type: "cancel" }; |
| 137 | + if (lower === "s") return { type: "save" }; |
| 138 | + if (lower === "r") return { type: "reset" }; |
| 139 | + if (raw === "1") return { type: "set-palette", palette: "green" }; |
| 140 | + if (raw === "2") return { type: "set-palette", palette: "blue" }; |
| 141 | + return undefined; |
| 142 | + }, |
| 143 | + }); |
| 144 | + |
| 145 | + if (!result || result.type === "cancel") { |
| 146 | + deps.applyUiThemeFromDashboardSettings(baseline); |
| 147 | + return null; |
| 148 | + } |
| 149 | + if (result.type === "save") return draft; |
| 150 | + if (result.type === "reset") { |
| 151 | + draft = deps.applyDashboardDefaultsForKeys(draft, deps.THEME_PANEL_KEYS); |
| 152 | + focus = { type: "set-palette", palette: draft.uiThemePreset ?? "green" }; |
| 153 | + deps.applyUiThemeFromDashboardSettings(draft); |
| 154 | + continue; |
| 155 | + } |
| 156 | + if (result.type === "set-palette") { |
| 157 | + draft = { ...draft, uiThemePreset: result.palette }; |
| 158 | + focus = result; |
| 159 | + deps.applyUiThemeFromDashboardSettings(draft); |
| 160 | + continue; |
| 161 | + } |
| 162 | + draft = { ...draft, uiAccentColor: result.accent }; |
| 163 | + focus = result; |
| 164 | + deps.applyUiThemeFromDashboardSettings(draft); |
| 165 | + } |
| 166 | +} |
0 commit comments