Split MimeIcon out from Icon

This commit is contained in:
Sasha Koshka 2024-08-10 21:44:03 -04:00
parent 572e0c49af
commit 9856cd327f
2 changed files with 52 additions and 25 deletions

30
icon.go
View File

@ -1,8 +1,6 @@
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 {
@ -18,35 +16,17 @@ func iconSizeString (size tomo.IconSize) string {
}
// NewIcon creates a new icon from an icon ID.
func NewIcon (id tomo.Icon, size tomo.IconSize) *Icon {
func NewIcon (icon 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))
this.SetIcon(icon, 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"))
// SetIcon sets the icon.
func (this *Icon) SetIcon (icon tomo.Icon, size tomo.IconSize) {
this.SetAttr(tomo.AIcon(icon, size))
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)))
}
}

47
mimeicon.go Normal file
View File

@ -0,0 +1,47 @@
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", "Icon"))
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.SetTag(iconSizeString(size), true)
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)))
}
}