|
| 1 | +import { Component, Show, createMemo, createResource } from "solid-js" |
| 2 | +import { ButtonV2 } from "@opencode-ai/ui/v2/button-v2" |
| 3 | +import { SelectV2 } from "@opencode-ai/ui/v2/select-v2" |
| 4 | +import { Switch } from "@opencode-ai/ui/v2/switch-v2" |
| 5 | +import { TextInputV2 } from "@opencode-ai/ui/v2/text-input-v2" |
| 6 | +import { useLanguage } from "@/context/language" |
| 7 | +import { usePlatform } from "@/context/platform" |
| 8 | +import { ExternalLink } from "../external-link" |
| 9 | +import { SettingsListV2 } from "./parts/list" |
| 10 | +import { SettingsRowV2 } from "./parts/row" |
| 11 | +import { |
| 12 | + createAppearanceSettingsController, |
| 13 | + type AppearanceSettingsController, |
| 14 | +} from "./general-controllers" |
| 15 | +import "./settings-v2.css" |
| 16 | + |
| 17 | +const schemeOptions: ("system" | "light" | "dark")[] = ["system", "light", "dark"] |
| 18 | +const fontSettings = { |
| 19 | + ui: { |
| 20 | + action: "settings-ui-font", |
| 21 | + title: "settings.general.row.uiFont.title", |
| 22 | + description: "settings.general.row.uiFont.description", |
| 23 | + font: "ui", |
| 24 | + input: "setUI", |
| 25 | + }, |
| 26 | + code: { |
| 27 | + action: "settings-code-font", |
| 28 | + title: "settings.general.row.font.title", |
| 29 | + description: "settings.general.row.font.description", |
| 30 | + font: "code", |
| 31 | + input: "setCode", |
| 32 | + }, |
| 33 | + terminal: { |
| 34 | + action: "settings-terminal-font", |
| 35 | + title: "settings.general.row.terminalFont.title", |
| 36 | + description: "settings.general.row.terminalFont.description", |
| 37 | + font: "terminal", |
| 38 | + input: "setTerminal", |
| 39 | + }, |
| 40 | +} as const |
| 41 | + |
| 42 | +const FontSetting: Component<{ |
| 43 | + kind: "ui" | "code" | "terminal" |
| 44 | + fonts: AppearanceSettingsController["fonts"] |
| 45 | +}> = (props) => { |
| 46 | + const language = useLanguage() |
| 47 | + const config = () => fontSettings[props.kind] |
| 48 | + return ( |
| 49 | + <SettingsRowV2 title={language.t(config().title)} description={language.t(config().description)}> |
| 50 | + <div class="w-full sm:w-[220px]"> |
| 51 | + <TextInputV2 |
| 52 | + data-action={config().action} |
| 53 | + type="text" |
| 54 | + appearance="base" |
| 55 | + value={props.fonts[config().font]().value} |
| 56 | + onInput={(event) => props.fonts[config().input](event.currentTarget.value)} |
| 57 | + placeholder={props.fonts[config().font]().placeholder} |
| 58 | + spellcheck={false} |
| 59 | + autocorrect="off" |
| 60 | + autocomplete="off" |
| 61 | + autocapitalize="off" |
| 62 | + aria-label={language.t(config().title)} |
| 63 | + style={{ "font-family": props.fonts[config().font]().family }} |
| 64 | + /> |
| 65 | + </div> |
| 66 | + </SettingsRowV2> |
| 67 | + ) |
| 68 | +} |
| 69 | + |
| 70 | +export const SettingsAppearanceV2: Component = () => { |
| 71 | + const language = useLanguage() |
| 72 | + const platform = usePlatform() |
| 73 | + const appearance = createAppearanceSettingsController() |
| 74 | + const desktop = createMemo(() => platform.platform === "desktop") |
| 75 | + |
| 76 | + const [pinchZoom, { mutate: setPinchZoom }] = createResource( |
| 77 | + () => desktop() && "getPinchZoomEnabled" in platform, |
| 78 | + () => Promise.resolve(platform.getPinchZoomEnabled?.() ?? false).catch(() => false), |
| 79 | + { initialValue: false }, |
| 80 | + ) |
| 81 | + |
| 82 | + const onPinchZoomChange = (checked: boolean) => { |
| 83 | + setPinchZoom(checked) |
| 84 | + const update = platform.setPinchZoomEnabled?.(checked) |
| 85 | + if (!update) return |
| 86 | + void update.catch(() => setPinchZoom(!checked)) |
| 87 | + } |
| 88 | + |
| 89 | + const restoreDefaults = () => { |
| 90 | + appearance.scheme.select("system") |
| 91 | + appearance.fonts.setUI("") |
| 92 | + appearance.fonts.setCode("") |
| 93 | + appearance.fonts.setTerminal("") |
| 94 | + } |
| 95 | + |
| 96 | + return ( |
| 97 | + <> |
| 98 | + <div class="settings-v2-tab-header"> |
| 99 | + <div class="settings-v2-tab-header-row"> |
| 100 | + <h2 class="settings-v2-tab-title">{language.t("settings.general.section.appearance")}</h2> |
| 101 | + <ButtonV2 size="small" variant="ghost-muted" onClick={restoreDefaults}> |
| 102 | + {language.t("common.reset")} |
| 103 | + </ButtonV2> |
| 104 | + </div> |
| 105 | + </div> |
| 106 | + |
| 107 | + <div class="settings-v2-tab-body"> |
| 108 | + <div class="settings-v2-section"> |
| 109 | + <SettingsListV2> |
| 110 | + <SettingsRowV2 |
| 111 | + title={language.t("settings.general.row.colorScheme.title")} |
| 112 | + description={language.t("settings.general.row.colorScheme.description")} |
| 113 | + > |
| 114 | + <SelectV2 |
| 115 | + appearance="inline" |
| 116 | + data-action="settings-color-scheme" |
| 117 | + options={schemeOptions} |
| 118 | + current={schemeOptions.find((option) => option === appearance.scheme.current())} |
| 119 | + placement="bottom-end" |
| 120 | + gutter={6} |
| 121 | + label={(option) => { |
| 122 | + if (option === "system") return language.t("theme.scheme.system") |
| 123 | + if (option === "light") return language.t("theme.scheme.light") |
| 124 | + return language.t("theme.scheme.dark") |
| 125 | + }} |
| 126 | + onSelect={(option) => option && appearance.scheme.select(option)} |
| 127 | + /> |
| 128 | + </SettingsRowV2> |
| 129 | + |
| 130 | + <SettingsRowV2 |
| 131 | + title={language.t("settings.general.row.theme.title")} |
| 132 | + description={ |
| 133 | + <> |
| 134 | + {language.t("settings.general.row.theme.description")}{" "} |
| 135 | + <ExternalLink class="settings-v2-link" href="https://opencode.ai/docs/themes/"> |
| 136 | + {language.t("common.learnMore")} |
| 137 | + </ExternalLink> |
| 138 | + </> |
| 139 | + } |
| 140 | + > |
| 141 | + <SelectV2 |
| 142 | + appearance="inline" |
| 143 | + data-action="settings-theme" |
| 144 | + options={appearance.theme.options()} |
| 145 | + current={appearance.theme.current()} |
| 146 | + placement="bottom-end" |
| 147 | + gutter={6} |
| 148 | + value={(option) => option.id} |
| 149 | + label={(option) => option.name} |
| 150 | + onSelect={appearance.theme.select} |
| 151 | + /> |
| 152 | + </SettingsRowV2> |
| 153 | + |
| 154 | + <FontSetting kind="ui" fonts={appearance.fonts} /> |
| 155 | + <FontSetting kind="code" fonts={appearance.fonts} /> |
| 156 | + <FontSetting kind="terminal" fonts={appearance.fonts} /> |
| 157 | + </SettingsListV2> |
| 158 | + </div> |
| 159 | + |
| 160 | + <Show when={desktop()}> |
| 161 | + <div class="settings-v2-section"> |
| 162 | + <h3 class="settings-v2-section-title">{language.t("settings.general.section.display")}</h3> |
| 163 | + <SettingsListV2> |
| 164 | + <SettingsRowV2 |
| 165 | + title={language.t("settings.general.row.pinchZoom.title")} |
| 166 | + description={language.t("settings.general.row.pinchZoom.description")} |
| 167 | + > |
| 168 | + <div data-action="settings-pinch-zoom"> |
| 169 | + <Switch checked={pinchZoom.latest} onChange={onPinchZoomChange} /> |
| 170 | + </div> |
| 171 | + </SettingsRowV2> |
| 172 | + </SettingsListV2> |
| 173 | + </div> |
| 174 | + </Show> |
| 175 | + </div> |
| 176 | + </> |
| 177 | + ) |
| 178 | +} |
0 commit comments