Skip to content

Commit 586fa90

Browse files
authored
fix(tui): draw text selections in theme colours (#65776)
1 parent 924b560 commit 586fa90

7 files changed

Lines changed: 63 additions & 5 deletions

File tree

‎packages/theme/src/tui/expand.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ function expandText(definition: TextDefinition | undefined): TextDefinition | un
5050
...definition,
5151
muted: definition.muted ?? (definition.base ? "$text.base" : undefined),
5252
faint: definition.faint ?? (definition.muted || definition.base ? "$text.muted" : undefined),
53+
// Selected text reads like the focused row of a list unless the theme picks its own colors.
54+
selection: definition.selection ?? (definition.base ? "$text.action.primary.$focused" : undefined),
5355
action: expandActions(definition.action, "text.action"),
5456
formfield: expandFormfield(definition.formfield, "text.formfield"),
5557
feedback: definition.feedback
@@ -72,6 +74,7 @@ function expandBackground(definition: BackgroundDefinition | undefined): Backgro
7274
if (!definition) return
7375
return {
7476
...definition,
77+
selection: definition.selection ?? (definition.base ? "$background.action.primary.$focused" : undefined),
7578
action: expandActions(definition.action, "background.action"),
7679
formfield: expandFormfield(definition.formfield, "background.formfield"),
7780
}

‎packages/theme/src/tui/schema.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ const TextDefinition = Schema.Struct({
9090
base: Schema.optional(ColorValue),
9191
muted: Schema.optional(ColorValue),
9292
faint: Schema.optional(ColorValue),
93+
selection: Schema.optional(ColorValue),
9394
action: Schema.optional(ActionColorDefinition),
9495
formfield: Schema.optional(StatefulColorDefinition),
9596
feedback: Schema.optional(
@@ -105,6 +106,7 @@ export type TextDefinition = Schema.Schema.Type<typeof TextDefinition>
105106

106107
const BackgroundDefinition = Schema.Struct({
107108
base: Schema.optional(ColorValue),
109+
selection: Schema.optional(ColorValue),
108110
raised: Schema.optional(
109111
Schema.Struct({
110112
base: Schema.optional(ColorValue),
@@ -230,6 +232,7 @@ const CompleteThemeTokensDefinition = Schema.Struct({
230232
base: ColorValue,
231233
muted: ColorValue,
232234
faint: Schema.optional(ColorValue),
235+
selection: Schema.optional(ColorValue),
233236
action: CompleteActionColorDefinition,
234237
formfield: CompleteStatefulColorDefinition,
235238
feedback: Schema.Struct({
@@ -241,6 +244,7 @@ const CompleteThemeTokensDefinition = Schema.Struct({
241244
}),
242245
background: Schema.Struct({
243246
base: ColorValue,
247+
selection: Schema.optional(ColorValue),
244248
raised: Schema.Struct({ base: ColorValue, high: ColorValue, max: ColorValue }),
245249
action: CompleteActionColorDefinition,
246250
formfield: CompleteStatefulColorDefinition,

‎packages/theme/src/tui/types.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,16 @@ export type ResolvedThemeTokens = {
3232
readonly base: RGBA
3333
readonly muted: RGBA
3434
readonly faint: RGBA
35+
/** Text inside a mouse or keyboard text selection. */
36+
readonly selection: RGBA
3537
readonly action: Readonly<Record<ActionVariant, StatefulColor>>
3638
readonly formfield: FormfieldColor
3739
readonly feedback: Readonly<Record<FeedbackKind, { readonly base: RGBA; readonly muted: RGBA }>>
3840
}
3941
readonly background: {
4042
readonly base: RGBA
43+
/** The highlight behind a mouse or keyboard text selection. */
44+
readonly selection: RGBA
4145
readonly raised: {
4246
readonly base: RGBA
4347
readonly high: RGBA

‎packages/tui/src/context/theme.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ const themeContext = createSimpleContext({
333333
const current = createComponentTheme(tokens)
334334

335335
createEffect(() => renderer.setBackgroundColor(tokens().background.base))
336+
createEffect(() => renderer.setSelectionColors(tokens().background.selection, tokens().text.selection))
336337

337338
const currentSyntax = createSyntaxStyleMemo(() => generateSyntax(tokens(), renderer.nativeScene))
338339
const service: Themes = {

‎packages/tui/test/theme/v2/resolve.test.ts‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@ import {
55
resolveTheme,
66
resolveThemeDocument,
77
selectTheme,
8+
themeModes,
89
type HueScale,
910
type Mode,
1011
type ResolvedTheme,
1112
type ThemeDefinition,
1213
} from "@opencode/theme/tui"
13-
import { getOpenCodeTheme, getOpenCodeClassicTheme, parseTheme, type ThemeDocumentSource } from "../../../src/theme"
14+
import {
15+
allThemes,
16+
getOpenCodeTheme,
17+
getOpenCodeClassicTheme,
18+
parseTheme,
19+
type ThemeDocumentSource,
20+
} from "../../../src/theme"
1421

1522
const opencodeLight = selectTheme(getOpenCodeClassicTheme(), "light")
1623
const opencodeDark = selectTheme(getOpenCodeClassicTheme(), "dark")
@@ -300,6 +307,43 @@ test("defaults faint text to muted unless a theme sets it", () => {
300307
expect(explicit.text.faint.toInts()).toEqual([18, 52, 86, 255])
301308
})
302309

310+
test("defaults the text selection to the focused primary action unless a theme sets it", () => {
311+
const fallback = resolveTheme(light)
312+
const explicit = resolveTheme({
313+
...light,
314+
text: { ...light.text, selection: "#123456" },
315+
background: { ...light.background, selection: "#abcdef" },
316+
})
317+
318+
expect(fallback.text.selection).toBe(fallback.text.action.primary.focused)
319+
expect(fallback.background.selection).toBe(fallback.background.action.primary.focused)
320+
expect(fallback.background.selection.equals(fallback.text.selection)).toBeFalse()
321+
expect(explicit.text.selection.toInts()).toEqual([18, 52, 86, 255])
322+
expect(explicit.background.selection.toInts()).toEqual([171, 205, 239, 255])
323+
})
324+
325+
test("resolves an opaque text selection in every built-in theme, readable in the default ones", () => {
326+
const contrast = (a: RGBA, b: RGBA) => {
327+
const luminance = (color: RGBA) =>
328+
[color.r, color.g, color.b]
329+
.map((value) => (value <= 0.03928 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4))
330+
.reduce((sum, value, index) => sum + value * [0.2126, 0.7152, 0.0722][index]!, 0)
331+
const [high, low] = [luminance(a), luminance(b)].sort((x, y) => y - x)
332+
return (high! + 0.05) / (low! + 0.05)
333+
}
334+
const failures = Object.entries(allThemes()).flatMap(([name, source]) => {
335+
const document = parseTheme(source, name)
336+
return themeModes(document).flatMap((mode) => {
337+
const theme = resolveThemeDocument(document, mode)
338+
const ratio = contrast(theme.text.selection, theme.background.selection)
339+
const readable = !["opencode", "system"].includes(name) || ratio >= 4.5
340+
return theme.background.selection.a === 1 && readable ? [] : [`${name} ${mode}: ${ratio.toFixed(2)}`]
341+
})
342+
})
343+
344+
expect(failures).toEqual([])
345+
})
346+
303347
test("resolves dialog surfaces from direct colors", () => {
304348
const theme = resolveSource(
305349
{

‎services/site/src/docs/content/cli/theme.mdx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ Semantic tokens assign colors to UI roles. Components use these roles instead of
175175

176176
| Group | Purpose |
177177
| --- | --- |
178-
| `text` | Base, muted, faint, action, form-field, and feedback text. `faint` defaults to `muted`. |
179-
| `background` | Base, raised surfaces, actions, form fields, and feedback fills. |
178+
| `text` | Base, muted, faint, selection, action, form-field, and feedback text. `faint` defaults to `muted`. |
179+
| `background` | Base, selection, raised surfaces, actions, form fields, and feedback fills. |
180180
| `border`, `scrollbar` | Border and scrollbar colors. |
181181
| `diff` | Added, removed, context, highlight, and line-number colors. |
182182
| `syntax` | Source-code highlighting. |
@@ -201,6 +201,8 @@ For example, `text.muted` controls secondary text while `background.raised` defi
201201
}
202202
```
203203

204+
`text.selection` and `background.selection` color text you select with the mouse or keyboard. They default to the focused primary action, `text.action.primary.$focused` on `background.action.primary.$focused`.
205+
204206
Syntax keys are `comment`, `keyword`, `function`, `variable`, `string`, `number`, `type`, `operator`, and `punctuation`.
205207

206208
Markdown keys are `text`, `heading`, `link`, `linkText`, `code`, `blockQuote`, `emphasis`, `strong`, `horizontalRule`, `listItem`, `listEnumeration`, `image`, `imageText`, and `codeBlock`.

‎services/site/src/docs/content/themes.mdx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ palette:
222222

223223
| Group | Purpose |
224224
| ------------ | ------------------------------------------------------------- |
225-
| `text` | Base, muted, faint, action, form-field, and feedback text |
226-
| `background` | Base, surfaces, actions, form fields, and feedback fills |
225+
| `text` | Base, muted, faint, selection, action, form-field, and feedback text |
226+
| `background` | Base, selection, surfaces, actions, form fields, and feedback fills |
227227
| `border` | Borders |
228228
| `scrollbar` | Scrollbars |
229229
| `diff` | Added, removed, context, highlight, and line-number colors |

0 commit comments

Comments
 (0)