attachments_picker.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.Picker
  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. Picker: picker.New(),
  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.SetSelectedFunc(ap.onSelected)
  37. ap.SetCancelFunc(ap.close)
  38. ap.SetKeyMap(&picker.KeyMap{
  39. Cancel: cfg.Keybinds.Picker.Cancel.Keybind,
  40. Up: cfg.Keybinds.Picker.Up.Keybind,
  41. Down: cfg.Keybinds.Picker.Down.Keybind,
  42. Top: cfg.Keybinds.Picker.Top.Keybind,
  43. Bottom: cfg.Keybinds.Picker.Bottom.Keybind,
  44. Select: cfg.Keybinds.Picker.Select.Keybind,
  45. })
  46. ap.SetScrollBarVisibility(cfg.Theme.ScrollBar.Visibility.ScrollBarVisibility)
  47. ap.SetScrollBar(tview.NewScrollBar().
  48. SetTrackStyle(cfg.Theme.ScrollBar.TrackStyle.Style).
  49. SetThumbStyle(cfg.Theme.ScrollBar.ThumbStyle.Style).
  50. SetGlyphSet(cfg.Theme.ScrollBar.GlyphSet.GlyphSet))
  51. return ap
  52. }
  53. func (ap *attachmentsPicker) SetItems(items []attachmentItem) {
  54. ap.items = items
  55. ap.ClearItems()
  56. for i, item := range items {
  57. ap.AddItem(picker.Item{
  58. Text: item.label,
  59. FilterText: item.label,
  60. Reference: i,
  61. })
  62. }
  63. ap.Update()
  64. }
  65. func (ap *attachmentsPicker) onSelected(item picker.Item) {
  66. index, ok := item.Reference.(int)
  67. if !ok {
  68. return
  69. }
  70. if index < 0 || index >= len(ap.items) {
  71. return
  72. }
  73. ap.items[index].open()
  74. ap.close()
  75. }
  76. func (ap *attachmentsPicker) close() {
  77. ap.chatView.RemoveLayer(attachmentsListLayerName)
  78. ap.chatView.app.SetFocus(ap.chatView.messagesList)
  79. }
  80. func (ap *attachmentsPicker) ShortHelp() []keybind.Keybind {
  81. cfg := ap.cfg.Keybinds.Picker
  82. return []keybind.Keybind{cfg.Up.Keybind, cfg.Down.Keybind, cfg.Select.Keybind, cfg.Cancel.Keybind}
  83. }
  84. func (ap *attachmentsPicker) FullHelp() [][]keybind.Keybind {
  85. cfg := ap.cfg.Keybinds.Picker
  86. return [][]keybind.Keybind{
  87. {cfg.Up.Keybind, cfg.Down.Keybind, cfg.Top.Keybind, cfg.Bottom.Keybind},
  88. {cfg.Select.Keybind, cfg.Cancel.Keybind},
  89. }
  90. }