channels_picker.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.Picker
  15. chatView *Model
  16. }
  17. var _ help.KeyMap = (*channelsPicker)(nil)
  18. func newChannelsPicker(cfg *config.Config, chatView *Model) *channelsPicker {
  19. cp := &channelsPicker{picker.New(), 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.SetSelectedFunc(cp.onSelected)
  32. cp.SetTitle("Channels")
  33. cp.SetScrollBarVisibility(cfg.Theme.ScrollBar.Visibility.ScrollBarVisibility)
  34. cp.SetScrollBar(tview.NewScrollBar().
  35. SetTrackStyle(cfg.Theme.ScrollBar.TrackStyle.Style).
  36. SetThumbStyle(cfg.Theme.ScrollBar.ThumbStyle.Style).
  37. SetGlyphSet(cfg.Theme.ScrollBar.GlyphSet.GlyphSet))
  38. cp.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. return cp
  47. }
  48. func (cp *channelsPicker) onSelected(item picker.Item) {
  49. channelID, ok := item.Reference.(discord.ChannelID)
  50. if !ok || !channelID.IsValid() {
  51. return
  52. }
  53. channel, err := cp.chatView.state.Cabinet.Channel(channelID)
  54. if err != nil {
  55. slog.Error("failed to get channel from state", "err", err, "channel_id", channelID)
  56. return
  57. }
  58. node := cp.chatView.guildsTree.findNodeByChannelID(channel.ID)
  59. if node == nil {
  60. slog.Error("failed to locate channel in tree", "channel_id", channel.ID)
  61. return
  62. }
  63. cp.chatView.guildsTree.expandPathToNode(node)
  64. cp.chatView.guildsTree.SetCurrentNode(node)
  65. if channel.Type != discord.GuildCategory {
  66. cp.chatView.guildsTree.onSelected(node)
  67. }
  68. cp.chatView.closePicker()
  69. cp.chatView.focusMessageInput()
  70. }
  71. func (cp *channelsPicker) update() {
  72. cp.ClearItems()
  73. state := cp.chatView.state
  74. privateChannels, err := state.Cabinet.PrivateChannels()
  75. if err != nil {
  76. slog.Error("failed to get private channels from state", "err", err)
  77. return
  78. }
  79. ui.SortPrivateChannels(privateChannels)
  80. for _, channel := range privateChannels {
  81. cp.addChannel(nil, channel)
  82. }
  83. guilds, err := state.Cabinet.Guilds()
  84. if err != nil {
  85. slog.Error("failed to get guilds from state", "err", err)
  86. return
  87. }
  88. for _, guild := range guilds {
  89. channels, err := state.Cabinet.Channels(guild.ID)
  90. if err != nil {
  91. slog.Error("failed to get channels from state", "err", err, "guild_id", guild.ID)
  92. continue
  93. }
  94. for _, channel := range channels {
  95. cp.addChannel(&guild, channel)
  96. }
  97. }
  98. cp.Update()
  99. }
  100. func (cp *channelsPicker) addChannel(guild *discord.Guild, channel discord.Channel) {
  101. var b strings.Builder
  102. b.WriteString(ui.ChannelToString(channel, cp.chatView.cfg.Icons, cp.chatView.state))
  103. if guild != nil {
  104. b.WriteString(" - ")
  105. b.WriteString(guild.Name)
  106. }
  107. name := b.String()
  108. cp.AddItem(picker.Item{Text: name, FilterText: name, Reference: channel.ID})
  109. }
  110. func (cp *channelsPicker) ShortHelp() []keybind.Keybind {
  111. cfg := cp.chatView.cfg.Keybinds.Picker
  112. return []keybind.Keybind{cfg.Up.Keybind, cfg.Down.Keybind, cfg.Select.Keybind, cfg.Cancel.Keybind}
  113. }
  114. func (cp *channelsPicker) FullHelp() [][]keybind.Keybind {
  115. cfg := cp.chatView.cfg.Keybinds.Picker
  116. return [][]keybind.Keybind{
  117. {cfg.Up.Keybind, cfg.Down.Keybind, cfg.Top.Keybind, cfg.Bottom.Keybind},
  118. {cfg.Select.Keybind, cfg.Cancel.Keybind},
  119. }
  120. }