2020-06-06 07:44:36 +00:00
|
|
|
package compact
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/diamondburned/cchat"
|
2020-11-05 19:08:30 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/gts"
|
2020-06-06 07:44:36 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/messages/container"
|
2021-03-29 21:46:52 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/messages/message"
|
2020-06-13 07:29:32 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/primitives"
|
2021-05-05 03:30:50 +00:00
|
|
|
"github.com/gotk3/gotk3/gtk"
|
2020-06-06 07:44:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Container struct {
|
2021-01-02 09:24:14 +00:00
|
|
|
*container.ListContainer
|
2021-05-05 03:30:50 +00:00
|
|
|
sg SizeGroups
|
|
|
|
}
|
|
|
|
|
|
|
|
type SizeGroups struct {
|
|
|
|
Timestamp *gtk.SizeGroup
|
|
|
|
Username *gtk.SizeGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSizeGroups() SizeGroups {
|
|
|
|
sg1, _ := gtk.SizeGroupNew(gtk.SIZE_GROUP_HORIZONTAL)
|
|
|
|
sg2, _ := gtk.SizeGroupNew(gtk.SIZE_GROUP_HORIZONTAL)
|
|
|
|
|
|
|
|
return SizeGroups{sg1, sg2}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sgs *SizeGroups) Add(msg Message) {
|
|
|
|
sgs.Timestamp.AddWidget(msg.Timestamp)
|
|
|
|
sgs.Username.AddWidget(msg.Username)
|
2020-06-06 07:44:36 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
var _ container.Container = (*Container)(nil)
|
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
func NewContainer(ctrl container.Controller) *Container {
|
2021-03-29 21:46:52 +00:00
|
|
|
c := container.NewListContainer(ctrl)
|
2021-01-05 02:05:33 +00:00
|
|
|
primitives.AddClass(c, "compact-container")
|
2021-05-05 03:30:50 +00:00
|
|
|
return &Container{c, NewSizeGroups()}
|
2020-06-06 07:44:36 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
func (c *Container) NewPresendMessage(state *message.PresendState) container.PresendMessageRow {
|
|
|
|
msg := WrapPresendMessage(state)
|
2021-05-05 03:30:50 +00:00
|
|
|
c.sg.Add(msg.Message)
|
2021-05-01 07:26:17 +00:00
|
|
|
c.addMessage(msg)
|
2021-03-29 21:46:52 +00:00
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2020-11-05 19:08:30 +00:00
|
|
|
func (c *Container) CreateMessage(msg cchat.MessageCreate) {
|
2020-11-06 03:23:06 +00:00
|
|
|
gts.ExecAsync(func() {
|
2021-03-29 21:46:52 +00:00
|
|
|
msg := WrapMessage(message.NewState(msg))
|
2021-05-05 03:30:50 +00:00
|
|
|
c.sg.Add(msg)
|
2021-05-01 07:26:17 +00:00
|
|
|
c.addMessage(msg)
|
|
|
|
c.CleanMessages()
|
2020-11-06 03:23:06 +00:00
|
|
|
})
|
2020-11-05 19:08:30 +00:00
|
|
|
}
|
|
|
|
|
2021-05-01 07:26:17 +00:00
|
|
|
func (c *Container) addMessage(msg container.MessageRow) {
|
|
|
|
_, at := container.InsertPosition(c, msg.Unwrap().Time)
|
|
|
|
c.AddMessageAt(msg, at)
|
|
|
|
}
|
|
|
|
|
2020-11-05 19:08:30 +00:00
|
|
|
func (c *Container) UpdateMessage(msg cchat.MessageUpdate) {
|
2021-03-29 21:46:52 +00:00
|
|
|
gts.ExecAsync(func() { container.UpdateMessage(c, msg) })
|
2020-11-05 19:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Container) DeleteMessage(msg cchat.MessageDelete) {
|
2021-03-29 21:46:52 +00:00
|
|
|
gts.ExecAsync(func() { c.PopMessage(msg.ID()) })
|
2020-06-06 07:44:36 +00:00
|
|
|
}
|