Add GTK application implementation
This commit is contained in:
parent
d1bb505e0a
commit
b7c0307f6b
29 changed files with 2564 additions and 266 deletions
78
internal/gui/pictureitemfactory.go
Normal file
78
internal/gui/pictureitemfactory.go
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
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
|
||||
}
|
||||
Loading…
Reference in a new issue