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
1.9 KiB
Go
Raw Normal View History

2023-03-31 03:19:04 +00:00
package elements
2023-03-05 03:56:44 +00:00
import "image"
2023-05-03 23:40:30 +00:00
import "tomo"
import "art"
2023-03-05 03:56:44 +00:00
2023-05-03 05:07:44 +00:00
var iconCase = tomo.C("tomo", "icon")
2023-04-15 02:03:22 +00:00
// Icon is an element capable of displaying a singular icon.
2023-03-05 03:56:44 +00:00
type Icon struct {
2023-04-15 02:03:22 +00:00
entity tomo.Entity
id tomo.Icon
size tomo.IconSize
2023-03-05 03:56:44 +00:00
}
2023-04-15 02:03:22 +00:00
// Icon creates a new icon element.
2023-03-31 05:06:29 +00:00
func NewIcon (id tomo.Icon, size tomo.IconSize) (element *Icon) {
2023-03-05 03:56:44 +00:00
element = &Icon {
id: id,
size: size,
}
2023-05-03 05:07:44 +00:00
element.entity = tomo.GetBackend().NewEntity(element)
element.updateMinimumSize()
2023-03-05 03:56:44 +00:00
return
}
// Entity returns this element's entity.
func (element *Icon) Entity () tomo.Entity {
return element.entity
2023-04-15 02:03:22 +00:00
}
// SetIcon sets the element's icon.
2023-03-31 05:06:29 +00:00
func (element *Icon) SetIcon (id tomo.Icon, size tomo.IconSize) {
2023-03-21 16:26:06 +00:00
element.id = id
element.size = size
2023-04-15 02:03:22 +00:00
if element.entity == nil { return }
2023-03-21 16:26:06 +00:00
element.updateMinimumSize()
2023-04-15 02:03:22 +00:00
element.entity.Invalidate()
2023-03-21 16:26:06 +00:00
}
2023-05-03 05:07:44 +00:00
func (element *Icon) HandleThemeChange () {
2023-03-05 03:56:44 +00:00
element.updateMinimumSize()
2023-04-15 02:03:22 +00:00
element.entity.Invalidate()
2023-03-05 03:56:44 +00:00
}
2023-04-15 02:03:22 +00:00
// Draw causes the element to draw to the specified destination canvas.
func (element *Icon) Draw (destination art.Canvas) {
2023-04-15 02:03:22 +00:00
if element.entity == nil { return }
bounds := element.entity.Bounds()
2023-03-31 05:06:29 +00:00
state := tomo.State { }
2023-05-03 05:07:44 +00:00
element.entity.Theme().
Pattern(tomo.PatternBackground, state, iconCase).
2023-04-15 02:03:22 +00:00
Draw(destination, bounds)
2023-03-05 03:56:44 +00: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-15 02:03:22 +00:00
destination,
2023-05-03 05:07:44 +00:00
element.entity.Theme().Color(tomo.ColorForeground, state, iconCase),
2023-03-05 03:56:44 +00:00
bounds.Min.Add(offset))
}
}
2023-04-15 02:03:22 +00:00
func (element *Icon) icon () art.Icon {
2023-05-03 05:07:44 +00:00
return element.entity.Theme().Icon(element.id, element.size, iconCase)
2023-04-15 02:03:22 +00: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())
}
}