mentions_list.go 1.8 KB

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