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

101 lines
2.4 KiB
Go
Raw Normal View History

2023-02-02 06:48:16 +00:00
package basicElements
2023-01-23 07:05:09 +00:00
import "image"
import "git.tebibyte.media/sashakoshka/tomo/theme"
2023-02-07 16:27:59 +00:00
import "git.tebibyte.media/sashakoshka/tomo/config"
2023-02-02 06:48:16 +00:00
import "git.tebibyte.media/sashakoshka/tomo/canvas"
2023-01-23 07:05:09 +00:00
import "git.tebibyte.media/sashakoshka/tomo/artist"
2023-02-15 23:45:58 +00:00
import "git.tebibyte.media/sashakoshka/tomo/textdraw"
2023-01-23 07:05:09 +00:00
// ListEntry is an item that can be added to a list.
type ListEntry struct {
2023-02-15 23:45:58 +00:00
drawer textdraw.Drawer
2023-01-23 07:05:09 +00:00
bounds image.Rectangle
text string
2023-02-11 05:18:21 +00:00
width int
minimumWidth int
2023-02-08 05:22:40 +00:00
2023-02-08 19:36:14 +00:00
config config.Wrapped
theme theme.Wrapped
2023-02-08 05:22:40 +00:00
onSelect func ()
2023-01-23 07:05:09 +00:00
}
2023-01-24 21:41:12 +00:00
func NewListEntry (text string, onSelect func ()) (entry ListEntry) {
2023-01-23 07:05:09 +00:00
entry = ListEntry {
2023-01-24 21:41:12 +00:00
text: text,
onSelect: onSelect,
2023-01-23 07:05:09 +00:00
}
2023-02-08 19:36:14 +00:00
entry.theme.Case = theme.C("basic", "listEntry")
2023-01-23 07:05:09 +00:00
entry.drawer.SetText([]rune(text))
entry.updateBounds()
return
}
2023-02-07 16:27:59 +00:00
func (entry *ListEntry) SetTheme (new theme.Theme) {
2023-02-08 19:36:14 +00:00
if new == entry.theme.Theme { return }
entry.theme.Theme = new
2023-02-07 16:27:59 +00:00
entry.drawer.SetFace (entry.theme.FontFace (
theme.FontStyleRegular,
2023-02-08 19:36:14 +00:00
theme.FontSizeNormal))
2023-02-07 16:27:59 +00:00
entry.updateBounds()
}
2023-02-08 19:36:14 +00:00
func (entry *ListEntry) SetConfig (new config.Config) {
if new == entry.config.Config { return }
entry.config.Config = new
2023-02-07 16:27:59 +00:00
}
2023-01-23 07:05:09 +00:00
func (entry *ListEntry) updateBounds () {
2023-02-27 03:20:17 +00:00
padding := entry.theme.Padding(theme.PatternRaised)
entry.bounds = padding.Inverse().Apply(entry.drawer.LayoutBounds())
2023-02-11 05:18:21 +00:00
entry.bounds = entry.bounds.Sub(entry.bounds.Min)
entry.minimumWidth = entry.bounds.Dx()
entry.bounds.Max.X = entry.width
2023-01-23 07:05:09 +00:00
}
func (entry *ListEntry) Draw (
2023-02-02 06:48:16 +00:00
destination canvas.Canvas,
2023-01-23 07:05:09 +00:00
offset image.Point,
2023-01-30 22:01:47 +00:00
focused bool,
on bool,
2023-01-23 07:05:09 +00:00
) (
updatedRegion image.Rectangle,
) {
2023-02-27 03:20:17 +00:00
state := theme.State {
2023-01-30 22:01:47 +00:00
Focused: focused,
On: on,
2023-02-07 16:27:59 +00:00
}
2023-02-11 05:18:21 +00:00
2023-02-08 19:36:14 +00:00
pattern := entry.theme.Pattern (theme.PatternRaised, state)
2023-02-27 03:20:17 +00:00
padding := entry.theme.Padding(theme.PatternRaised)
bounds := entry.Bounds().Add(offset)
artist.DrawBounds(destination, pattern, bounds)
2023-02-11 05:18:21 +00:00
2023-02-27 03:20:17 +00:00
foreground := entry.theme.Color (theme.ColorForeground, state)
2023-01-23 07:05:09 +00:00
return entry.drawer.Draw (
destination,
foreground,
2023-02-27 03:20:17 +00:00
offset.Add(image.Pt(padding[3], padding[0])).
2023-02-11 05:18:21 +00:00
Sub(entry.drawer.LayoutBounds().Min))
2023-01-23 07:05:09 +00:00
}
2023-01-24 21:41:12 +00:00
func (entry *ListEntry) RunSelect () {
if entry.onSelect != nil {
entry.onSelect()
}
}
2023-01-23 07:05:09 +00:00
func (entry *ListEntry) Bounds () (bounds image.Rectangle) {
return entry.bounds
}
2023-02-11 05:18:21 +00:00
func (entry *ListEntry) Resize (width int) {
entry.width = width
entry.updateBounds()
}
func (entry *ListEntry) MinimumWidth () (width int) {
return entry.minimumWidth
}