mirror of
https://github.com/aljazceru/opencode.git
synced 2026-01-07 09:54:56 +01:00
tui: simplify theme selection API by renaming setSelectedTheme to set
This commit is contained in:
@@ -12,15 +12,16 @@ export function DialogThemeList() {
|
|||||||
const dialog = useDialog()
|
const dialog = useDialog()
|
||||||
let confirmed = false
|
let confirmed = false
|
||||||
let ref: DialogSelectRef<keyof typeof THEMES>
|
let ref: DialogSelectRef<keyof typeof THEMES>
|
||||||
const initial = theme.selectedTheme
|
const initial = theme.selected
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
// highlight the first theme in the list when we open it for UX
|
// highlight the first theme in the list when we open it for UX
|
||||||
theme.setSelectedTheme(Object.keys(THEMES)[0] as keyof typeof THEMES)
|
theme.set(Object.keys(THEMES)[0] as keyof typeof THEMES)
|
||||||
})
|
})
|
||||||
|
|
||||||
onCleanup(() => {
|
onCleanup(() => {
|
||||||
// if we close the dialog without confirming, reset back to the initial theme
|
// if we close the dialog without confirming, reset back to the initial theme
|
||||||
if (!confirmed) theme.setSelectedTheme(initial)
|
if (!confirmed) theme.set(initial)
|
||||||
})
|
})
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -28,10 +29,10 @@ export function DialogThemeList() {
|
|||||||
title="Themes"
|
title="Themes"
|
||||||
options={options}
|
options={options}
|
||||||
onMove={(opt) => {
|
onMove={(opt) => {
|
||||||
theme.setSelectedTheme(opt.value)
|
theme.set(opt.value)
|
||||||
}}
|
}}
|
||||||
onSelect={(opt) => {
|
onSelect={(opt) => {
|
||||||
theme.setSelectedTheme(opt.value)
|
theme.set(opt.value)
|
||||||
confirmed = true
|
confirmed = true
|
||||||
dialog.clear()
|
dialog.clear()
|
||||||
}}
|
}}
|
||||||
@@ -40,12 +41,12 @@ export function DialogThemeList() {
|
|||||||
}}
|
}}
|
||||||
onFilter={(query) => {
|
onFilter={(query) => {
|
||||||
if (query.length === 0) {
|
if (query.length === 0) {
|
||||||
theme.setSelectedTheme(initial)
|
theme.set(initial)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const first = ref.filtered[0]
|
const first = ref.filtered[0]
|
||||||
if (first) theme.setSelectedTheme(first.value)
|
if (first) theme.set(first.value)
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -642,10 +642,11 @@ export const { use: useTheme, provider: ThemeProvider } = createSimpleContext({
|
|||||||
return values()[prop]
|
return values()[prop]
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
get selectedTheme() {
|
get selected() {
|
||||||
return kv.data.theme
|
return kv.data.theme
|
||||||
},
|
},
|
||||||
setSelectedTheme(theme: string) {
|
set(theme: string) {
|
||||||
|
if (!THEMES[theme]) return
|
||||||
setTheme(theme)
|
setTheme(theme)
|
||||||
kv.set("theme", theme)
|
kv.set("theme", theme)
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -59,7 +59,8 @@ export function DialogSelect<T>(props: DialogSelectProps<T>) {
|
|||||||
props.options,
|
props.options,
|
||||||
filter((x) => x.disabled !== true),
|
filter((x) => x.disabled !== true),
|
||||||
take(props.limit ?? Infinity),
|
take(props.limit ?? Infinity),
|
||||||
(x) => (!needle ? x : fuzzysort.go(needle, x, { keys: ["title", "category"] }).map((x) => x.obj)),
|
(x) =>
|
||||||
|
!needle ? x : fuzzysort.go(needle, x, { keys: ["title", "category"] }).map((x) => x.obj),
|
||||||
)
|
)
|
||||||
return result
|
return result
|
||||||
})
|
})
|
||||||
@@ -128,8 +129,10 @@ export function DialogSelect<T>(props: DialogSelectProps<T>) {
|
|||||||
if (evt.name === "pagedown") move(10)
|
if (evt.name === "pagedown") move(10)
|
||||||
if (evt.name === "return") {
|
if (evt.name === "return") {
|
||||||
const option = selected()
|
const option = selected()
|
||||||
if (option.onSelect) option.onSelect(dialog)
|
if (option) {
|
||||||
props.onSelect?.(option)
|
if (option.onSelect) option.onSelect(dialog)
|
||||||
|
props.onSelect?.(option)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const item of props.keybind ?? []) {
|
for (const item of props.keybind ?? []) {
|
||||||
@@ -206,11 +209,15 @@ export function DialogSelect<T>(props: DialogSelectProps<T>) {
|
|||||||
props.onSelect?.(option)
|
props.onSelect?.(option)
|
||||||
}}
|
}}
|
||||||
onMouseOver={() => {
|
onMouseOver={() => {
|
||||||
const index = filtered().findIndex((x) => isDeepEqual(x.value, option.value))
|
const index = filtered().findIndex((x) =>
|
||||||
|
isDeepEqual(x.value, option.value),
|
||||||
|
)
|
||||||
if (index === -1) return
|
if (index === -1) return
|
||||||
moveTo(index)
|
moveTo(index)
|
||||||
}}
|
}}
|
||||||
backgroundColor={active() ? (option.bg ?? theme.primary) : RGBA.fromInts(0, 0, 0, 0)}
|
backgroundColor={
|
||||||
|
active() ? (option.bg ?? theme.primary) : RGBA.fromInts(0, 0, 0, 0)
|
||||||
|
}
|
||||||
paddingLeft={1}
|
paddingLeft={1}
|
||||||
paddingRight={1}
|
paddingRight={1}
|
||||||
gap={1}
|
gap={1}
|
||||||
@@ -218,7 +225,9 @@ export function DialogSelect<T>(props: DialogSelectProps<T>) {
|
|||||||
<Option
|
<Option
|
||||||
title={option.title}
|
title={option.title}
|
||||||
footer={option.footer}
|
footer={option.footer}
|
||||||
description={option.description !== category ? option.description : undefined}
|
description={
|
||||||
|
option.description !== category ? option.description : undefined
|
||||||
|
}
|
||||||
active={active()}
|
active={active()}
|
||||||
current={isDeepEqual(option.value, props.current)}
|
current={isDeepEqual(option.value, props.current)}
|
||||||
/>
|
/>
|
||||||
@@ -234,7 +243,9 @@ export function DialogSelect<T>(props: DialogSelectProps<T>) {
|
|||||||
<For each={props.keybind ?? []}>
|
<For each={props.keybind ?? []}>
|
||||||
{(item) => (
|
{(item) => (
|
||||||
<text>
|
<text>
|
||||||
<span style={{ fg: theme.text, attributes: TextAttributes.BOLD }}>{Keybind.toString(item.keybind)}</span>
|
<span style={{ fg: theme.text, attributes: TextAttributes.BOLD }}>
|
||||||
|
{Keybind.toString(item.keybind)}
|
||||||
|
</span>
|
||||||
<span style={{ fg: theme.textMuted }}> {item.title}</span>
|
<span style={{ fg: theme.textMuted }}> {item.title}</span>
|
||||||
</text>
|
</text>
|
||||||
)}
|
)}
|
||||||
@@ -263,7 +274,10 @@ function Option(props: {
|
|||||||
wrapMode="none"
|
wrapMode="none"
|
||||||
>
|
>
|
||||||
{Locale.truncate(props.title, 62)}
|
{Locale.truncate(props.title, 62)}
|
||||||
<span style={{ fg: props.active ? theme.background : theme.textMuted }}> {props.description}</span>
|
<span style={{ fg: props.active ? theme.background : theme.textMuted }}>
|
||||||
|
{" "}
|
||||||
|
{props.description}
|
||||||
|
</span>
|
||||||
</text>
|
</text>
|
||||||
<Show when={props.footer}>
|
<Show when={props.footer}>
|
||||||
<box flexShrink={0}>
|
<box flexShrink={0}>
|
||||||
|
|||||||
Reference in New Issue
Block a user