From d7e1077392b83dab1dd6dc86638845ac55d44106 Mon Sep 17 00:00:00 2001 From: ayn2op Date: Tue, 6 Oct 2026 17:36:36 -0400 Subject: [PATCH] refactor(composer): derive whether the mentions list is shown from its items and the focus --- internal/ui/chat/composer/model.go | 31 ++++++++++++++---------------- internal/ui/chat/composer/msg.go | 1 - internal/ui/chat/model.go | 5 +---- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/internal/ui/chat/composer/model.go b/internal/ui/chat/composer/model.go index f813c1a..0139c8a 100644 --- a/internal/ui/chat/composer/model.go +++ b/internal/ui/chat/composer/model.go @@ -69,9 +69,6 @@ type Model struct { mentionsList mentionslist.Model lastSearch time.Time - // mentionsVisible reports whether the mentions list is shown above the composer. - mentionsVisible bool - typingUntil time.Time } @@ -260,7 +257,7 @@ func (m Model) Update(msg tview.Msg) (Model, tview.Cmd) { m.pasteText() return m, pasteImage() case keybind.Matches(msg, m.cfg.Keybinds.Composer.Send.Keybind): - if m.mentionsVisible { + if m.mentionsListVisible() { return m, m.tabComplete() } return m, m.send() @@ -271,14 +268,14 @@ func (m Model) Update(msg tview.Msg) (Model, tview.Cmd) { m.stopTabCompletion() return m, m.pickFiles() case keybind.Matches(msg, m.cfg.Keybinds.Composer.Cancel.Keybind): - if m.mentionsVisible { + if m.mentionsListVisible() { m.stopTabCompletion() return m, nil } m.reset() return m, nil case keybind.Matches(msg, m.cfg.Keybinds.Composer.TabComplete.Keybind): - if m.mentionsVisible { + if m.mentionsListVisible() { return m, m.tabComplete() } m.insert("\t") @@ -301,7 +298,7 @@ func (m Model) Update(msg tview.Msg) (Model, tview.Cmd) { func (m Model) canEditLastMessage() bool { return !m.Disabled() && m.editing == nil && m.editState.Value() == "" && m.sendMessageData.Reference == nil && len(m.sendMessageData.Files) == 0 && - !m.mentionsVisible + !m.mentionsListVisible() } // StartEdit puts message in the composer to be edited. @@ -493,9 +490,9 @@ func (m Model) OnGuildMemberRemove(event *gateway.GuildMemberRemoveEvent) { } } -// MentionsView places the mentions list just above the composer, near the cursor, or returns nil if it is hidden. -func (m *Model) MentionsView() tview.Widget { - if !m.mentionsVisible { +// MentionsListView places the mentions list just above the composer, near the cursor, or returns nil if it is empty or the composer is not focused. +func (m Model) MentionsListView(focused bool) tview.Widget { + if !focused || !m.mentionsListVisible() { return nil } return mentionsPopup{m} @@ -503,7 +500,7 @@ func (m *Model) MentionsView() tview.Widget { // mentionsPopup lays out the mentions list within the area above the composer, where it is drawn over the messages. type mentionsPopup struct { - c *Model + c Model } // Size returns Fill, as the popup places the list within its whole area. @@ -601,16 +598,16 @@ func (m *Model) addMentionUser(user *discord.User) { }) } -// stopTabCompletion clears and closes the mentions list when it is in use. +// stopTabCompletion clears the mentions list, which hides it, when it is in use. func (m *Model) stopTabCompletion() { if m.cfg.AutocompleteLimit > 0 { m.mentionsList.Clear() - m.CloseMentions() } } -func (m *Model) CloseMentions() { - m.mentionsVisible = false +// mentionsListVisible reports whether the mentions list has items to show above the composer. +func (m Model) mentionsListVisible() bool { + return m.mentionsList.ItemCount() != 0 } func (m Model) attach(name string, reader io.Reader) { @@ -633,7 +630,7 @@ func (m Model) editAction(key tview.KeyMsg) textarea.Action { } func (m Model) ShortHelp() []keybind.Keybind { - if m.mentionsVisible { + if m.mentionsListVisible() { cfg := m.cfg.Keybinds.MentionsList ccfg := m.cfg.Keybinds.Composer short := []keybind.Keybind{cfg.SelectUp.Keybind, cfg.SelectDown.Keybind, ccfg.TabComplete.Keybind, ccfg.Cancel.Keybind} @@ -658,7 +655,7 @@ func (m Model) ShortHelp() []keybind.Keybind { } func (m Model) FullHelp() [][]keybind.Keybind { - if m.mentionsVisible { + if m.mentionsListVisible() { mcfg := m.cfg.Keybinds.MentionsList ccfg := m.cfg.Keybinds.Composer return [][]keybind.Keybind{ diff --git a/internal/ui/chat/composer/msg.go b/internal/ui/chat/composer/msg.go index 4693832..8c395f4 100644 --- a/internal/ui/chat/composer/msg.go +++ b/internal/ui/chat/composer/msg.go @@ -261,7 +261,6 @@ func (m *Model) tabSuggest() tview.Cmd { return nil } m.mentionsList.Rebuild() - m.mentionsVisible = true return nil } diff --git a/internal/ui/chat/model.go b/internal/ui/chat/model.go index 4237671..bd33820 100644 --- a/internal/ui/chat/model.go +++ b/internal/ui/chat/model.go @@ -262,15 +262,12 @@ func (m Model) Update(msg tview.Msg) (Model, tview.Cmd) { case tview.KeyMsg: switch { case keybind.Matches(msg, m.cfg.Keybinds.FocusGuildsTree.Keybind): - m.composer.CloseMentions() m.setFocus(paneGuildsTree) return m, nil case keybind.Matches(msg, m.cfg.Keybinds.FocusMembersTree.Keybind): - m.composer.CloseMentions() m.setFocus(paneMembersTree) return m, nil case keybind.Matches(msg, m.cfg.Keybinds.FocusMessagesList.Keybind): - m.composer.CloseMentions() m.setFocus(paneMessagesList) return m, nil case keybind.Matches(msg, m.cfg.Keybinds.FocusComposer.Keybind): @@ -425,7 +422,7 @@ func (m Model) View() tview.Widget { m.paneView(paneMessagesList), column.New(m.paneView(paneComposer)).Height(layout.Fixed(m.composer.Height())), ) - if mentions := m.composer.MentionsView(); mentions != nil { + if mentions := m.composer.MentionsListView(m.focused == paneComposer); mentions != nil { right = stack.New(right, mentions) }