summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMárk Magyar <[email protected]>2025-06-20 22:14:05 +0200
committerGitHub <[email protected]>2025-06-20 15:14:05 -0500
commiteee396f903df22209c9d910afd67b69a91430119 (patch)
tree02e79408b753819acade8efe8bcb429f85c4b984
parent0d2f8e175ad01f4eed7487c798a6bf658a86a6c1 (diff)
downloadopencode-eee396f903df22209c9d910afd67b69a91430119.tar.gz
opencode-eee396f903df22209c9d910afd67b69a91430119.zip
feat(tui): theme switcher with preview (#264)
-rw-r--r--packages/tui/internal/components/dialog/theme.go28
-rw-r--r--packages/tui/internal/tui/tui.go9
2 files changed, 27 insertions, 10 deletions
diff --git a/packages/tui/internal/components/dialog/theme.go b/packages/tui/internal/components/dialog/theme.go
index 63135bc81..2bbcebc71 100644
--- a/packages/tui/internal/components/dialog/theme.go
+++ b/packages/tui/internal/components/dialog/theme.go
@@ -47,8 +47,10 @@ type themeDialog struct {
width int
height int
- modal *modal.Modal
- list list.List[themeItem]
+ modal *modal.Modal
+ list list.List[themeItem]
+ originalTheme string
+ themeApplied bool
}
func (t *themeDialog) Init() tea.Cmd {
@@ -64,26 +66,31 @@ func (t *themeDialog) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.String() {
case "enter":
if item, idx := t.list.GetSelectedItem(); idx >= 0 {
- previousTheme := theme.CurrentThemeName()
selectedTheme := item.name
- if previousTheme == selectedTheme {
- return t, util.CmdHandler(modal.CloseModalMsg{})
- }
if err := theme.SetTheme(selectedTheme); err != nil {
// status.Error(err.Error())
return t, nil
}
+ t.themeApplied = true
return t, tea.Sequence(
util.CmdHandler(modal.CloseModalMsg{}),
util.CmdHandler(ThemeSelectedMsg{ThemeName: selectedTheme}),
)
}
+
}
}
+ _, prevIdx := t.list.GetSelectedItem()
+
var cmd tea.Cmd
listModel, cmd := t.list.Update(msg)
t.list = listModel.(list.List[themeItem])
+
+ if item, newIdx := t.list.GetSelectedItem(); newIdx >= 0 && newIdx != prevIdx {
+ theme.SetTheme(item.name)
+ }
+
return t, cmd
}
@@ -92,6 +99,9 @@ func (t *themeDialog) Render(background string) string {
}
func (t *themeDialog) Close() tea.Cmd {
+ if !t.themeApplied {
+ theme.SetTheme(t.originalTheme)
+ }
return nil
}
@@ -120,7 +130,9 @@ func NewThemeDialog() ThemeDialog {
list.SetSelectedIndex(selectedIdx)
return &themeDialog{
- list: list,
- modal: modal.New(modal.WithTitle("Select Theme"), modal.WithMaxWidth(40)),
+ list: list,
+ modal: modal.New(modal.WithTitle("Select Theme"), modal.WithMaxWidth(40)),
+ originalTheme: currentTheme,
+ themeApplied: false,
}
}
diff --git a/packages/tui/internal/tui/tui.go b/packages/tui/internal/tui/tui.go
index f88e3e976..5de8bfbd6 100644
--- a/packages/tui/internal/tui/tui.go
+++ b/packages/tui/internal/tui/tui.go
@@ -77,8 +77,9 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch keyString {
// Escape always closes current modal
case "esc", "ctrl+c":
+ cmd := a.modal.Close()
a.modal = nil
- return a, nil
+ return a, cmd
}
// Pass all other key presses to the modal
@@ -194,8 +195,12 @@ func (a appModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
slog.Debug("Background color", "isDark", msg.IsDark())
case modal.CloseModalMsg:
+ var cmd tea.Cmd
+ if a.modal != nil {
+ cmd = a.modal.Close()
+ }
a.modal = nil
- return a, nil
+ return a, cmd
case commands.ExecuteCommandMsg:
updated, cmd := a.executeCommand(commands.Command(msg))
return updated, cmd