mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-06 18:05:35 +00:00
widget: test cursor motion in all editor permutations
This commit adds a testcase to catch unexpected panics in the editor's scroll offset logic introduced by using different setting combinations that affect editor layout. It also fixes a panic for single-line editors with alignments other than text.Start. Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
This commit is contained in:
+1
-1
@@ -644,7 +644,7 @@ func (e *Editor) layout(gtx layout.Context, content layout.Widget) layout.Dimens
|
|||||||
|
|
||||||
var scrollRange image.Rectangle
|
var scrollRange image.Rectangle
|
||||||
if e.SingleLine {
|
if e.SingleLine {
|
||||||
scrollRange.Min.X = -e.scrollOff.X
|
scrollRange.Min.X = min(-e.scrollOff.X, 0)
|
||||||
scrollRange.Max.X = max(0, e.dims.Size.X-(e.scrollOff.X+e.viewSize.X))
|
scrollRange.Max.X = max(0, e.dims.Size.X-(e.scrollOff.X+e.viewSize.X))
|
||||||
} else {
|
} else {
|
||||||
scrollRange.Min.Y = -e.scrollOff.Y
|
scrollRange.Min.Y = -e.scrollOff.Y
|
||||||
|
|||||||
@@ -27,6 +27,40 @@ import (
|
|||||||
"golang.org/x/image/math/fixed"
|
"golang.org/x/image/math/fixed"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestEditorConfigurations(t *testing.T) {
|
||||||
|
gtx := layout.Context{
|
||||||
|
Ops: new(op.Ops),
|
||||||
|
Constraints: layout.Exact(image.Pt(100, 100)),
|
||||||
|
}
|
||||||
|
cache := text.NewCache(gofont.Collection())
|
||||||
|
fontSize := unit.Px(10)
|
||||||
|
font := text.Font{}
|
||||||
|
sentence := "the quick brown fox jumps over the lazy dog"
|
||||||
|
runes := len([]rune(sentence))
|
||||||
|
|
||||||
|
// Ensure that both ends of the text are reachable in all permutations
|
||||||
|
// of settings that influence layout.
|
||||||
|
for _, lineMode := range []bool{true, false} {
|
||||||
|
for _, alignment := range []text.Alignment{text.Start, text.Middle, text.End} {
|
||||||
|
t.Run(fmt.Sprintf("SingleLine: %v Alignment: %v", lineMode, alignment), func(t *testing.T) {
|
||||||
|
defer func() {
|
||||||
|
if err := recover(); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
e := new(Editor)
|
||||||
|
e.SingleLine = lineMode
|
||||||
|
e.Alignment = alignment
|
||||||
|
e.SetText(sentence)
|
||||||
|
e.SetCaret(0, 0)
|
||||||
|
e.Layout(gtx, cache, font, fontSize, nil)
|
||||||
|
e.SetCaret(runes, runes)
|
||||||
|
e.Layout(gtx, cache, font, fontSize, nil)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestEditor(t *testing.T) {
|
func TestEditor(t *testing.T) {
|
||||||
e := new(Editor)
|
e := new(Editor)
|
||||||
gtx := layout.Context{
|
gtx := layout.Context{
|
||||||
|
|||||||
Reference in New Issue
Block a user