78 lines
2 KiB
Go
78 lines
2 KiB
Go
package gui
|
|
|
|
import (
|
|
"github.com/diamondburned/gotk4/pkg/core/glib"
|
|
"github.com/diamondburned/gotk4/pkg/gdk/v4"
|
|
"github.com/diamondburned/gotk4/pkg/gdkpixbuf/v2"
|
|
"github.com/diamondburned/gotk4/pkg/gtk/v4"
|
|
)
|
|
|
|
type pictureItemFactory[T comparable] struct {
|
|
model modelAccessor[T]
|
|
value valueAccessor[T]
|
|
listItemFactory *gtk.SignalListItemFactory
|
|
pictures map[T]*gtk.Picture
|
|
}
|
|
|
|
type (
|
|
modelAccessor[T any] func(item *glib.Object) T
|
|
valueAccessor[T any] func(T) (tooltip string, pixbuf *gdkpixbuf.Pixbuf)
|
|
)
|
|
|
|
func newPictureItemFactory[T comparable](model modelAccessor[T], value valueAccessor[T]) *pictureItemFactory[T] {
|
|
factory := pictureItemFactory[T]{
|
|
model: model,
|
|
value: value,
|
|
listItemFactory: gtk.NewSignalListItemFactory(),
|
|
pictures: make(map[T]*gtk.Picture),
|
|
}
|
|
factory.listItemFactory.Connect("setup", factory.setup)
|
|
factory.listItemFactory.Connect("bind", factory.bind)
|
|
factory.listItemFactory.Connect("unbind", factory.unbind)
|
|
|
|
return &factory
|
|
}
|
|
|
|
func (f *pictureItemFactory[T]) setup(listitem *gtk.ListItem) {
|
|
picture := gtk.NewPicture()
|
|
picture.SetContentFit(gtk.ContentFitContain)
|
|
picture.SetCanShrink(false)
|
|
listitem.SetChild(picture)
|
|
}
|
|
|
|
func (f *pictureItemFactory[T]) bind(listitem *gtk.ListItem) {
|
|
item := listitem.Item()
|
|
if item == nil {
|
|
return
|
|
}
|
|
itemID := f.model(item)
|
|
|
|
tooltip, pixbuf := f.value(itemID)
|
|
picture := listitem.Child().(*gtk.Picture)
|
|
picture.SetTooltipText(tooltip)
|
|
if pixbuf != nil {
|
|
picture.SetPaintable(gdk.NewTextureForPixbuf(pixbuf))
|
|
}
|
|
|
|
f.pictures[itemID] = picture
|
|
}
|
|
|
|
func (f *pictureItemFactory[T]) unbind(listitem *gtk.ListItem) {
|
|
itemObj := listitem.Item()
|
|
if itemObj == nil {
|
|
return
|
|
}
|
|
|
|
itemID := f.model(itemObj)
|
|
delete(f.pictures, itemID)
|
|
}
|
|
|
|
func (f *pictureItemFactory[T]) ListItemFactory() *gtk.ListItemFactory {
|
|
return &f.listItemFactory.ListItemFactory
|
|
}
|
|
|
|
func (f *pictureItemFactory[T]) Picture(itemID T) (*gtk.Picture, bool) {
|
|
picture, found := f.pictures[itemID]
|
|
|
|
return picture, found
|
|
}
|