widget: fix text selection and selection area rendering issues

1. When selecting multiple lines of text, the rendered selection area does not include the last character of the first line.
2. When selecting a specific line (other than the last line) in multiline text, the last character of that line cannot be selected.

Signed-off-by: CoyAce <AkeyCoy@gmail.com>
This commit is contained in:
CoyAce
2026-01-20 23:31:47 +08:00
committed by Chris Waldon
parent 3af0ebb3a8
commit 9966e922f9
3 changed files with 122 additions and 35 deletions
+53
View File
@@ -1042,6 +1042,59 @@ func TestEditorSelectShortcuts(t *testing.T) {
}
}
func TestEditorSelect(t *testing.T) {
tFont := font.Font{}
tFontSize := unit.Sp(10)
tShaper := text.NewShaper(text.NoSystemFonts(), text.WithCollection(gofont.Collection()))
tEditor := &Editor{
SingleLine: false,
ReadOnly: true,
}
lines := "abc abc abc def def def ghi ghi ghi"
tEditor.SetText(lines)
tRouter := new(input.Router)
gtx := layout.Context{
Ops: new(op.Ops),
Locale: english,
Constraints: layout.Exact(image.Pt(50, 100)),
Source: tRouter.Source(),
}
gtx.Execute(key.FocusCmd{Tag: tEditor})
tEditor.Layout(gtx, tShaper, tFont, tFontSize, op.CallOp{}, op.CallOp{})
index := tEditor.text.index
firstLineGlyphs := index.lines[0].glyphs
firstLine := lines[:firstLineGlyphs]
yOffFirstLineCenter := index.lines[0].yOff / 2
tEditor.ClearSelection()
tEditor.text.MoveCoord(image.Pt(100, yOffFirstLineCenter))
if cStart, cEnd := tEditor.Selection(); cStart != len(firstLine) || cEnd != 0 {
t.Errorf("TestEditorSelect %d: initial selection", len(firstLine))
}
if got := tEditor.SelectedText(); got != firstLine {
t.Errorf("TestEditorSelect : Expected %q, got %q", firstLine, got)
}
yOffSecondLineCenter := (index.lines[1].yOff-index.lines[0].yOff)/2 + index.lines[0].yOff
tEditor.text.MoveCoord(image.Pt(100, yOffSecondLineCenter))
secondLineGlyphs := index.lines[1].glyphs
firstTwoLines := lines[:firstLineGlyphs+secondLineGlyphs]
if got := tEditor.SelectedText(); got != firstTwoLines {
t.Errorf("TestEditorSelect : Expected %q, got %q", firstTwoLines, got)
}
tEditor.Layout(gtx, tShaper, tFont, tFontSize, op.CallOp{}, op.CallOp{})
firstLineEnd := index.lines[0].width.Round()
firstRegionMaxWidth := tEditor.text.regions[0].Bounds.Max.X
if firstRegionMaxWidth != firstLineEnd {
t.Errorf("Selection paint should contain last character")
}
}
// Verify that an existing selection is dismissed when you press arrow keys.
func TestSelectMove(t *testing.T) {
e := new(Editor)