2023-01-23 00:05:09 -07:00
|
|
|
package basic
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
import "image"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/theme"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
|
|
|
|
2023-01-29 23:30:13 -07:00
|
|
|
var listCase = theme.C("basic", "list")
|
|
|
|
|
2023-01-23 00:05:09 -07:00
|
|
|
// List is an element that contains several objects that a user can select.
|
|
|
|
type List struct {
|
|
|
|
*core.Core
|
2023-01-30 15:01:47 -07:00
|
|
|
*core.FocusableCore
|
2023-01-23 00:05:09 -07:00
|
|
|
core core.CoreControl
|
2023-01-30 15:01:47 -07:00
|
|
|
focusableControl core.FocusableCoreControl
|
2023-01-27 15:55:49 -07:00
|
|
|
|
2023-01-23 21:54:12 -07:00
|
|
|
pressed bool
|
|
|
|
|
2023-01-23 00:05:09 -07:00
|
|
|
contentHeight int
|
|
|
|
forcedMinimumWidth int
|
|
|
|
forcedMinimumHeight int
|
2023-01-23 21:54:12 -07:00
|
|
|
|
2023-01-23 00:05:09 -07:00
|
|
|
selectedEntry int
|
|
|
|
scroll int
|
|
|
|
entries []ListEntry
|
|
|
|
|
|
|
|
onScrollBoundsChange func ()
|
2023-01-26 10:05:28 -07:00
|
|
|
onNoEntrySelected func ()
|
2023-01-23 00:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewList creates a new list element with the specified entries.
|
|
|
|
func NewList (entries ...ListEntry) (element *List) {
|
2023-01-27 15:55:49 -07:00
|
|
|
element = &List { selectedEntry: -1 }
|
2023-01-23 00:05:09 -07:00
|
|
|
element.Core, element.core = core.NewCore(element)
|
2023-01-30 15:01:47 -07:00
|
|
|
element.FocusableCore,
|
|
|
|
element.focusableControl = core.NewFocusableCore (func () {
|
2023-01-27 15:55:49 -07:00
|
|
|
if element.core.HasImage () {
|
|
|
|
element.draw()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
})
|
2023-01-23 00:05:09 -07:00
|
|
|
|
|
|
|
element.entries = make([]ListEntry, len(entries))
|
|
|
|
for index, entry := range entries {
|
|
|
|
element.entries[index] = entry
|
|
|
|
}
|
|
|
|
|
|
|
|
element.updateMinimumSize()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resize changes the element's size.
|
|
|
|
func (element *List) Resize (width, height int) {
|
|
|
|
element.core.AllocateCanvas(width, height)
|
|
|
|
|
|
|
|
for index, entry := range element.entries {
|
|
|
|
element.entries[index] = element.resizeEntryToFit(entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
element.draw()
|
|
|
|
if element.onScrollBoundsChange != nil {
|
|
|
|
element.onScrollBoundsChange()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collapse forces a minimum width and height upon the list. If a zero value is
|
|
|
|
// given for a dimension, its minimum will be determined by the list's content.
|
|
|
|
// If the list's height goes beyond the forced size, it will need to be accessed
|
|
|
|
// via scrolling. If an entry's width goes beyond the forced size, its text will
|
|
|
|
// be truncated so that it fits.
|
|
|
|
func (element *List) Collapse (width, height int) {
|
|
|
|
element.forcedMinimumWidth = width
|
|
|
|
element.forcedMinimumHeight = height
|
|
|
|
element.updateMinimumSize()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *List) HandleMouseDown (x, y int, button tomo.Button) {
|
2023-01-27 15:55:49 -07:00
|
|
|
if !element.Enabled() { return }
|
2023-01-30 15:01:47 -07:00
|
|
|
if !element.Focused() { element.Focus() }
|
2023-01-23 00:05:09 -07:00
|
|
|
if button != tomo.ButtonLeft { return }
|
2023-01-23 21:54:12 -07:00
|
|
|
element.pressed = true
|
|
|
|
if element.selectUnderMouse(x, y) && element.core.HasImage() {
|
|
|
|
element.draw()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
2023-01-23 00:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (element *List) HandleMouseUp (x, y int, button tomo.Button) {
|
|
|
|
if button != tomo.ButtonLeft { return }
|
2023-01-23 21:54:12 -07:00
|
|
|
element.pressed = false
|
2023-01-23 00:05:09 -07:00
|
|
|
}
|
|
|
|
|
2023-01-23 21:54:12 -07:00
|
|
|
func (element *List) HandleMouseMove (x, y int) {
|
|
|
|
if element.pressed {
|
|
|
|
if element.selectUnderMouse(x, y) && element.core.HasImage() {
|
|
|
|
element.draw()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-23 00:05:09 -07:00
|
|
|
func (element *List) HandleMouseScroll (x, y int, deltaX, deltaY float64) { }
|
|
|
|
|
|
|
|
func (element *List) HandleKeyDown (key tomo.Key, modifiers tomo.Modifiers) {
|
2023-01-27 15:55:49 -07:00
|
|
|
if !element.Enabled() { return }
|
2023-01-26 10:05:28 -07:00
|
|
|
|
|
|
|
altered := false
|
2023-01-26 09:53:49 -07:00
|
|
|
switch key {
|
2023-01-26 10:05:28 -07:00
|
|
|
case tomo.KeyLeft, tomo.KeyUp:
|
|
|
|
altered = element.changeSelectionBy(-1)
|
|
|
|
|
|
|
|
case tomo.KeyRight, tomo.KeyDown:
|
|
|
|
altered = element.changeSelectionBy(1)
|
|
|
|
|
|
|
|
case tomo.KeyEscape:
|
|
|
|
altered = element.selectEntry(-1)
|
2023-01-26 09:53:49 -07:00
|
|
|
}
|
|
|
|
|
2023-01-26 10:05:28 -07:00
|
|
|
if altered && element.core.HasImage () {
|
2023-01-26 09:53:49 -07:00
|
|
|
element.draw()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
2023-01-23 00:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (element *List) HandleKeyUp(key tomo.Key, modifiers tomo.Modifiers) { }
|
|
|
|
|
|
|
|
// ScrollContentBounds returns the full content size of the element.
|
|
|
|
func (element *List) ScrollContentBounds () (bounds image.Rectangle) {
|
|
|
|
return image.Rect (
|
|
|
|
0, 0,
|
|
|
|
1, element.contentHeight)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScrollViewportBounds returns the size and position of the element's viewport
|
|
|
|
// relative to ScrollBounds.
|
|
|
|
func (element *List) ScrollViewportBounds () (bounds image.Rectangle) {
|
|
|
|
return image.Rect (
|
|
|
|
0, element.scroll,
|
|
|
|
0, element.scroll + element.scrollViewportHeight())
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScrollTo scrolls the viewport to the specified point relative to
|
|
|
|
// ScrollBounds.
|
|
|
|
func (element *List) ScrollTo (position image.Point) {
|
|
|
|
element.scroll = position.Y
|
|
|
|
if element.scroll < 0 {
|
|
|
|
element.scroll = 0
|
|
|
|
} else if element.scroll > element.maxScrollHeight() {
|
|
|
|
element.scroll = element.maxScrollHeight()
|
|
|
|
}
|
|
|
|
|
|
|
|
if element.core.HasImage () {
|
|
|
|
element.draw()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
if element.onScrollBoundsChange != nil {
|
|
|
|
element.onScrollBoundsChange()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScrollAxes returns the supported axes for scrolling.
|
|
|
|
func (element *List) ScrollAxes () (horizontal, vertical bool) {
|
|
|
|
return false, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *List) scrollViewportHeight () (height int) {
|
2023-01-29 23:30:13 -07:00
|
|
|
_, inset := theme.ListPattern(theme.PatternState {
|
|
|
|
Case: listCase,
|
|
|
|
})
|
2023-01-29 10:51:43 -07:00
|
|
|
return element.Bounds().Dy() - inset[0] - inset[2]
|
2023-01-23 00:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (element *List) maxScrollHeight () (height int) {
|
|
|
|
height =
|
|
|
|
element.contentHeight -
|
|
|
|
element.scrollViewportHeight()
|
|
|
|
if height < 0 { height = 0 }
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *List) OnScrollBoundsChange (callback func ()) {
|
|
|
|
element.onScrollBoundsChange = callback
|
|
|
|
}
|
|
|
|
|
2023-01-26 10:05:28 -07:00
|
|
|
// OnNoEntrySelected sets a function to be called when the user chooses to
|
|
|
|
// deselect the current selected entry by clicking on empty space within the
|
|
|
|
// list or by pressing the escape key.
|
|
|
|
func (element *List) OnNoEntrySelected (callback func ()) {
|
|
|
|
element.onNoEntrySelected = callback
|
|
|
|
}
|
|
|
|
|
2023-01-23 00:05:09 -07:00
|
|
|
// CountEntries returns the amount of entries in the list.
|
|
|
|
func (element *List) CountEntries () (count int) {
|
|
|
|
return len(element.entries)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append adds an entry to the end of the list.
|
|
|
|
func (element *List) Append (entry ListEntry) {
|
|
|
|
// append
|
|
|
|
entry.Collapse(element.forcedMinimumWidth)
|
|
|
|
element.entries = append(element.entries, entry)
|
|
|
|
|
|
|
|
// recalculate, redraw, notify
|
|
|
|
element.updateMinimumSize()
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.draw()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
if element.onScrollBoundsChange != nil {
|
|
|
|
element.onScrollBoundsChange()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// EntryAt returns the entry at the specified index. If the index is out of
|
|
|
|
// bounds, it panics.
|
|
|
|
func (element *List) EntryAt (index int) (entry ListEntry) {
|
|
|
|
if index < 0 || index >= len(element.entries) {
|
|
|
|
panic(fmt.Sprint("basic.List.EntryAt index out of range: ", index))
|
|
|
|
}
|
|
|
|
return element.entries[index]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert inserts an entry into the list at the speified index. If the index is
|
|
|
|
// out of bounds, it is constrained either to zero or len(entries).
|
|
|
|
func (element *List) Insert (index int, entry ListEntry) {
|
|
|
|
if index < 0 { index = 0 }
|
|
|
|
if index > len(element.entries) { index = len(element.entries) }
|
|
|
|
|
|
|
|
// insert
|
|
|
|
element.entries = append (
|
|
|
|
element.entries[:index + 1],
|
|
|
|
element.entries[index:]...)
|
|
|
|
entry.Collapse(element.forcedMinimumWidth)
|
|
|
|
element.entries[index] = entry
|
|
|
|
|
|
|
|
// recalculate, redraw, notify
|
|
|
|
element.updateMinimumSize()
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.draw()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
if element.onScrollBoundsChange != nil {
|
|
|
|
element.onScrollBoundsChange()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes the entry at the specified index. If the index is out of
|
|
|
|
// bounds, it panics.
|
|
|
|
func (element *List) Remove (index int) {
|
|
|
|
if index < 0 || index >= len(element.entries) {
|
|
|
|
panic(fmt.Sprint("basic.List.Remove index out of range: ", index))
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete
|
|
|
|
element.entries = append (
|
|
|
|
element.entries[:index],
|
|
|
|
element.entries[index + 1:]...)
|
|
|
|
|
|
|
|
// recalculate, redraw, notify
|
|
|
|
element.updateMinimumSize()
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.draw()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
if element.onScrollBoundsChange != nil {
|
|
|
|
element.onScrollBoundsChange()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace replaces the entry at the specified index with another. If the index
|
|
|
|
// is out of bounds, it panics.
|
|
|
|
func (element *List) Replace (index int, entry ListEntry) {
|
|
|
|
if index < 0 || index >= len(element.entries) {
|
|
|
|
panic(fmt.Sprint("basic.List.Replace index out of range: ", index))
|
|
|
|
}
|
|
|
|
|
|
|
|
// replace
|
|
|
|
entry.Collapse(element.forcedMinimumWidth)
|
|
|
|
element.entries[index] = entry
|
|
|
|
|
|
|
|
// redraw
|
|
|
|
element.updateMinimumSize()
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.draw()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
if element.onScrollBoundsChange != nil {
|
|
|
|
element.onScrollBoundsChange()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-23 21:54:12 -07:00
|
|
|
func (element *List) selectUnderMouse (x, y int) (updated bool) {
|
2023-01-29 10:51:43 -07:00
|
|
|
_, inset := theme.ListPattern(theme.PatternState { })
|
|
|
|
bounds := inset.Apply(element.Bounds())
|
2023-01-23 21:54:12 -07:00
|
|
|
mousePoint := image.Pt(x, y)
|
|
|
|
dot := image.Pt (
|
2023-01-29 10:51:43 -07:00
|
|
|
bounds.Min.X,
|
|
|
|
bounds.Min.Y - element.scroll)
|
2023-01-23 21:54:12 -07:00
|
|
|
|
|
|
|
newlySelectedEntryIndex := -1
|
|
|
|
for index, entry := range element.entries {
|
|
|
|
entryPosition := dot
|
|
|
|
dot.Y += entry.Bounds().Dy()
|
|
|
|
if entryPosition.Y > bounds.Max.Y { break }
|
|
|
|
if mousePoint.In(entry.Bounds().Add(entryPosition)) {
|
|
|
|
newlySelectedEntryIndex = index
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 09:53:49 -07:00
|
|
|
return element.selectEntry(newlySelectedEntryIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *List) selectEntry (index int) (updated bool) {
|
|
|
|
if element.selectedEntry == index { return false }
|
|
|
|
element.selectedEntry = index
|
2023-01-26 10:05:28 -07:00
|
|
|
if element.selectedEntry < 0 {
|
|
|
|
if element.onNoEntrySelected != nil {
|
|
|
|
element.onNoEntrySelected()
|
|
|
|
}
|
|
|
|
} else {
|
2023-01-24 14:41:12 -07:00
|
|
|
element.entries[element.selectedEntry].RunSelect()
|
|
|
|
}
|
2023-01-23 21:54:12 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-01-26 10:05:28 -07:00
|
|
|
func (element *List) changeSelectionBy (delta int) (updated bool) {
|
|
|
|
newIndex := element.selectedEntry + delta
|
|
|
|
if newIndex < 0 { newIndex = len(element.entries) - 1 }
|
|
|
|
if newIndex >= len(element.entries) { newIndex = 0 }
|
|
|
|
return element.selectEntry(newIndex)
|
|
|
|
}
|
|
|
|
|
2023-01-23 00:05:09 -07:00
|
|
|
func (element *List) resizeEntryToFit (entry ListEntry) (resized ListEntry) {
|
2023-01-29 23:30:13 -07:00
|
|
|
_, inset := theme.ListPattern(theme.PatternState {
|
|
|
|
Case: listCase,
|
|
|
|
})
|
2023-01-28 23:49:01 -07:00
|
|
|
entry.Collapse(element.forcedMinimumWidth - inset[3] - inset[1])
|
2023-01-23 00:05:09 -07:00
|
|
|
return entry
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *List) updateMinimumSize () {
|
|
|
|
element.contentHeight = 0
|
|
|
|
for _, entry := range element.entries {
|
|
|
|
element.contentHeight += entry.Bounds().Dy()
|
|
|
|
}
|
|
|
|
|
|
|
|
minimumWidth := element.forcedMinimumWidth
|
|
|
|
minimumHeight := element.forcedMinimumHeight
|
|
|
|
|
|
|
|
if minimumWidth == 0 {
|
2023-01-23 21:54:12 -07:00
|
|
|
for _, entry := range element.entries {
|
|
|
|
entryWidth := entry.Bounds().Dx()
|
|
|
|
if entryWidth > minimumWidth {
|
|
|
|
minimumWidth = entryWidth
|
|
|
|
}
|
|
|
|
}
|
2023-01-23 00:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if minimumHeight == 0 {
|
2023-01-29 10:51:43 -07:00
|
|
|
minimumHeight = element.contentHeight
|
2023-01-23 00:05:09 -07:00
|
|
|
}
|
|
|
|
|
2023-01-29 23:30:13 -07:00
|
|
|
_, inset := theme.ListPattern(theme.PatternState {
|
|
|
|
Case: listCase,
|
|
|
|
})
|
2023-01-28 23:49:01 -07:00
|
|
|
minimumHeight += inset[0] + inset[2]
|
|
|
|
|
2023-01-23 00:05:09 -07:00
|
|
|
element.core.SetMinimumSize(minimumWidth, minimumHeight)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *List) draw () {
|
|
|
|
bounds := element.Bounds()
|
|
|
|
|
2023-01-28 23:49:01 -07:00
|
|
|
pattern, inset := theme.ListPattern(theme.PatternState {
|
2023-01-29 23:30:13 -07:00
|
|
|
Case: listCase,
|
2023-01-28 23:49:01 -07:00
|
|
|
Disabled: !element.Enabled(),
|
2023-01-30 15:01:47 -07:00
|
|
|
Focused: element.Focused(),
|
2023-01-28 23:49:01 -07:00
|
|
|
})
|
|
|
|
artist.FillRectangle(element.core, pattern, bounds)
|
2023-01-23 00:05:09 -07:00
|
|
|
|
2023-01-28 23:49:01 -07:00
|
|
|
bounds = inset.Apply(bounds)
|
2023-01-23 00:05:09 -07:00
|
|
|
dot := image.Point {
|
2023-01-23 21:54:12 -07:00
|
|
|
bounds.Min.X,
|
2023-01-29 10:51:43 -07:00
|
|
|
bounds.Min.Y - element.scroll,
|
2023-01-23 00:05:09 -07:00
|
|
|
}
|
2023-01-30 00:22:16 -07:00
|
|
|
innerCanvas := tomo.Cut(element, bounds)
|
2023-01-23 00:05:09 -07:00
|
|
|
for index, entry := range element.entries {
|
|
|
|
entryPosition := dot
|
|
|
|
dot.Y += entry.Bounds().Dy()
|
|
|
|
if dot.Y < bounds.Min.Y { continue }
|
|
|
|
if entryPosition.Y > bounds.Max.Y { break }
|
|
|
|
entry.Draw (
|
2023-01-30 00:22:16 -07:00
|
|
|
innerCanvas, entryPosition,
|
2023-01-30 15:01:47 -07:00
|
|
|
element.Focused(), element.selectedEntry == index)
|
2023-01-23 00:05:09 -07:00
|
|
|
}
|
|
|
|
}
|