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:
gmp
2026-05-18 21:03:22 -04:00
parent 8739bae517
commit 22c1d5bed1
5 changed files with 163 additions and 140 deletions
+5 -34
View File
@@ -30,59 +30,31 @@ func main() {
func run(w *app.Window) error {
var ops op.Ops
shaper := text.NewShaper(text.WithCollection(gofont.Collection()))
// Initial DP size from app.Size()
initialDPW := ui.Dp(390)
initialDPH := ui.Dp(844)
// Create logic instance
logic := editor.NewLogic(initialDPW, initialDPH)
// Create renderer — reads scale from State via ScaleProvider
logic := editor.NewLogic()
renderer := ui.New(ui.Theme{FontSize: 14}, shaper, logic.State())
// Track pixel dimensions from ConfigEvent
var pixelW, pixelH int
// Shared state protected by mutex
var mu sync.Mutex
var elems []ui.Element
// Start logic goroutine
go logic.Run()
// Start frame receiver goroutine
go frameReceiver(w, &mu, &elems, logic.FrameChan())
// Main event loop
for {
switch e := w.Event().(type) {
case app.DestroyEvent:
return e.Err
case app.ConfigEvent:
pixelW = e.Config.Size.X
pixelH = e.Config.Size.Y
// Convert pixels to DP using current scale
scale := logic.Scale()
dpW := ui.ToDp(ui.Px(pixelW), scale)
dpH := ui.ToDp(ui.Px(pixelH), scale)
// ConfigEvent: raw pixel dimensions only.
logic.ConfigChan() <- editor.ConfigEvent{
Width: dpW,
Height: dpH,
PixelWidth: e.Config.Size.X,
PixelHeight: e.Config.Size.Y,
}
case app.FrameEvent:
gtx := app.NewContext(&ops, e)
// Update scale if changed
newScale := gtx.Metric.PxPerDp
curScale := logic.Scale()
curScale := logic.State().Scale()
if newScale != curScale {
logic.ConfigChan() <- editor.ScaleEvent{newScale}
}
// Acquire mutex, read frame, draw, release mutex
mu.Lock()
currentElems := elems
renderer.Draw(gtx, currentElems)
@@ -92,7 +64,6 @@ func run(w *app.Window) error {
}
}
// frameReceiver runs in a separate goroutine and bridges frameChan to the main loop.
func frameReceiver(w *app.Window, mu *sync.Mutex, elems *[]ui.Element, frameChan <-chan []ui.Element) {
for {
frame := <-frameChan