theme.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package dialog
  2. import (
  3. tea "github.com/charmbracelet/bubbletea/v2"
  4. list "github.com/sst/opencode/internal/components/list"
  5. "github.com/sst/opencode/internal/components/modal"
  6. "github.com/sst/opencode/internal/layout"
  7. "github.com/sst/opencode/internal/theme"
  8. "github.com/sst/opencode/internal/util"
  9. )
  10. // ThemeSelectedMsg is sent when the theme is changed
  11. type ThemeSelectedMsg struct {
  12. ThemeName string
  13. }
  14. // ThemeDialog interface for the theme switching dialog
  15. type ThemeDialog interface {
  16. layout.Modal
  17. }
  18. type themeDialog struct {
  19. width int
  20. height int
  21. modal *modal.Modal
  22. list list.List[list.StringItem]
  23. originalTheme string
  24. themeApplied bool
  25. }
  26. func (t *themeDialog) Init() tea.Cmd {
  27. return nil
  28. }
  29. func (t *themeDialog) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  30. switch msg := msg.(type) {
  31. case tea.WindowSizeMsg:
  32. t.width = msg.Width
  33. t.height = msg.Height
  34. case tea.KeyMsg:
  35. switch msg.String() {
  36. case "enter":
  37. if item, idx := t.list.GetSelectedItem(); idx >= 0 {
  38. selectedTheme := string(item)
  39. if err := theme.SetTheme(selectedTheme); err != nil {
  40. // status.Error(err.Error())
  41. return t, nil
  42. }
  43. t.themeApplied = true
  44. return t, tea.Sequence(
  45. util.CmdHandler(modal.CloseModalMsg{}),
  46. util.CmdHandler(ThemeSelectedMsg{ThemeName: selectedTheme}),
  47. )
  48. }
  49. }
  50. }
  51. _, prevIdx := t.list.GetSelectedItem()
  52. var cmd tea.Cmd
  53. listModel, cmd := t.list.Update(msg)
  54. t.list = listModel.(list.List[list.StringItem])
  55. if item, newIdx := t.list.GetSelectedItem(); newIdx >= 0 && newIdx != prevIdx {
  56. theme.SetTheme(string(item))
  57. return t, util.CmdHandler(ThemeSelectedMsg{ThemeName: string(item)})
  58. }
  59. return t, cmd
  60. }
  61. func (t *themeDialog) Render(background string) string {
  62. return t.modal.Render(t.list.View(), background)
  63. }
  64. func (t *themeDialog) Close() tea.Cmd {
  65. if !t.themeApplied {
  66. theme.SetTheme(t.originalTheme)
  67. return util.CmdHandler(ThemeSelectedMsg{ThemeName: t.originalTheme})
  68. }
  69. return nil
  70. }
  71. // NewThemeDialog creates a new theme switching dialog
  72. func NewThemeDialog() ThemeDialog {
  73. themes := theme.AvailableThemes()
  74. currentTheme := theme.CurrentThemeName()
  75. var selectedIdx int
  76. for i, name := range themes {
  77. if name == currentTheme {
  78. selectedIdx = i
  79. }
  80. }
  81. list := list.NewStringList(
  82. themes,
  83. 10, // maxVisibleThemes
  84. "No themes available",
  85. true,
  86. )
  87. // Set the initial selection to the current theme
  88. list.SetSelectedIndex(selectedIdx)
  89. // Set the max width for the list to match the modal width
  90. list.SetMaxWidth(36) // 40 (modal max width) - 4 (modal padding)
  91. return &themeDialog{
  92. list: list,
  93. modal: modal.New(modal.WithTitle("Select Theme"), modal.WithMaxWidth(40)),
  94. originalTheme: currentTheme,
  95. themeApplied: false,
  96. }
  97. }