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.8 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-03-30 23:06:29 -06:00
import "git.tebibyte.media/sashakoshka/tomo/default/theme"
2023-03-04 20:56:44 -07:00
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
type Icon struct {
*core.Core
core core.CoreControl
theme theme.Wrapped
2023-03-30 23:06:29 -06:00
id tomo.Icon
size tomo.IconSize
2023-03-04 20:56:44 -07:00
}
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-03-30 23:06:29 -06:00
element.theme.Case = tomo.C("tomo", "icon")
2023-03-14 23:41:23 -06:00
element.Core, element.core = core.NewCore(element, element.draw)
2023-03-04 20:56:44 -07:00
element.updateMinimumSize()
return
}
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
element.updateMinimumSize()
if element.core.HasImage() {
element.draw()
element.core.DamageAll()
}
}
2023-03-04 20:56:44 -07:00
// SetTheme sets the element's theme.
2023-03-30 23:06:29 -06:00
func (element *Icon) SetTheme (new tomo.Theme) {
2023-03-04 20:56:44 -07:00
if new == element.theme.Theme { return }
element.theme.Theme = new
element.updateMinimumSize()
if element.core.HasImage() {
element.draw()
element.core.DamageAll()
}
}
func (element *Icon) icon () artist.Icon {
return element.theme.Icon(element.id, element.size)
}
func (element *Icon) updateMinimumSize () {
icon := element.icon()
if icon == nil {
element.core.SetMinimumSize(0, 0)
} else {
bounds := icon.Bounds()
element.core.SetMinimumSize(bounds.Dx(), bounds.Dy())
}
}
func (element *Icon) draw () {
bounds := element.Bounds()
2023-03-30 23:06:29 -06:00
state := tomo.State { }
2023-03-04 20:56:44 -07:00
element.theme.
2023-03-30 23:06:29 -06:00
Pattern(tomo.PatternBackground, state).
2023-03-04 20:56:44 -07:00
Draw(element.core, bounds)
icon := element.icon()
if icon != nil {
iconBounds := icon.Bounds()
offset := image.Pt (
(bounds.Dx() - iconBounds.Dx()) / 2,
(bounds.Dy() - iconBounds.Dy()) / 2)
icon.Draw (
element.core,
2023-03-30 23:06:29 -06:00
element.theme.Color(tomo.ColorForeground, state),
2023-03-04 20:56:44 -07:00
bounds.Min.Add(offset))
}
}