2023-03-04 20:56:44 -07:00
|
|
|
package basicElements
|
|
|
|
|
|
|
|
import "image"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/theme"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
|
|
|
|
|
|
|
type Icon struct {
|
|
|
|
*core.Core
|
|
|
|
core core.CoreControl
|
|
|
|
theme theme.Wrapped
|
|
|
|
id theme.Icon
|
|
|
|
size theme.IconSize
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIcon (id theme.Icon, size theme.IconSize) (element *Icon) {
|
|
|
|
element = &Icon {
|
|
|
|
id: id,
|
|
|
|
size: size,
|
|
|
|
}
|
|
|
|
element.theme.Case = theme.C("basic", "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-21 10:26:06 -06:00
|
|
|
func (element *Icon) SetIcon (id theme.Icon, size theme.IconSize) {
|
|
|
|
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.
|
|
|
|
func (element *Icon) SetTheme (new theme.Theme) {
|
|
|
|
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()
|
|
|
|
state := theme.State { }
|
|
|
|
element.theme.
|
|
|
|
Pattern(theme.PatternBackground, state).
|
|
|
|
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,
|
|
|
|
element.theme.Color (
|
|
|
|
theme.ColorForeground, state),
|
|
|
|
bounds.Min.Add(offset))
|
|
|
|
}
|
|
|
|
}
|