mentions_list.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package chat
  2. import (
  3. "github.com/ayn2op/discordo/internal/config"
  4. "github.com/ayn2op/discordo/internal/ui"
  5. "github.com/ayn2op/tview"
  6. "github.com/gdamore/tcell/v3"
  7. )
  8. type mentionsListItem struct {
  9. insertText string
  10. displayText string
  11. style tcell.Style
  12. }
  13. type mentionsList struct {
  14. *tview.List
  15. items []mentionsListItem
  16. }
  17. func newMentionsList(cfg *config.Config) *mentionsList {
  18. m := &mentionsList{
  19. List: tview.NewList(),
  20. }
  21. m.Box = ui.ConfigureBox(m.Box, &cfg.Theme)
  22. m.SetSnapToItems(true).SetTitle("Mentions")
  23. b := m.GetBorderSet()
  24. b.BottomLeft, b.BottomRight = b.BottomT, b.BottomT
  25. m.SetBorderSet(b)
  26. return m
  27. }
  28. func (m *mentionsList) append(item mentionsListItem) {
  29. m.items = append(m.items, item)
  30. }
  31. func (m *mentionsList) clear() {
  32. m.items = nil
  33. m.Clear()
  34. }
  35. func (m *mentionsList) rebuild() {
  36. m.SetBuilder(func(index int, cursor int) tview.ListItem {
  37. if index < 0 || index >= len(m.items) {
  38. return nil
  39. }
  40. item := m.items[index]
  41. style := item.style
  42. if index == cursor {
  43. style = style.Reverse(true)
  44. }
  45. line := tview.NewLine(tview.NewSegment(item.displayText, style))
  46. return tview.NewTextView().
  47. SetScrollable(false).
  48. SetWrap(false).
  49. SetWordWrap(false).
  50. SetTextStyle(style).
  51. SetLines([]tview.Line{line})
  52. })
  53. if len(m.items) == 0 {
  54. m.SetCursor(-1)
  55. return
  56. }
  57. m.SetCursor(0)
  58. }
  59. func (m *mentionsList) itemCount() int {
  60. return len(m.items)
  61. }
  62. func (m *mentionsList) selectedInsertText() (string, bool) {
  63. index := m.Cursor()
  64. if index < 0 || index >= len(m.items) {
  65. return "", false
  66. }
  67. return m.items[index].insertText, true
  68. }
  69. func (m *mentionsList) maxDisplayWidth() int {
  70. width := 0
  71. for _, item := range m.items {
  72. width = max(width, tview.TaggedStringWidth(item.displayText))
  73. }
  74. return width
  75. }