diff --git a/internal/ui/chat/composer/keymap.go b/internal/ui/chat/composer/keymap.go index ff16d5d..5952d6b 100644 --- a/internal/ui/chat/composer/keymap.go +++ b/internal/ui/chat/composer/keymap.go @@ -1,8 +1,10 @@ package composer import ( + "github.com/ayn2op/tview" "github.com/ayn2op/tview/help" "github.com/ayn2op/tview/keybind" + "github.com/ayn2op/tview/textarea" ) var _ help.KeyMap = Model{} @@ -60,3 +62,17 @@ func (m Model) FullHelp() [][]keybind.Keybind { openEditor, } } + +// keybind binds the configured newline key and textarea's other default keys, leaving the composer's own keys to Update. +func (m Model) keybind(key tview.KeyMsg) textarea.Action { + if m.isBound(key) { + return textarea.ActionNone + } + if keybind.Matches(key, m.cfg.Keybinds.Composer.Newline.Keybind) { + return textarea.ActionNewline + } + if action := textarea.DefaultKeybind(key); action != textarea.ActionNewline { + return action + } + return textarea.ActionNone +} diff --git a/internal/ui/chat/composer/model.go b/internal/ui/chat/composer/model.go index c809432..271554e 100644 --- a/internal/ui/chat/composer/model.go +++ b/internal/ui/chat/composer/model.go @@ -4,7 +4,6 @@ import ( "bytes" "cmp" "context" - "github.com/ayn2op/tview/layout" "io" "log/slog" "regexp" @@ -14,6 +13,8 @@ import ( "unicode" "unicode/utf8" + "github.com/ayn2op/tview/layout" + "github.com/ayn2op/arikawa/v3/api" "github.com/ayn2op/arikawa/v3/discord" "github.com/ayn2op/arikawa/v3/gateway" @@ -612,20 +613,6 @@ func (m Model) canAttachFiles() bool { return m.channel != nil && m.state.HasPermissions(m.channel.ID, discord.PermissionAttachFiles) } -// keybind binds the configured newline key and textarea's other default keys, leaving the composer's own keys to Update. -func (m Model) keybind(key tview.KeyMsg) textarea.Action { - if m.isBound(key) { - return textarea.ActionNone - } - if keybind.Matches(key, m.cfg.Keybinds.Composer.Newline.Keybind) { - return textarea.ActionNewline - } - if action := textarea.DefaultKeybind(key); action != textarea.ActionNewline { - return action - } - return textarea.ActionNone -} - func humanJoin(items []string) string { count := len(items) switch count { diff --git a/internal/ui/login/keymap.go b/internal/ui/login/keymap.go index 4ec3ecf..c8adf66 100644 --- a/internal/ui/login/keymap.go +++ b/internal/ui/login/keymap.go @@ -12,7 +12,7 @@ var ( nextTab = keybind.NewSingleKeybind("ctrl+l", "next tab") ) -func tabAction(key tview.KeyMsg) tabs.Action { +func tabsKeybind(key tview.KeyMsg) tabs.Action { switch { case keybind.Matches(key, previousTab): return tabs.ActionPrevious diff --git a/internal/ui/login/model.go b/internal/ui/login/model.go index 00bb19d..f3f8b0a 100644 --- a/internal/ui/login/model.go +++ b/internal/ui/login/model.go @@ -64,7 +64,7 @@ func (m Model) View() tview.Widget { } t := m.tabs(). Active(int(m.active)). - Keybind(tabAction). + Keybind(tabsKeybind). OnSelect(func(i int) tview.Msg { return selectTabMsg(i) }) return ui.Box(column.New(t, content), m.cfg, false) } diff --git a/internal/ui/picker/keymap.go b/internal/ui/picker/keymap.go new file mode 100644 index 0000000..4febc0d --- /dev/null +++ b/internal/ui/picker/keymap.go @@ -0,0 +1,20 @@ +package picker + +import ( + "github.com/ayn2op/discordo/internal/config" + "github.com/ayn2op/tview" + "github.com/ayn2op/tview/keybind" + "github.com/ayn2op/tview/picker" +) + +func pickerKeybind(kbs config.PickerKeybinds) func(tview.KeyMsg) picker.Action { + return func(key tview.KeyMsg) picker.Action { + switch { + case keybind.Matches(key, kbs.Select.Keybind): + return picker.ActionSelect + case keybind.Matches(key, kbs.Cancel.Keybind): + return picker.ActionCancel + } + return picker.ActionNone + } +} diff --git a/internal/ui/picker/picker.go b/internal/ui/picker/picker.go index 1a8c3b0..36aa9d9 100644 --- a/internal/ui/picker/picker.go +++ b/internal/ui/picker/picker.go @@ -5,7 +5,6 @@ import ( "github.com/ayn2op/discordo/internal/config" "github.com/ayn2op/discordo/internal/ui" "github.com/ayn2op/tview" - "github.com/ayn2op/tview/keybind" "github.com/ayn2op/tview/picker" "github.com/ayn2op/tview/scrollbar" ) @@ -33,7 +32,7 @@ func (m Model) View(onSelect func(picker.Item) tview.Msg, onCancel tview.Msg) tv ThumbStyle(cfg.Theme.ScrollBar.ThumbStyle.Style) p := picker.New(m.items, &m.searchState). ScrollBar(bar, cfg.UI.ScrollBar.Visibility.ScrollBarVisibility). - Keybind(bindKeys(cfg.Keybinds.Picker)). + Keybind(pickerKeybind(cfg.Keybinds.Picker)). ListKeybind(ui.SelectionKeybind(cfg.Keybinds.Picker.SelectionKeybinds)). OnChange(func(s *picker.SearchState) tview.Msg { return changeMsg(*s) }). OnSelect(onSelect). @@ -56,15 +55,3 @@ func (m *Model) SetItems(items picker.Items) { // Reset clears the query. func (m *Model) Reset() { m.searchState.Reset() } - -func bindKeys(kbs config.PickerKeybinds) func(tview.KeyMsg) picker.Action { - return func(key tview.KeyMsg) picker.Action { - switch { - case keybind.Matches(key, kbs.Select.Keybind): - return picker.ActionSelect - case keybind.Matches(key, kbs.Cancel.Keybind): - return picker.ActionCancel - } - return picker.ActionNone - } -} diff --git a/internal/ui/tree/keymap.go b/internal/ui/tree/keymap.go index 2500871..60145be 100644 --- a/internal/ui/tree/keymap.go +++ b/internal/ui/tree/keymap.go @@ -2,8 +2,11 @@ package tree import ( "github.com/ayn2op/discordo/internal/config" + "github.com/ayn2op/discordo/internal/ui" + "github.com/ayn2op/tview" "github.com/ayn2op/tview/keybind" "github.com/ayn2op/tview/tree" + "github.com/ayn2op/tview/viewport" ) // SelectHelp returns the up and down keybinds that move the selection away from current. @@ -32,3 +35,36 @@ func canSelectDown(root, current *tree.Node) bool { } return last != root && current != last } + +func treeKeybind(kbs config.TreeKeybinds) func(tview.KeyMsg) tree.Action { + return func(key tview.KeyMsg) tree.Action { + switch { + case keybind.Matches(key, kbs.SelectUp.Keybind): + return tree.ActionUp + case keybind.Matches(key, kbs.SelectDown.Keybind): + return tree.ActionDown + case keybind.Matches(key, kbs.SelectTop.Keybind): + return tree.ActionTop + case keybind.Matches(key, kbs.SelectBottom.Keybind): + return tree.ActionBottom + case keybind.Matches(key, kbs.MoveToParentNode.Keybind): + return tree.ActionMoveToParent + case keybind.Matches(key, kbs.SelectCurrent.Keybind): + return tree.ActionSelect + } + return tree.ActionNone + } +} + +func scrollKeybind(kbs config.TreeKeybinds) func(tview.KeyMsg) viewport.Action { + scroll := ui.ScrollKeybind(kbs.ScrollKeybinds) + return func(key tview.KeyMsg) viewport.Action { + switch { + case keybind.Matches(key, kbs.ScrollLeft.Keybind): + return viewport.ActionLeft + case keybind.Matches(key, kbs.ScrollRight.Keybind): + return viewport.ActionRight + } + return scroll(key) + } +} diff --git a/internal/ui/tree/tree.go b/internal/ui/tree/tree.go index a322d5f..692b949 100644 --- a/internal/ui/tree/tree.go +++ b/internal/ui/tree/tree.go @@ -6,7 +6,6 @@ import ( "github.com/ayn2op/discordo/internal/ui" "github.com/ayn2op/tview" "github.com/ayn2op/tview/box" - "github.com/ayn2op/tview/keybind" "github.com/ayn2op/tview/layout" "github.com/ayn2op/tview/tree" "github.com/ayn2op/tview/viewport" @@ -44,7 +43,7 @@ func New( Graphics(graphics). GraphicsSet(set). GraphicsStyle(tcell.StyleDefault.Foreground(tcell.GetColor(theme.GraphicsColor))). - Keybind(bindKeys(kbs)). + Keybind(treeKeybind(kbs)). Focused(focused). OnChange(onChange). OnSelect(onSelect) @@ -53,41 +52,8 @@ func New( ContentWidth(t.RowsWidth). OnChildMsg(func(s *viewport.ScrollState, msg tview.Msg) tview.Msg { return ScrollMsg{ScrollState: *s, Msg: msg} }). Axes(layout.Axes{Width: true, Height: true}). - Keybind(bindScrollKeys(kbs)). + Keybind(scrollKeybind(kbs)). Focused(focused). OnChange(func(s *viewport.ScrollState) tview.Msg { return ScrollMsg{ScrollState: *s} }) return ui.Box(v, cfg, focused) } - -func bindKeys(kbs config.TreeKeybinds) func(tview.KeyMsg) tree.Action { - return func(key tview.KeyMsg) tree.Action { - switch { - case keybind.Matches(key, kbs.SelectUp.Keybind): - return tree.ActionUp - case keybind.Matches(key, kbs.SelectDown.Keybind): - return tree.ActionDown - case keybind.Matches(key, kbs.SelectTop.Keybind): - return tree.ActionTop - case keybind.Matches(key, kbs.SelectBottom.Keybind): - return tree.ActionBottom - case keybind.Matches(key, kbs.MoveToParentNode.Keybind): - return tree.ActionMoveToParent - case keybind.Matches(key, kbs.SelectCurrent.Keybind): - return tree.ActionSelect - } - return tree.ActionNone - } -} - -func bindScrollKeys(kbs config.TreeKeybinds) func(tview.KeyMsg) viewport.Action { - scroll := ui.ScrollKeybind(kbs.ScrollKeybinds) - return func(key tview.KeyMsg) viewport.Action { - switch { - case keybind.Matches(key, kbs.ScrollLeft.Keybind): - return viewport.ActionLeft - case keybind.Matches(key, kbs.ScrollRight.Keybind): - return viewport.ActionRight - } - return scroll(key) - } -}