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 "tab", "shift+tab", "down", "up":
38 if msg.String() == "shift+tab" || msg.String() == "up" {
39 m.encFocusIndex--
40 if m.encFocusIndex < 0 {
41 m.encFocusIndex = 2
42 }
43 } else {
44 m.encFocusIndex++
45 if m.encFocusIndex > 2 {
46 m.encFocusIndex = 0
47 }
48 }
49 m.encPasswordInput.Blur()
50 m.encConfirmInput.Blur()
51 var cmds []tea.Cmd
52 if m.encFocusIndex == 0 {
53 cmds = append(cmds, m.encPasswordInput.Focus())
54 }
55 if m.encFocusIndex == 1 {
56 cmds = append(cmds, m.encConfirmInput.Focus())
57 }
58 return m, tea.Batch(cmds...)
59 case "enter":
60 switch m.encFocusIndex {
61 case 0:
62 m.encFocusIndex = 1
63 m.encPasswordInput.Blur()
64 return m, m.encConfirmInput.Focus()
65 case 1:
66 m.encFocusIndex = 2
67 m.encConfirmInput.Blur()
68 return m, nil
69 case 2:
70 password := m.encPasswordInput.Value()
71 confirm := m.encConfirmInput.Value()
72 if password == "" {
73 m.encError = "Password cannot be empty"
74 return m, nil
75 }
76 if password != confirm {
77 m.encError = "Passwords do not match"
78 return m, nil
79 }
80 m.encEnabling = true
81 m.encError = ""
82 cfg := m.cfg
83 return m, func() tea.Msg {
84 err := config.EnableSecureMode(password, cfg)
85 return SecureModeEnabledMsg{Err: err}
86 }
87 }
88 }
89 return m, nil
90}
91
92func (m *Settings) viewEncryption() string {
93 var b strings.Builder
94 isEnabled := config.IsSecureModeEnabled()
95
96 b.WriteString(titleStyle.Render("App Encryption") + "\n\n")
97
98 if isEnabled {
99 if m.confirmingDisable {
100 dialog := DialogBoxStyle.Render(
101 lipgloss.JoinVertical(lipgloss.Center,
102 dangerStyle.Render("Disable encryption?"),
103 accountEmailStyle.Render("All data will be stored unencrypted."),
104 HelpStyle.Render("\n(y/n)"),
105 ),
106 )
107 b.WriteString(dialog + "\n")
108 } else {
109 b.WriteString(settingsFocusedStyle.Render(" Encryption is currently enabled.") + "\n\n")
110 b.WriteString(accountEmailStyle.Render(" Press enter to disable encryption.") + "\n\n")
111 b.WriteString(helpStyle.Render("enter: disable"))
112 }
113 } else {
114 b.WriteString(accountEmailStyle.Render("Set a password to encrypt all data.") + "\n\n")
115
116 if m.encFocusIndex == 0 {
117 b.WriteString(settingsFocusedStyle.Render("Password:\n"))
118 } else {
119 b.WriteString(settingsBlurredStyle.Render("Password:\n"))
120 }
121 b.WriteString(m.encPasswordInput.View() + "\n\n")
122
123 if m.encFocusIndex == 1 {
124 b.WriteString(settingsFocusedStyle.Render("Confirm Password:\n"))
125 } else {
126 b.WriteString(settingsBlurredStyle.Render("Confirm Password:\n"))
127 }
128 b.WriteString(m.encConfirmInput.View() + "\n\n")
129
130 saveBtn := "[ Enable Encryption ]"
131 if m.encFocusIndex == 2 {
132 b.WriteString(settingsFocusedStyle.Render(saveBtn) + "\n")
133 } else {
134 b.WriteString(settingsBlurredStyle.Render(saveBtn) + "\n")
135 }
136
137 if m.encEnabling {
138 b.WriteString("\n" + accountEmailStyle.Render(" Encrypting data...") + "\n")
139 }
140
141 b.WriteString("\n" + helpStyle.Render("tab: next • enter: save"))
142 }
143
144 if m.encError != "" {
145 b.WriteString("\n" + dangerStyle.Render(" "+m.encError) + "\n")
146 }
147
148 return b.String()
149}