attachments_picker.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package chat
  2. import (
  3. "github.com/ayn2op/discordo/internal/config"
  4. "github.com/ayn2op/discordo/internal/ui"
  5. "github.com/ayn2op/discordo/pkg/picker"
  6. "github.com/ayn2op/tview"
  7. "github.com/ayn2op/tview/help"
  8. "github.com/ayn2op/tview/keybind"
  9. "github.com/gdamore/tcell/v3"
  10. )
  11. type attachmentItem struct {
  12. label string
  13. open func()
  14. }
  15. type attachmentsPicker struct {
  16. *picker.Picker
  17. cfg *config.Config
  18. chatView *Model
  19. items []attachmentItem
  20. }
  21. var _ help.KeyMap = (*attachmentsPicker)(nil)
  22. func newAttachmentsPicker(cfg *config.Config, chatView *Model) *attachmentsPicker {
  23. ap := &attachmentsPicker{
  24. Picker: picker.New(),
  25. cfg: cfg,
  26. chatView: chatView,
  27. }
  28. ap.Box = ui.ConfigureBox(tview.NewBox(), &cfg.Theme)
  29. ap.
  30. SetBlurFunc(nil).
  31. SetFocusFunc(nil).
  32. SetBorderSet(cfg.Theme.Border.ActiveSet.BorderSet).
  33. SetBorderStyle(cfg.Theme.Border.ActiveStyle.Style).
  34. SetTitleStyle(cfg.Theme.Title.ActiveStyle.Style).
  35. SetFooterStyle(cfg.Theme.Footer.ActiveStyle.Style)
  36. ap.SetTitle("Attachments")
  37. ap.SetKeyMap(&picker.KeyMap{
  38. Cancel: cfg.Keybinds.Picker.Cancel.Keybind,
  39. Up: cfg.Keybinds.Picker.Up.Keybind,
  40. Down: cfg.Keybinds.Picker.Down.Keybind,
  41. Top: cfg.Keybinds.Picker.Top.Keybind,
  42. Bottom: cfg.Keybinds.Picker.Bottom.Keybind,
  43. Select: cfg.Keybinds.Picker.Select.Keybind,
  44. })
  45. ap.SetScrollBarVisibility(cfg.Theme.ScrollBar.Visibility.ScrollBarVisibility)
  46. ap.SetScrollBar(tview.NewScrollBar().
  47. SetTrackStyle(cfg.Theme.ScrollBar.TrackStyle.Style).
  48. SetThumbStyle(cfg.Theme.ScrollBar.ThumbStyle.Style).
  49. SetGlyphSet(cfg.Theme.ScrollBar.GlyphSet.GlyphSet))
  50. return ap
  51. }
  52. func (ap *attachmentsPicker) SetItems(items []attachmentItem) {
  53. ap.items = items
  54. ap.ClearItems()
  55. for i, item := range items {
  56. ap.AddItem(picker.Item{
  57. Text: item.label,
  58. FilterText: item.label,
  59. Reference: i,
  60. })
  61. }
  62. ap.Update()
  63. }
  64. func (ap *attachmentsPicker) close() {
  65. ap.chatView.RemoveLayer(attachmentsListLayerName)
  66. ap.chatView.app.SetFocus(ap.chatView.messagesList)
  67. }
  68. func (ap *attachmentsPicker) HandleEvent(event tcell.Event) tview.Command {
  69. switch event := event.(type) {
  70. case *picker.SelectedEvent:
  71. index, ok := event.Reference.(int)
  72. if !ok {
  73. return nil
  74. }
  75. if index < 0 || index >= len(ap.items) {
  76. return nil
  77. }
  78. ap.items[index].open()
  79. ap.close()
  80. return nil
  81. case *picker.CancelEvent:
  82. ap.close()
  83. return nil
  84. }
  85. return ap.Picker.HandleEvent(event)
  86. }
  87. func (ap *attachmentsPicker) ShortHelp() []keybind.Keybind {
  88. cfg := ap.cfg.Keybinds.Picker
  89. return []keybind.Keybind{cfg.Up.Keybind, cfg.Down.Keybind, cfg.Select.Keybind, cfg.Cancel.Keybind}
  90. }
  91. func (ap *attachmentsPicker) FullHelp() [][]keybind.Keybind {
  92. cfg := ap.cfg.Keybinds.Picker
  93. return [][]keybind.Keybind{
  94. {cfg.Up.Keybind, cfg.Down.Keybind, cfg.Top.Keybind, cfg.Bottom.Keybind},
  95. {cfg.Select.Keybind, cfg.Cancel.Keybind},
  96. }
  97. }