objects/mimeicon.go

54 lines
1.4 KiB
Go
Raw Normal View History

2024-08-10 19:44:03 -06:00
package objects
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/data"
import "git.tebibyte.media/tomo/tomo/canvas"
2024-08-24 18:10:49 -06:00
var _ tomo.Object = new(MimeIcon)
2024-08-10 19:44:03 -06:00
// MimeIcon displays an icon of a MIME type.
type MimeIcon struct {
2024-08-24 18:10:49 -06:00
box tomo.Box
2024-08-10 19:44:03 -06:00
mime data.Mime
size tomo.IconSize
}
// NewMimeIcon creates a new icon from a MIME type.
func NewMimeIcon (mime data.Mime, size tomo.IconSize) *MimeIcon {
2024-08-24 18:10:49 -06:00
mimeIcon := &MimeIcon {
box: tomo.NewBox(),
2024-08-10 19:44:03 -06:00
}
2024-08-24 18:10:49 -06:00
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
2024-08-10 19:44:03 -06:00
}
// 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) {
2024-08-24 18:10:49 -06:00
this.box.SetAttr(tomo.ATexture(texture))
this.box.SetAttr(tomo.ATextureMode(tomo.TextureModeCenter))
2024-08-10 19:44:03 -06:00
if texture == nil {
2024-08-24 18:10:49 -06:00
this.box.SetAttr(tomo.AMinimumSize(0, 0))
2024-08-10 19:44:03 -06:00
} else {
bounds := texture.Bounds()
2024-08-24 18:10:49 -06:00
this.box.SetAttr(tomo.AttrMinimumSize(bounds.Max.Sub(bounds.Min)))
2024-08-10 19:44:03 -06:00
}
}