refactor(ui): rename keybind funcs to *Keybind and move them to keymap.go
This commit is contained in:
parent
b46624796d
commit
bee05cbb25
8 changed files with 79 additions and 67 deletions
|
|
@ -1,8 +1,10 @@
|
||||||
package composer
|
package composer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/ayn2op/tview"
|
||||||
"github.com/ayn2op/tview/help"
|
"github.com/ayn2op/tview/help"
|
||||||
"github.com/ayn2op/tview/keybind"
|
"github.com/ayn2op/tview/keybind"
|
||||||
|
"github.com/ayn2op/tview/textarea"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ help.KeyMap = Model{}
|
var _ help.KeyMap = Model{}
|
||||||
|
|
@ -60,3 +62,17 @@ func (m Model) FullHelp() [][]keybind.Keybind {
|
||||||
openEditor,
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"cmp"
|
"cmp"
|
||||||
"context"
|
"context"
|
||||||
"github.com/ayn2op/tview/layout"
|
|
||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
@ -14,6 +13,8 @@ import (
|
||||||
"unicode"
|
"unicode"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
|
"github.com/ayn2op/tview/layout"
|
||||||
|
|
||||||
"github.com/ayn2op/arikawa/v3/api"
|
"github.com/ayn2op/arikawa/v3/api"
|
||||||
"github.com/ayn2op/arikawa/v3/discord"
|
"github.com/ayn2op/arikawa/v3/discord"
|
||||||
"github.com/ayn2op/arikawa/v3/gateway"
|
"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)
|
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 {
|
func humanJoin(items []string) string {
|
||||||
count := len(items)
|
count := len(items)
|
||||||
switch count {
|
switch count {
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ var (
|
||||||
nextTab = keybind.NewSingleKeybind("ctrl+l", "next tab")
|
nextTab = keybind.NewSingleKeybind("ctrl+l", "next tab")
|
||||||
)
|
)
|
||||||
|
|
||||||
func tabAction(key tview.KeyMsg) tabs.Action {
|
func tabsKeybind(key tview.KeyMsg) tabs.Action {
|
||||||
switch {
|
switch {
|
||||||
case keybind.Matches(key, previousTab):
|
case keybind.Matches(key, previousTab):
|
||||||
return tabs.ActionPrevious
|
return tabs.ActionPrevious
|
||||||
|
|
|
||||||
|
|
@ -64,7 +64,7 @@ func (m Model) View() tview.Widget {
|
||||||
}
|
}
|
||||||
t := m.tabs().
|
t := m.tabs().
|
||||||
Active(int(m.active)).
|
Active(int(m.active)).
|
||||||
Keybind(tabAction).
|
Keybind(tabsKeybind).
|
||||||
OnSelect(func(i int) tview.Msg { return selectTabMsg(i) })
|
OnSelect(func(i int) tview.Msg { return selectTabMsg(i) })
|
||||||
return ui.Box(column.New(t, content), m.cfg, false)
|
return ui.Box(column.New(t, content), m.cfg, false)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
20
internal/ui/picker/keymap.go
Normal file
20
internal/ui/picker/keymap.go
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"github.com/ayn2op/discordo/internal/config"
|
"github.com/ayn2op/discordo/internal/config"
|
||||||
"github.com/ayn2op/discordo/internal/ui"
|
"github.com/ayn2op/discordo/internal/ui"
|
||||||
"github.com/ayn2op/tview"
|
"github.com/ayn2op/tview"
|
||||||
"github.com/ayn2op/tview/keybind"
|
|
||||||
"github.com/ayn2op/tview/picker"
|
"github.com/ayn2op/tview/picker"
|
||||||
"github.com/ayn2op/tview/scrollbar"
|
"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)
|
ThumbStyle(cfg.Theme.ScrollBar.ThumbStyle.Style)
|
||||||
p := picker.New(m.items, &m.searchState).
|
p := picker.New(m.items, &m.searchState).
|
||||||
ScrollBar(bar, cfg.UI.ScrollBar.Visibility.ScrollBarVisibility).
|
ScrollBar(bar, cfg.UI.ScrollBar.Visibility.ScrollBarVisibility).
|
||||||
Keybind(bindKeys(cfg.Keybinds.Picker)).
|
Keybind(pickerKeybind(cfg.Keybinds.Picker)).
|
||||||
ListKeybind(ui.SelectionKeybind(cfg.Keybinds.Picker.SelectionKeybinds)).
|
ListKeybind(ui.SelectionKeybind(cfg.Keybinds.Picker.SelectionKeybinds)).
|
||||||
OnChange(func(s *picker.SearchState) tview.Msg { return changeMsg(*s) }).
|
OnChange(func(s *picker.SearchState) tview.Msg { return changeMsg(*s) }).
|
||||||
OnSelect(onSelect).
|
OnSelect(onSelect).
|
||||||
|
|
@ -56,15 +55,3 @@ func (m *Model) SetItems(items picker.Items) {
|
||||||
|
|
||||||
// Reset clears the query.
|
// Reset clears the query.
|
||||||
func (m *Model) Reset() { m.searchState.Reset() }
|
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,11 @@ package tree
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/ayn2op/discordo/internal/config"
|
"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/keybind"
|
||||||
"github.com/ayn2op/tview/tree"
|
"github.com/ayn2op/tview/tree"
|
||||||
|
"github.com/ayn2op/tview/viewport"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SelectHelp returns the up and down keybinds that move the selection away from current.
|
// 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
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"github.com/ayn2op/discordo/internal/ui"
|
"github.com/ayn2op/discordo/internal/ui"
|
||||||
"github.com/ayn2op/tview"
|
"github.com/ayn2op/tview"
|
||||||
"github.com/ayn2op/tview/box"
|
"github.com/ayn2op/tview/box"
|
||||||
"github.com/ayn2op/tview/keybind"
|
|
||||||
"github.com/ayn2op/tview/layout"
|
"github.com/ayn2op/tview/layout"
|
||||||
"github.com/ayn2op/tview/tree"
|
"github.com/ayn2op/tview/tree"
|
||||||
"github.com/ayn2op/tview/viewport"
|
"github.com/ayn2op/tview/viewport"
|
||||||
|
|
@ -44,7 +43,7 @@ func New(
|
||||||
Graphics(graphics).
|
Graphics(graphics).
|
||||||
GraphicsSet(set).
|
GraphicsSet(set).
|
||||||
GraphicsStyle(tcell.StyleDefault.Foreground(tcell.GetColor(theme.GraphicsColor))).
|
GraphicsStyle(tcell.StyleDefault.Foreground(tcell.GetColor(theme.GraphicsColor))).
|
||||||
Keybind(bindKeys(kbs)).
|
Keybind(treeKeybind(kbs)).
|
||||||
Focused(focused).
|
Focused(focused).
|
||||||
OnChange(onChange).
|
OnChange(onChange).
|
||||||
OnSelect(onSelect)
|
OnSelect(onSelect)
|
||||||
|
|
@ -53,41 +52,8 @@ func New(
|
||||||
ContentWidth(t.RowsWidth).
|
ContentWidth(t.RowsWidth).
|
||||||
OnChildMsg(func(s *viewport.ScrollState, msg tview.Msg) tview.Msg { return ScrollMsg{ScrollState: *s, Msg: msg} }).
|
OnChildMsg(func(s *viewport.ScrollState, msg tview.Msg) tview.Msg { return ScrollMsg{ScrollState: *s, Msg: msg} }).
|
||||||
Axes(layout.Axes{Width: true, Height: true}).
|
Axes(layout.Axes{Width: true, Height: true}).
|
||||||
Keybind(bindScrollKeys(kbs)).
|
Keybind(scrollKeybind(kbs)).
|
||||||
Focused(focused).
|
Focused(focused).
|
||||||
OnChange(func(s *viewport.ScrollState) tview.Msg { return ScrollMsg{ScrollState: *s} })
|
OnChange(func(s *viewport.ScrollState) tview.Msg { return ScrollMsg{ScrollState: *s} })
|
||||||
return ui.Box(v, cfg, focused)
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue