This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/elements/icon.go

81 lines
2.0 KiB
Go
Raw Normal View History

2023-03-30 21:19:04 -06:00
package elements
2023-03-04 20:56:44 -07:00
import "image"
2023-03-30 23:06:29 -06:00
import "git.tebibyte.media/sashakoshka/tomo"
2023-03-04 20:56:44 -07:00
import "git.tebibyte.media/sashakoshka/tomo/artist"
2023-05-02 23:07:44 -06:00
var iconCase = tomo.C("tomo", "icon")
2023-04-14 20:03:22 -06:00
// Icon is an element capable of displaying a singular icon.
2023-03-04 20:56:44 -07:00
type Icon struct {
2023-04-14 20:03:22 -06:00
entity tomo.Entity
id tomo.Icon
size tomo.IconSize
2023-03-04 20:56:44 -07:00
}
2023-04-14 20:03:22 -06:00
// Icon creates a new icon element.
2023-03-30 23:06:29 -06:00
func NewIcon (id tomo.Icon, size tomo.IconSize) (element *Icon) {
2023-03-04 20:56:44 -07:00
element = &Icon {
id: id,
size: size,
}
2023-05-02 23:07:44 -06:00
element.entity = tomo.GetBackend().NewEntity(element)
element.updateMinimumSize()
2023-03-04 20:56:44 -07:00
return
}
// Entity returns this element's entity.
func (element *Icon) Entity () tomo.Entity {
return element.entity
2023-04-14 20:03:22 -06:00
}
// SetIcon sets the element's icon.
2023-03-30 23:06:29 -06:00
func (element *Icon) SetIcon (id tomo.Icon, size tomo.IconSize) {
2023-03-21 10:26:06 -06:00
element.id = id
element.size = size
2023-04-14 20:03:22 -06:00
if element.entity == nil { return }
2023-03-21 10:26:06 -06:00
element.updateMinimumSize()
2023-04-14 20:03:22 -06:00
element.entity.Invalidate()
2023-03-21 10:26:06 -06:00
}
2023-05-02 23:07:44 -06:00
func (element *Icon) HandleThemeChange () {
2023-03-04 20:56:44 -07:00
element.updateMinimumSize()
2023-04-14 20:03:22 -06:00
element.entity.Invalidate()
2023-03-04 20:56:44 -07:00
}
2023-04-14 20:03:22 -06:00
// Draw causes the element to draw to the specified destination canvas.
2023-05-02 20:19:29 -06:00
func (element *Icon) Draw (destination artist.Canvas) {
2023-04-14 20:03:22 -06:00
if element.entity == nil { return }
bounds := element.entity.Bounds()
2023-03-30 23:06:29 -06:00
state := tomo.State { }
2023-05-02 23:07:44 -06:00
element.entity.Theme().
Pattern(tomo.PatternBackground, state, iconCase).
2023-04-14 20:03:22 -06:00
Draw(destination, bounds)
2023-03-04 20:56:44 -07:00
icon := element.icon()
if icon != nil {
iconBounds := icon.Bounds()
offset := image.Pt (
(bounds.Dx() - iconBounds.Dx()) / 2,
(bounds.Dy() - iconBounds.Dy()) / 2)
icon.Draw (
2023-04-14 20:03:22 -06:00
destination,
2023-05-02 23:07:44 -06:00
element.entity.Theme().Color(tomo.ColorForeground, state, iconCase),
2023-03-04 20:56:44 -07:00
bounds.Min.Add(offset))
}
}
2023-04-14 20:03:22 -06:00
func (element *Icon) icon () artist.Icon {
2023-05-02 23:07:44 -06:00
return element.entity.Theme().Icon(element.id, element.size, iconCase)
2023-04-14 20:03:22 -06:00
}
func (element *Icon) updateMinimumSize () {
icon := element.icon()
if icon == nil {
element.entity.SetMinimumSize(0, 0)
} else {
bounds := icon.Bounds()
element.entity.SetMinimumSize(bounds.Dx(), bounds.Dy())
}
}