mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-06 09:55:40 +00:00
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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user