Fixed the list widget

This commit is contained in:
Sasha Koshka 2023-02-11 00:18:21 -05:00
parent dce0321e9b
commit 981c11bd44
3 changed files with 41 additions and 40 deletions

View File

@ -110,9 +110,22 @@ func (element *List) redo () {
// 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) {
if
element.forcedMinimumWidth == width &&
element.forcedMinimumHeight == height {
return
}
element.forcedMinimumWidth = width
element.forcedMinimumHeight = height
element.updateMinimumSize()
for index, entry := range element.entries {
element.entries[index] = element.resizeEntryToFit(entry)
}
element.redo()
}
func (element *List) HandleMouseDown (x, y int, button input.Button) {
@ -236,7 +249,7 @@ func (element *List) CountEntries () (count int) {
// Append adds an entry to the end of the list.
func (element *List) Append (entry ListEntry) {
// append
entry.Collapse(element.forcedMinimumWidth)
entry = element.resizeEntryToFit(entry)
entry.SetTheme(element.theme.Theme)
entry.SetConfig(element.config)
element.entries = append(element.entries, entry)
@ -271,7 +284,7 @@ func (element *List) Insert (index int, entry ListEntry) {
element.entries = append (
element.entries[:index + 1],
element.entries[index:]...)
entry.Collapse(element.forcedMinimumWidth)
entry = element.resizeEntryToFit(entry)
element.entries[index] = entry
// recalculate, redraw, notify
@ -316,7 +329,7 @@ func (element *List) Replace (index int, entry ListEntry) {
}
// replace
entry.Collapse(element.forcedMinimumWidth)
entry = element.resizeEntryToFit(entry)
element.entries[index] = entry
// redraw
@ -381,8 +394,9 @@ func (element *List) changeSelectionBy (delta int) (updated bool) {
}
func (element *List) resizeEntryToFit (entry ListEntry) (resized ListEntry) {
bounds := element.Bounds()
inset := element.theme.Inset(theme.PatternSunken)
entry.Collapse(element.forcedMinimumWidth - inset[3] - inset[1])
entry.Resize(inset.Apply(bounds).Dx())
return entry
}
@ -397,7 +411,7 @@ func (element *List) updateMinimumSize () {
if minimumWidth == 0 {
for _, entry := range element.entries {
entryWidth := entry.Bounds().Dx()
entryWidth := entry.MinimumWidth()
if entryWidth > minimumWidth {
minimumWidth = entryWidth
}

View File

@ -10,9 +10,9 @@ import "git.tebibyte.media/sashakoshka/tomo/artist"
type ListEntry struct {
drawer artist.TextDrawer
bounds image.Rectangle
textPoint image.Point
text string
forcedMinimumWidth int
width int
minimumWidth int
config config.Wrapped
theme theme.Wrapped
@ -31,12 +31,6 @@ func NewListEntry (text string, onSelect func ()) (entry ListEntry) {
return
}
func (entry *ListEntry) Collapse (width int) {
if entry.forcedMinimumWidth == width { return }
entry.forcedMinimumWidth = width
entry.updateBounds()
}
func (entry *ListEntry) SetTheme (new theme.Theme) {
if new == entry.theme.Theme { return }
entry.theme.Theme = new
@ -52,20 +46,11 @@ func (entry *ListEntry) SetConfig (new config.Config) {
}
func (entry *ListEntry) updateBounds () {
entry.bounds = image.Rectangle { }
entry.bounds.Max.Y = entry.drawer.LineHeight().Round()
if entry.forcedMinimumWidth > 0 {
entry.bounds.Max.X = entry.forcedMinimumWidth
} else {
entry.bounds.Max.X = entry.drawer.LayoutBounds().Dx()
}
inset := entry.theme.Inset(theme.PatternRaised)
entry.bounds.Max.Y += inset[0] + inset[2]
entry.textPoint =
image.Pt(inset[3], inset[0]).
Sub(entry.drawer.LayoutBounds().Min)
entry.bounds = inset.Inverse().Apply(entry.drawer.LayoutBounds())
entry.bounds = entry.bounds.Sub(entry.bounds.Min)
entry.minimumWidth = entry.bounds.Dx()
entry.bounds.Max.X = entry.width
}
func (entry *ListEntry) Draw (
@ -80,16 +65,20 @@ func (entry *ListEntry) Draw (
Focused: focused,
On: on,
}
pattern := entry.theme.Pattern (theme.PatternRaised, state)
inset := entry.theme.Inset(theme.PatternRaised)
artist.FillRectangle (
destination,
pattern,
entry.Bounds().Add(offset))
entry.Bounds().Add(offset))
foreground := entry.theme.Pattern (theme.PatternForeground, state)
return entry.drawer.Draw (
destination,
foreground,
offset.Add(entry.textPoint))
offset.Add(image.Pt(inset[3], inset[0])).
Sub(entry.drawer.LayoutBounds().Min))
}
func (entry *ListEntry) RunSelect () {
@ -101,3 +90,12 @@ func (entry *ListEntry) RunSelect () {
func (entry *ListEntry) Bounds () (bounds image.Rectangle) {
return entry.bounds
}
func (entry *ListEntry) Resize (width int) {
entry.width = width
entry.updateBounds()
}
func (entry *ListEntry) MinimumWidth () (width int) {
return entry.minimumWidth
}

View File

@ -44,19 +44,8 @@ func run () {
basicElements.NewListEntry("Saw", func(){ waveform = 2 }),
basicElements.NewListEntry("Supersaw", func(){ waveform = 4 }),
)
waveformList.OnNoEntrySelected (func(){waveformList.Select(0)})
waveformList.Select(0)
// waveformButton := basicElements.NewButton("Sine")
// waveformButton.OnClick (func () {
// waveform = (waveform + 1) % 5
// switch waveform {
// case 0: waveformButton.SetText("Sine")
// case 1: waveformButton.SetText("Square")
// case 2: waveformButton.SetText("Saw")
// case 3: waveformButton.SetText("Triangle")
// case 4: waveformButton.SetText("Supersaw")
// }
// })
attackSlider := basicElements.NewLerpSlider(0, 3 * time.Second, adsr.Attack, true)
decaySlider := basicElements.NewLerpSlider(0, 3 * time.Second, adsr.Decay, true)