attachments_picker.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/help"
  7. "github.com/ayn2op/tview/keybind"
  8. "github.com/ayn2op/tview/list"
  9. "github.com/ayn2op/tview/picker"
  10. )
  11. type attachmentItem struct {
  12. label string
  13. open func()
  14. }
  15. type attachmentsPicker struct {
  16. *picker.Model
  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. Model: picker.NewModel(),
  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.SetKeybinds(picker.Keybinds{
  38. Cancel: cfg.Keybinds.Picker.Cancel.Keybind,
  39. Keybinds: list.Keybinds{
  40. SelectUp: cfg.Keybinds.Picker.Up.Keybind,
  41. SelectDown: cfg.Keybinds.Picker.Down.Keybind,
  42. SelectTop: cfg.Keybinds.Picker.Top.Keybind,
  43. SelectBottom: cfg.Keybinds.Picker.Bottom.Keybind,
  44. },
  45. Select: cfg.Keybinds.Picker.Select.Keybind,
  46. })
  47. ap.SetScrollBarVisibility(cfg.Theme.ScrollBar.Visibility.ScrollBarVisibility)
  48. ap.SetScrollBar(tview.NewScrollBar().
  49. SetTrackStyle(cfg.Theme.ScrollBar.TrackStyle.Style).
  50. SetThumbStyle(cfg.Theme.ScrollBar.ThumbStyle.Style).
  51. SetGlyphSet(cfg.Theme.ScrollBar.GlyphSet.GlyphSet))
  52. return ap
  53. }
  54. func (ap *attachmentsPicker) SetItems(items []attachmentItem) {
  55. ap.items = items
  56. ap.ClearItems()
  57. for i, item := range items {
  58. ap.AddItem(picker.Item{
  59. Text: item.label,
  60. FilterText: item.label,
  61. Reference: i,
  62. })
  63. }
  64. ap.Update()
  65. }
  66. func (ap *attachmentsPicker) close() {
  67. ap.chatView.RemoveLayer(attachmentsListLayerName)
  68. ap.chatView.app.SetFocus(ap.chatView.messagesList)
  69. }
  70. func (ap *attachmentsPicker) HandleEvent(event tview.Event) tview.Command {
  71. switch event := event.(type) {
  72. case *picker.SelectedEvent:
  73. index, ok := event.Reference.(int)
  74. if !ok {
  75. return nil
  76. }
  77. if index < 0 || index >= len(ap.items) {
  78. return nil
  79. }
  80. ap.items[index].open()
  81. ap.close()
  82. return nil
  83. case *picker.CancelEvent:
  84. ap.close()
  85. return nil
  86. }
  87. return ap.Model.HandleEvent(event)
  88. }
  89. func (ap *attachmentsPicker) ShortHelp() []keybind.Keybind {
  90. cfg := ap.cfg.Keybinds.Picker
  91. return []keybind.Keybind{cfg.Up.Keybind, cfg.Down.Keybind, cfg.Select.Keybind, cfg.Cancel.Keybind}
  92. }
  93. func (ap *attachmentsPicker) FullHelp() [][]keybind.Keybind {
  94. cfg := ap.cfg.Keybinds.Picker
  95. return [][]keybind.Keybind{
  96. {cfg.Up.Keybind, cfg.Down.Keybind, cfg.Top.Keybind, cfg.Bottom.Keybind},
  97. {cfg.Select.Keybind, cfg.Cancel.Keybind},
  98. }
  99. }