1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-gtk.git synced 2024-11-16 11:12:44 +00:00
cchat-gtk/internal/ui/service/children.go

81 lines
1.8 KiB
Go
Raw Normal View History

2020-06-07 04:27:28 +00:00
package service
import (
"github.com/diamondburned/cchat-gtk/internal/ui/primitives"
2020-06-07 04:27:28 +00:00
"github.com/diamondburned/cchat-gtk/internal/ui/service/session"
"github.com/gotk3/gotk3/gtk"
)
type children struct {
*gtk.Box
sessions map[string]*session.Row
2020-06-07 04:27:28 +00:00
}
func newChildren() *children {
box, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0)
box.Show()
return &children{box, map[string]*session.Row{}}
}
func (c *children) Sessions() []*session.Row {
// We already know the size beforehand. Allocate it wisely.
var rows = make([]*session.Row, 0, len(c.sessions))
// Loop over widget children.
primitives.EachChildren(c.Box, func(i int, v interface{}) bool {
var id = primitives.GetName(v.(primitives.Namer))
if row, ok := c.sessions[id]; ok {
rows = append(rows, row)
}
return false
})
return rows
}
func (c *children) AddSessionRow(id string, row *session.Row) {
c.sessions[id] = row
2020-06-07 04:27:28 +00:00
c.Box.Add(row)
// Bind the mover.
row.BindMover(id)
// Assert that a name can be obtained.
namer := primitives.Namer(row)
namer.SetName(id) // set ID here, get it in Move
2020-06-07 04:27:28 +00:00
}
func (c *children) RemoveSessionRow(sessionID string) bool {
row, ok := c.sessions[sessionID]
if ok {
delete(c.sessions, sessionID)
2020-06-07 04:27:28 +00:00
c.Box.Remove(row)
}
return ok
}
func (c *children) MoveSession(id, movingID string) {
// Get the widget of the row that is moving.
var moving = c.sessions[movingID]
// Find the current position of the row that we're moving the other one
// underneath of.
var rowix = -1
primitives.EachChildren(c.Box, func(i int, v interface{}) bool {
// The obtained name will be the ID set in AddSessionRow.
if primitives.GetName(v.(primitives.Namer)) == id {
rowix = i
return true
}
return false
})
// Reorder the box.
c.Box.ReorderChild(moving, rowix)
2020-06-07 04:27:28 +00:00
}