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-31 03:19:04 +00:00
package elements
2023-03-05 03:56:44 +00:00
import "image"
2023-03-31 05:06:29 +00:00
import "git.tebibyte.media/sashakoshka/tomo"
2023-04-15 02:03:22 +00:00
import "git.tebibyte.media/sashakoshka/tomo/canvas"
2023-03-05 03:56:44 +00:00
import "git.tebibyte.media/sashakoshka/tomo/artist"
2023-03-31 05:06:29 +00:00
import "git.tebibyte.media/sashakoshka/tomo/default/theme"
2023-03-05 03:56:44 +00:00
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
theme theme.Wrapped
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-03-31 05:06:29 +00:00
element.theme.Case = tomo.C("tomo", "icon")
2023-03-05 03:56:44 +00:00
return
}
2023-04-15 02:03:22 +00:00
// Bind binds this element to an entity.
func (element *Icon) Bind (entity tomo.Entity) {
if entity == nil { element.entity = nil; return }
element.entity = entity
element.updateMinimumSize()
}
// 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-03-05 03:56:44 +00:00
// SetTheme sets the element's theme.
2023-03-31 05:06:29 +00:00
func (element *Icon) SetTheme (new tomo.Theme) {
2023-03-05 03:56:44 +00:00
if new == element.theme.Theme { return }
element.theme.Theme = new
2023-04-15 02:03:22 +00:00
if element.entity == nil { return }
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 canvas.Canvas) {
if element.entity == nil { return }
bounds := element.entity.Bounds()
2023-03-31 05:06:29 +00:00
state := tomo.State { }
2023-03-05 03:56:44 +00:00
element.theme.
2023-03-31 05:06:29 +00:00
Pattern(tomo.PatternBackground, state).
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-03-31 05:06:29 +00:00
element.theme.Color(tomo.ColorForeground, state),
2023-03-05 03:56:44 +00:00
bounds.Min.Add(offset))
}
}
2023-04-15 02:03:22 +00: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())
}
}