List element's events make more sense
Removed redundant selected entry change event, and added an event that fires when the user deselects the current element.
This commit is contained in:
parent
48679c8ad2
commit
9aea6d8c0f
@ -25,9 +25,9 @@ type List struct {
|
|||||||
entries []ListEntry
|
entries []ListEntry
|
||||||
|
|
||||||
onSelectionRequest func () (granted bool)
|
onSelectionRequest func () (granted bool)
|
||||||
onSelectedEntryChange func (index int)
|
|
||||||
onSelectionMotionRequest func (tomo.SelectionDirection) (granted bool)
|
onSelectionMotionRequest func (tomo.SelectionDirection) (granted bool)
|
||||||
onScrollBoundsChange func ()
|
onScrollBoundsChange func ()
|
||||||
|
onNoEntrySelected func ()
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewList creates a new list element with the specified entries.
|
// NewList creates a new list element with the specified entries.
|
||||||
@ -98,18 +98,20 @@ func (element *List) HandleMouseScroll (x, y int, deltaX, deltaY float64) { }
|
|||||||
|
|
||||||
func (element *List) HandleKeyDown (key tomo.Key, modifiers tomo.Modifiers) {
|
func (element *List) HandleKeyDown (key tomo.Key, modifiers tomo.Modifiers) {
|
||||||
if !element.enabled { return }
|
if !element.enabled { return }
|
||||||
|
|
||||||
newIndex := element.selectedEntry
|
altered := false
|
||||||
switch key {
|
switch key {
|
||||||
case tomo.KeyLeft, tomo.KeyUp: newIndex --
|
case tomo.KeyLeft, tomo.KeyUp:
|
||||||
case tomo.KeyRight, tomo.KeyDown: newIndex ++
|
altered = element.changeSelectionBy(-1)
|
||||||
default: return
|
|
||||||
|
case tomo.KeyRight, tomo.KeyDown:
|
||||||
|
altered = element.changeSelectionBy(1)
|
||||||
|
|
||||||
|
case tomo.KeyEscape:
|
||||||
|
altered = element.selectEntry(-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if newIndex < 0 { newIndex = len(element.entries) - 1 }
|
if altered && element.core.HasImage () {
|
||||||
if newIndex >= len(element.entries) { newIndex = 0 }
|
|
||||||
|
|
||||||
if element.selectEntry(newIndex) && element.core.HasImage () {
|
|
||||||
element.draw()
|
element.draw()
|
||||||
element.core.DamageAll()
|
element.core.DamageAll()
|
||||||
}
|
}
|
||||||
@ -165,12 +167,6 @@ func (element *List) OnSelectionMotionRequest (
|
|||||||
element.onSelectionMotionRequest = callback
|
element.onSelectionMotionRequest = callback
|
||||||
}
|
}
|
||||||
|
|
||||||
// OnSelectedEntryChange sets the function to be called when the user selects an
|
|
||||||
// entry in this list.
|
|
||||||
func (element *List) OnSelectedEntryChange (callback func (index int)) {
|
|
||||||
element.onSelectedEntryChange = callback
|
|
||||||
}
|
|
||||||
|
|
||||||
// ScrollContentBounds returns the full content size of the element.
|
// ScrollContentBounds returns the full content size of the element.
|
||||||
func (element *List) ScrollContentBounds () (bounds image.Rectangle) {
|
func (element *List) ScrollContentBounds () (bounds image.Rectangle) {
|
||||||
return image.Rect (
|
return image.Rect (
|
||||||
@ -236,6 +232,13 @@ func (element *List) SetEnabled (enabled bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
// CountEntries returns the amount of entries in the list.
|
// CountEntries returns the amount of entries in the list.
|
||||||
func (element *List) CountEntries () (count int) {
|
func (element *List) CountEntries () (count int) {
|
||||||
return len(element.entries)
|
return len(element.entries)
|
||||||
@ -360,15 +363,23 @@ func (element *List) selectUnderMouse (x, y int) (updated bool) {
|
|||||||
func (element *List) selectEntry (index int) (updated bool) {
|
func (element *List) selectEntry (index int) (updated bool) {
|
||||||
if element.selectedEntry == index { return false }
|
if element.selectedEntry == index { return false }
|
||||||
element.selectedEntry = index
|
element.selectedEntry = index
|
||||||
if element.onSelectedEntryChange != nil {
|
if element.selectedEntry < 0 {
|
||||||
element.onSelectedEntryChange(element.selectedEntry)
|
if element.onNoEntrySelected != nil {
|
||||||
}
|
element.onNoEntrySelected()
|
||||||
if element.selectedEntry >= 0 {
|
}
|
||||||
|
} else {
|
||||||
element.entries[element.selectedEntry].RunSelect()
|
element.entries[element.selectedEntry].RunSelect()
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
func (element *List) resizeEntryToFit (entry ListEntry) (resized ListEntry) {
|
func (element *List) resizeEntryToFit (entry ListEntry) (resized ListEntry) {
|
||||||
entry.Collapse(element.forcedMinimumWidth)
|
entry.Collapse(element.forcedMinimumWidth)
|
||||||
return entry
|
return entry
|
||||||
|
@ -29,6 +29,9 @@ func run () {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
intro := basic.NewLabel (
|
||||||
|
"The List element can be easily used as a sidebar. " +
|
||||||
|
"Click on entries to flip pages!", true)
|
||||||
button := basic.NewButton("I do nothing!")
|
button := basic.NewButton("I do nothing!")
|
||||||
button.OnClick (func () {
|
button.OnClick (func () {
|
||||||
popups.NewDialog(popups.DialogKindInfo, "", "Sike!")
|
popups.NewDialog(popups.DialogKindInfo, "", "Sike!")
|
||||||
@ -47,12 +50,11 @@ func run () {
|
|||||||
basic.NewListEntry("mouse", func () { turnPage(mouse) }),
|
basic.NewListEntry("mouse", func () { turnPage(mouse) }),
|
||||||
basic.NewListEntry("input", func () { turnPage(input) }),
|
basic.NewListEntry("input", func () { turnPage(input) }),
|
||||||
basic.NewListEntry("form", func () { turnPage(form) }))
|
basic.NewListEntry("form", func () { turnPage(form) }))
|
||||||
|
list.OnNoEntrySelected(func () { turnPage (intro) })
|
||||||
list.Collapse(96, 0)
|
list.Collapse(96, 0)
|
||||||
|
|
||||||
container.Adopt(list, false)
|
container.Adopt(list, false)
|
||||||
turnPage (basic.NewLabel (
|
turnPage(intro)
|
||||||
"The List element can be easily used as a sidebar. " +
|
|
||||||
"Click on entries to flip pages!", true))
|
|
||||||
|
|
||||||
window.OnClose(tomo.Stop)
|
window.OnClose(tomo.Stop)
|
||||||
window.Show()
|
window.Show()
|
||||||
|
Reference in New Issue
Block a user