2023-02-01 23:48:16 -07:00
|
|
|
package basicElements
|
2023-01-23 00:05:09 -07:00
|
|
|
|
|
|
|
import "image"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/theme"
|
2023-02-07 09:27:59 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/config"
|
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-01-29 23:30:13 -07:00
|
|
|
var listEntryCase = theme.C("basic", "listEntry")
|
|
|
|
|
2023-01-23 00:05:09 -07:00
|
|
|
// 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-02-07 09:27:59 -07:00
|
|
|
theme theme.Theme
|
|
|
|
config config.Config
|
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.updateBounds()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *ListEntry) Collapse (width int) {
|
|
|
|
if entry.forcedMinimumWidth == width { return }
|
|
|
|
entry.forcedMinimumWidth = width
|
|
|
|
entry.updateBounds()
|
|
|
|
}
|
|
|
|
|
2023-02-07 09:27:59 -07:00
|
|
|
func (entry *ListEntry) SetTheme (new theme.Theme) {
|
|
|
|
entry.theme = new
|
|
|
|
entry.drawer.SetFace (entry.theme.FontFace (
|
|
|
|
theme.FontStyleRegular,
|
|
|
|
theme.FontSizeNormal,
|
|
|
|
listEntryCase))
|
|
|
|
entry.updateBounds()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (entry *ListEntry) SetConfig (config config.Config) {
|
|
|
|
entry.config = config
|
|
|
|
}
|
|
|
|
|
2023-01-23 00:05:09 -07:00
|
|
|
func (entry *ListEntry) updateBounds () {
|
|
|
|
entry.bounds = image.Rectangle { }
|
2023-01-29 10:51:43 -07:00
|
|
|
entry.bounds.Max.Y = entry.drawer.LineHeight().Round()
|
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 {
|
2023-01-29 10:51:43 -07:00
|
|
|
entry.bounds.Max.X = entry.drawer.LayoutBounds().Dx()
|
2023-01-23 00:05:09 -07:00
|
|
|
}
|
|
|
|
|
2023-02-07 09:27:59 -07:00
|
|
|
inset := entry.theme.Inset(theme.PatternRaised, listEntryCase)
|
2023-01-28 23:49:01 -07:00
|
|
|
entry.bounds.Max.Y += inset[0] + inset[2]
|
|
|
|
|
2023-01-23 21:54:12 -07:00
|
|
|
entry.textPoint =
|
2023-01-29 10:51:43 -07:00
|
|
|
image.Pt(inset[3], inset[0]).
|
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 (
|
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-02-07 09:27:59 -07:00
|
|
|
state := theme.PatternState {
|
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
|
|
|
}
|
|
|
|
pattern := entry.theme.Pattern (theme.PatternRaised, listEntryCase, state)
|
2023-01-30 00:22:16 -07:00
|
|
|
artist.FillRectangle (
|
|
|
|
destination,
|
|
|
|
pattern,
|
|
|
|
entry.Bounds().Add(offset))
|
2023-02-07 09:27:59 -07:00
|
|
|
foreground := entry.theme.Pattern (theme.PatternForeground, listEntryCase, state)
|
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
|
|
|
|
}
|