zntg/ui/images.go

49 lines
822 B
Go
Raw Normal View History

package ui
type ImageFactoryFn func(Context) (Image, error)
type Images struct {
Factories map[string]ImageFactoryFn
Images map[string]Image
}
func NewImages() *Images {
return &Images{map[string]ImageFactoryFn{}, map[string]Image{}}
}
func (i *Images) AddFactory(name string, fn ImageFactoryFn) {
i.Factories[name] = fn
}
func (i *Images) AddImage(name string, im Image) {
curr := i.Images[name]
if curr != nil {
curr.Destroy()
}
i.Images[name] = im
}
func (i *Images) Destroy() {
for _, im := range i.Images {
im.Destroy()
}
i.Images = nil
}
func (i *Images) Image(ctx Context, name string) Image {
im, ok := i.Images[name]
if ok {
return im
}
fact, ok := i.Factories[name]
if !ok {
return nil
}
im, err := fact(ctx)
if err != nil {
return nil
}
i.Images[name] = im
return im
}