53 lines
1.3 KiB
Go
53 lines
1.3 KiB
Go
package objects
|
|
|
|
import "git.tebibyte.media/tomo/tomo"
|
|
import "git.tebibyte.media/tomo/tomo/data"
|
|
import "git.tebibyte.media/tomo/tomo/canvas"
|
|
|
|
// Icon displays a single icon.
|
|
type Icon struct {
|
|
tomo.Box
|
|
}
|
|
|
|
func iconSizeString (size tomo.IconSize) string {
|
|
switch size {
|
|
case tomo.IconSizeLarge: return "large"
|
|
case tomo.IconSizeMedium: return "medium"
|
|
default: return "small"
|
|
}
|
|
}
|
|
|
|
// NewIcon creates a new icon from an icon ID.
|
|
func NewIcon (id tomo.Icon, size tomo.IconSize) *Icon {
|
|
this := &Icon {
|
|
Box: tomo.NewBox(),
|
|
}
|
|
this.SetRole(tomo.R("objects", "Icon"))
|
|
this.SetTag(iconSizeString(size), true)
|
|
this.setTexture(id.Texture(size))
|
|
return this
|
|
}
|
|
|
|
// NewMimeIcon creates a new icon from a MIME type.
|
|
func NewMimeIcon (mime data.Mime, size tomo.IconSize) *Icon {
|
|
this := &Icon {
|
|
Box: tomo.NewBox(),
|
|
}
|
|
this.SetRole(tomo.R("objects", "Icon"))
|
|
this.SetTag(iconSizeString(size), true)
|
|
this.setTexture(tomo.MimeIcon(mime, size))
|
|
return this
|
|
}
|
|
|
|
func (this *Icon) 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)))
|
|
}
|
|
}
|