1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-gtk.git synced 2024-10-31 19:44:23 +00:00
cchat-gtk/internal/ui/message/view.go

77 lines
1.8 KiB
Go
Raw Normal View History

2020-05-26 06:51:06 +00:00
package message
import (
"github.com/diamondburned/cchat"
"github.com/diamondburned/cchat-gtk/internal/log"
2020-06-04 23:00:41 +00:00
"github.com/diamondburned/cchat-gtk/internal/ui/message/compact"
2020-05-26 06:51:06 +00:00
"github.com/diamondburned/cchat-gtk/internal/ui/message/input"
"github.com/gotk3/gotk3/gtk"
"github.com/pkg/errors"
)
2020-06-04 23:00:41 +00:00
type Container interface {
gtk.IWidget
cchat.MessagesContainer
Reset()
ScrollToBottom()
// PresendMessage is for unsent messages.
PresendMessage(input.PresendMessage) (done func(sendError error))
}
2020-05-26 06:51:06 +00:00
type View struct {
*gtk.Box
2020-06-04 23:00:41 +00:00
Container Container
2020-05-26 06:51:06 +00:00
SendInput *input.Field
current cchat.ServerMessage
2020-06-04 23:00:41 +00:00
author string
2020-05-26 06:51:06 +00:00
}
func NewView() *View {
2020-06-04 23:00:41 +00:00
view := &View{}
view.Container = compact.NewContainer()
view.SendInput = input.NewField(view)
view.Box, _ = gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0)
view.Box.PackStart(view.Container, true, true, 0)
view.Box.PackStart(view.SendInput, false, false, 0)
view.Box.Show()
return view
2020-05-26 06:51:06 +00:00
}
2020-06-04 23:00:41 +00:00
// JoinServer is not thread-safe, but it calls backend functions asynchronously.
func (v *View) JoinServer(session cchat.Session, server cchat.ServerMessage) {
2020-05-26 06:51:06 +00:00
if v.current != nil {
2020-06-04 23:00:41 +00:00
// Backend should handle synchronizing joins and leaves if it needs to.
go func() {
if err := v.current.LeaveServer(); err != nil {
log.Error(errors.Wrap(err, "Error leaving server"))
}
}()
2020-05-28 19:26:55 +00:00
// Clean all messages.
v.Container.Reset()
2020-05-26 06:51:06 +00:00
}
v.current = server
// Skipping ok check because sender can be nil. Without the empty check, Go
// will panic.
sender, _ := server.(cchat.ServerMessageSender)
2020-06-04 23:00:41 +00:00
v.SendInput.SetSender(session, sender)
2020-05-26 06:51:06 +00:00
2020-06-04 23:00:41 +00:00
go func() {
if err := v.current.JoinServer(v.Container); err != nil {
log.Error(errors.Wrap(err, "Failed to join server"))
}
}()
}
func (v *View) PresendMessage(msg input.PresendMessage) func(error) {
return v.Container.PresendMessage(msg)
2020-05-26 06:51:06 +00:00
}