refactor(ui): alias the keybinds config in key switches

This commit is contained in:
ayn2op 2026-10-16 18:52:15 -04:00
parent 12a4a6af48
commit 25957fd3fd
5 changed files with 66 additions and 60 deletions

View file

@ -186,20 +186,20 @@ func (m Model) footer() string {
// isBound reports whether key is one of the composer's keybinds. // isBound reports whether key is one of the composer's keybinds.
func (m Model) isBound(key tview.KeyMsg) bool { func (m Model) isBound(key tview.KeyMsg) bool {
k := m.cfg.Keybinds.Composer kbs := m.cfg.Keybinds.Composer
if keybind.Matches(key, if keybind.Matches(key,
k.Paste.Keybind, kbs.Paste.Keybind,
k.Send.Keybind, kbs.Send.Keybind,
k.OpenEditor.Keybind, kbs.OpenEditor.Keybind,
k.OpenFilePicker.Keybind, kbs.OpenFilePicker.Keybind,
k.Cancel.Keybind, kbs.Cancel.Keybind,
k.TabComplete.Keybind, kbs.TabComplete.Keybind,
k.ToggleReplyMention.Keybind, kbs.ToggleReplyMention.Keybind,
k.Undo.Keybind, kbs.Undo.Keybind,
) { ) {
return true return true
} }
return keybind.Matches(key, k.EditLast.Keybind) && m.canEditLastMessage() return keybind.Matches(key, kbs.EditLast.Keybind) && m.canEditLastMessage()
} }
// setText replaces the text and moves the cursor, remembering what they were for undo if the text changed. // setText replaces the text and moves the cursor, remembering what they were for undo if the text changed.
@ -280,40 +280,41 @@ func (m Model) Update(msg tview.Msg) (Model, tview.Cmd) {
return m, typingCmd return m, typingCmd
case tview.KeyMsg: case tview.KeyMsg:
kbs := m.cfg.Keybinds.Composer
switch { switch {
case keybind.Matches(msg, m.cfg.Keybinds.Composer.EditLast.Keybind) && m.canEditLastMessage(): case keybind.Matches(msg, kbs.EditLast.Keybind) && m.canEditLastMessage():
return m, m.editLastMessage() return m, m.editLastMessage()
case keybind.Matches(msg, m.cfg.Keybinds.Composer.Paste.Keybind): case keybind.Matches(msg, kbs.Paste.Keybind):
m.pasteText() m.pasteText()
return m, pasteImage() return m, pasteImage()
case keybind.Matches(msg, m.cfg.Keybinds.Composer.Send.Keybind): case keybind.Matches(msg, kbs.Send.Keybind):
if m.mentionsListVisible() { if m.mentionsListVisible() {
return m, m.tabComplete() return m, m.tabComplete()
} }
return m, m.send() return m, m.send()
case keybind.Matches(msg, m.cfg.Keybinds.Composer.OpenEditor.Keybind): case keybind.Matches(msg, kbs.OpenEditor.Keybind):
m.stopTabCompletion() m.stopTabCompletion()
return m, m.openEditor() return m, m.openEditor()
case keybind.Matches(msg, m.cfg.Keybinds.Composer.OpenFilePicker.Keybind): case keybind.Matches(msg, kbs.OpenFilePicker.Keybind):
m.stopTabCompletion() m.stopTabCompletion()
return m, m.pickFiles() return m, m.pickFiles()
case keybind.Matches(msg, m.cfg.Keybinds.Composer.Cancel.Keybind): case keybind.Matches(msg, kbs.Cancel.Keybind):
if m.mentionsListVisible() { if m.mentionsListVisible() {
m.stopTabCompletion() m.stopTabCompletion()
return m, nil return m, nil
} }
m.reset() m.reset()
return m, nil return m, nil
case keybind.Matches(msg, m.cfg.Keybinds.Composer.TabComplete.Keybind): case keybind.Matches(msg, kbs.TabComplete.Keybind):
if m.mentionsListVisible() { if m.mentionsListVisible() {
return m, m.tabComplete() return m, m.tabComplete()
} }
m.insert("\t") m.insert("\t")
return m, nil return m, nil
case keybind.Matches(msg, m.cfg.Keybinds.Composer.ToggleReplyMention.Keybind): case keybind.Matches(msg, kbs.ToggleReplyMention.Keybind):
m.toggleReplyMention() m.toggleReplyMention()
return m, nil return m, nil
case keybind.Matches(msg, m.cfg.Keybinds.Composer.Undo.Keybind): case keybind.Matches(msg, kbs.Undo.Keybind):
m.undo() m.undo()
return m, nil return m, nil
} }

View file

@ -549,17 +549,18 @@ func (m Model) Update(msg tview.Msg) (Model, tview.Cmd) {
m.setCurrentNode(msg.Node) m.setCurrentNode(msg.Node)
return m, m.selectNode(msg.Node) return m, m.selectNode(msg.Node)
case tview.KeyMsg: case tview.KeyMsg:
kbs := m.cfg.Keybinds.GuildsTree
switch { switch {
case keybind.Matches(msg, m.cfg.Keybinds.GuildsTree.CollapseAll.Keybind): case keybind.Matches(msg, kbs.CollapseAll.Keybind):
for _, node := range m.root.Children() { for _, node := range m.root.Children() {
node.CollapseAll() node.CollapseAll()
} }
return m, nil return m, nil
case keybind.Matches(msg, m.cfg.Keybinds.GuildsTree.CollapseParentNode.Keybind): case keybind.Matches(msg, kbs.CollapseParentNode.Keybind):
m.collapseParentNode(m.current) m.collapseParentNode(m.current)
return m, nil return m, nil
case keybind.Matches(msg, m.cfg.Keybinds.GuildsTree.SelectPreviousUnread.Keybind, m.cfg.Keybinds.GuildsTree.SelectNextUnread.Keybind): case keybind.Matches(msg, kbs.SelectPreviousUnread.Keybind, kbs.SelectNextUnread.Keybind):
previous := keybind.Matches(msg, m.cfg.Keybinds.GuildsTree.SelectPreviousUnread.Keybind) previous := keybind.Matches(msg, kbs.SelectPreviousUnread.Keybind)
if node := adjacentNode(m.root, m.current, m.isUnread, previous); node != nil { if node := adjacentNode(m.root, m.current, m.isUnread, previous); node != nil {
// A guild or the direct messages without channels yet: load them and move on to the unread one. // A guild or the direct messages without channels yet: load them and move on to the unread one.
if _, ok := node.Reference().(discord.ChannelID); !ok { if _, ok := node.Reference().(discord.ChannelID); !ok {
@ -572,9 +573,9 @@ func (m Model) Update(msg tview.Msg) (Model, tview.Cmd) {
m.setCurrentNode(node) m.setCurrentNode(node)
} }
return m, nil return m, nil
case keybind.Matches(msg, m.cfg.Keybinds.GuildsTree.YankID.Keybind): case keybind.Matches(msg, kbs.YankID.Keybind):
return m, uitree.YankID(m.current) return m, uitree.YankID(m.current)
case keybind.Matches(msg, m.cfg.Keybinds.GuildsTree.ToggleVoice.Keybind): case keybind.Matches(msg, kbs.ToggleVoice.Keybind):
return m, m.toggleVoice(m.current) return m, m.toggleVoice(m.current)
case keybind.Matches(msg, m.cfg.Keybinds.ToggleMute.Keybind): case keybind.Matches(msg, m.cfg.Keybinds.ToggleMute.Keybind):
return m, m.toggleMute() return m, m.toggleMute()

View file

@ -1045,48 +1045,49 @@ func (m Model) selectedMessage() (*discord.Message, bool) {
func (m Model) Update(msg tview.Msg) (Model, tview.Cmd) { func (m Model) Update(msg tview.Msg) (Model, tview.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
case tview.KeyMsg: case tview.KeyMsg:
kbs := m.cfg.Keybinds.MessagesList
switch { switch {
case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.Cancel.Keybind): case keybind.Matches(msg, kbs.Cancel.Keybind):
m.clearSelection() m.clearSelection()
case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.SelectUp.Keybind): case keybind.Matches(msg, kbs.SelectUp.Keybind):
return m, m.selectUp() return m, m.selectUp()
case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.SelectDown.Keybind): case keybind.Matches(msg, kbs.SelectDown.Keybind):
m.selectDown() m.selectDown()
case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.SelectTop.Keybind): case keybind.Matches(msg, kbs.SelectTop.Keybind):
return m, m.selectTop() return m, m.selectTop()
case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.SelectBottom.Keybind): case keybind.Matches(msg, kbs.SelectBottom.Keybind):
m.selectBottom() m.selectBottom()
case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.SelectReply.Keybind): case keybind.Matches(msg, kbs.SelectReply.Keybind):
m.selectReply() m.selectReply()
case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.YankID.Keybind): case keybind.Matches(msg, kbs.YankID.Keybind):
return m, m.yankMessageID() return m, m.yankMessageID()
case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.YankContent.Keybind): case keybind.Matches(msg, kbs.YankContent.Keybind):
return m, m.yankContent() return m, m.yankContent()
case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.YankURL.Keybind): case keybind.Matches(msg, kbs.YankURL.Keybind):
return m, m.yankURL() return m, m.yankURL()
case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.Open.Keybind): case keybind.Matches(msg, kbs.Open.Keybind):
return m, m.open() return m, m.open()
case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.OpenInBrowser.Keybind): case keybind.Matches(msg, kbs.OpenInBrowser.Keybind):
return m, m.openInBrowser() return m, m.openInBrowser()
case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.Play.Keybind): case keybind.Matches(msg, kbs.Play.Keybind):
if message, ok := m.selectedMessage(); ok && isVoiceMessage(*message) { if message, ok := m.selectedMessage(); ok && isVoiceMessage(*message) {
return m, m.play(message.Attachments[0]) return m, m.play(message.Attachments[0])
} }
case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.SeekBackward.Keybind): case keybind.Matches(msg, kbs.SeekBackward.Keybind):
return m, m.seek(m.voiceStatus.Position - seekStep) return m, m.seek(m.voiceStatus.Position - seekStep)
case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.SeekForward.Keybind): case keybind.Matches(msg, kbs.SeekForward.Keybind):
return m, m.seek(m.voiceStatus.Position + seekStep) return m, m.seek(m.voiceStatus.Position + seekStep)
case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.Download.Keybind): case keybind.Matches(msg, kbs.Download.Keybind):
return m, m.download() return m, m.download()
case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.Reply.Keybind): case keybind.Matches(msg, kbs.Reply.Keybind):
return m, m.reply(false) return m, m.reply(false)
case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.ReplyMention.Keybind): case keybind.Matches(msg, kbs.ReplyMention.Keybind):
return m, m.reply(true) return m, m.reply(true)
case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.Edit.Keybind): case keybind.Matches(msg, kbs.Edit.Keybind):
return m, m.editSelectedMessage() return m, m.editSelectedMessage()
case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.Delete.Keybind): case keybind.Matches(msg, kbs.Delete.Keybind):
return m, m.deleteSelectedMessage() return m, m.deleteSelectedMessage()
case keybind.Matches(msg, m.cfg.Keybinds.MessagesList.DeleteConfirm.Keybind): case keybind.Matches(msg, kbs.DeleteConfirm.Keybind):
return m, m.confirmDelete() return m, m.confirmDelete()
} }
case olderMessagesLoadedMsg: case olderMessagesLoadedMsg:

View file

@ -262,41 +262,42 @@ func (m Model) Update(msg tview.Msg) (Model, tview.Cmd) {
case QuitMsg: case QuitMsg:
return m, closeState(m.state) return m, closeState(m.state)
case tview.KeyMsg: case tview.KeyMsg:
kbs := m.cfg.Keybinds
switch { switch {
case keybind.Matches(msg, m.cfg.Keybinds.FocusGuildsTree.Keybind): case keybind.Matches(msg, kbs.FocusGuildsTree.Keybind):
m.setFocus(paneGuildsTree) m.setFocus(paneGuildsTree)
return m, nil return m, nil
case keybind.Matches(msg, m.cfg.Keybinds.FocusMembersTree.Keybind): case keybind.Matches(msg, kbs.FocusMembersTree.Keybind):
m.setFocus(paneMembersTree) m.setFocus(paneMembersTree)
return m, nil return m, nil
case keybind.Matches(msg, m.cfg.Keybinds.FocusMessagesList.Keybind): case keybind.Matches(msg, kbs.FocusMessagesList.Keybind):
m.setFocus(paneMessagesList) m.setFocus(paneMessagesList)
return m, nil return m, nil
case keybind.Matches(msg, m.cfg.Keybinds.FocusComposer.Keybind): case keybind.Matches(msg, kbs.FocusComposer.Keybind):
m.setFocus(paneComposer) m.setFocus(paneComposer)
return m, nil return m, nil
case keybind.Matches(msg, m.cfg.Keybinds.FocusPrevious.Keybind): case keybind.Matches(msg, kbs.FocusPrevious.Keybind):
m.cycleFocus(-1) m.cycleFocus(-1)
return m, nil return m, nil
case keybind.Matches(msg, m.cfg.Keybinds.FocusNext.Keybind): case keybind.Matches(msg, kbs.FocusNext.Keybind):
m.cycleFocus(1) m.cycleFocus(1)
return m, nil return m, nil
case keybind.Matches(msg, m.cfg.Keybinds.ToggleGuildsTree.Keybind): case keybind.Matches(msg, kbs.ToggleGuildsTree.Keybind):
m.toggle(&m.guildsTreeVisible, paneGuildsTree) m.toggle(&m.guildsTreeVisible, paneGuildsTree)
return m, nil return m, nil
case keybind.Matches(msg, m.cfg.Keybinds.ToggleMembersTree.Keybind): case keybind.Matches(msg, kbs.ToggleMembersTree.Keybind):
m.toggle(&m.membersTreeVisible, paneMembersTree) m.toggle(&m.membersTreeVisible, paneMembersTree)
return m, nil return m, nil
case keybind.Matches(msg, m.cfg.Keybinds.ToggleChannelsPicker.Keybind): case keybind.Matches(msg, kbs.ToggleChannelsPicker.Keybind):
m.togglePicker() m.togglePicker()
return m, nil return m, nil
// The guilds tree is what the voice channel joined is in. // The guilds tree is what the voice channel joined is in.
case keybind.Matches(msg, m.cfg.Keybinds.ToggleMute.Keybind, m.cfg.Keybinds.ToggleDeafen.Keybind, m.cfg.Keybinds.LeaveVoice.Keybind): case keybind.Matches(msg, kbs.ToggleMute.Keybind, kbs.ToggleDeafen.Keybind, kbs.LeaveVoice.Keybind):
return m, m.updatePane(paneGuildsTree, msg) return m, m.updatePane(paneGuildsTree, msg)
case keybind.Matches(msg, m.cfg.Keybinds.Logout.Keybind): case keybind.Matches(msg, kbs.Logout.Keybind):
return m, ui.ShowDialog( return m, ui.ShowDialog(
"Are you sure you want to log out?", "Are you sure you want to log out?",
ui.DialogButton{Label: "No"}, ui.DialogButton{Label: "No"},

View file

@ -107,18 +107,20 @@ func (m Model) Update(msg tview.Msg) (Model, tview.Cmd) {
if m.dialog != nil { if m.dialog != nil {
return m, nil return m, nil
} }
kbs := m.cfg.Keybinds
switch { switch {
case keybind.Matches(msg, m.cfg.Keybinds.ToggleHelp.Keybind): case keybind.Matches(msg, kbs.ToggleHelp.Keybind):
m.toggleHelp() m.toggleHelp()
return m, nil return m, nil
case keybind.Matches(msg, m.cfg.Keybinds.ToggleFullHelp.Keybind): case keybind.Matches(msg, kbs.ToggleFullHelp.Keybind):
if m.cfg.Help.Enabled { if m.cfg.Help.Enabled {
m.helpShowAll = !m.helpShowAll m.helpShowAll = !m.helpShowAll
} }
return m, nil return m, nil
case keybind.Matches(msg, m.cfg.Keybinds.Suspend.Keybind): case keybind.Matches(msg, kbs.Suspend.Keybind):
return m, suspend() return m, suspend()
case keybind.Matches(msg, m.cfg.Keybinds.Quit.Keybind): case keybind.Matches(msg, kbs.Quit.Keybind):
return m, ui.ShowDialog( return m, ui.ShowDialog(
"Are you sure you want to quit?", "Are you sure you want to quit?",
ui.DialogButton{Label: "No"}, ui.DialogButton{Label: "No"},