objects/icon.go

53 lines
1.3 KiB
Go
Raw Normal View History

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
}
2024-07-21 09:48:28 -06:00
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.
2024-05-26 15:21:58 -06:00
func NewIcon (id tomo.Icon, size tomo.IconSize) *Icon {
this := &Icon {
Box: tomo.NewBox(),
}
2024-07-21 09:48:28 -06:00
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.
2024-05-26 15:21:58 -06:00
func NewMimeIcon (mime data.Mime, size tomo.IconSize) *Icon {
this := &Icon {
Box: tomo.NewBox(),
}
2024-07-21 09:48:28 -06:00
this.SetRole(tomo.R("objects", "Icon"))
this.SetTag(iconSizeString(size), true)
this.setTexture(tomo.MimeIcon(mime, size))
return this
}
2024-07-21 09:48:28 -06:00
func (this *Icon) setTexture (texture canvas.Texture) {
this.SetAttr(tomo.ATexture(texture))
this.SetAttr(tomo.ATextureMode(tomo.TextureModeCenter))
2023-09-05 11:04:06 -06:00
if texture == nil {
2024-07-21 09:48:28 -06:00
this.SetAttr(tomo.AMinimumSize(0, 0))
2023-09-05 11:04:06 -06:00
} else {
bounds := texture.Bounds()
2024-07-21 09:48:28 -06:00
this.SetAttr(tomo.AttrMinimumSize(bounds.Max.Sub(bounds.Min)))
2023-09-05 11:04:06 -06:00
}
}