Fixed Container.HandleSelection
It now handles nested containers properly.
This commit is contained in:
parent
9aea6d8c0f
commit
93019b1b38
@ -271,34 +271,35 @@ func (element *Container) HandleSelection (direction tomo.SelectionDirection) (o
|
|||||||
|
|
||||||
firstSelected := element.firstSelected()
|
firstSelected := element.firstSelected()
|
||||||
if firstSelected < 0 {
|
if firstSelected < 0 {
|
||||||
found := false
|
// no element is currently selected, so we need to select either
|
||||||
|
// the first or last selectable element depending on the
|
||||||
|
// direction.
|
||||||
switch direction {
|
switch direction {
|
||||||
case tomo.SelectionDirectionBackward:
|
|
||||||
element.forSelectableBackward (func (child tomo.Selectable) bool {
|
|
||||||
if child.HandleSelection(direction) {
|
|
||||||
element.selected = true
|
|
||||||
found = true
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
return true
|
|
||||||
|
|
||||||
case tomo.SelectionDirectionNeutral, tomo.SelectionDirectionForward:
|
case tomo.SelectionDirectionNeutral, tomo.SelectionDirectionForward:
|
||||||
element.forSelectable (func (child tomo.Selectable) bool {
|
// if we recieve a neutral or forward direction, select
|
||||||
if child.HandleSelection(direction) {
|
// the first selectable element.
|
||||||
element.selected = true
|
return element.selectFirstSelectableElement(direction)
|
||||||
found = true
|
|
||||||
return false
|
case tomo.SelectionDirectionBackward:
|
||||||
}
|
// if we recieve a backward direction, select the last
|
||||||
return true
|
// selectable element.
|
||||||
})
|
return element.selectLastSelectableElement(direction)
|
||||||
}
|
}
|
||||||
return found
|
|
||||||
} else {
|
} else {
|
||||||
|
// an element is currently selected, so we need to move the
|
||||||
|
// selection in the specified direction
|
||||||
firstSelectedChild :=
|
firstSelectedChild :=
|
||||||
element.children[firstSelected].Element.(tomo.Selectable)
|
element.children[firstSelected].Element.(tomo.Selectable)
|
||||||
|
|
||||||
|
// before we move the selection, the currently selected child
|
||||||
|
// may also be able to move its selection. if the child is able
|
||||||
|
// to do that, we will let it and not move ours.
|
||||||
|
if firstSelectedChild.HandleSelection(direction) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// find the previous/next selectable element relative to the
|
||||||
|
// currently selected element, if it exists.
|
||||||
for index := firstSelected + int(direction);
|
for index := firstSelected + int(direction);
|
||||||
index < len(element.children) && index >= 0;
|
index < len(element.children) && index >= 0;
|
||||||
index += int(direction) {
|
index += int(direction) {
|
||||||
@ -307,6 +308,8 @@ func (element *Container) HandleSelection (direction tomo.SelectionDirection) (o
|
|||||||
element.children[index].
|
element.children[index].
|
||||||
Element.(tomo.Selectable)
|
Element.(tomo.Selectable)
|
||||||
if selectable && child.HandleSelection(direction) {
|
if selectable && child.HandleSelection(direction) {
|
||||||
|
// we have found one, so we now actually move
|
||||||
|
// the selection.
|
||||||
firstSelectedChild.HandleDeselection()
|
firstSelectedChild.HandleDeselection()
|
||||||
element.selected = true
|
element.selected = true
|
||||||
return true
|
return true
|
||||||
@ -317,6 +320,38 @@ func (element *Container) HandleSelection (direction tomo.SelectionDirection) (o
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (element *Container) selectFirstSelectableElement (
|
||||||
|
direction tomo.SelectionDirection,
|
||||||
|
) (
|
||||||
|
ok bool,
|
||||||
|
) {
|
||||||
|
element.forSelectable (func (child tomo.Selectable) bool {
|
||||||
|
if child.HandleSelection(direction) {
|
||||||
|
element.selected = true
|
||||||
|
ok = true
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (element *Container) selectLastSelectableElement (
|
||||||
|
direction tomo.SelectionDirection,
|
||||||
|
) (
|
||||||
|
ok bool,
|
||||||
|
) {
|
||||||
|
element.forSelectableBackward (func (child tomo.Selectable) bool {
|
||||||
|
if child.HandleSelection(direction) {
|
||||||
|
element.selected = true
|
||||||
|
ok = true
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (element *Container) FlexibleHeightFor (width int) (height int) {
|
func (element *Container) FlexibleHeightFor (width int) (height int) {
|
||||||
return element.layout.FlexibleHeightFor(element.children, width)
|
return element.layout.FlexibleHeightFor(element.children, width)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user