Skip to content

Commit

Permalink
widget.Table: keep focused cell in view when navigating with keyboard
Browse files Browse the repository at this point in the history
The table size at the focused cell does not need to account for the
header's.

Fixes 5088.
  • Loading branch information
pierre committed Sep 8, 2024
1 parent 6eeeb42 commit f400306
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
2 changes: 0 additions & 2 deletions widget/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,6 @@ func (t *Table) ScrollTo(id TableCellID) {
cellX -= t.stuckXOff + t.stuckWidth
}
if t.ShowHeaderColumn {
cellX += t.headerSize.Width
stickCols--
}
if stickCols == 0 || id.Col > stickCols {
Expand All @@ -491,7 +490,6 @@ func (t *Table) ScrollTo(id TableCellID) {
cellY -= t.stuckYOff + t.stuckHeight
}
if t.ShowHeaderRow {
cellY += t.headerSize.Height
stickRows--
}
if stickRows == 0 || id.Row >= stickRows {
Expand Down
38 changes: 38 additions & 0 deletions widget/table_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,44 @@ func TestTable_ScrollTo(t *testing.T) {
}
}

func TestTable_ScrollToShowHeaders(t *testing.T) {
test.NewTempApp(t)

// for this test the separator thickness is 0
test.ApplyTheme(t, &paddingZeroTheme{test.Theme()})

// we will test a 20 row x 5 column table where each cell is 50x50
const (
maxRows int = 20
maxCols int = 5
width float32 = 50
height float32 = 50
)

templ := canvas.NewRectangle(color.Gray16{})
templ.SetMinSize(fyne.Size{Width: width, Height: height})

table := NewTable(
func() (int, int) { return maxRows, maxCols },
func() fyne.CanvasObject { return templ },
func(TableCellID, fyne.CanvasObject) {})
table.ShowHeaderRow = true
table.ShowHeaderColumn = true

w := test.NewWindow(table)
defer w.Close()

first := TableCellID{}
last := TableCellID{Row: maxRows - 1, Col: maxCols - 1}
table.ScrollTo(last)
lastPos := fyne.NewPos(float32(last.Col)*width, float32(last.Row)*height)
assert.Equal(t, lastPos, table.offset)
assert.Equal(t, lastPos, table.content.Offset)
table.ScrollTo(first)
assert.Equal(t, fyne.Position{}, table.offset)
assert.Equal(t, fyne.Position{}, table.content.Offset)
}

func TestTable_ScrollToBottom(t *testing.T) {
test.NewTempApp(t)
test.ApplyTheme(t, test.NewTheme())
Expand Down

0 comments on commit f400306

Please sign in to comment.