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