| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- package chat
- import (
- "fmt"
- "log/slog"
- "sync"
- "time"
- "github.com/ayn2op/discordo/internal/config"
- "github.com/ayn2op/discordo/internal/keyring"
- "github.com/ayn2op/discordo/internal/ui"
- "github.com/ayn2op/tview"
- "github.com/diamondburned/arikawa/v3/discord"
- "github.com/diamondburned/ningen/v3"
- "github.com/diamondburned/ningen/v3/states/read"
- "github.com/gdamore/tcell/v3"
- )
- const typingDuration = 10 * time.Second
- const (
- flexPageName = "flex"
- mentionsListPageName = "mentionsList"
- attachmentsListPageName = "attachmentsList"
- confirmModalPageName = "confirmModal"
- channelsPickerPageName = "channelsPicker"
- )
- type View struct {
- *tview.Pages
- mainFlex *tview.Flex
- rightFlex *tview.Flex
- guildsTree *guildsTree
- messagesList *messagesList
- messageInput *messageInput
- channelsPicker *channelsPicker
- selectedChannel *discord.Channel
- selectedChannelMu sync.RWMutex
- typersMu sync.RWMutex
- typers map[discord.UserID]*time.Timer
- app *tview.Application
- cfg *config.Config
- state *ningen.State
- onLogout func()
- }
- func NewView(app *tview.Application, cfg *config.Config, onLogout func()) *View {
- v := &View{
- Pages: tview.NewPages(),
- mainFlex: tview.NewFlex(),
- rightFlex: tview.NewFlex(),
- typers: make(map[discord.UserID]*time.Timer),
- app: app,
- cfg: cfg,
- onLogout: onLogout,
- }
- v.guildsTree = newGuildsTree(cfg, v)
- v.messagesList = newMessagesList(cfg, v)
- v.messageInput = newMessageInput(cfg, v)
- v.channelsPicker = newChannelsPicker(cfg, v)
- v.channelsPicker.SetCancelFunc(v.closePicker)
- v.SetInputCapture(v.onInputCapture)
- v.buildLayout()
- return v
- }
- func (v *View) SelectedChannel() *discord.Channel {
- v.selectedChannelMu.RLock()
- defer v.selectedChannelMu.RUnlock()
- return v.selectedChannel
- }
- func (v *View) SetSelectedChannel(channel *discord.Channel) {
- v.selectedChannelMu.Lock()
- v.selectedChannel = channel
- v.selectedChannelMu.Unlock()
- }
- func (v *View) buildLayout() {
- v.Clear()
- v.rightFlex.Clear()
- v.mainFlex.Clear()
- v.rightFlex.
- SetDirection(tview.FlexRow).
- AddItem(v.messagesList, 0, 1, false).
- AddItem(v.messageInput, 3, 1, false)
- // The guilds tree is always focused first at start-up.
- v.mainFlex.
- AddItem(v.guildsTree, 0, 1, true).
- AddItem(v.rightFlex, 0, 4, false)
- v.AddAndSwitchToPage(flexPageName, v.mainFlex, true)
- }
- func (v *View) togglePicker() {
- if v.HasPage(channelsPickerPageName) {
- v.closePicker()
- } else {
- v.openPicker()
- }
- }
- func (v *View) openPicker() {
- v.AddAndSwitchToPage(channelsPickerPageName, ui.Centered(v.channelsPicker, v.cfg.Picker.Width, v.cfg.Picker.Height), true).ShowPage(flexPageName)
- v.channelsPicker.update()
- }
- func (v *View) closePicker() {
- v.RemovePage(channelsPickerPageName).SwitchToPage(flexPageName)
- v.channelsPicker.Update()
- }
- func (v *View) toggleGuildsTree() {
- // The guilds tree is visible if the number of items is two.
- if v.mainFlex.GetItemCount() == 2 {
- v.mainFlex.RemoveItem(v.guildsTree)
- if v.guildsTree.HasFocus() {
- v.app.SetFocus(v.mainFlex)
- }
- } else {
- v.buildLayout()
- v.app.SetFocus(v.guildsTree)
- }
- }
- func (v *View) focusGuildsTree() bool {
- // The guilds tree is not hidden if the number of items is two.
- if v.mainFlex.GetItemCount() == 2 {
- v.app.SetFocus(v.guildsTree)
- return true
- }
- return false
- }
- func (v *View) focusMessageInput() bool {
- if !v.messageInput.GetDisabled() {
- v.app.SetFocus(v.messageInput)
- return true
- }
- return false
- }
- func (v *View) focusPrevious() {
- switch v.app.GetFocus() {
- case v.guildsTree:
- v.focusMessageInput()
- case v.messagesList: // Handle both a.messagesList and a.flex as well as other edge cases (if there is).
- if ok := v.focusGuildsTree(); !ok {
- v.app.SetFocus(v.messageInput)
- }
- case v.messageInput:
- v.app.SetFocus(v.messagesList)
- }
- }
- func (v *View) focusNext() {
- switch v.app.GetFocus() {
- case v.guildsTree:
- v.app.SetFocus(v.messagesList)
- case v.messagesList:
- v.focusMessageInput()
- case v.messageInput: // Handle both a.messageInput and a.flex as well as other edge cases (if there is).
- if ok := v.focusGuildsTree(); !ok {
- v.app.SetFocus(v.messagesList)
- }
- }
- }
- func (v *View) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
- switch event.Name() {
- case v.cfg.Keys.FocusGuildsTree:
- v.messageInput.removeMentionsList()
- v.focusGuildsTree()
- return nil
- case v.cfg.Keys.FocusMessagesList:
- v.messageInput.removeMentionsList()
- v.app.SetFocus(v.messagesList)
- return nil
- case v.cfg.Keys.FocusMessageInput:
- v.focusMessageInput()
- return nil
- case v.cfg.Keys.FocusPrevious:
- v.focusPrevious()
- return nil
- case v.cfg.Keys.FocusNext:
- v.focusNext()
- return nil
- case v.cfg.Keys.Logout:
- if v.onLogout != nil {
- v.onLogout()
- }
- if err := keyring.DeleteToken(); err != nil {
- slog.Error("failed to delete token from keyring", "err", err)
- return nil
- }
- return nil
- case v.cfg.Keys.ToggleGuildsTree:
- v.toggleGuildsTree()
- return nil
- case v.cfg.Keys.Picker.Toggle:
- v.togglePicker()
- return nil
- }
- return event
- }
- func (v *View) showConfirmModal(prompt string, buttons []string, onDone func(label string)) {
- previousFocus := v.app.GetFocus()
- modal := tview.NewModal().
- SetText(prompt).
- AddButtons(buttons).
- SetDoneFunc(func(_ int, buttonLabel string) {
- v.RemovePage(confirmModalPageName).SwitchToPage(flexPageName)
- v.app.SetFocus(previousFocus)
- if onDone != nil {
- onDone(buttonLabel)
- }
- })
- v.
- AddAndSwitchToPage(confirmModalPageName, ui.Centered(modal, 0, 0), true).
- ShowPage(flexPageName)
- }
- func (v *View) onReadUpdate(event *read.UpdateEvent) {
- var guildNode *tview.TreeNode
- v.guildsTree.
- GetRoot().
- Walk(func(node, parent *tview.TreeNode) bool {
- switch node.GetReference() {
- case event.GuildID:
- node.SetTextStyle(v.guildsTree.getGuildNodeStyle(event.GuildID))
- guildNode = node
- return false
- case event.ChannelID:
- // private channel
- if !event.GuildID.IsValid() {
- style := v.guildsTree.getChannelNodeStyle(event.ChannelID)
- node.SetTextStyle(style)
- return false
- }
- }
- return true
- })
- if guildNode != nil {
- guildNode.Walk(func(node, parent *tview.TreeNode) bool {
- if node.GetReference() == event.ChannelID {
- node.SetTextStyle(v.guildsTree.getChannelNodeStyle(event.ChannelID))
- return false
- }
- return true
- })
- }
- v.app.Draw()
- }
- func (v *View) clearTypers() {
- v.typersMu.Lock()
- for _, timer := range v.typers {
- timer.Stop()
- }
- clear(v.typers)
- v.typersMu.Unlock()
- v.updateFooter()
- }
- func (v *View) addTyper(userID discord.UserID) {
- v.typersMu.Lock()
- typer, ok := v.typers[userID]
- if ok {
- typer.Reset(typingDuration)
- } else {
- v.typers[userID] = time.AfterFunc(typingDuration, func() {
- v.removeTyper(userID)
- })
- }
- v.typersMu.Unlock()
- v.updateFooter()
- }
- func (v *View) removeTyper(userID discord.UserID) {
- v.typersMu.Lock()
- if typer, ok := v.typers[userID]; ok {
- typer.Stop()
- delete(v.typers, userID)
- }
- v.typersMu.Unlock()
- v.updateFooter()
- }
- func (v *View) updateFooter() {
- selectedChannel := v.SelectedChannel()
- if selectedChannel == nil {
- return
- }
- guildID := selectedChannel.GuildID
- v.typersMu.RLock()
- defer v.typersMu.RUnlock()
- var footer string
- if len(v.typers) > 0 {
- var names []string
- for userID := range v.typers {
- var name string
- if guildID.IsValid() {
- member, err := v.state.Cabinet.Member(guildID, userID)
- if err != nil {
- slog.Error("failed to get member from state", "err", err, "guild_id", guildID, "user_id", userID)
- continue
- }
- if member.Nick != "" {
- name = member.Nick
- } else {
- name = member.User.DisplayOrUsername()
- }
- } else {
- for _, recipient := range selectedChannel.DMRecipients {
- if recipient.ID == userID {
- name = recipient.DisplayOrUsername()
- break
- }
- }
- }
- if name != "" {
- names = append(names, name)
- }
- }
- switch len(names) {
- case 1:
- footer = fmt.Sprintf("%s is typing...", names[0])
- case 2:
- footer = fmt.Sprintf("%s and %s are typing...", names[0], names[1])
- case 3:
- footer = fmt.Sprintf("%s, %s, and %s are typing...", names[0], names[1], names[2])
- default:
- footer = "Several people are typing..."
- }
- }
- go v.app.QueueUpdateDraw(func() { v.messagesList.SetFooter(footer) })
- }
|