|
| 1 | +import { createMemo, createResource, onMount, type Accessor } from "solid-js" |
| 2 | +import type { ColorScheme } from "@opencode-ai/ui/theme/context" |
| 3 | +import { useTheme } from "@opencode-ai/ui/theme/context" |
| 4 | +import { usePermission } from "@/context/permission" |
| 5 | +import { useServerSDK } from "@/context/server-sdk" |
| 6 | +import { useServerSync } from "@/context/server-sync" |
| 7 | +import { |
| 8 | + monoDefault, |
| 9 | + monoFontFamily, |
| 10 | + monoInput, |
| 11 | + sansDefault, |
| 12 | + sansFontFamily, |
| 13 | + sansInput, |
| 14 | + terminalDefault, |
| 15 | + terminalFontFamily, |
| 16 | + terminalInput, |
| 17 | + useSettings, |
| 18 | +} from "@/context/settings" |
| 19 | +import { playSoundById, SOUND_OPTIONS } from "@/utils/sound" |
| 20 | +import { createSoundPreviewController, type ShellOption } from "./general-controller-behavior" |
| 21 | + |
| 22 | +export { createShellOptions, createSoundPreviewController } from "./general-controller-behavior" |
| 23 | +export type { ShellOption, ShellSelectOption } from "./general-controller-behavior" |
| 24 | + |
| 25 | +export function createPermissionScopeController(sessionID: Accessor<string | undefined>) { |
| 26 | + const permission = usePermission() |
| 27 | + const serverSync = useServerSync() |
| 28 | + const directory = createMemo(() => { |
| 29 | + const id = sessionID() |
| 30 | + if (!id) return undefined |
| 31 | + return serverSync().session.lineage.peek(id)?.session.directory |
| 32 | + }) |
| 33 | + |
| 34 | + return { |
| 35 | + accepting: createMemo(() => { |
| 36 | + const id = sessionID() |
| 37 | + const dir = directory() |
| 38 | + if (!id || !dir) return false |
| 39 | + return permission.isAutoAccepting(id, dir) |
| 40 | + }), |
| 41 | + enabled: createMemo(() => !!directory()), |
| 42 | + set: (checked: boolean) => { |
| 43 | + const id = sessionID() |
| 44 | + const dir = directory() |
| 45 | + if (!id || !dir) return |
| 46 | + if (checked) return permission.enableAutoAccept(id, dir) |
| 47 | + permission.disableAutoAccept(id, dir) |
| 48 | + }, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +export function createShellSettingsController() { |
| 53 | + const serverSdk = useServerSDK() |
| 54 | + const serverSync = useServerSync() |
| 55 | + const [shells] = createResource( |
| 56 | + async () => { |
| 57 | + const sdk = serverSdk() |
| 58 | + if ((await sdk.protocol) === "v1") return (await sdk.client.pty.shells()).data ?? [] |
| 59 | + return [] as ShellOption[] |
| 60 | + }, |
| 61 | + { initialValue: [] as ShellOption[] }, |
| 62 | + ) |
| 63 | + const current = createMemo(() => serverSync().data.config.shell ?? "") |
| 64 | + |
| 65 | + return { |
| 66 | + shells: () => shells.latest, |
| 67 | + current, |
| 68 | + select: (value: string) => { |
| 69 | + if (value === current()) return |
| 70 | + void serverSync().updateConfig({ shell: value }) |
| 71 | + }, |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +export function createAppearanceSettingsController() { |
| 76 | + const settings = useSettings() |
| 77 | + const theme = useTheme() |
| 78 | + const themes = createMemo(() => theme.ids().map((id) => ({ id, name: theme.name(id) }))) |
| 79 | + |
| 80 | + onMount(() => void theme.loadThemes()) |
| 81 | + |
| 82 | + return { |
| 83 | + scheme: { |
| 84 | + current: theme.colorScheme, |
| 85 | + select: (value: ColorScheme) => theme.setColorScheme(value), |
| 86 | + }, |
| 87 | + theme: { |
| 88 | + options: themes, |
| 89 | + current: createMemo(() => themes().find((option) => option.id === theme.themeId())), |
| 90 | + select: (option: { id: string } | null) => option && theme.setTheme(option.id), |
| 91 | + }, |
| 92 | + fonts: { |
| 93 | + ui: createMemo(() => ({ |
| 94 | + value: sansInput(settings.appearance.uiFont()), |
| 95 | + family: sansFontFamily(settings.appearance.uiFont()), |
| 96 | + placeholder: sansDefault, |
| 97 | + })), |
| 98 | + code: createMemo(() => ({ |
| 99 | + value: monoInput(settings.appearance.font()), |
| 100 | + family: monoFontFamily(settings.appearance.font()), |
| 101 | + placeholder: monoDefault, |
| 102 | + })), |
| 103 | + terminal: createMemo(() => ({ |
| 104 | + value: terminalInput(settings.appearance.terminalFont()), |
| 105 | + family: terminalFontFamily(settings.appearance.terminalFont()), |
| 106 | + placeholder: terminalDefault, |
| 107 | + })), |
| 108 | + setUI: (value: string) => settings.appearance.setUIFont(value), |
| 109 | + setCode: (value: string) => settings.appearance.setFont(value), |
| 110 | + setTerminal: (value: string) => settings.appearance.setTerminalFont(value), |
| 111 | + }, |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +const noneSound = { id: "none", label: "sound.option.none" } as const |
| 116 | +export const soundOptions = [noneSound, ...SOUND_OPTIONS] |
| 117 | +export type SoundSelectOption = (typeof soundOptions)[number] |
| 118 | + |
| 119 | +export function createSoundSettingsController() { |
| 120 | + const settings = useSettings() |
| 121 | + const preview = createSoundPreviewController(playSoundById) |
| 122 | + const channel = ( |
| 123 | + enabled: Accessor<boolean>, |
| 124 | + current: Accessor<string>, |
| 125 | + setEnabled: (value: boolean) => void, |
| 126 | + set: (id: string) => void, |
| 127 | + ) => ({ |
| 128 | + current: createMemo(() => |
| 129 | + enabled() ? (soundOptions.find((option) => option.id === current()) ?? noneSound) : noneSound, |
| 130 | + ), |
| 131 | + highlight: (option: SoundSelectOption | undefined) => { |
| 132 | + if (!option) return |
| 133 | + preview.play(option.id === "none" ? undefined : option.id) |
| 134 | + }, |
| 135 | + select: (option: SoundSelectOption | null) => { |
| 136 | + if (!option) return |
| 137 | + if (option.id === "none") { |
| 138 | + setEnabled(false) |
| 139 | + preview.stop() |
| 140 | + return |
| 141 | + } |
| 142 | + setEnabled(true) |
| 143 | + set(option.id) |
| 144 | + preview.play(option.id) |
| 145 | + }, |
| 146 | + }) |
| 147 | + |
| 148 | + return { |
| 149 | + agent: channel( |
| 150 | + settings.sounds.agentEnabled, |
| 151 | + settings.sounds.agent, |
| 152 | + (value) => settings.sounds.setAgentEnabled(value), |
| 153 | + (id) => settings.sounds.setAgent(id), |
| 154 | + ), |
| 155 | + permissions: channel( |
| 156 | + settings.sounds.permissionsEnabled, |
| 157 | + settings.sounds.permissions, |
| 158 | + (value) => settings.sounds.setPermissionsEnabled(value), |
| 159 | + (id) => settings.sounds.setPermissions(id), |
| 160 | + ), |
| 161 | + errors: channel( |
| 162 | + settings.sounds.errorsEnabled, |
| 163 | + settings.sounds.errors, |
| 164 | + (value) => settings.sounds.setErrorsEnabled(value), |
| 165 | + (id) => settings.sounds.setErrors(id), |
| 166 | + ), |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +export type PermissionScopeController = ReturnType<typeof createPermissionScopeController> |
| 171 | +export type ShellSettingsController = ReturnType<typeof createShellSettingsController> |
| 172 | +export type AppearanceSettingsController = ReturnType<typeof createAppearanceSettingsController> |
| 173 | +export type SoundSettingsController = ReturnType<typeof createSoundSettingsController> |
0 commit comments