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
|
2023-01-24 14:41:12 -07:00
|
|
|
onSelect func ()
|
2023-01-23 00:05:09 -07:00
|
|
|
}
|
|
|
|
|
2023-01-24 14:41:12 -07:00
|
|
|
func NewListEntry (text string, onSelect func ()) (entry ListEntry) {
|
2023-01-23 00:05:09 -07:00
|
|
|
entry = ListEntry {
|
2023-01-24 14:41:12 -07:00
|
|
|
text: text,
|
|
|
|
onSelect: onSelect,
|
2023-01-23 00:05:09 -07:00
|
|
|
}
|
|
|
|
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 () {
|
2023-01-23 21:54:12 -07:00
|
|
|
padding := theme.Padding()
|
|
|
|
|
2023-01-23 00:05:09 -07:00
|
|
|
entry.bounds = image.Rectangle { }
|
2023-01-23 21:54:12 -07:00
|
|
|
entry.bounds.Max.Y = entry.drawer.LineHeight().Round() + padding
|
2023-01-23 00:05:09 -07:00
|
|
|
if entry.forcedMinimumWidth > 0 {
|
2023-01-23 21:54:12 -07:00
|
|
|
entry.bounds.Max.X = entry.forcedMinimumWidth
|
|
|
|
} else {
|
|
|
|
entry.bounds.Max.X =
|
|
|
|
entry.drawer.LayoutBounds().Dx() + padding * 2
|
2023-01-23 00:05:09 -07:00
|
|
|
}
|
|
|
|
|
2023-01-28 23:49:01 -07:00
|
|
|
_, inset := theme.ItemPattern(theme.PatternState { })
|
|
|
|
entry.bounds.Max.Y += inset[0] + inset[2]
|
|
|
|
|
2023-01-23 21:54:12 -07:00
|
|
|
entry.textPoint =
|
2023-01-28 23:49:01 -07:00
|
|
|
image.Pt(inset[3] + padding, inset[0] + padding / 2).
|
2023-01-23 21:54:12 -07:00
|
|
|
Sub(entry.drawer.LayoutBounds().Min)
|
2023-01-23 00:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *ListEntry) Draw (
|
|
|
|
destination tomo.Canvas,
|
|
|
|
offset image.Point,
|
|
|
|
selected bool,
|
|
|
|
) (
|
|
|
|
updatedRegion image.Rectangle,
|
|
|
|
) {
|
2023-01-28 23:49:01 -07:00
|
|
|
foreground, _ := theme.ForegroundPattern (theme.PatternState { })
|
2023-01-23 00:05:09 -07:00
|
|
|
return entry.drawer.Draw (
|
|
|
|
destination,
|
2023-01-28 23:49:01 -07:00
|
|
|
foreground,
|
2023-01-23 00:05:09 -07:00
|
|
|
offset.Add(entry.textPoint))
|
|
|
|
}
|
|
|
|
|
2023-01-24 14:41:12 -07:00
|
|
|
func (entry *ListEntry) RunSelect () {
|
|
|
|
if entry.onSelect != nil {
|
|
|
|
entry.onSelect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-23 00:05:09 -07:00
|
|
|
func (entry *ListEntry) Bounds () (bounds image.Rectangle) {
|
|
|
|
return entry.bounds
|
|
|
|
}
|