1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-gtk.git synced 2024-11-16 19:22:51 +00:00
cchat-gtk/internal/ui/messages/container/compact/compact.go

70 lines
1.7 KiB
Go
Raw Normal View History

2020-06-06 07:44:36 +00:00
package compact
import (
"github.com/diamondburned/cchat"
"github.com/diamondburned/cchat-gtk/internal/gts"
2020-06-06 07:44:36 +00:00
"github.com/diamondburned/cchat-gtk/internal/ui/messages/container"
"github.com/diamondburned/cchat-gtk/internal/ui/messages/message"
"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 {
*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
}
var _ container.Container = (*Container)(nil)
func NewContainer(ctrl container.Controller) *Container {
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
}
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)
c.addMessage(msg)
return msg
}
func (c *Container) CreateMessage(msg cchat.MessageCreate) {
2020-11-06 03:23:06 +00:00
gts.ExecAsync(func() {
msg := WrapMessage(message.NewState(msg))
2021-05-05 03:30:50 +00:00
c.sg.Add(msg)
c.addMessage(msg)
c.CleanMessages()
2020-11-06 03:23:06 +00:00
})
}
func (c *Container) addMessage(msg container.MessageRow) {
_, at := container.InsertPosition(c, msg.Unwrap().Time)
c.AddMessageAt(msg, at)
}
func (c *Container) UpdateMessage(msg cchat.MessageUpdate) {
gts.ExecAsync(func() { container.UpdateMessage(c, msg) })
}
func (c *Container) DeleteMessage(msg cchat.MessageDelete) {
gts.ExecAsync(func() { c.PopMessage(msg.ID()) })
2020-06-06 07:44:36 +00:00
}