Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
"category_encryption": "App Encryption",
"category_plugins": "Plugins",
"help_menu": "↑/↓: navigate • right/enter: select • esc: go back",
"help_content": "esc: back to menu"
"help_content": "left/esc: back to menu"
Comment thread
andrinoff marked this conversation as resolved.
},
"settings_accounts": {
"title": "Account Settings",
Expand Down
1 change: 1 addition & 0 deletions tui/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tui
const (
keyEnter = "enter"
keyDown = "down"
keyLeft = "left"
keyRight = "right"
keyCount = "count"
keyINBOX = "INBOX"
Expand Down
100 changes: 100 additions & 0 deletions tui/navigation_wrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,106 @@ func TestSettingsNavigationWraps(t *testing.T) {
})
}

func TestSettingsHorizontalPaneFocus(t *testing.T) {
t.Run("right moves focus from menu to content", func(t *testing.T) {
settings := NewSettings(&config.Config{})
settings.activePane = PaneMenu
settings.menuCursor = int(CategoryGeneral)

model, _ := settings.Update(tea.KeyPressMsg{Code: tea.KeyRight})
settings = model.(*Settings)

if settings.activePane != PaneContent {
t.Fatalf("right from menu pane should focus content, got %d", settings.activePane)
}
})

t.Run("esc moves focus from content to menu", func(t *testing.T) {
settings := NewSettings(&config.Config{})
settings.activePane = PaneContent
settings.activeCategory = CategoryGeneral
settings.menuCursor = int(CategoryGeneral)

model, _ := settings.Update(tea.KeyPressMsg{Code: tea.KeyEsc})
settings = model.(*Settings)

if settings.activePane != PaneMenu {
t.Fatalf("esc from content pane should focus menu, got %d", settings.activePane)
}
})

t.Run("left moves focus from content to menu", func(t *testing.T) {
settings := NewSettings(&config.Config{})
settings.activePane = PaneContent
settings.activeCategory = CategoryGeneral
settings.menuCursor = int(CategoryGeneral)

model, _ := settings.Update(tea.KeyPressMsg{Code: tea.KeyLeft})
settings = model.(*Settings)

if settings.activePane != PaneMenu {
t.Fatalf("left from content pane should focus menu, got %d", settings.activePane)
}
})

t.Run("left does not exit settings from menu", func(t *testing.T) {
settings := NewSettings(&config.Config{})
settings.activePane = PaneMenu

model, cmd := settings.Update(tea.KeyPressMsg{Code: tea.KeyLeft})
settings = model.(*Settings)

if cmd != nil {
t.Fatal("left from menu pane should not return to choice menu")
}
if settings.activePane != PaneMenu {
t.Fatalf("left from menu pane should keep menu focused, got %d", settings.activePane)
}
})
}

func TestSettingsEncryptionLeftKeyInInput(t *testing.T) {
t.Run("at input start returns to menu", func(t *testing.T) {
settings := NewSettings(&config.Config{})
settings.activePane = PaneContent
settings.activeCategory = CategoryEncryption
settings.encFocusIndex = 0
settings.encPasswordInput.SetValue("secret")
settings.encPasswordInput.SetCursor(0)
settings.encPasswordInput.Focus()

model, _ := settings.Update(tea.KeyPressMsg{Code: tea.KeyLeft})
settings = model.(*Settings)

if settings.activePane != PaneMenu {
t.Fatalf("left at start of encryption input should focus menu, got %d", settings.activePane)
}
if settings.encPasswordInput.Value() != "" {
t.Fatal("left at start of encryption input should clear input like esc")
}
})

t.Run("inside input moves cursor", func(t *testing.T) {
settings := NewSettings(&config.Config{})
settings.activePane = PaneContent
settings.activeCategory = CategoryEncryption
settings.encFocusIndex = 0
settings.encPasswordInput.SetValue("secret")
settings.encPasswordInput.SetCursor(1)
settings.encPasswordInput.Focus()

model, _ := settings.Update(tea.KeyPressMsg{Code: tea.KeyLeft})
settings = model.(*Settings)

if settings.activePane != PaneContent {
t.Fatalf("left inside encryption input should keep content focused, got %d", settings.activePane)
}
if settings.encPasswordInput.Position() != 0 {
t.Fatalf("left inside encryption input should move cursor left, got position %d", settings.encPasswordInput.Position())
}
})
}

func TestFilePickerNavigationWraps(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "a.txt"), []byte("a"), 0o600); err != nil {
Expand Down
109 changes: 80 additions & 29 deletions tui/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,34 +194,7 @@ func (m *Settings) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil

case tea.KeyPressMsg:
// Global shortcut to return to menu from content pane
if m.activePane == PaneContent && msg.String() == "esc" {
// unless we are in crypto config or encryption editing which have their own esc logic
if (m.activeCategory != CategoryAccounts || !m.isCryptoConfig) &&
(m.activeCategory != CategoryEncryption || m.encFocusIndex <= -1) &&
(m.activeCategory != CategoryPlugins || (!m.pluginEditing && m.pluginSelected == "")) {
m.activePane = PaneMenu
return m, nil
}
}

if m.activePane == PaneMenu {
return m.updateMenu(msg)
}
switch m.activeCategory {
case CategoryGeneral:
return m.updateGeneral(msg)
case CategoryAccounts:
return m.updateAccounts(msg)
case CategoryTheme:
return m.updateTheme(msg)
case CategoryMailingLists:
return m.updateMailingLists(msg)
case CategoryEncryption:
return m.updateEncryption(msg)
case CategoryPlugins:
return m.updatePlugins(msg)
}
return m.updateKeyPress(msg)

case SecureModeEnabledMsg:
m.encEnabling = false
Expand Down Expand Up @@ -270,6 +243,80 @@ func (m *Settings) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Batch(cmds...)
}

func (m *Settings) updateKeyPress(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
// Global shortcut to return to menu from content pane
if m.activePane == PaneContent && msg.String() == "esc" {
// unless we are in crypto config or encryption editing which have their own esc logic
if (m.activeCategory != CategoryAccounts || !m.isCryptoConfig) &&
(m.activeCategory != CategoryEncryption || m.encFocusIndex <= -1) &&
(m.activeCategory != CategoryPlugins || (!m.pluginEditing && m.pluginSelected == "")) {
m.activePane = PaneMenu
return m, nil
}
}

if m.activePane == PaneContent && msg.String() == keyLeft && m.canFocusSettingsMenuWithLeft() {
m.activePane = PaneMenu
return m, nil
}

if m.activePane == PaneMenu {
return m.updateMenu(msg)
}
switch m.activeCategory {
case CategoryGeneral:
return m.updateGeneral(msg)
case CategoryAccounts:
return m.updateAccounts(msg)
case CategoryTheme:
return m.updateTheme(msg)
case CategoryMailingLists:
return m.updateMailingLists(msg)
case CategoryEncryption:
return m.updateEncryption(msg)
case CategoryPlugins:
return m.updatePlugins(msg)
}

return m, nil
}

func (m *Settings) canFocusSettingsMenuWithLeft() bool {
switch m.activeCategory {
case CategoryAccounts:
return !m.isCryptoConfig && !m.confirmingDelete
case CategoryEncryption:
return config.IsSecureModeEnabled() && !m.confirmingDisable
case CategoryPlugins:
return !m.pluginEditing && m.pluginSelected == ""
case CategoryGeneral, CategoryTheme, CategoryMailingLists:
return true
default:
return true
}
}

func (m *Settings) contentItemStyle(selected bool) lipgloss.Style {
if selected && m.activePane == PaneContent {
return selectedAccountItemStyle
}
return accountItemStyle
}

func (m *Settings) contentCursor(selected bool) string {
if selected && m.activePane == PaneContent {
return "> "
}
return " "
}

func (m *Settings) contentFocusStyle() lipgloss.Style {
if m.activePane == PaneContent {
return settingsFocusedStyle
}
return settingsBlurredStyle
}

func (m *Settings) updateMenu(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
categoryCount := int(CategoryPlugins) + 1

Expand Down Expand Up @@ -340,7 +387,11 @@ func (m *Settings) View() tea.View {

style := accountItemStyle
if m.menuCursor == i {
style = selectedAccountItemStyle
if m.activePane == PaneMenu {
style = selectedAccountItemStyle
} else {
style = selectedAccountItemStyle.UnsetBold()
}
}

left.WriteString(style.Render(cursor+c) + "\n")
Expand Down
18 changes: 6 additions & 12 deletions tui/settings_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,23 +153,17 @@ func (m *Settings) viewAccounts() string {

line := fmt.Sprintf("%s - %s", displayName, accountEmailStyle.Render(providerInfo))

cursor := " "
style := accountItemStyle
if m.accountsCursor == i {
cursor = "> "
style = selectedAccountItemStyle
}
selected := m.accountsCursor == i
cursor := m.contentCursor(selected)
style := m.contentItemStyle(selected)

b.WriteString(style.Render(cursor+line) + "\n")
}

// Add Account option
cursor := " "
style := accountItemStyle
if m.accountsCursor == len(m.cfg.Accounts) {
cursor = "> "
style = selectedAccountItemStyle
}
selected := m.accountsCursor == len(m.cfg.Accounts)
cursor := m.contentCursor(selected)
style := m.contentItemStyle(selected)
b.WriteString(style.Render(cursor+t("settings_accounts.add_account")) + "\n\n")

b.WriteString(helpStyle.Render(t("settings_accounts.help")))
Expand Down
6 changes: 3 additions & 3 deletions tui/settings_crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (m *Settings) viewSMIMEConfig() string {

renderField := func(index int, label, content string) {
if m.cryptoFocusIndex == index {
b.WriteString(settingsFocusedStyle.Render(label) + "\n")
b.WriteString(m.contentFocusStyle().Render(label) + "\n")
} else {
b.WriteString(settingsBlurredStyle.Render(label) + "\n")
}
Expand Down Expand Up @@ -162,12 +162,12 @@ func (m *Settings) viewSMIMEConfig() string {
saveBtn := "[ Save ]"
cancelBtn := "[ Cancel ]"
if m.cryptoFocusIndex == 8 {
saveBtn = settingsFocusedStyle.Render(saveBtn)
saveBtn = m.contentFocusStyle().Render(saveBtn)
} else {
saveBtn = settingsBlurredStyle.Render(saveBtn)
}
if m.cryptoFocusIndex == 9 {
cancelBtn = settingsFocusedStyle.Render(cancelBtn)
cancelBtn = m.contentFocusStyle().Render(cancelBtn)
} else {
cancelBtn = settingsBlurredStyle.Render(cancelBtn)
}
Expand Down
Loading
Loading