objects/mimeicon.go

47 lines
1.2 KiB
Go

package objects
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/data"
import "git.tebibyte.media/tomo/tomo/canvas"
// MimeIcon displays an icon of a MIME type.
type MimeIcon struct {
tomo.Box
mime data.Mime
size tomo.IconSize
}
// NewMimeIcon creates a new icon from a MIME type.
func NewMimeIcon (mime data.Mime, size tomo.IconSize) *MimeIcon {
this := &MimeIcon {
Box: tomo.NewBox(),
}
this.SetRole(tomo.R("objects", "MimeIcon"))
this.SetIcon(mime, size)
this.OnIconSetChange(this.handleIconSetChange)
return this
}
// SetIcon sets the MIME type and size of the icon.
func (this *MimeIcon) SetIcon (mime data.Mime, size tomo.IconSize) {
if this.mime == mime && this.size == size { return }
this.mime = mime
this.size = size
this.setTexture(tomo.MimeIconTexture(mime, size))
}
func (this *MimeIcon) handleIconSetChange () {
this.setTexture(tomo.MimeIconTexture(this.mime, this.size))
}
func (this *MimeIcon) setTexture (texture canvas.Texture) {
this.SetAttr(tomo.ATexture(texture))
this.SetAttr(tomo.ATextureMode(tomo.TextureModeCenter))
if texture == nil {
this.SetAttr(tomo.AMinimumSize(0, 0))
} else {
bounds := texture.Bounds()
this.SetAttr(tomo.AttrMinimumSize(bounds.Max.Sub(bounds.Min)))
}
}