Added an Icon element

This commit is contained in:
Sasha Koshka 2023-03-04 22:56:44 -05:00
parent 1e8bb56b7c
commit 61bbe0e346
2 changed files with 106 additions and 0 deletions

70
elements/basic/icon.go Normal file
View File

@ -0,0 +1,70 @@
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")
element.Core, element.core = core.NewCore(element.draw)
element.updateMinimumSize()
return
}
// 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))
}
}

36
examples/icons/main.go Normal file
View File

@ -0,0 +1,36 @@
package main
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/layouts/basic"
import "git.tebibyte.media/sashakoshka/tomo/elements/basic"
import _ "git.tebibyte.media/sashakoshka/tomo/backends/x"
func main () {
tomo.Run(run)
}
func run () {
window, _ := tomo.NewWindow(360, 2)
window.SetTitle("Icons")
container := basicElements.NewContainer(basicLayouts.Vertical { true, true })
window.Adopt(container)
container.Adopt(basicElements.NewLabel("Just some of the wonderful icons we have:", false), false)
container.Adopt(basicElements.NewSpacer(true), false)
container.Adopt(icons(theme.IconHome, theme.IconRepositories), true)
container.Adopt(icons(theme.IconFile, theme.IconCD), true)
container.Adopt(icons(theme.IconOpen, theme.IconRemoveBookmark), true)
window.OnClose(tomo.Stop)
window.Show()
}
func icons (min, max theme.Icon) (container *basicElements.Container) {
container = basicElements.NewContainer(basicLayouts.Horizontal { true, false })
for index := min; index <= max; index ++ {
container.Adopt(basicElements.NewIcon(index, theme.IconSizeSmall), true)
}
return
}