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
+6 -13
View File
@@ -38,14 +38,15 @@ func run(w *app.Window) error {
initialDPW := ui.Dp(390)
initialDPH := ui.Dp(844)
// Track pixel dimensions from ConfigEvent (for resize handling)
var pixelW, pixelH int
// Track previous scale to detect changes
prevScale := float32(1.0)
// Create logic instance with initial DP size and scale=1.0
logic := editor.NewLogic(initialDPW, initialDPH)
// Set State reference on Renderer — it reads scale/window size from State directly
renderer.SetState(logic.State())
// Shared state protected by mutex
var mu sync.Mutex
var elems []ui.Element
@@ -62,19 +63,14 @@ func run(w *app.Window) error {
case app.DestroyEvent:
return e.Err
case app.ConfigEvent:
// Store pixel dimensions from ConfigEvent
pixelW = e.Config.Size.X
pixelH = e.Config.Size.Y
// Convert to DP using current scale (starts at 1.0)
dpW := ui.ToDp(ui.Px(pixelW), 1.0)
dpH := ui.ToDp(ui.Px(pixelH), 1.0)
// Convert pixel dimensions to DP using current scale (starts at 1.0)
dpW := ui.ToDp(ui.Px(e.Config.Size.X), 1.0)
dpH := ui.ToDp(ui.Px(e.Config.Size.Y), 1.0)
// Send config event to logic goroutine (in DP)
logic.ConfigChan() <- editor.ConfigEvent{
Width: dpW,
Height: dpH,
}
// Update renderer with window size from State
renderer.SetWindowSize(pixelW, pixelH)
case app.FrameEvent:
gtx := app.NewContext(&ops, e)
newScale := gtx.Metric.PxPerDp
@@ -85,9 +81,6 @@ func run(w *app.Window) error {
prevScale = newScale
}
// Update renderer with current scale from State
renderer.SetScale(logic.Scale())
// Acquire mutex, read frame, draw, release mutex
mu.Lock()
currentElems := elems