| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- package chat
- import (
- "github.com/ayn2op/discordo/internal/config"
- "github.com/ayn2op/discordo/internal/ui"
- "github.com/ayn2op/tview"
- "github.com/ayn2op/tview/list"
- "github.com/gdamore/tcell/v3"
- )
- type mentionsListItem struct {
- insertText string
- displayText string
- style tcell.Style
- }
- type mentionsList struct {
- *list.Model
- items []mentionsListItem
- }
- func newMentionsList(cfg *config.Config) *mentionsList {
- m := &mentionsList{
- Model: list.NewModel(),
- }
- m.Box = ui.ConfigureBox(m.Box, &cfg.Theme)
- m.SetSnapToItems(true).SetTitle("Mentions")
- b := m.GetBorderSet()
- b.BottomLeft, b.BottomRight = b.BottomT, b.BottomT
- m.SetBorderSet(b)
- return m
- }
- func (m *mentionsList) append(item mentionsListItem) {
- m.items = append(m.items, item)
- }
- func (m *mentionsList) clear() {
- m.items = nil
- m.Clear()
- }
- func (m *mentionsList) rebuild() {
- m.SetBuilder(func(index int, cursor int) list.Item {
- if index < 0 || index >= len(m.items) {
- return nil
- }
- item := m.items[index]
- style := item.style
- if index == cursor {
- style = style.Reverse(true)
- }
- line := tview.NewLine(tview.NewSegment(item.displayText, style))
- return tview.NewTextView().
- SetScrollable(false).
- SetWrap(false).
- SetWordWrap(false).
- SetTextStyle(style).
- SetLines([]tview.Line{line})
- })
- if len(m.items) == 0 {
- m.SetCursor(-1)
- return
- }
- m.SetCursor(0)
- }
- func (m *mentionsList) itemCount() int {
- return len(m.items)
- }
- func (m *mentionsList) selectedInsertText() (string, bool) {
- index := m.Cursor()
- if index < 0 || index >= len(m.items) {
- return "", false
- }
- return m.items[index].insertText, true
- }
- func (m *mentionsList) maxDisplayWidth() int {
- width := 0
- for _, item := range m.items {
- width = max(width, tview.TaggedStringWidth(item.displayText))
- }
- return width
- }
|