settings_general.go

  1package tui
  2
  3import (
  4	"fmt"
  5	"strings"
  6
  7	tea "charm.land/bubbletea/v2"
  8	"github.com/floatpane/matcha/config"
  9	"github.com/floatpane/matcha/i18n"
 10)
 11
 12func (m *Settings) updateGeneral(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
 13	switch msg.String() {
 14	case "up", "k":
 15		if m.generalCursor > 0 {
 16			m.generalCursor--
 17		}
 18	case "down", "j":
 19		if m.generalCursor < 5 {
 20			m.generalCursor++
 21		}
 22	case "enter", "space", "right", "l":
 23		switch m.generalCursor {
 24		case 0: // Image Display
 25			m.cfg.DisableImages = !m.cfg.DisableImages
 26			_ = config.SaveConfig(m.cfg)
 27		case 1: // Contextual Tips
 28			m.cfg.HideTips = !m.cfg.HideTips
 29			_ = config.SaveConfig(m.cfg)
 30		case 2: // Desktop Notifications
 31			m.cfg.DisableNotifications = !m.cfg.DisableNotifications
 32			_ = config.SaveConfig(m.cfg)
 33		case 3: // Date Format
 34			switch m.cfg.DateFormat {
 35			case config.DateFormatEU:
 36				m.cfg.DateFormat = config.DateFormatUS
 37			case config.DateFormatUS:
 38				m.cfg.DateFormat = config.DateFormatISO
 39			default: // or ISO
 40				m.cfg.DateFormat = config.DateFormatEU
 41			}
 42			_ = config.SaveConfig(m.cfg)
 43		case 4: // Language
 44			// Cycle through available languages
 45			langs := i18n.LanguageCodes()
 46			currentLang := m.cfg.GetLanguage()
 47			currentIdx := -1
 48			for i, lang := range langs {
 49				if lang == currentLang {
 50					currentIdx = i
 51					break
 52				}
 53			}
 54			nextIdx := (currentIdx + 1) % len(langs)
 55			m.cfg.Language = langs[nextIdx]
 56			_ = config.SaveConfig(m.cfg)
 57			// Apply language change immediately
 58			i18n.GetManager().SetLanguage(m.cfg.Language)
 59			// Trigger full UI rebuild
 60			return m, func() tea.Msg { return LanguageChangedMsg{} }
 61		case 5: // Edit Signature
 62			if msg.String() == "enter" || msg.String() == "right" || msg.String() == "l" {
 63				return m, func() tea.Msg { return GoToSignatureEditorMsg{} }
 64			}
 65		}
 66	}
 67	return m, nil
 68}
 69
 70func (m *Settings) viewGeneral() string {
 71	var b strings.Builder
 72
 73	b.WriteString(titleStyle.Render("General Settings") + "\n\n")
 74
 75	options := []struct {
 76		labelKey string
 77		value    string
 78		tip      string
 79	}{
 80		{"settings_general.disable_images", onOff(m.cfg.DisableImages), "Prevent images from loading automatically in emails."},
 81		{"settings_general.hide_tips", onOff(m.cfg.HideTips), "Hide helpful hints displayed at the bottom of the screen."},
 82		{"settings_general.disable_notifications", onOff(m.cfg.DisableNotifications), "Turn off desktop notifications for new mail."},
 83		{"settings_general.date_format", getDateFormatLabel(m.cfg.DateFormat), "Change how dates and times are displayed."},
 84		{"settings_general.language", getLanguageLabel(m.cfg.GetLanguage()), "Change the interface language. Changes apply instantly."},
 85		{"settings_general.signature", getSignatureStatus(), "Configure the signature appended to your outgoing emails."},
 86	}
 87
 88	for i, opt := range options {
 89		cursor := "  "
 90		style := accountItemStyle
 91		if m.generalCursor == i {
 92			cursor = "> "
 93			style = selectedAccountItemStyle
 94		}
 95
 96		label := t(opt.labelKey)
 97		text := fmt.Sprintf("%s: %s", label, opt.value)
 98		if opt.labelKey == "settings_general.signature" {
 99			text = fmt.Sprintf("%s (%s)", label, opt.value)
100		}
101
102		b.WriteString(style.Render(cursor+text) + "\n")
103	}
104
105	b.WriteString("\n\n")
106
107	if !m.cfg.HideTips && m.generalCursor < len(options) {
108		b.WriteString(TipStyle.Render("Tip: " + options[m.generalCursor].tip))
109	}
110
111	return b.String()
112}
113
114func onOff(b bool) string {
115	if b {
116		return t("settings_general.on")
117	}
118	return t("settings_general.off")
119}
120
121func getDateFormatLabel(f string) string {
122	if f == "" {
123		f = config.DateFormatEU
124	}
125	switch f {
126	case config.DateFormatUS:
127		return "US (MM/DD/YYYY hh:MM AM)"
128	case config.DateFormatISO:
129		return "ISO (YYYY-MM-DD HH:MM)"
130	default:
131		return "EU (DD/MM/YYYY HH:MM)"
132	}
133}
134
135func getSignatureStatus() string {
136	if config.HasSignature() {
137		return t("settings_general.signature_configured")
138	}
139	return t("settings_general.signature_not_configured")
140}
141
142func getLanguageLabel(langCode string) string {
143	if locale, ok := i18n.GetLanguage(langCode); ok {
144		return fmt.Sprintf("%s (%s)", locale.NativeName, locale.Code)
145	}
146	return langCode
147}