widget: replace Editor.makeValidCaret

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2022-02-05 22:03:49 +01:00
parent 45078813b3
commit ff245361a4
+5 -13
View File
@@ -223,12 +223,13 @@ func (e *Editor) processEvents(gtx layout.Context) {
}
}
func (e *Editor) makeValid(positions ...*combinedPos) {
func (e *Editor) makeValid() {
if e.valid {
return
}
e.lines, e.dims = e.layoutText(e.shaper)
e.makeValidCaret(positions...)
e.caret.start = e.closestPosition(combinedPos{runes: e.caret.start.runes})
e.caret.end = e.closestPosition(combinedPos{runes: e.caret.end.runes})
e.valid = true
}
@@ -1119,21 +1120,12 @@ func (e *Editor) Selection() (start, end int) {
// and end are in runes, and represent offsets into the editor text.
func (e *Editor) SetCaret(start, end int) {
e.makeValid()
e.caret.start.runes, e.caret.end.runes = start, end
e.makeValidCaret()
e.caret.start = e.closestPosition(combinedPos{runes: start})
e.caret.end = e.closestPosition(combinedPos{runes: end})
e.caret.scroll = true
e.scroller.Stop()
}
func (e *Editor) makeValidCaret(positions ...*combinedPos) {
// Jump through some hoops to order the offsets given to offsetToScreenPos,
// but still be able to update them correctly with the results thereof.
positions = append(positions, &e.caret.start, &e.caret.end)
for _, cp := range positions {
*cp = e.closestPosition(combinedPos{runes: cp.runes})
}
}
// SelectedText returns the currently selected text (if any) from the editor.
func (e *Editor) SelectedText() string {
start := min(e.caret.start.ofs, e.caret.end.ofs)