events.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // events.go defines the custom event types and command factories that drive
  2. // the chat UI. Commands (tview.Command) run off the main goroutine and return
  3. // events (tview.Event) that are dispatched back to HandleEvent on the UI thread.
  4. package chat
  5. import (
  6. "context"
  7. "log/slog"
  8. "github.com/ayn2op/tview"
  9. "github.com/diamondburned/arikawa/v3/discord"
  10. "github.com/diamondburned/arikawa/v3/gateway"
  11. "github.com/gdamore/tcell/v3"
  12. )
  13. func (m *Model) openState() tview.Command {
  14. return func() tview.Event {
  15. if err := m.state.Open(context.Background()); err != nil {
  16. slog.Error("failed to open chat state", "err", err)
  17. return tcell.NewEventError(err)
  18. }
  19. return nil
  20. }
  21. }
  22. func (m *Model) closeState() tview.Command {
  23. return func() tview.Event {
  24. if m.state != nil {
  25. if err := m.state.Close(); err != nil {
  26. slog.Error("failed to close the session", "err", err)
  27. return tcell.NewEventError(err)
  28. }
  29. }
  30. return nil
  31. }
  32. }
  33. type gatewayEvent struct {
  34. tcell.EventTime
  35. gateway.Event
  36. }
  37. func (m *Model) listen() tview.Command {
  38. return func() tview.Event {
  39. return &gatewayEvent{Event: <-m.events}
  40. }
  41. }
  42. type channelLoadedEvent struct {
  43. tcell.EventTime
  44. Channel discord.Channel
  45. Messages []discord.Message
  46. }
  47. func newChannelLoadedEvent(channel discord.Channel, messages []discord.Message) *channelLoadedEvent {
  48. return &channelLoadedEvent{Channel: channel, Messages: messages}
  49. }
  50. type olderMessagesLoadedEvent struct {
  51. tcell.EventTime
  52. ChannelID discord.ChannelID
  53. Older []discord.Message
  54. }
  55. func newOlderMessagesLoadedEvent(channelID discord.ChannelID, older []discord.Message) *olderMessagesLoadedEvent {
  56. return &olderMessagesLoadedEvent{ChannelID: channelID, Older: older}
  57. }
  58. type LogoutEvent struct{ tcell.EventTime }
  59. func (m *Model) logout() tview.Command {
  60. return func() tview.Event {
  61. return &LogoutEvent{}
  62. }
  63. }
  64. type QuitEvent struct{ tcell.EventTime }
  65. type closeLayerEvent struct {
  66. tcell.EventTime
  67. name string
  68. }
  69. func closeLayer(name string) tview.Command {
  70. return func() tview.Event {
  71. return &closeLayerEvent{name: name}
  72. }
  73. }