channels_picker.go 4.3 KB

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