mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-07 18:35:34 +00:00
widget: remove Editor references to text.Line.Len and text.Glyph.Rune
In preparation for adding editor masking, Editor can't rely on the Rune and Len fields of the laid out text. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+32
-30
@@ -7,7 +7,6 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unicode/utf8"
|
|
||||||
|
|
||||||
"gioui.org/f32"
|
"gioui.org/f32"
|
||||||
"gioui.org/gesture"
|
"gioui.org/gesture"
|
||||||
@@ -210,7 +209,7 @@ func (e *Editor) processKey(gtx layout.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *Editor) moveLines(distance int) {
|
func (e *Editor) moveLines(distance int) {
|
||||||
e.caret.xoff = e.moveToLine(e.caret.x+e.caret.xoff, e.caret.line+distance)
|
e.moveToLine(e.caret.x+e.caret.xoff, e.caret.line+distance)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Editor) command(k key.Event) bool {
|
func (e *Editor) command(k key.Event) bool {
|
||||||
@@ -465,8 +464,8 @@ func (e *Editor) moveCoord(pos image.Point) {
|
|||||||
carLine++
|
carLine++
|
||||||
}
|
}
|
||||||
x := fixed.I(pos.X + e.scrollOff.X)
|
x := fixed.I(pos.X + e.scrollOff.X)
|
||||||
e.caret.xoff = 0
|
|
||||||
e.moveToLine(x, carLine)
|
e.moveToLine(x, carLine)
|
||||||
|
e.caret.xoff = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Editor) layoutText(s text.Shaper) ([]text.Line, layout.Dimensions) {
|
func (e *Editor) layoutText(s text.Shaper) ([]text.Line, layout.Dimensions) {
|
||||||
@@ -504,22 +503,25 @@ func (e *Editor) layoutCaret() (line, col int, x fixed.Int26_6, y int) {
|
|||||||
var idx int
|
var idx int
|
||||||
var prevDesc fixed.Int26_6
|
var prevDesc fixed.Int26_6
|
||||||
loop:
|
loop:
|
||||||
for line = 0; line < len(e.lines); line++ {
|
for {
|
||||||
|
x = 0
|
||||||
|
col = 0
|
||||||
l := e.lines[line]
|
l := e.lines[line]
|
||||||
y += (prevDesc + l.Ascent).Ceil()
|
y += (prevDesc + l.Ascent).Ceil()
|
||||||
prevDesc = l.Descent
|
prevDesc = l.Descent
|
||||||
if line == len(e.lines)-1 || idx+l.Len > e.rr.caret {
|
|
||||||
for _, g := range l.Layout {
|
for _, g := range l.Layout {
|
||||||
if idx == e.rr.caret {
|
if idx == e.rr.caret {
|
||||||
break loop
|
break loop
|
||||||
}
|
}
|
||||||
x += g.Advance
|
x += g.Advance
|
||||||
idx += utf8.RuneLen(g.Rune)
|
_, s := e.rr.runeAt(idx)
|
||||||
|
idx += s
|
||||||
col++
|
col++
|
||||||
}
|
}
|
||||||
|
if line == len(e.lines)-1 || idx > e.rr.caret {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
idx += l.Len
|
line++
|
||||||
}
|
}
|
||||||
x += align(e.Alignment, e.lines[line].Width, e.viewSize.X)
|
x += align(e.Alignment, e.lines[line].Width, e.viewSize.X)
|
||||||
return
|
return
|
||||||
@@ -579,10 +581,10 @@ func (e *Editor) movePages(pages int) {
|
|||||||
y2 += h
|
y2 += h
|
||||||
carLine2++
|
carLine2++
|
||||||
}
|
}
|
||||||
e.caret.xoff = e.moveToLine(e.caret.x+e.caret.xoff, carLine2)
|
e.moveToLine(e.caret.x+e.caret.xoff, carLine2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Editor) moveToLine(x fixed.Int26_6, line int) fixed.Int26_6 {
|
func (e *Editor) moveToLine(x fixed.Int26_6, line int) {
|
||||||
e.makeValid()
|
e.makeValid()
|
||||||
if line < 0 {
|
if line < 0 {
|
||||||
line = 0
|
line = 0
|
||||||
@@ -590,31 +592,31 @@ func (e *Editor) moveToLine(x fixed.Int26_6, line int) fixed.Int26_6 {
|
|||||||
if line >= len(e.lines) {
|
if line >= len(e.lines) {
|
||||||
line = len(e.lines) - 1
|
line = len(e.lines) - 1
|
||||||
}
|
}
|
||||||
// Move to start of line.
|
|
||||||
for i := e.caret.col - 1; i >= 0; i-- {
|
prevDesc := e.lines[line].Descent
|
||||||
|
for e.caret.line < line {
|
||||||
|
e.moveEnd()
|
||||||
|
l := e.lines[e.caret.line]
|
||||||
|
_, s := e.rr.runeAt(e.rr.caret)
|
||||||
|
e.rr.caret += s
|
||||||
|
e.caret.y += (prevDesc + l.Ascent).Ceil()
|
||||||
|
e.caret.col = 0
|
||||||
|
prevDesc = l.Descent
|
||||||
|
e.caret.line++
|
||||||
|
}
|
||||||
|
for e.caret.line > line {
|
||||||
|
e.moveStart()
|
||||||
|
l := e.lines[e.caret.line]
|
||||||
_, s := e.rr.runeBefore(e.rr.caret)
|
_, s := e.rr.runeBefore(e.rr.caret)
|
||||||
e.rr.caret -= s
|
e.rr.caret -= s
|
||||||
}
|
|
||||||
e.caret.col = 0
|
|
||||||
if line != e.caret.line {
|
|
||||||
prevDesc := e.lines[line].Descent
|
|
||||||
if line > e.caret.line {
|
|
||||||
for i := e.caret.line; i < line; i++ {
|
|
||||||
l := e.lines[i]
|
|
||||||
e.rr.caret += l.Len
|
|
||||||
e.caret.y += (prevDesc + l.Ascent).Ceil()
|
|
||||||
prevDesc = l.Descent
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for i := e.caret.line - 1; i >= line; i-- {
|
|
||||||
l := e.lines[i]
|
|
||||||
e.rr.caret -= l.Len
|
|
||||||
e.caret.y -= (prevDesc + l.Ascent).Ceil()
|
e.caret.y -= (prevDesc + l.Ascent).Ceil()
|
||||||
prevDesc = l.Descent
|
prevDesc = l.Descent
|
||||||
|
e.caret.line--
|
||||||
|
l = e.lines[e.caret.line]
|
||||||
|
e.caret.col = len(l.Layout) - 1
|
||||||
}
|
}
|
||||||
}
|
|
||||||
e.caret.line = line
|
e.moveStart()
|
||||||
}
|
|
||||||
l := e.lines[line]
|
l := e.lines[line]
|
||||||
e.caret.x = align(e.Alignment, l.Width, e.viewSize.X)
|
e.caret.x = align(e.Alignment, l.Width, e.viewSize.X)
|
||||||
// Only move past the end of the last line
|
// Only move past the end of the last line
|
||||||
@@ -636,7 +638,7 @@ func (e *Editor) moveToLine(x fixed.Int26_6, line int) fixed.Int26_6 {
|
|||||||
e.rr.caret += s
|
e.rr.caret += s
|
||||||
e.caret.col++
|
e.caret.col++
|
||||||
}
|
}
|
||||||
return x - e.caret.x
|
e.caret.xoff = x - e.caret.x
|
||||||
}
|
}
|
||||||
|
|
||||||
// Move the caret: positive distance moves forward, negative distance moves
|
// Move the caret: positive distance moves forward, negative distance moves
|
||||||
|
|||||||
Reference in New Issue
Block a user