Updates to rendering pipeline and documentation.

This commit is contained in:
gmp
2026-05-25 17:40:35 -04:00
parent 22c1d5bed1
commit 50e9ca5b34
9 changed files with 299 additions and 219 deletions
+9 -11
View File
@@ -1,7 +1,6 @@
package ui
import (
"fmt"
"gioui.org/layout"
"gioui.org/unit"
)
@@ -42,7 +41,6 @@ func (c Container) Draw(gtx layout.Context, r *Renderer) {
// 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,
@@ -68,22 +66,22 @@ 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) {
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)
r.drawText(gtx, l.Text, l.FontSize, l.region, l.Align, col)
}
func (l Label) ID() string { return l.id }
// NewLabel creates a visible Label element.
func NewLabel(text string, fontSize unit.Sp, region Region) Label {
func NewLabel(text string, fontSize unit.Sp, region Region, align TextAlign) Label {
return Label{
region: region,
visible: true,
Text: text,
FontSize: fontSize,
Align: align,
}
}
@@ -99,16 +97,16 @@ 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
}
size := i.Size
if size == 0 {
size = Dp(16)
// Auto-scale: if Size is 0, use region dimensions so the icon fills its region
w, h := Dp(0), Dp(0)
if i.Size == 0 {
w, h = i.region.W, i.region.H
}
r.drawPng(gtx, img, i.region, size, i.Size)
r.drawPng(gtx, img, i.region, w, h)
}
func (i Icon) ID() string { return i.id }
@@ -204,7 +202,7 @@ 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) {
col := Color{R: 0, G: 0, B: 0, A: 255}
r.drawText(gtx, b.Text, r.theme.FontSize, b.region, col)
r.drawText(gtx, b.Text, r.theme.FontSize, b.region, AlignStart, col)
}
func (b Button) ID() string { return b.id }
+8 -8
View File
@@ -2,14 +2,14 @@ package ui
const (
// Standard dimensions in Dp
statusBarLineHeight = Dp(24)
statusBarFilenameLine = Dp(24)
statusBarIconsLine = Dp(24)
bottomBarHeight = Dp(24)
iconSize = Dp(24)
iconGap = Dp(36)
padding = Dp(8)
buttonPadding = Dp(8)
StatusBarLineHeight = Dp(24)
StatusBarFilenameLine = Dp(24)
StatusBarIconsLine = Dp(24)
BottomBarHeight = Dp(24)
IconSize = Dp(24)
IconGap = Dp(36)
Padding = Dp(8)
ButtonPadding = Dp(8)
)
// NOTE: EditorLayout is defined in internal/editor/state.go.
+74 -40
View File
@@ -3,7 +3,6 @@ package ui
import (
"bytes"
"embed"
"fmt"
"image"
"image/color"
_ "image/png"
@@ -46,15 +45,17 @@ func New(th Theme, shp *text.Shaper, scale ScaleProvider) *Renderer {
// loadIcons loads PNG icons from the embedded filesystem.
func (r *Renderer) loadIcons() {
data, err := iconFS.ReadFile("icons/cut.png")
if err != nil {
return
for _, name := range []string{"cut", "copy", "paste"} {
data, err := iconFS.ReadFile("icons/" + name + ".png")
if err != nil {
continue
}
img, _, err := image.Decode(bytes.NewReader(data))
if err != nil {
continue
}
r.icons[name] = img
}
img, _, err := image.Decode(bytes.NewReader(data))
if err != nil {
return
}
r.icons["cut"] = img
}
// icon returns a loaded icon image by name, or nil if not found.
@@ -74,8 +75,6 @@ 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) {
fmt.Println("[DEBUG Draw] elems:", len(elems), "scale:", r.scale.Scale())
// 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
@@ -83,10 +82,8 @@ func (r *Renderer) Draw(gtx layout.Context, elems []Element) {
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())
for _, e := range elems {
if !e.Visible() {
continue
}
@@ -98,24 +95,20 @@ func (r *Renderer) Draw(gtx layout.Context, elems []Element) {
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)
if container, ok := e.(Container); ok {
// 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)
for _, child := range container.children {
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)
@@ -123,7 +116,6 @@ func (r *Renderer) drawElement(gtx layout.Context, e Element) {
}
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))},
@@ -131,23 +123,49 @@ 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")
}
func (r *Renderer) drawText(gtx layout.Context, str string, size unit.Sp, reg Region, col Color) {
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)
func (r *Renderer) drawText(gtx layout.Context, str string, size unit.Sp, reg Region, align TextAlign, col Color) {
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")
params := text.Parameters{
PxPerEm: fixed.I(gtx.Sp(size)),
MinWidth: 0,
MaxWidth: maxInt32,
MaxLines: 1,
}
if align == AlignStart {
// No measurement needed — draw at reg.X directly
r.shp.LayoutString(params, str)
r.drawLineText(gtx, reg.X, reg.Y, col)
} else {
// Phase 1: measure text width via glyph iteration (consumes iterator)
r.shp.LayoutString(params, str)
var totalAdvance fixed.Int26_6
for g, ok := r.shp.NextGlyph(); ok; g, ok = r.shp.NextGlyph() {
totalAdvance += g.Advance
}
// totalAdvance>>6 is in device pixels (shaper outputs device pixels).
// Divide by scale to convert to Dp so it matches region coordinate space.
textW := Dp(float32(totalAdvance>>6) / r.scale.Scale())
// Compute aligned X position
var drawX Dp
switch align {
case AlignCenter:
drawX = reg.X + (reg.W - textW) / 2
case AlignEnd:
drawX = reg.X + reg.W - textW
default:
drawX = reg.X
}
// Phase 2: layout again (iterator consumed) and draw
r.shp.LayoutString(params, str)
r.drawLineText(gtx, drawX, reg.Y, col)
}
}
// drawLineText iterates laid-out glyphs and draws each line using op.Record for clipping safety.
@@ -198,19 +216,35 @@ func (r *Renderer) drawLine(gtx layout.Context, line []text.Glyph, x, y Dp, col
}
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
}
// Auto-size: if width or height is 0, use the region dimensions
w := width
h := height
if w == 0 && h == 0 {
w, h = reg.W, reg.H
} else if w == 0 {
w = h
} else if h == 0 {
h = w
}
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)
wPx := int(r.toPx(w))
hPx := int(r.toPx(h))
origW := img.Bounds().Dx()
origH := img.Bounds().Dy()
if origW == 0 || origH == 0 {
return
}
sx := float32(wPx) / float32(origW)
sy := float32(hPx) / float32(origH)
// Position, then scale so the image fills the target size
offset := op.Offset(image.Pt(xPx, yPx)).Push(gtx.Ops)
scale := op.Affine(f32.Affine2D{}.Scale(f32.Pt(0, 0), f32.Pt(sx, sy))).Push(gtx.Ops)
paint.NewImageOp(img).Add(gtx.Ops)
paint.PaintOp{}.Add(gtx.Ops)
stack.Pop()
fmt.Println("[DEBUG drawPng] png drawn")
scale.Pop()
offset.Pop()
}