Updates to rendering pipeline and documentation.
This commit is contained in:
+10
-12
@@ -1,7 +1,6 @@
|
||||
package editor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"pad/internal/ui"
|
||||
)
|
||||
|
||||
@@ -49,17 +48,17 @@ func EditorLayout(screenWidth, screenHeight ui.Dp) []ui.Element {
|
||||
statusBarRegion,
|
||||
ui.Color{R: 230, G: 230, B: 230, A: 255},
|
||||
[]ui.Element{
|
||||
// Row 1: filename centered (relative to container)
|
||||
ui.NewLabel("a very long file name that probably will eventually need to be truncated.txt", 14, ui.Region{X: statusBarW/2 - ui.Dp(40), Y: ui.Dp(2)}),
|
||||
// Row 2: cut icon left (relative to container)
|
||||
ui.NewIcon("cut", ui.Region{X: ui.Dp(8), Y: ui.Dp(28)}, ui.Dp(16)),
|
||||
// Row 2: status icons right (relative to container)
|
||||
ui.NewLabel("Ln 47, Col 12", 12, ui.Region{X: statusBarW - ui.Dp(70), Y: ui.Dp(28)}),
|
||||
// Row 1: filename
|
||||
ui.NewLabel("a very long file name that probably will eventually need to be truncated.txt", 14, ui.Region{X: 0, Y: ui.Dp(2), W: statusBarW, H: ui.Dp(20)}, ui.AlignStart),
|
||||
// Row 2: cut, copy, paste icons
|
||||
ui.NewIcon("cut", ui.Region{X: ui.Dp(0), Y: ui.Dp(28), W: ui.IconSize, H: ui.IconSize}, 0),
|
||||
ui.NewIcon("copy", ui.Region{X: ui.Dp(48), Y: ui.Dp(28), W: ui.IconSize, H: ui.IconSize}, 0),
|
||||
ui.NewIcon("paste", ui.Region{X: ui.Dp(96), Y: ui.Dp(28), W: ui.IconSize, H: ui.IconSize}, 0),
|
||||
},
|
||||
)
|
||||
|
||||
// --- Bottom bar ---
|
||||
bottomBarHeight := ui.Dp(24)
|
||||
bottomBarHeight := ui.BottomBarHeight
|
||||
bottomBarY := screenHeight - margin - bottomBarHeight
|
||||
bottomBarRegion := ui.Region{
|
||||
X: margin, Y: bottomBarY,
|
||||
@@ -71,12 +70,11 @@ func EditorLayout(screenWidth, screenHeight ui.Dp) []ui.Element {
|
||||
bottomBarRegion,
|
||||
ui.Color{R: 230, G: 230, B: 230, A: 255},
|
||||
[]ui.Element{
|
||||
ui.NewLabel("Ln 47, Col 12", 12, ui.Region{X: ui.Dp(8), Y: ui.Dp(2)}),
|
||||
ui.NewLabel("1024 / 50000", 12, ui.Region{X: bottomBarW/2 - ui.Dp(40), Y: ui.Dp(2)}),
|
||||
ui.NewLabel("Wrap: On", 12, ui.Region{X: bottomBarW - ui.Dp(60), Y: ui.Dp(2)}),
|
||||
ui.NewLabel("Ln 47, Col 12", 12, ui.Region{X: 0, Y: ui.Dp(2), W: bottomBarW, H: ui.Dp(20)}, ui.AlignStart),
|
||||
ui.NewLabel("1024 / 50000", 12, ui.Region{X: 0, Y: ui.Dp(2), W: bottomBarW, H: ui.Dp(20)}, ui.AlignCenter),
|
||||
ui.NewLabel("Wrap: On", 12, ui.Region{X: 0, Y: ui.Dp(2), W: bottomBarW, H: ui.Dp(20)}, ui.AlignEnd),
|
||||
},
|
||||
)
|
||||
|
||||
fmt.Println("[DEBUG EditorLayout] created", len([]ui.Element{statusBar, bottomBar}), "containers")
|
||||
return []ui.Element{statusBar, bottomBar}
|
||||
}
|
||||
|
||||
+9
-11
@@ -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 }
|
||||
|
||||
|
||||
@@ -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
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user