attachments_picker.go 2.8 KB

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