discordo/ui/channels.go
ayntgl 119772a983 feat: rename toggle_messages_text_view to toggle_messages_panel
BREAKING: create a backup of your existing configuration file, delete it, and restart the application again to generate a new configuration file with updated fields.
2022-08-19 23:57:54 +05:30

77 lines
1.6 KiB
Go

package ui
import (
"github.com/diamondburned/arikawa/v3/discord"
"github.com/rivo/tview"
)
type ChannelsTree struct {
*tview.TreeView
app *App
}
func NewChannelsTree(app *App) *ChannelsTree {
ct := &ChannelsTree{
TreeView: tview.NewTreeView(),
app: app,
}
ct.SetRoot(tview.NewTreeNode(""))
ct.SetTopLevel(1)
ct.SetSelectedFunc(ct.onSelected)
ct.SetTitle("Channels")
ct.SetTitleAlign(tview.AlignLeft)
ct.SetBorder(true)
ct.SetBorderPadding(0, 0, 1, 1)
return ct
}
func (ct *ChannelsTree) onSelected(node *tview.TreeNode) {
ct.app.SelectedChannel = nil
ct.app.SelectedMessage = -1
ct.app.MessagesPanel.
Highlight().
Clear().
SetTitle("")
ct.app.MessageInputField.SetText("")
ref := node.GetReference()
c, err := ct.app.State.Cabinet.Channel(ref.(discord.ChannelID))
if err != nil {
return
}
// If the channel is a category channel, expand the selected node if it is collapsed, otherwise collapse.
if c.Type == discord.GuildCategory {
node.SetExpanded(!node.IsExpanded())
return
}
ct.app.SelectedChannel = c
ct.app.SetFocus(ct.app.MessageInputField)
title := channelToString(*c)
if c.Topic != "" {
title += " - " + parseMarkdown(c.Topic)
}
ct.app.MessagesPanel.SetTitle(title)
go func() {
// The returned slice will be sorted from latest to oldest.
ms, err := ct.app.State.Messages(c.ID, ct.app.Config.MessagesLimit)
if err != nil {
return
}
for i := len(ms) - 1; i >= 0; i-- {
_, err = ct.app.MessagesPanel.Write(buildMessage(ct.app, ms[i]))
if err != nil {
return
}
}
ct.app.MessagesPanel.ScrollToEnd()
}()
}