mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-07 18:35:34 +00:00
layout: truncate negative List.Position.First positions to 0
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+6
-2
@@ -65,10 +65,10 @@ type Position struct {
|
|||||||
BeforeEnd bool
|
BeforeEnd bool
|
||||||
// First is the index of the first visible child.
|
// First is the index of the first visible child.
|
||||||
First int
|
First int
|
||||||
// Offset is the distance in pixels from the top edge to the child at index
|
// Offset is the distance in pixels from the leading edge to the child at index
|
||||||
// First.
|
// First.
|
||||||
Offset int
|
Offset int
|
||||||
// OffsetLast is the signed distance in pixels from the bottom edge to the
|
// OffsetLast is the signed distance in pixels from the trailing edge to the
|
||||||
// bottom edge of the child at index First+Count.
|
// bottom edge of the child at index First+Count.
|
||||||
OffsetLast int
|
OffsetLast int
|
||||||
// Count is the number of visible children.
|
// Count is the number of visible children.
|
||||||
@@ -95,6 +95,10 @@ func (l *List) init(gtx Context, len int) {
|
|||||||
l.children = l.children[:0]
|
l.children = l.children[:0]
|
||||||
l.len = len
|
l.len = len
|
||||||
l.update(gtx)
|
l.update(gtx)
|
||||||
|
if l.Position.First < 0 {
|
||||||
|
l.Position.Offset = 0
|
||||||
|
l.Position.First = 0
|
||||||
|
}
|
||||||
if l.scrollToEnd() || l.Position.First > len {
|
if l.scrollToEnd() || l.Position.First > len {
|
||||||
l.Position.Offset = 0
|
l.Position.Offset = 0
|
||||||
l.Position.First = len
|
l.Position.First = len
|
||||||
|
|||||||
@@ -13,6 +13,25 @@ import (
|
|||||||
"gioui.org/op"
|
"gioui.org/op"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestListPositionExtremes(t *testing.T) {
|
||||||
|
var l List
|
||||||
|
gtx := Context{
|
||||||
|
Ops: new(op.Ops),
|
||||||
|
Constraints: Exact(image.Pt(20, 10)),
|
||||||
|
}
|
||||||
|
const n = 3
|
||||||
|
layout := func(_ Context, idx int) Dimensions {
|
||||||
|
if idx < 0 || idx >= n {
|
||||||
|
t.Errorf("list index %d out of bounds [0;%d]", idx, n-1)
|
||||||
|
}
|
||||||
|
return Dimensions{}
|
||||||
|
}
|
||||||
|
l.Position.First = -1
|
||||||
|
l.Layout(gtx, n, layout)
|
||||||
|
l.Position.First = n + 1
|
||||||
|
l.Layout(gtx, n, layout)
|
||||||
|
}
|
||||||
|
|
||||||
func TestEmptyList(t *testing.T) {
|
func TestEmptyList(t *testing.T) {
|
||||||
var l List
|
var l List
|
||||||
gtx := Context{
|
gtx := Context{
|
||||||
|
|||||||
Reference in New Issue
Block a user