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/basic/listentry.go

61 lines
1.4 KiB
Go
Raw Normal View History

2023-01-23 00:05:09 -07:00
package basic
import "image"
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/artist"
// ListEntry is an item that can be added to a list.
type ListEntry struct {
drawer artist.TextDrawer
bounds image.Rectangle
textPoint image.Point
text string
forcedMinimumWidth int
onClick func ()
}
func NewListEntry (text string, onClick func ()) (entry ListEntry) {
entry = ListEntry {
text: text,
onClick: onClick,
}
entry.drawer.SetText([]rune(text))
entry.drawer.SetFace(theme.FontFaceRegular())
entry.updateBounds()
return
}
func (entry *ListEntry) Collapse (width int) {
if entry.forcedMinimumWidth == width { return }
entry.forcedMinimumWidth = width
entry.updateBounds()
}
func (entry *ListEntry) updateBounds () {
entry.bounds = image.Rectangle { }
entry.bounds.Max.Y = entry.drawer.LineHeight().Round()
if entry.forcedMinimumWidth > 0 {
entry.bounds.Max.X = entry.drawer.LayoutBounds().Dx()
}
entry.textPoint = image.Pt(0, 0).Sub(entry.drawer.LayoutBounds().Min)
}
func (entry *ListEntry) Draw (
destination tomo.Canvas,
offset image.Point,
selected bool,
) (
updatedRegion image.Rectangle,
) {
return entry.drawer.Draw (
destination,
theme.ForegroundPattern(true),
offset.Add(entry.textPoint))
}
func (entry *ListEntry) Bounds () (bounds image.Rectangle) {
return entry.bounds
}