Break import cycle: use StateReader interface instead of direct State reference

- StateReader interface in ui package (Scale(), ScreenWidth(), ScreenHeight())
- State implements StateReader
- Renderer reads scale/window size via interface, never imports editor
- State fields are private (screenWidth, screenHeight, scale) with accessor methods
- No SetScale/SetWindowSize calls from main.go — Renderer reads from State directly
This commit is contained in:
gmp
2026-05-09 20:33:00 -04:00
parent 21b4df59a3
commit 620d534454
4 changed files with 57 additions and 48 deletions
+24 -22
View File
@@ -15,50 +15,52 @@ import (
"golang.org/x/image/math/fixed"
)
// StateReader provides access to scale and window dimensions from State.
// Implemented by editor.State to avoid import cycles.
type StateReader interface {
Scale() float32
ScreenWidth() Dp
ScreenHeight() Dp
}
// Renderer consumes a slice of elements and draws them.
type Renderer struct {
theme Theme
shp *text.Shaper
scale float32 // pixels per DP, from State.Scale
// Window dimensions in physical pixels, from State
windowW int
windowH int
state StateReader // read-only access to State for scale/window size
}
// New creates a new Renderer.
func New(th Theme, shp *text.Shaper) *Renderer {
return &Renderer{theme: th, shp: shp, scale: 1.0}
return &Renderer{theme: th, shp: shp}
}
// SetScale updates the renderer's scale factor from State.Scale.
func (r *Renderer) SetScale(scale float32) {
r.scale = scale
// SetState sets the State reference for the Renderer.
// The Renderer reads scale and window size from State directly.
func (r *Renderer) SetState(state StateReader) {
r.state = state
}
// SetWindowSize updates the window dimensions in physical pixels from State.
func (r *Renderer) SetWindowSize(w, h int) {
r.windowW = w
r.windowH = h
}
// toPx converts Dp to physical pixels using the renderer's scale.
// toPx converts Dp to physical pixels using the scale from State.
func (r *Renderer) toPx(dp Dp) Px {
return ToPx(dp, r.scale)
return ToPx(dp, r.state.Scale())
}
// toDp converts physical pixels to Dp using the renderer's scale.
// toDp converts physical pixels to Dp using the scale from State.
func (r *Renderer) toDp(px Px) Dp {
return ToDp(px, r.scale)
return ToDp(px, r.state.Scale())
}
// Draw iterates elements and draws each in slice order (back-to-front).
// Window constraints come from State (set via SetWindowSize), not from gtx.
// Scale comes from State (set via SetScale), not from gtx.Metric.PxPerDp.
// All scale and window size values come from State, never from gtx.
func (r *Renderer) Draw(gtx layout.Context, elems []Element) {
// Use window dimensions from State — never from gtx.Constraints
// Use window dimensions from State — convert DP to pixels using State.Scale
state := r.state
windowWPx := int(r.toPx(state.ScreenWidth()))
windowHPx := int(r.toPx(state.ScreenHeight()))
initialConstraints := WindowConstraints{
Min: image.Point{X: 0, Y: 0},
Max: image.Point{X: r.windowW, Y: r.windowH},
Max: image.Point{X: windowWPx, Y: windowHPx},
}
for _, e := range elems {