theme.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. import { SyntaxStyle, RGBA } from "@opentui/core"
  2. import { createMemo, createSignal } from "solid-js"
  3. import { useSync } from "@tui/context/sync"
  4. import { createSimpleContext } from "./helper"
  5. import aura from "../../../../../../tui/internal/theme/themes/aura.json" with { type: "json" }
  6. import ayu from "../../../../../../tui/internal/theme/themes/ayu.json" with { type: "json" }
  7. import catppuccin from "../../../../../../tui/internal/theme/themes/catppuccin.json" with { type: "json" }
  8. import cobalt2 from "../../../../../../tui/internal/theme/themes/cobalt2.json" with { type: "json" }
  9. import dracula from "../../../../../../tui/internal/theme/themes/dracula.json" with { type: "json" }
  10. import everforest from "../../../../../../tui/internal/theme/themes/everforest.json" with { type: "json" }
  11. import github from "../../../../../../tui/internal/theme/themes/github.json" with { type: "json" }
  12. import gruvbox from "../../../../../../tui/internal/theme/themes/gruvbox.json" with { type: "json" }
  13. import kanagawa from "../../../../../../tui/internal/theme/themes/kanagawa.json" with { type: "json" }
  14. import material from "../../../../../../tui/internal/theme/themes/material.json" with { type: "json" }
  15. import matrix from "../../../../../../tui/internal/theme/themes/matrix.json" with { type: "json" }
  16. import monokai from "../../../../../../tui/internal/theme/themes/monokai.json" with { type: "json" }
  17. import nord from "../../../../../../tui/internal/theme/themes/nord.json" with { type: "json" }
  18. import onedark from "../../../../../../tui/internal/theme/themes/one-dark.json" with { type: "json" }
  19. import opencode from "../../../../../../tui/internal/theme/themes/opencode.json" with { type: "json" }
  20. import palenight from "../../../../../../tui/internal/theme/themes/palenight.json" with { type: "json" }
  21. import rosepine from "../../../../../../tui/internal/theme/themes/rosepine.json" with { type: "json" }
  22. import solarized from "../../../../../../tui/internal/theme/themes/solarized.json" with { type: "json" }
  23. import synthwave84 from "../../../../../../tui/internal/theme/themes/synthwave84.json" with { type: "json" }
  24. import tokyonight from "../../../../../../tui/internal/theme/themes/tokyonight.json" with { type: "json" }
  25. import vesper from "../../../../../../tui/internal/theme/themes/vesper.json" with { type: "json" }
  26. import zenburn from "../../../../../../tui/internal/theme/themes/zenburn.json" with { type: "json" }
  27. import { useKV } from "./kv"
  28. type Theme = {
  29. primary: RGBA
  30. secondary: RGBA
  31. accent: RGBA
  32. error: RGBA
  33. warning: RGBA
  34. success: RGBA
  35. info: RGBA
  36. text: RGBA
  37. textMuted: RGBA
  38. background: RGBA
  39. backgroundPanel: RGBA
  40. backgroundElement: RGBA
  41. border: RGBA
  42. borderActive: RGBA
  43. borderSubtle: RGBA
  44. diffAdded: RGBA
  45. diffRemoved: RGBA
  46. diffContext: RGBA
  47. diffHunkHeader: RGBA
  48. diffHighlightAdded: RGBA
  49. diffHighlightRemoved: RGBA
  50. diffAddedBg: RGBA
  51. diffRemovedBg: RGBA
  52. diffContextBg: RGBA
  53. diffLineNumber: RGBA
  54. diffAddedLineNumberBg: RGBA
  55. diffRemovedLineNumberBg: RGBA
  56. markdownText: RGBA
  57. markdownHeading: RGBA
  58. markdownLink: RGBA
  59. markdownLinkText: RGBA
  60. markdownCode: RGBA
  61. markdownBlockQuote: RGBA
  62. markdownEmph: RGBA
  63. markdownStrong: RGBA
  64. markdownHorizontalRule: RGBA
  65. markdownListItem: RGBA
  66. markdownListEnumeration: RGBA
  67. markdownImage: RGBA
  68. markdownImageText: RGBA
  69. markdownCodeBlock: RGBA
  70. }
  71. type HexColor = `#${string}`
  72. type RefName = string
  73. type ColorModeObj = {
  74. dark: HexColor | RefName
  75. light: HexColor | RefName
  76. }
  77. type ColorValue = HexColor | RefName | ColorModeObj
  78. type ThemeJson = {
  79. $schema?: string
  80. defs?: Record<string, HexColor | RefName>
  81. theme: Record<keyof Theme, ColorValue>
  82. }
  83. export const THEMES: Record<string, Theme> = {
  84. aura: resolveTheme(aura),
  85. ayu: resolveTheme(ayu),
  86. catppuccin: resolveTheme(catppuccin),
  87. cobalt2: resolveTheme(cobalt2),
  88. dracula: resolveTheme(dracula),
  89. everforest: resolveTheme(everforest),
  90. github: resolveTheme(github),
  91. gruvbox: resolveTheme(gruvbox),
  92. kanagawa: resolveTheme(kanagawa),
  93. material: resolveTheme(material),
  94. matrix: resolveTheme(matrix),
  95. monokai: resolveTheme(monokai),
  96. nord: resolveTheme(nord),
  97. ["one-dark"]: resolveTheme(onedark),
  98. opencode: resolveTheme(opencode),
  99. palenight: resolveTheme(palenight),
  100. rosepine: resolveTheme(rosepine),
  101. solarized: resolveTheme(solarized),
  102. synthwave84: resolveTheme(synthwave84),
  103. tokyonight: resolveTheme(tokyonight),
  104. vesper: resolveTheme(vesper),
  105. zenburn: resolveTheme(zenburn),
  106. }
  107. function resolveTheme(theme: ThemeJson) {
  108. const defs = theme.defs ?? {}
  109. function resolveColor(c: ColorValue): RGBA {
  110. if (typeof c === "string") return c.startsWith("#") ? RGBA.fromHex(c) : resolveColor(defs[c])
  111. // TODO: support light theme when opentui has the equivalent of lipgloss.AdaptiveColor
  112. return resolveColor(c.dark)
  113. }
  114. return Object.fromEntries(
  115. Object.entries(theme.theme).map(([key, value]) => {
  116. return [key, resolveColor(value)]
  117. }),
  118. ) as Theme
  119. }
  120. const syntaxThemeDark = [
  121. {
  122. scope: ["prompt"],
  123. style: {
  124. foreground: "#7dcfff",
  125. },
  126. },
  127. {
  128. scope: ["extmark.file"],
  129. style: {
  130. foreground: "#ff9e64",
  131. bold: true,
  132. },
  133. },
  134. {
  135. scope: ["extmark.agent"],
  136. style: {
  137. foreground: "#bb9af7",
  138. bold: true,
  139. },
  140. },
  141. {
  142. scope: ["extmark.paste"],
  143. style: {
  144. foreground: "#1a1b26",
  145. background: "#ff9e64",
  146. bold: true,
  147. },
  148. },
  149. {
  150. scope: ["comment"],
  151. style: {
  152. foreground: "#565f89",
  153. italic: true,
  154. },
  155. },
  156. {
  157. scope: ["comment.documentation"],
  158. style: {
  159. foreground: "#565f89",
  160. italic: true,
  161. },
  162. },
  163. {
  164. scope: ["string", "symbol"],
  165. style: {
  166. foreground: "#9ece6a",
  167. },
  168. },
  169. {
  170. scope: ["number", "boolean"],
  171. style: {
  172. foreground: "#ff9e64",
  173. },
  174. },
  175. {
  176. scope: ["character.special"],
  177. style: {
  178. foreground: "#9ece6a",
  179. },
  180. },
  181. {
  182. scope: ["keyword.return", "keyword.conditional", "keyword.repeat", "keyword.coroutine"],
  183. style: {
  184. foreground: "#bb9af7",
  185. italic: true,
  186. },
  187. },
  188. {
  189. scope: ["keyword.type"],
  190. style: {
  191. foreground: "#2ac3de",
  192. bold: true,
  193. italic: true,
  194. },
  195. },
  196. {
  197. scope: ["keyword.function", "function.method"],
  198. style: {
  199. foreground: "#bb9af7",
  200. },
  201. },
  202. {
  203. scope: ["keyword"],
  204. style: {
  205. foreground: "#bb9af7",
  206. italic: true,
  207. },
  208. },
  209. {
  210. scope: ["keyword.import"],
  211. style: {
  212. foreground: "#bb9af7",
  213. },
  214. },
  215. {
  216. scope: ["operator", "keyword.operator", "punctuation.delimiter"],
  217. style: {
  218. foreground: "#89ddff",
  219. },
  220. },
  221. {
  222. scope: ["keyword.conditional.ternary"],
  223. style: {
  224. foreground: "#89ddff",
  225. },
  226. },
  227. {
  228. scope: ["variable", "variable.parameter", "function.method.call", "function.call"],
  229. style: {
  230. foreground: "#7dcfff",
  231. },
  232. },
  233. {
  234. scope: ["variable.member", "function", "constructor"],
  235. style: {
  236. foreground: "#7aa2f7",
  237. },
  238. },
  239. {
  240. scope: ["type", "module"],
  241. style: {
  242. foreground: "#2ac3de",
  243. },
  244. },
  245. {
  246. scope: ["constant"],
  247. style: {
  248. foreground: "#ff9e64",
  249. },
  250. },
  251. {
  252. scope: ["property"],
  253. style: {
  254. foreground: "#73daca",
  255. },
  256. },
  257. {
  258. scope: ["class"],
  259. style: {
  260. foreground: "#2ac3de",
  261. },
  262. },
  263. {
  264. scope: ["parameter"],
  265. style: {
  266. foreground: "#e0af68",
  267. },
  268. },
  269. {
  270. scope: ["punctuation", "punctuation.bracket"],
  271. style: {
  272. foreground: "#89ddff",
  273. },
  274. },
  275. {
  276. scope: [
  277. "variable.builtin",
  278. "type.builtin",
  279. "function.builtin",
  280. "module.builtin",
  281. "constant.builtin",
  282. ],
  283. style: {
  284. foreground: "#f7768e",
  285. },
  286. },
  287. {
  288. scope: ["variable.super"],
  289. style: {
  290. foreground: "#f7768e",
  291. },
  292. },
  293. {
  294. scope: ["string.escape", "string.regexp"],
  295. style: {
  296. foreground: "#bb9af7",
  297. },
  298. },
  299. {
  300. scope: ["keyword.directive"],
  301. style: {
  302. foreground: "#bb9af7",
  303. italic: true,
  304. },
  305. },
  306. {
  307. scope: ["punctuation.special"],
  308. style: {
  309. foreground: "#89ddff",
  310. },
  311. },
  312. {
  313. scope: ["keyword.modifier"],
  314. style: {
  315. foreground: "#bb9af7",
  316. italic: true,
  317. },
  318. },
  319. {
  320. scope: ["keyword.exception"],
  321. style: {
  322. foreground: "#bb9af7",
  323. italic: true,
  324. },
  325. },
  326. // Markdown specific styles
  327. {
  328. scope: ["markup.heading"],
  329. style: {
  330. foreground: "#7aa2f7",
  331. bold: true,
  332. },
  333. },
  334. {
  335. scope: ["markup.heading.1"],
  336. style: {
  337. foreground: "#bb9af7",
  338. bold: true,
  339. },
  340. },
  341. {
  342. scope: ["markup.heading.2"],
  343. style: {
  344. foreground: "#7aa2f7",
  345. bold: true,
  346. },
  347. },
  348. {
  349. scope: ["markup.heading.3"],
  350. style: {
  351. foreground: "#7dcfff",
  352. bold: true,
  353. },
  354. },
  355. {
  356. scope: ["markup.heading.4"],
  357. style: {
  358. foreground: "#73daca",
  359. bold: true,
  360. },
  361. },
  362. {
  363. scope: ["markup.heading.5"],
  364. style: {
  365. foreground: "#9ece6a",
  366. bold: true,
  367. },
  368. },
  369. {
  370. scope: ["markup.heading.6"],
  371. style: {
  372. foreground: "#565f89",
  373. bold: true,
  374. },
  375. },
  376. {
  377. scope: ["markup.bold", "markup.strong"],
  378. style: {
  379. foreground: "#e6edf3",
  380. bold: true,
  381. },
  382. },
  383. {
  384. scope: ["markup.italic"],
  385. style: {
  386. foreground: "#e6edf3",
  387. italic: true,
  388. },
  389. },
  390. {
  391. scope: ["markup.list"],
  392. style: {
  393. foreground: "#ff9e64",
  394. },
  395. },
  396. {
  397. scope: ["markup.quote"],
  398. style: {
  399. foreground: "#565f89",
  400. italic: true,
  401. },
  402. },
  403. {
  404. scope: ["markup.raw", "markup.raw.block"],
  405. style: {
  406. foreground: "#9ece6a",
  407. },
  408. },
  409. {
  410. scope: ["markup.raw.inline"],
  411. style: {
  412. foreground: "#9ece6a",
  413. background: "#1a1b26",
  414. },
  415. },
  416. {
  417. scope: ["markup.link"],
  418. style: {
  419. foreground: "#7aa2f7",
  420. underline: true,
  421. },
  422. },
  423. {
  424. scope: ["markup.link.label"],
  425. style: {
  426. foreground: "#7dcfff",
  427. underline: true,
  428. },
  429. },
  430. {
  431. scope: ["markup.link.url"],
  432. style: {
  433. foreground: "#7aa2f7",
  434. underline: true,
  435. },
  436. },
  437. {
  438. scope: ["label"],
  439. style: {
  440. foreground: "#73daca",
  441. },
  442. },
  443. {
  444. scope: ["spell", "nospell"],
  445. style: {
  446. foreground: "#e6edf3",
  447. },
  448. },
  449. {
  450. scope: ["conceal"],
  451. style: {
  452. foreground: "#565f89",
  453. },
  454. },
  455. // Additional common highlight groups
  456. {
  457. scope: ["string.special", "string.special.url"],
  458. style: {
  459. foreground: "#73daca",
  460. underline: true,
  461. },
  462. },
  463. {
  464. scope: ["character"],
  465. style: {
  466. foreground: "#9ece6a",
  467. },
  468. },
  469. {
  470. scope: ["float"],
  471. style: {
  472. foreground: "#ff9e64",
  473. },
  474. },
  475. {
  476. scope: ["comment.error"],
  477. style: {
  478. foreground: "#f7768e",
  479. italic: true,
  480. bold: true,
  481. },
  482. },
  483. {
  484. scope: ["comment.warning"],
  485. style: {
  486. foreground: "#e0af68",
  487. italic: true,
  488. bold: true,
  489. },
  490. },
  491. {
  492. scope: ["comment.todo", "comment.note"],
  493. style: {
  494. foreground: "#7aa2f7",
  495. italic: true,
  496. bold: true,
  497. },
  498. },
  499. {
  500. scope: ["namespace"],
  501. style: {
  502. foreground: "#2ac3de",
  503. },
  504. },
  505. {
  506. scope: ["field"],
  507. style: {
  508. foreground: "#73daca",
  509. },
  510. },
  511. {
  512. scope: ["type.definition"],
  513. style: {
  514. foreground: "#2ac3de",
  515. bold: true,
  516. },
  517. },
  518. {
  519. scope: ["keyword.export"],
  520. style: {
  521. foreground: "#bb9af7",
  522. },
  523. },
  524. {
  525. scope: ["attribute", "annotation"],
  526. style: {
  527. foreground: "#e0af68",
  528. },
  529. },
  530. {
  531. scope: ["tag"],
  532. style: {
  533. foreground: "#f7768e",
  534. },
  535. },
  536. {
  537. scope: ["tag.attribute"],
  538. style: {
  539. foreground: "#bb9af7",
  540. },
  541. },
  542. {
  543. scope: ["tag.delimiter"],
  544. style: {
  545. foreground: "#89ddff",
  546. },
  547. },
  548. {
  549. scope: ["markup.strikethrough"],
  550. style: {
  551. foreground: "#565f89",
  552. },
  553. },
  554. {
  555. scope: ["markup.underline"],
  556. style: {
  557. foreground: "#e6edf3",
  558. underline: true,
  559. },
  560. },
  561. {
  562. scope: ["markup.list.checked"],
  563. style: {
  564. foreground: "#9ece6a",
  565. },
  566. },
  567. {
  568. scope: ["markup.list.unchecked"],
  569. style: {
  570. foreground: "#565f89",
  571. },
  572. },
  573. {
  574. scope: ["diff.plus"],
  575. style: {
  576. foreground: "#9ece6a",
  577. },
  578. },
  579. {
  580. scope: ["diff.minus"],
  581. style: {
  582. foreground: "#f7768e",
  583. },
  584. },
  585. {
  586. scope: ["diff.delta"],
  587. style: {
  588. foreground: "#7dcfff",
  589. },
  590. },
  591. {
  592. scope: ["error"],
  593. style: {
  594. foreground: "#f7768e",
  595. bold: true,
  596. },
  597. },
  598. {
  599. scope: ["warning"],
  600. style: {
  601. foreground: "#e0af68",
  602. bold: true,
  603. },
  604. },
  605. {
  606. scope: ["info"],
  607. style: {
  608. foreground: "#7dcfff",
  609. },
  610. },
  611. {
  612. scope: ["debug"],
  613. style: {
  614. foreground: "#565f89",
  615. },
  616. },
  617. ]
  618. export const SyntaxTheme = SyntaxStyle.fromTheme(syntaxThemeDark)
  619. export const { use: useTheme, provider: ThemeProvider } = createSimpleContext({
  620. name: "Theme",
  621. init: () => {
  622. const sync = useSync()
  623. const kv = useKV()
  624. const [theme, setTheme] = createSignal(sync.data.config.theme ?? kv.data.theme)
  625. const values = createMemo(() => {
  626. return THEMES[theme()] ?? THEMES.opencode
  627. })
  628. return {
  629. theme: new Proxy(values(), {
  630. get(_target, prop) {
  631. // @ts-expect-error
  632. return values()[prop]
  633. },
  634. }),
  635. get selectedTheme() {
  636. return kv.data.theme
  637. },
  638. setSelectedTheme(theme: string) {
  639. setTheme(theme)
  640. kv.set("theme", theme)
  641. },
  642. get ready() {
  643. return sync.ready
  644. },
  645. }
  646. },
  647. })