channels_picker.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package chat
  2. import (
  3. "log/slog"
  4. "strings"
  5. "github.com/ayn2op/discordo/internal/config"
  6. "github.com/ayn2op/discordo/internal/ui"
  7. "github.com/ayn2op/discordo/pkg/picker"
  8. "github.com/ayn2op/tview"
  9. "github.com/ayn2op/tview/help"
  10. "github.com/ayn2op/tview/keybind"
  11. "github.com/diamondburned/arikawa/v3/discord"
  12. )
  13. type channelsPicker struct {
  14. *picker.Model
  15. chatView *Model
  16. }
  17. var _ help.KeyMap = (*channelsPicker)(nil)
  18. func newChannelsPicker(cfg *config.Config, chatView *Model) *channelsPicker {
  19. cp := &channelsPicker{picker.NewModel(), chatView}
  20. cp.Box = ui.ConfigureBox(tview.NewBox(), &cfg.Theme)
  21. // When a child of the parent flex is focused, the parent layout itself is not reported as focused.
  22. // Instead, the focused child (picker) is considered focused.
  23. // Therefore, we manually set the active border style on the picker to ensure it displays the correct focused appearance.
  24. cp.
  25. SetBlurFunc(nil).
  26. SetFocusFunc(nil).
  27. SetBorderSet(cfg.Theme.Border.ActiveSet.BorderSet).
  28. SetBorderStyle(cfg.Theme.Border.ActiveStyle.Style).
  29. SetTitleStyle(cfg.Theme.Title.ActiveStyle.Style).
  30. SetFooterStyle(cfg.Theme.Footer.ActiveStyle.Style)
  31. cp.SetTitle("Channels")
  32. cp.SetScrollBarVisibility(cfg.Theme.ScrollBar.Visibility.ScrollBarVisibility)
  33. cp.SetScrollBar(tview.NewScrollBar().
  34. SetTrackStyle(cfg.Theme.ScrollBar.TrackStyle.Style).
  35. SetThumbStyle(cfg.Theme.ScrollBar.ThumbStyle.Style).
  36. SetGlyphSet(cfg.Theme.ScrollBar.GlyphSet.GlyphSet))
  37. cp.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. return cp
  46. }
  47. func (cp *channelsPicker) HandleEvent(event tview.Event) tview.Command {
  48. switch event := event.(type) {
  49. case *picker.SelectedEvent:
  50. channelID, ok := event.Reference.(discord.ChannelID)
  51. if !ok || !channelID.IsValid() {
  52. return nil
  53. }
  54. channel, err := cp.chatView.state.Cabinet.Channel(channelID)
  55. if err != nil {
  56. slog.Error("failed to get channel from state", "err", err, "channel_id", channelID)
  57. return nil
  58. }
  59. node := cp.chatView.guildsTree.findNodeByChannelID(channel.ID)
  60. if node == nil {
  61. slog.Error("failed to locate channel in tree", "channel_id", channel.ID)
  62. return nil
  63. }
  64. cp.chatView.guildsTree.expandPathToNode(node)
  65. cp.chatView.guildsTree.SetCurrentNode(node)
  66. if channel.Type != discord.GuildCategory {
  67. cp.chatView.guildsTree.onSelected(node)
  68. }
  69. cp.chatView.closePicker()
  70. cp.chatView.focusMessageInput()
  71. return nil
  72. case *picker.CancelEvent:
  73. cp.chatView.closePicker()
  74. return nil
  75. }
  76. return cp.Model.HandleEvent(event)
  77. }
  78. func (cp *channelsPicker) update() {
  79. cp.ClearItems()
  80. state := cp.chatView.state
  81. privateChannels, err := state.Cabinet.PrivateChannels()
  82. if err != nil {
  83. slog.Error("failed to get private channels from state", "err", err)
  84. return
  85. }
  86. ui.SortPrivateChannels(privateChannels)
  87. for _, channel := range privateChannels {
  88. cp.addChannel(nil, channel)
  89. }
  90. guilds, err := state.Cabinet.Guilds()
  91. if err != nil {
  92. slog.Error("failed to get guilds from state", "err", err)
  93. return
  94. }
  95. for _, guild := range guilds {
  96. channels, err := state.Cabinet.Channels(guild.ID)
  97. if err != nil {
  98. slog.Error("failed to get channels from state", "err", err, "guild_id", guild.ID)
  99. continue
  100. }
  101. for _, channel := range channels {
  102. cp.addChannel(&guild, channel)
  103. }
  104. }
  105. cp.Update()
  106. }
  107. func (cp *channelsPicker) addChannel(guild *discord.Guild, channel discord.Channel) {
  108. var b strings.Builder
  109. b.WriteString(ui.ChannelToString(channel, cp.chatView.cfg.Icons, cp.chatView.state))
  110. if guild != nil {
  111. b.WriteString(" - ")
  112. b.WriteString(guild.Name)
  113. }
  114. name := b.String()
  115. cp.AddItem(picker.Item{Text: name, FilterText: name, Reference: channel.ID})
  116. }
  117. func (cp *channelsPicker) ShortHelp() []keybind.Keybind {
  118. cfg := cp.chatView.cfg.Keybinds.Picker
  119. return []keybind.Keybind{cfg.Up.Keybind, cfg.Down.Keybind, cfg.Select.Keybind, cfg.Cancel.Keybind}
  120. }
  121. func (cp *channelsPicker) FullHelp() [][]keybind.Keybind {
  122. cfg := cp.chatView.cfg.Keybinds.Picker
  123. return [][]keybind.Keybind{
  124. {cfg.Up.Keybind, cfg.Down.Keybind, cfg.Top.Keybind, cfg.Bottom.Keybind},
  125. {cfg.Select.Keybind, cfg.Cancel.Keybind},
  126. }
  127. }