Table now has keynav support
This commit is contained in:
parent
d633e0f5f6
commit
260e2b31b6
@ -9,6 +9,10 @@ import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
|||||||
import "git.tebibyte.media/sashakoshka/tomo/default/theme"
|
import "git.tebibyte.media/sashakoshka/tomo/default/theme"
|
||||||
import "git.tebibyte.media/sashakoshka/tomo/default/config"
|
import "git.tebibyte.media/sashakoshka/tomo/default/config"
|
||||||
|
|
||||||
|
// TODO: using the event propagator core might not be the best idea here. we
|
||||||
|
// should have slightly different behavior to sync the focused element with the
|
||||||
|
// selected cell.
|
||||||
|
|
||||||
type tableCell struct {
|
type tableCell struct {
|
||||||
tomo.Element
|
tomo.Element
|
||||||
tomo.Pattern
|
tomo.Pattern
|
||||||
@ -245,29 +249,29 @@ func (element *TableContainer) DrawBackground (bounds image.Rectangle) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (element *TableContainer) HandleMouseDown (x, y int, button input.Button) {
|
func (element *TableContainer) HandleMouseDown (x, y int, button input.Button) {
|
||||||
|
element.Focus()
|
||||||
element.Propagator.HandleMouseDown(x, y, button)
|
element.Propagator.HandleMouseDown(x, y, button)
|
||||||
if button != input.ButtonLeft { return }
|
if button != input.ButtonLeft { return }
|
||||||
|
|
||||||
for rowIndex, row := range element.grid {
|
for rowIndex, row := range element.grid {
|
||||||
for columnIndex, child := range row {
|
for columnIndex, child := range row {
|
||||||
if image.Pt(x, y).In(child.Rectangle) {
|
if image.Pt(x, y).In(child.Rectangle) {
|
||||||
selected :=
|
element.selectCell(columnIndex, rowIndex)
|
||||||
rowIndex == element.selectedRow &&
|
|
||||||
columnIndex == element.selectedColumn
|
|
||||||
if selected { return }
|
|
||||||
oldColumn, oldRow := element.selectedColumn, element.selectedRow
|
|
||||||
element.selectedColumn, element.selectedRow = columnIndex, rowIndex
|
|
||||||
if oldColumn >= 0 && oldRow >= 0 {
|
|
||||||
element.core.DamageRegion(element.redoCell(oldColumn, oldRow))
|
|
||||||
}
|
|
||||||
element.core.DamageRegion(element.redoCell(columnIndex, rowIndex))
|
|
||||||
if element.onSelect != nil {
|
|
||||||
element.onSelect()
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}}}
|
}}}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (element *TableContainer) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
|
||||||
|
switch key {
|
||||||
|
case input.KeyLeft: element.changeSelectionBy(-1, 0)
|
||||||
|
case input.KeyRight: element.changeSelectionBy(1, 0)
|
||||||
|
case input.KeyUp: element.changeSelectionBy(0, -1)
|
||||||
|
case input.KeyDown: element.changeSelectionBy(0, 1)
|
||||||
|
case input.KeyEscape: element.selectCell(-1, -1)
|
||||||
|
default: element.Propagator.HandleKeyDown(key, modifiers)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ScrollContentBounds returns the full content size of the element.
|
// ScrollContentBounds returns the full content size of the element.
|
||||||
func (element *TableContainer) ScrollContentBounds () image.Rectangle {
|
func (element *TableContainer) ScrollContentBounds () image.Rectangle {
|
||||||
return element.contentBounds
|
return element.contentBounds
|
||||||
@ -316,6 +320,38 @@ func (element *TableContainer) ScrollAxes () (horizontal, vertical bool) {
|
|||||||
return true, true
|
return true, true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (element *TableContainer) changeSelectionBy (column, row int) {
|
||||||
|
column += element.selectedColumn
|
||||||
|
row += element.selectedRow
|
||||||
|
if column < 0 { column = 0 }
|
||||||
|
if row < 0 { row = 0 }
|
||||||
|
element.selectCell(column, row)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (element *TableContainer) selectCell (column, row int) {
|
||||||
|
if column < -1 { column = -1 }
|
||||||
|
if row < -1 { row = -1 }
|
||||||
|
if column >= element.columns { column = element.columns - 1 }
|
||||||
|
if row >= element.rows { row = element.rows - 1 }
|
||||||
|
|
||||||
|
if column == element.selectedColumn && row == element.selectedRow {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
oldColumn, oldRow := element.selectedColumn, element.selectedRow
|
||||||
|
element.selectedColumn = column
|
||||||
|
element.selectedRow = row
|
||||||
|
if oldColumn >= 0 && oldRow >= 0 {
|
||||||
|
element.core.DamageRegion(element.redoCell(oldColumn, oldRow))
|
||||||
|
}
|
||||||
|
if column >= 0 && row >= 0 {
|
||||||
|
element.core.DamageRegion(element.redoCell(column, row))
|
||||||
|
}
|
||||||
|
if element.onSelect != nil {
|
||||||
|
element.onSelect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (element *TableContainer) maxScrollHeight () (height int) {
|
func (element *TableContainer) maxScrollHeight () (height int) {
|
||||||
viewportHeight := element.Bounds().Dy()
|
viewportHeight := element.Bounds().Dy()
|
||||||
height = element.contentBounds.Dy() - viewportHeight
|
height = element.contentBounds.Dy() - viewportHeight
|
||||||
|
Reference in New Issue
Block a user