Add support for AttrIcon

This commit is contained in:
Sasha Koshka 2024-08-10 21:07:31 -04:00
parent a60a729ad9
commit 8aa8dc9570

View File

@ -27,12 +27,15 @@ type box struct {
focusQueued *bool
attrColor attrHierarchy[tomo.AttrColor]
attrIcon attrHierarchy[tomo.AttrIcon]
attrTexture attrHierarchy[tomo.AttrTexture]
attrTextureMode attrHierarchy[tomo.AttrTextureMode]
attrBorder attrHierarchy[tomo.AttrBorder]
attrMinimumSize attrHierarchy[tomo.AttrMinimumSize]
attrPadding attrHierarchy[tomo.AttrPadding]
icon canvas.Texture
dndData data.Data
dndAccept []data.Mime
focusable bool
@ -192,6 +195,11 @@ func (this *box) setAttr (attr tomo.Attr, user bool) {
this.invalidateDraw()
}
case tomo.AttrIcon:
if this.attrIcon.Set(attr, user) {
this.handleIconChange()
}
case tomo.AttrTexture:
if this.attrTexture.Set(attr, user) {
this.invalidateDraw()
@ -227,6 +235,11 @@ func (this *box) unsetAttr (kind tomo.AttrKind, user bool) {
this.invalidateDraw()
}
case tomo.AttrKindIcon:
if this.attrIcon.Unset(user) {
this.handleIconChange()
}
case tomo.AttrKindTexture:
if this.attrTexture.Unset(user) {
this.invalidateDraw()
@ -423,20 +436,31 @@ func (this *box) Draw (can canvas.Canvas) {
// centered texture
if textureMode == tomo.TextureModeCenter && texture != nil {
textureBounds := texture.Bounds()
textureOrigin :=
bounds.Min.
Add(image.Pt (
bounds.Dx() / 2,
bounds.Dy() / 2)).
Sub(image.Pt (
textureBounds.Dx() / 2,
textureBounds.Dy() / 2))
pen.Fill(color.Transparent)
pen.Texture(texture)
pen.Rectangle(textureBounds.Sub(textureBounds.Min).Add(textureOrigin))
this.centeredTexture(can, texture)
}
// centered icon
if this.icon != nil {
this.centeredTexture(can, this.icon)
}
}
func (this *box) centeredTexture (can canvas.Canvas, texture canvas.Texture) {
pen := can.Pen()
bounds := this.Bounds()
textureBounds := texture.Bounds()
textureOrigin :=
bounds.Min.
Add(image.Pt (
bounds.Dx() / 2,
bounds.Dy() / 2)).
Sub(image.Pt (
textureBounds.Dx() / 2,
textureBounds.Dy() / 2))
pen.Fill(color.Transparent)
pen.Texture(texture)
pen.Rectangle(textureBounds.Sub(textureBounds.Min).Add(textureOrigin))
}
func (this *box) drawBorders (can canvas.Canvas) {
@ -576,6 +600,15 @@ func (this *box) handleBorderChange (previousBorderSum tomo.Inset, different boo
}
}
func (this *box) handleIconChange () {
this.icon = nil
hierarchy := this.getHierarchy()
if hierarchy == nil { return }
icon := this.attrIcon.Value()
if icon.Icon == tomo.IconUnknown { return }
this.icon = hierarchy.getIconSet().Icon(icon.Icon, icon.Size)
}
func (this *box) invalidateStyle () {
hierarchy := this.getHierarchy()
if hierarchy == nil { return }