Browse Source

feat(tui): theme switcher with preview (#264)

Márk Magyar 8 months ago
parent
commit
eee396f903
2 changed files with 27 additions and 10 deletions
  1. 20 8
      packages/tui/internal/components/dialog/theme.go
  2. 7 2
      packages/tui/internal/tui/tui.go

+ 20 - 8
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,
 	}
 }

+ 7 - 2
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