widget: fix Editor and Label clipping

Commit gioui.org/commit/94d242d18c9245 broke Editor and Label clipping,
most visible for single-line Editors. Restore the correct clipping.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-12-06 22:57:11 +01:00
parent 9b8e847a05
commit ede632b265
2 changed files with 14 additions and 12 deletions
+5 -4
View File
@@ -146,7 +146,7 @@ type SubmitEvent struct {
} }
type line struct { type line struct {
offset f32.Point offset image.Point
clip op.CallOp clip op.CallOp
} }
@@ -447,12 +447,13 @@ func (e *Editor) layout(gtx layout.Context) layout.Dimensions {
} }
func (e *Editor) PaintText(gtx layout.Context) { func (e *Editor) PaintText(gtx layout.Context) {
clip := textPadding(e.lines) cl := textPadding(e.lines)
clip.Max = clip.Max.Add(e.viewSize) cl.Max = cl.Max.Add(e.viewSize)
for _, shape := range e.shapes { for _, shape := range e.shapes {
stack := op.Push(gtx.Ops) stack := op.Push(gtx.Ops)
op.Offset(shape.offset).Add(gtx.Ops) op.Offset(layout.FPt(shape.offset)).Add(gtx.Ops)
shape.clip.Add(gtx.Ops) shape.clip.Add(gtx.Ops)
clip.Rect(cl.Sub(shape.offset)).Add(gtx.Ops)
paint.PaintOp{}.Add(gtx.Ops) paint.PaintOp{}.Add(gtx.Ops)
stack.Pop() stack.Pop()
} }
+9 -8
View File
@@ -7,9 +7,9 @@ import (
"image" "image"
"unicode/utf8" "unicode/utf8"
"gioui.org/f32"
"gioui.org/layout" "gioui.org/layout"
"gioui.org/op" "gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint" "gioui.org/op/paint"
"gioui.org/text" "gioui.org/text"
"gioui.org/unit" "gioui.org/unit"
@@ -38,7 +38,7 @@ type lineIterator struct {
const inf = 1e6 const inf = 1e6
func (l *lineIterator) Next() (text.Layout, f32.Point, bool) { func (l *lineIterator) Next() (text.Layout, image.Point, bool) {
for len(l.Lines) > 0 { for len(l.Lines) > 0 {
line := l.Lines[0] line := l.Lines[0]
l.Lines = l.Lines[1:] l.Lines = l.Lines[1:]
@@ -82,10 +82,10 @@ func (l *lineIterator) Next() (text.Layout, f32.Point, bool) {
endx += layout.Advances[rune] endx += layout.Advances[rune]
rune++ rune++
} }
offf := f32.Point{X: float32(off.X) / 64, Y: float32(off.Y) / 64} offf := image.Point{X: off.X.Floor(), Y: off.Y.Floor()}
return layout, offf, true return layout, offf, true
} }
return text.Layout{}, f32.Point{}, false return text.Layout{}, image.Point{}, false
} }
func (l Label) Layout(gtx layout.Context, s text.Shaper, font text.Font, size unit.Value, txt string) layout.Dimensions { func (l Label) Layout(gtx layout.Context, s text.Shaper, font text.Font, size unit.Value, txt string) layout.Dimensions {
@@ -97,11 +97,11 @@ func (l Label) Layout(gtx layout.Context, s text.Shaper, font text.Font, size un
} }
dims := linesDimens(lines) dims := linesDimens(lines)
dims.Size = cs.Constrain(dims.Size) dims.Size = cs.Constrain(dims.Size)
clip := textPadding(lines) cl := textPadding(lines)
clip.Max = clip.Max.Add(dims.Size) cl.Max = cl.Max.Add(dims.Size)
it := lineIterator{ it := lineIterator{
Lines: lines, Lines: lines,
Clip: clip, Clip: cl,
Alignment: l.Alignment, Alignment: l.Alignment,
Width: dims.Size.X, Width: dims.Size.X,
} }
@@ -111,8 +111,9 @@ func (l Label) Layout(gtx layout.Context, s text.Shaper, font text.Font, size un
break break
} }
stack := op.Push(gtx.Ops) stack := op.Push(gtx.Ops)
op.Offset(off).Add(gtx.Ops) op.Offset(layout.FPt(off)).Add(gtx.Ops)
s.Shape(font, textSize, l).Add(gtx.Ops) s.Shape(font, textSize, l).Add(gtx.Ops)
clip.Rect(cl.Sub(off)).Add(gtx.Ops)
paint.PaintOp{}.Add(gtx.Ops) paint.PaintOp{}.Add(gtx.Ops)
stack.Pop() stack.Pop()
} }