package objects import "git.tebibyte.media/tomo/tomo" import "git.tebibyte.media/tomo/tomo/data" import "git.tebibyte.media/tomo/tomo/canvas" var _ tomo.Object = new(MimeIcon) // MimeIcon displays an icon of a MIME type. type MimeIcon struct { box 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 { mimeIcon := &MimeIcon { box: tomo.NewBox(), } mimeIcon.box.SetRole(tomo.R("objects", "MimeIcon")) mimeIcon.SetIcon(mime, size) mimeIcon.box.OnIconSetChange(mimeIcon.handleIconSetChange) return mimeIcon } // GetBox returns the underlying box. func (this *MimeIcon) GetBox () tomo.Box { return this.box } // 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.box.SetAttr(tomo.ATexture(texture)) this.box.SetAttr(tomo.ATextureMode(tomo.TextureModeCenter)) if texture == nil { this.box.SetAttr(tomo.AMinimumSize(0, 0)) } else { bounds := texture.Bounds() this.box.SetAttr(tomo.AttrMinimumSize(bounds.Max.Sub(bounds.Min))) } }