fix: correct text layout and rendering pipeline
- Fix render.go: replace broken material.Label with shaper-based text rendering (LayoutString + Shape + glyph iteration) per Gio's paintGlyph - Fix render.go: only apply clip rects at container boundaries; leaf elements have zero-size regions that were clipping all text out - Fix render.go: call r.drawElement recursively for container children so nested containers work and clips are applied correctly - Fix render.go: add drawLineText/drawLine helpers matching Gio's paintGlyph offset calculation (x + first.X, y + first.Y) - Fix element.go: Button.Draw now uses r.drawText instead of duplicate broken material.Label code; Label defaults to black when color is unset - Fix state.go: container children use relative coordinates instead of mixed screen-space/container-relative positions - Fix main.go: ConfigEvent carries raw pixel dimensions only; ScaleEvent carries scale only; layout is computed once per frame using both, eliminating the infinite Invalidate() loop
This commit is contained in:
+12
-12
@@ -1,9 +1,9 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/unit"
|
||||
"gioui.org/widget/material"
|
||||
)
|
||||
|
||||
// Region defines a screen area in device-independent pixels (Dp).
|
||||
@@ -34,18 +34,15 @@ type Container struct {
|
||||
func (c Container) Region() Region { return c.region }
|
||||
func (c Container) Visible() bool { return c.visible }
|
||||
func (c Container) Draw(gtx layout.Context, r *Renderer) {
|
||||
// Draw background
|
||||
// Draw background — children are drawn by drawElement, not here
|
||||
if c.background != (Color{}) {
|
||||
r.drawBg(gtx, c.region, c.background)
|
||||
}
|
||||
// Draw children
|
||||
for _, child := range c.children {
|
||||
child.Draw(gtx, r)
|
||||
}
|
||||
}
|
||||
|
||||
// NewContainer creates a Container with the given region, background, and children.
|
||||
func NewContainer(region Region, bg Color, children []Element) Container {
|
||||
fmt.Printf("[DEBUG NewContainer] region={X=%.0f Y=%.0f W=%.0f H=%.0f} children=%d\n", region.X, region.Y, region.W, region.H, len(children))
|
||||
return Container{
|
||||
region: region,
|
||||
visible: true,
|
||||
@@ -71,7 +68,12 @@ type Label struct {
|
||||
func (l Label) Region() Region { return l.region }
|
||||
func (l Label) Visible() bool { return l.visible }
|
||||
func (l Label) Draw(gtx layout.Context, r *Renderer) {
|
||||
r.drawText(gtx, l.Text, l.FontSize, l.region, l.Color)
|
||||
fmt.Printf("[DEBUG Label.Draw] text=%q region={X=%.0f Y=%.0f}\n", l.Text, l.region.X, l.region.Y)
|
||||
col := l.Color
|
||||
if col == (Color{}) {
|
||||
col = Color{R: 0, G: 0, B: 0, A: 255}
|
||||
}
|
||||
r.drawText(gtx, l.Text, l.FontSize, l.region, col)
|
||||
}
|
||||
func (l Label) ID() string { return l.id }
|
||||
|
||||
@@ -97,6 +99,7 @@ type Icon struct {
|
||||
func (i Icon) Region() Region { return i.region }
|
||||
func (i Icon) Visible() bool { return i.visible }
|
||||
func (i Icon) Draw(gtx layout.Context, r *Renderer) {
|
||||
fmt.Printf("[DEBUG Icon.Draw] name=%q icon=%v region={X=%.0f Y=%.0f}\n", i.Name, r.icon(i.Name) != nil, i.region.X, i.region.Y)
|
||||
img := r.icon(i.Name)
|
||||
if img == nil {
|
||||
return
|
||||
@@ -200,11 +203,8 @@ type Button struct {
|
||||
func (b Button) Region() Region { return b.region }
|
||||
func (b Button) Visible() bool { return b.visible }
|
||||
func (b Button) Draw(gtx layout.Context, r *Renderer) {
|
||||
th := material.NewTheme()
|
||||
th.Shaper = r.shp
|
||||
gtx.Constraints.Min.X = int(r.toPx(b.region.X))
|
||||
gtx.Constraints.Min.Y = int(r.toPx(b.region.Y))
|
||||
material.Label(th, r.theme.FontSize, b.Text).Layout(gtx)
|
||||
col := Color{R: 0, G: 0, B: 0, A: 255}
|
||||
r.drawText(gtx, b.Text, r.theme.FontSize, b.region, col)
|
||||
}
|
||||
func (b Button) ID() string { return b.id }
|
||||
|
||||
|
||||
+104
-36
@@ -3,19 +3,24 @@ package ui
|
||||
import (
|
||||
"bytes"
|
||||
"embed"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
_ "image/png"
|
||||
|
||||
"gioui.org/f32"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op"
|
||||
"gioui.org/op/clip"
|
||||
"gioui.org/op/paint"
|
||||
"gioui.org/text"
|
||||
"gioui.org/unit"
|
||||
"gioui.org/widget/material"
|
||||
"golang.org/x/image/math/fixed"
|
||||
)
|
||||
|
||||
// maxInt32 is a large value used as MaxWidth for single-line text layout.
|
||||
const maxInt32 = 1<<31 - 1
|
||||
|
||||
//go:embed icons/*.png
|
||||
var iconFS embed.FS
|
||||
|
||||
@@ -69,13 +74,19 @@ func (r *Renderer) toDp(px Px) Dp {
|
||||
|
||||
// Draw iterates elements and draws each in slice order (back-to-front).
|
||||
func (r *Renderer) Draw(gtx layout.Context, elems []Element) {
|
||||
// Clip to window bounds
|
||||
clipRect := clip.Rect{
|
||||
Min: gtx.Constraints.Min,
|
||||
Max: gtx.Constraints.Max,
|
||||
}.Push(gtx.Ops)
|
||||
fmt.Println("[DEBUG Draw] elems:", len(elems), "scale:", r.scale.Scale())
|
||||
|
||||
for _, e := range elems {
|
||||
// Gio sets constraints to layout.Exact(windowSize), so Min==Max. Use (0,0) as Min.
|
||||
winW := gtx.Constraints.Max.X
|
||||
winH := gtx.Constraints.Max.Y
|
||||
clipRect := clip.Rect{
|
||||
Min: image.Point{X: 0, Y: 0},
|
||||
Max: image.Point{X: winW, Y: winH},
|
||||
}.Push(gtx.Ops)
|
||||
fmt.Println("[DEBUG Draw] window clip:", image.Point{X: 0, Y: 0}, image.Point{X: winW, Y: winH})
|
||||
|
||||
for i, e := range elems {
|
||||
fmt.Printf("[DEBUG Draw] elem[%d]: %T visible=%v\n", i, e, e.Visible())
|
||||
if !e.Visible() {
|
||||
continue
|
||||
}
|
||||
@@ -85,32 +96,34 @@ func (r *Renderer) Draw(gtx layout.Context, elems []Element) {
|
||||
clipRect.Pop()
|
||||
}
|
||||
|
||||
// drawElement clips to the element's bounds, draws background if any,
|
||||
// then dispatches to the element's Draw method.
|
||||
func (r *Renderer) drawElement(gtx layout.Context, e Element) {
|
||||
reg := e.Region()
|
||||
fmt.Printf("[DEBUG drawElement] %T region={X=%.0f Y=%.0f W=%.0f H=%.0f}\n", e, reg.X, reg.Y, reg.W, reg.H)
|
||||
|
||||
// Clip to element bounds
|
||||
clipRect := clip.Rect{
|
||||
Min: image.Point{X: int(r.toPx(reg.X)), Y: int(r.toPx(reg.Y))},
|
||||
Max: image.Point{X: int(r.toPx(reg.X + reg.W)), Y: int(r.toPx(reg.Y + reg.H))},
|
||||
}.Push(gtx.Ops)
|
||||
|
||||
// Draw children if this is a container
|
||||
if container, ok := e.(Container); ok {
|
||||
for _, child := range container.children {
|
||||
child.Draw(gtx, r)
|
||||
// Clip to container bounds, draw background, then offset children
|
||||
fmt.Printf("[DEBUG drawElement] container children=%d originPx=(%d,%d)\n", len(container.children), int(r.toPx(reg.X)), int(r.toPx(reg.Y)))
|
||||
clipRect := clip.Rect{
|
||||
Min: image.Point{X: int(r.toPx(reg.X)), Y: int(r.toPx(reg.Y))},
|
||||
Max: image.Point{X: int(r.toPx(reg.X + reg.W)), Y: int(r.toPx(reg.Y + reg.H))},
|
||||
}.Push(gtx.Ops)
|
||||
e.Draw(gtx, r) // draw container background
|
||||
offset := op.Offset(image.Pt(int(r.toPx(reg.X)), int(r.toPx(reg.Y)))).Push(gtx.Ops)
|
||||
for i, child := range container.children {
|
||||
fmt.Printf("[DEBUG drawElement] child[%d]: %T region={X=%.0f Y=%.0f}\n", i, child, child.Region().X, child.Region().Y)
|
||||
r.drawElement(gtx, child)
|
||||
}
|
||||
offset.Pop()
|
||||
clipRect.Pop()
|
||||
fmt.Println("[DEBUG drawElement] clip popped")
|
||||
} else {
|
||||
// Leaf element: no self-clip (region may be zero-sized; clip is handled by parent container)
|
||||
e.Draw(gtx, r)
|
||||
}
|
||||
|
||||
// Dispatch to element's Draw method
|
||||
e.Draw(gtx, r)
|
||||
|
||||
clipRect.Pop()
|
||||
}
|
||||
|
||||
// drawBg draws a solid-color background for the given region.
|
||||
func (r *Renderer) drawBg(gtx layout.Context, reg Region, col Color) {
|
||||
fmt.Printf("[DEBUG drawBg] region={X=%.0f Y=%.0f W=%.0f H=%.0f} col={%d,%d,%d,%d}\n", reg.X, reg.Y, reg.W, reg.H, col.R, col.G, col.B, col.A)
|
||||
bgClip := clip.Rect{
|
||||
Min: image.Point{X: int(r.toPx(reg.X)), Y: int(r.toPx(reg.Y))},
|
||||
Max: image.Point{X: int(r.toPx(reg.X + reg.W)), Y: int(r.toPx(reg.Y + reg.H))},
|
||||
@@ -118,31 +131,86 @@ func (r *Renderer) drawBg(gtx layout.Context, reg Region, col Color) {
|
||||
paint.ColorOp{Color: color.NRGBA{R: col.R, G: col.G, B: col.B, A: col.A}}.Add(gtx.Ops)
|
||||
paint.PaintOp{}.Add(gtx.Ops)
|
||||
bgClip.Pop()
|
||||
fmt.Println("[DEBUG drawBg] bg drawn")
|
||||
}
|
||||
|
||||
// drawText draws text using Gio's textView approach.
|
||||
func (r *Renderer) drawText(gtx layout.Context, str string, size unit.Sp, reg Region, col Color) {
|
||||
th := material.NewTheme()
|
||||
th.Shaper = r.shp
|
||||
gtx.Constraints.Min.X = int(r.toPx(reg.X))
|
||||
gtx.Constraints.Min.Y = int(r.toPx(reg.Y))
|
||||
material.Label(th, size, str).Layout(gtx)
|
||||
}
|
||||
|
||||
// drawPng draws a PNG image at the given region and size.
|
||||
func (r *Renderer) drawPng(gtx layout.Context, img image.Image, reg Region, width, height Dp) {
|
||||
if img == nil {
|
||||
fmt.Printf("[DEBUG drawText] str=%q size=%.0f region={X=%.0f Y=%.0f} col={%d,%d,%d,%d}\n", str, float32(size), reg.X, reg.Y, col.R, col.G, col.B, col.A)
|
||||
if str == "" {
|
||||
return
|
||||
}
|
||||
// Layout text with width constraints (per layout_rendering.md)
|
||||
r.shp.LayoutString(text.Parameters{
|
||||
PxPerEm: fixed.I(gtx.Sp(size)),
|
||||
MinWidth: 0,
|
||||
MaxWidth: maxInt32,
|
||||
MaxLines: 1,
|
||||
}, str)
|
||||
r.drawLineText(gtx, reg.X, reg.Y, col)
|
||||
fmt.Println("[DEBUG drawText] text drawn")
|
||||
}
|
||||
|
||||
// drawLineText iterates laid-out glyphs and draws each line using op.Record for clipping safety.
|
||||
func (r *Renderer) drawLineText(gtx layout.Context, x, y Dp, col Color) {
|
||||
m := op.Record(gtx.Ops)
|
||||
var glyphs [32]text.Glyph
|
||||
line := glyphs[:0]
|
||||
for g, ok := r.shp.NextGlyph(); ok; g, ok = r.shp.NextGlyph() {
|
||||
line = append(line, g)
|
||||
if g.Flags&text.FlagLineBreak != 0 || cap(line)-len(line) == 0 {
|
||||
r.drawLine(gtx, line, x, y, col)
|
||||
line = line[:0]
|
||||
}
|
||||
}
|
||||
if len(line) > 0 {
|
||||
r.drawLine(gtx, line, x, y, col)
|
||||
}
|
||||
call := m.Stop()
|
||||
call.Add(gtx.Ops)
|
||||
}
|
||||
|
||||
// drawLine draws a single line of glyphs at the given position.
|
||||
// Matches Gio's paintGlyph: offset by (x + first.X, y + first.Y).
|
||||
func (r *Renderer) drawLine(gtx layout.Context, line []text.Glyph, x, y Dp, col Color) {
|
||||
if len(line) == 0 {
|
||||
return
|
||||
}
|
||||
first := line[0]
|
||||
// Offset: desired document position + first glyph's relative position.
|
||||
// first.X is in fixed.Int26_6 (divide by 64 for pixels), first.Y is in pixels.
|
||||
offX := float32(gtx.Dp(unit.Dp(x))) + float32(first.X)/64.0
|
||||
offY := float32(gtx.Dp(unit.Dp(y))) + float32(first.Y)
|
||||
t := op.Affine(f32.Affine2D{}.Offset(f32.Pt(offX, offY))).Push(gtx.Ops)
|
||||
|
||||
// Draw vector glyphs
|
||||
path := r.shp.Shape(line)
|
||||
outline := clip.Outline{Path: path}.Op().Push(gtx.Ops)
|
||||
paint.ColorOp{Color: color.NRGBA{R: col.R, G: col.G, B: col.B, A: col.A}}.Add(gtx.Ops)
|
||||
paint.PaintOp{}.Add(gtx.Ops)
|
||||
outline.Pop()
|
||||
|
||||
// Draw bitmap glyphs (emoji, etc.)
|
||||
if call := r.shp.Bitmaps(line); call != (op.CallOp{}) {
|
||||
call.Add(gtx.Ops)
|
||||
}
|
||||
|
||||
t.Pop()
|
||||
}
|
||||
|
||||
func (r *Renderer) drawPng(gtx layout.Context, img image.Image, reg Region, width, height Dp) {
|
||||
fmt.Printf("[DEBUG drawPng] icon=%v region={X=%.0f Y=%.0f} size={W=%.0f H=%.0f}\n", img != nil, reg.X, reg.Y, width, height)
|
||||
if img == nil {
|
||||
fmt.Println("[DEBUG drawPng] icon nil, skipping")
|
||||
return
|
||||
}
|
||||
xPx := int(r.toPx(reg.X))
|
||||
yPx := int(r.toPx(reg.Y))
|
||||
wPx := int(r.toPx(width))
|
||||
hPx := int(r.toPx(height))
|
||||
_ = image.Rect(xPx, yPx, xPx+wPx, yPx+hPx)
|
||||
|
||||
stack := op.Offset(image.Pt(xPx, yPx)).Push(gtx.Ops)
|
||||
paint.NewImageOp(img).Add(gtx.Ops)
|
||||
paint.PaintOp{}.Add(gtx.Ops)
|
||||
stack.Pop()
|
||||
fmt.Println("[DEBUG drawPng] png drawn")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user