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

87 lines
2.2 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-04-14 20:03:22 -06:00
import "git.tebibyte.media/sashakoshka/tomo/canvas"
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
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
theme theme.Wrapped
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,
}
element.entity = tomo.NewEntity(element)
2023-03-30 23:06:29 -06:00
element.theme.Case = tomo.C("tomo", "icon")
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-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
2023-04-14 20:03:22 -06:00
if element.entity == nil { return }
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.
func (element *Icon) Draw (destination canvas.Canvas) {
if element.entity == nil { return }
bounds := element.entity.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-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-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))
}
}
2023-04-14 20:03:22 -06:00
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.entity.SetMinimumSize(0, 0)
} else {
bounds := icon.Bounds()
element.entity.SetMinimumSize(bounds.Dx(), bounds.Dy())
}
}