fix: remove duplicate scale storage, read from State via ScaleProvider interface
- Remove Renderer.scale field and SetScale method - Add ScaleProvider interface to avoid editor/ui import cycle - Add Scale() method to State, rename Scale field to scale - Renderer reads scale through r.scale.Scale() - Fix icons.go blank import preventing embed.FS usage - Update main.go to pass State as ScaleProvider to Renderer
This commit is contained in:
+15
-20
@@ -32,18 +32,19 @@ func run(w *app.Window) error {
|
||||
var ops op.Ops
|
||||
|
||||
shaper := text.NewShaper(text.WithCollection(gofont.Collection()))
|
||||
renderer := ui.New(ui.Theme{FontSize: 14}, shaper)
|
||||
|
||||
// Initial DP size from app.Size() — used before we know the scale factor
|
||||
initialDPW := ui.Dp(390)
|
||||
initialDPH := ui.Dp(844)
|
||||
|
||||
// Track pixel dimensions from ConfigEvent (for resize handling)
|
||||
var pixelW, pixelH int
|
||||
|
||||
// Create logic instance with initial DP size and scale=1.0
|
||||
logic := editor.NewLogic(initialDPW, initialDPH)
|
||||
|
||||
renderer := ui.New(ui.Theme{FontSize: 14}, shaper, logic.State())
|
||||
|
||||
// Track pixel dimensions from ConfigEvent (for resize handling)
|
||||
var pixelW, pixelH int
|
||||
|
||||
// Shared state protected by mutex
|
||||
var mu sync.Mutex
|
||||
var elems []ui.Element
|
||||
@@ -53,6 +54,7 @@ func run(w *app.Window) error {
|
||||
|
||||
// Start frame receiver goroutine
|
||||
go frameReceiver(w, &mu, &elems, logic.FrameChan())
|
||||
scale := float32(1.0)
|
||||
|
||||
// Main event loop
|
||||
for {
|
||||
@@ -63,9 +65,9 @@ func run(w *app.Window) error {
|
||||
// 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 to DP using current scale
|
||||
dpW := ui.ToDp(ui.Px(pixelW), scale)
|
||||
dpH := ui.ToDp(ui.Px(pixelH), scale)
|
||||
// Send config event to logic goroutine (in DP)
|
||||
logic.ConfigChan() <- editor.ConfigEvent{
|
||||
Width: dpW,
|
||||
@@ -75,28 +77,21 @@ func run(w *app.Window) error {
|
||||
gtx := app.NewContext(&ops, e)
|
||||
|
||||
// Get scale from State — this is updated by ScaleEvent
|
||||
newScale := logic.Scale()
|
||||
newScale := gtx.Metric.PxPerDp
|
||||
|
||||
// Convert pixel dimensions to DP using actual scale
|
||||
dpW := ui.ToDp(ui.Px(pixelW), newScale)
|
||||
dpH := ui.ToDp(ui.Px(pixelH), newScale)
|
||||
|
||||
// Send updated config to logic goroutine
|
||||
logic.ConfigChan() <- editor.ConfigEvent{
|
||||
Width: dpW,
|
||||
Height: dpH,
|
||||
if newScale != scale {
|
||||
// Send updated scale to logic goroutine
|
||||
logic.ConfigChan() <- editor.ScaleEvent{newScale}
|
||||
scale = newScale
|
||||
}
|
||||
|
||||
// Update renderer with current scale from State
|
||||
renderer.SetScale(newScale)
|
||||
|
||||
// Acquire mutex, read frame, draw, release mutex
|
||||
mu.Lock()
|
||||
currentElems := elems
|
||||
mu.Unlock()
|
||||
|
||||
renderer.Draw(gtx, currentElems)
|
||||
e.Frame(&ops)
|
||||
mu.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user