1package tui
2
3import (
4 "strings"
5
6 tea "charm.land/bubbletea/v2"
7 "charm.land/lipgloss/v2"
8 "github.com/floatpane/matcha/config"
9)
10
11func (m *Settings) updateEncryption(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
12 isEnabled := config.IsSecureModeEnabled()
13
14 if isEnabled {
15 if m.confirmingDisable {
16 switch msg.String() {
17 case "y", "Y":
18 m.confirmingDisable = false
19 cfg := m.cfg
20 return m, func() tea.Msg {
21 err := config.DisableSecureMode(cfg)
22 return SecureModeDisabledMsg{Err: err}
23 }
24 case "n", "N", "esc":
25 m.confirmingDisable = false
26 return m, nil
27 }
28 return m, nil
29 }
30 if msg.String() == "enter" {
31 m.confirmingDisable = true
32 }
33 return m, nil
34 }
35
36 switch msg.String() {
37 case "esc":
38 // Clear inputs and return to menu
39 m.encPasswordInput.SetValue("")
40 m.encConfirmInput.SetValue("")
41 m.encPasswordInput.Blur()
42 m.encConfirmInput.Blur()
43 m.encError = ""
44 m.activePane = PaneMenu
45 return m, nil
46 case "tab", "shift+tab", "down", "up":
47 if msg.String() == "shift+tab" || msg.String() == "up" {
48 m.encFocusIndex--
49 if m.encFocusIndex < 0 {
50 m.encFocusIndex = 2
51 }
52 } else {
53 m.encFocusIndex++
54 if m.encFocusIndex > 2 {
55 m.encFocusIndex = 0
56 }
57 }
58 m.encPasswordInput.Blur()
59 m.encConfirmInput.Blur()
60 var cmds []tea.Cmd
61 if m.encFocusIndex == 0 {
62 cmds = append(cmds, m.encPasswordInput.Focus())
63 }
64 if m.encFocusIndex == 1 {
65 cmds = append(cmds, m.encConfirmInput.Focus())
66 }
67 return m, tea.Batch(cmds...)
68 case "enter":
69 switch m.encFocusIndex {
70 case 0:
71 m.encFocusIndex = 1
72 m.encPasswordInput.Blur()
73 return m, m.encConfirmInput.Focus()
74 case 1:
75 m.encFocusIndex = 2
76 m.encConfirmInput.Blur()
77 return m, nil
78 case 2:
79 password := m.encPasswordInput.Value()
80 confirm := m.encConfirmInput.Value()
81 if password == "" {
82 m.encError = t("settings_encryption.error_empty")
83 return m, nil
84 }
85 if password != confirm {
86 m.encError = t("settings_encryption.error_mismatch")
87 return m, nil
88 }
89 m.encEnabling = true
90 m.encError = ""
91 cfg := m.cfg
92 return m, func() tea.Msg {
93 err := config.EnableSecureMode(password, cfg)
94 return SecureModeEnabledMsg{Err: err}
95 }
96 }
97 default:
98 // Forward input to focused textinput
99 var cmd tea.Cmd
100 if m.encFocusIndex == 0 {
101 m.encPasswordInput, cmd = m.encPasswordInput.Update(msg)
102 } else if m.encFocusIndex == 1 {
103 m.encConfirmInput, cmd = m.encConfirmInput.Update(msg)
104 }
105 return m, cmd
106 }
107 return m, nil
108}
109
110func (m *Settings) viewEncryption() string {
111 var b strings.Builder
112 isEnabled := config.IsSecureModeEnabled()
113
114 b.WriteString(titleStyle.Render(t("settings_encryption.title")) + "\n\n")
115
116 if isEnabled {
117 if m.confirmingDisable {
118 dialog := DialogBoxStyle.Render(
119 lipgloss.JoinVertical(lipgloss.Center,
120 dangerStyle.Render(t("settings_encryption.disable_confirm")),
121 accountEmailStyle.Render(t("settings_encryption.disable_warning")),
122 HelpStyle.Render("\n(y/n)"),
123 ),
124 )
125 b.WriteString(dialog + "\n")
126 } else {
127 b.WriteString(settingsFocusedStyle.Render(" "+t("settings_encryption.enabled")) + "\n\n")
128 b.WriteString(accountEmailStyle.Render(" "+t("settings_encryption.disable_button")) + "\n\n")
129 b.WriteString(helpStyle.Render("enter: disable"))
130 }
131 } else {
132 b.WriteString(accountEmailStyle.Render(t("settings_encryption.disabled")) + "\n\n")
133
134 if m.encFocusIndex == 0 {
135 b.WriteString(settingsFocusedStyle.Render(t("settings_encryption.password_label") + "\n"))
136 } else {
137 b.WriteString(settingsBlurredStyle.Render(t("settings_encryption.password_label") + "\n"))
138 }
139 b.WriteString(m.encPasswordInput.View() + "\n\n")
140
141 if m.encFocusIndex == 1 {
142 b.WriteString(settingsFocusedStyle.Render(t("settings_encryption.confirm_label") + "\n"))
143 } else {
144 b.WriteString(settingsBlurredStyle.Render(t("settings_encryption.confirm_label") + "\n"))
145 }
146 b.WriteString(m.encConfirmInput.View() + "\n\n")
147
148 saveBtn := "[ " + t("settings_encryption.enable_button") + " ]"
149 if m.encFocusIndex == 2 {
150 b.WriteString(settingsFocusedStyle.Render(saveBtn) + "\n")
151 } else {
152 b.WriteString(settingsBlurredStyle.Render(saveBtn) + "\n")
153 }
154
155 if m.encEnabling {
156 b.WriteString("\n" + accountEmailStyle.Render(" "+t("settings_encryption.encrypting")) + "\n")
157 }
158
159 b.WriteString("\n" + helpStyle.Render(t("settings_encryption.help")))
160 }
161
162 if m.encError != "" {
163 b.WriteString("\n" + dangerStyle.Render(" "+m.encError) + "\n")
164 }
165
166 return b.String()
167}