1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-gtk.git synced 2024-11-01 03:54:16 +00:00
cchat-gtk/internal/ui/messages/view.go

88 lines
2.1 KiB
Go
Raw Normal View History

2020-06-06 07:44:36 +00:00
package messages
2020-05-26 06:51:06 +00:00
import (
"github.com/diamondburned/cchat"
"github.com/diamondburned/cchat-gtk/internal/log"
2020-06-06 07:44:36 +00:00
"github.com/diamondburned/cchat-gtk/internal/ui/messages/container"
2020-06-07 04:27:28 +00:00
"github.com/diamondburned/cchat-gtk/internal/ui/messages/container/cozy"
2020-06-06 07:44:36 +00:00
"github.com/diamondburned/cchat-gtk/internal/ui/messages/input"
2020-05-26 06:51:06 +00:00
"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-06 07:44:36 +00:00
Container 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{}
2020-06-06 07:44:36 +00:00
// TODO: change
2020-06-07 04:27:28 +00:00
// view.Container = compact.NewContainer()
view.Container = cozy.NewContainer()
2020-06-04 23:00:41 +00:00
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-07 07:06:13 +00:00
func (v *View) Reset() {
// Leave the server if any.
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-26 06:51:06 +00:00
}
2020-06-07 07:06:13 +00:00
// Clean all messages.
v.Container.Reset()
// Reset the input.
v.SendInput.Reset()
}
// JoinServer is not thread-safe, but it calls backend functions asynchronously.
func (v *View) JoinServer(session cchat.Session, server cchat.ServerMessage) {
// Reset before setting.
v.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
}