1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Fixes #39: Todo crashes when space pressed with no selection

This commit is contained in:
Chris Cummer 2018-05-11 09:15:38 -07:00
parent 432669ab9b
commit 89b1b0f517

View File

@ -24,6 +24,10 @@ func (list *List) Delete() {
}
func (list *List) Demote() {
if list.isUnselectable() {
return
}
j := list.selected + 1
if j >= len(list.Items) {
j = 0
@ -48,6 +52,10 @@ func (list *List) Prev() {
}
func (list *List) Promote() {
if list.isUnselectable() {
return
}
j := list.selected - 1
if j < 0 {
j = len(list.Items) - 1
@ -58,7 +66,7 @@ func (list *List) Promote() {
}
func (list *List) Selected() *Item {
if list.selected < 0 || list.selected >= len(list.Items) {
if list.isUnselectable() {
return nil
}
@ -67,6 +75,10 @@ func (list *List) Selected() *Item {
// Toggle switches the checked state of the selected item
func (list *List) Toggle() {
if list.isUnselectable() {
return
}
list.Items[list.selected].Toggle()
}
@ -97,3 +109,13 @@ func (list *List) Less(i, j int) bool {
func (list *List) Swap(i, j int) {
list.Items[i], list.Items[j] = list.Items[j], list.Items[i]
}
/* -------------------- Unexported Functions -------------------- */
func (list *List) isSelectable() bool {
return list.selected >= 0 && list.selected < len(list.Items)
}
func (list *List) isUnselectable() bool {
return !list.isSelectable()
}