mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-07 18:35:34 +00:00
widget: tolerate nil shader in Editor movement methods
Fixes gio#142 Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+32
-3
@@ -3,6 +3,7 @@
|
|||||||
package widget
|
package widget
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"image"
|
"image"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
@@ -172,7 +173,9 @@ func (e *Editor) processEvents(gtx layout.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *Editor) makeValid() {
|
func (e *Editor) makeValid() {
|
||||||
if !e.valid {
|
if e.valid {
|
||||||
|
return
|
||||||
|
}
|
||||||
e.lines, e.dims = e.layoutText(e.shaper)
|
e.lines, e.dims = e.layoutText(e.shaper)
|
||||||
line, col, x, y := e.layoutCaret()
|
line, col, x, y := e.layoutCaret()
|
||||||
e.caret.line = line
|
e.caret.line = line
|
||||||
@@ -180,7 +183,6 @@ func (e *Editor) makeValid() {
|
|||||||
e.caret.x = x
|
e.caret.x = x
|
||||||
e.caret.y = y
|
e.caret.y = y
|
||||||
e.valid = true
|
e.valid = true
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Editor) processPointer(gtx layout.Context) {
|
func (e *Editor) processPointer(gtx layout.Context) {
|
||||||
@@ -530,7 +532,12 @@ func (e *Editor) layoutText(s text.Shaper) ([]text.Line, layout.Dimensions) {
|
|||||||
e.maskReader.Reset(&e.rr, e.Mask)
|
e.maskReader.Reset(&e.rr, e.Mask)
|
||||||
r = &e.maskReader
|
r = &e.maskReader
|
||||||
}
|
}
|
||||||
lines, _ := s.Layout(e.font, e.textSize, e.maxWidth, r)
|
var lines []text.Line
|
||||||
|
if s != nil {
|
||||||
|
lines, _ = s.Layout(e.font, e.textSize, e.maxWidth, r)
|
||||||
|
} else {
|
||||||
|
lines, _ = nullLayout(r)
|
||||||
|
}
|
||||||
dims := linesDimens(lines)
|
dims := linesDimens(lines)
|
||||||
for i := 0; i < len(lines)-1; i++ {
|
for i := 0; i < len(lines)-1; i++ {
|
||||||
// To avoid layout flickering while editing, assume a soft newline takes
|
// To avoid layout flickering while editing, assume a soft newline takes
|
||||||
@@ -799,5 +806,27 @@ func (e *Editor) NumLines() int {
|
|||||||
return len(e.lines)
|
return len(e.lines)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func nullLayout(r io.Reader) ([]text.Line, error) {
|
||||||
|
var layout []text.Glyph
|
||||||
|
rr := bufio.NewReader(r)
|
||||||
|
var rerr error
|
||||||
|
var n int
|
||||||
|
for {
|
||||||
|
r, s, err := rr.ReadRune()
|
||||||
|
n += s
|
||||||
|
layout = append(layout, text.Glyph{Rune: r})
|
||||||
|
if err != nil {
|
||||||
|
rerr = err
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return []text.Line{
|
||||||
|
{
|
||||||
|
Layout: layout,
|
||||||
|
Len: n,
|
||||||
|
},
|
||||||
|
}, rerr
|
||||||
|
}
|
||||||
|
|
||||||
func (s ChangeEvent) isEditorEvent() {}
|
func (s ChangeEvent) isEditorEvent() {}
|
||||||
func (s SubmitEvent) isEditorEvent() {}
|
func (s SubmitEvent) isEditorEvent() {}
|
||||||
|
|||||||
@@ -155,6 +155,12 @@ func TestEditorCaretConsistency(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEditorNoLayout(t *testing.T) {
|
||||||
|
var e Editor
|
||||||
|
e.SetText("hi!\n")
|
||||||
|
e.Move(1)
|
||||||
|
}
|
||||||
|
|
||||||
// Generate generates a value of itself, for testing/quick.
|
// Generate generates a value of itself, for testing/quick.
|
||||||
func (editMutation) Generate(rand *rand.Rand, size int) reflect.Value {
|
func (editMutation) Generate(rand *rand.Rand, size int) reflect.Value {
|
||||||
t := editMutation(rand.Intn(int(moveLast)))
|
t := editMutation(rand.Intn(int(moveLast)))
|
||||||
|
|||||||
Reference in New Issue
Block a user