mentions_list.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.List.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.Foreground(tview.Styles.PrimitiveBackgroundColor).Background(tview.Styles.PrimaryTextColor)
  44. }
  45. return tview.NewTextView().
  46. SetScrollable(false).
  47. SetWrap(false).
  48. SetWordWrap(false).
  49. SetTextStyle(style).
  50. SetLines([]tview.Line{{{Text: item.displayText, Style: style}}})
  51. })
  52. if len(m.items) == 0 {
  53. m.SetCursor(-1)
  54. return
  55. }
  56. m.SetCursor(0)
  57. }
  58. func (m *mentionsList) itemCount() int {
  59. return len(m.items)
  60. }
  61. func (m *mentionsList) selectedInsertText() (string, bool) {
  62. index := m.Cursor()
  63. if index < 0 || index >= len(m.items) {
  64. return "", false
  65. }
  66. return m.items[index].insertText, true
  67. }
  68. func (m *mentionsList) maxDisplayWidth() int {
  69. width := 0
  70. for _, item := range m.items {
  71. width = max(width, tview.TaggedStringWidth(item.displayText))
  72. }
  73. return width
  74. }