Introduce distinct Dp/Px types to prevent coordinate mixing at compile time
- Add ui.Dp and ui.Px as distinct named types - Logic layer works exclusively in Dp (positions, sizes, regions) - Renderer converts Dp→Px at Gio interop boundaries - Capture gtx.Constraints once at start of Draw() for consistent positioning - main.go converts app.ConfigEvent pixels to Dp before passing to logic layer - Update documentation with new coordinate system architecture
This commit is contained in:
+29
-9
@@ -34,12 +34,13 @@ func run(w *app.Window) error {
|
||||
shaper := text.NewShaper(text.WithCollection(gofont.Collection()))
|
||||
renderer := ui.New(ui.Theme{FontSize: 14}, shaper)
|
||||
|
||||
// Initial screen size
|
||||
screenWidth := unit.Dp(390)
|
||||
screenHeight := unit.Dp(844)
|
||||
// Track window dimensions in pixels (from ConfigEvent) and DP (for logic layer)
|
||||
var pixelW, pixelH int
|
||||
var dpW, dpH ui.Dp
|
||||
var scale float32 = 1.0 // updated on first FrameEvent
|
||||
|
||||
// Create logic instance
|
||||
logic := editor.NewLogic(screenWidth, screenHeight)
|
||||
// Create logic instance with initial DP size
|
||||
logic := editor.NewLogic(dpW, dpH)
|
||||
|
||||
// Shared state protected by mutex
|
||||
var mu sync.Mutex
|
||||
@@ -57,18 +58,37 @@ func run(w *app.Window) error {
|
||||
case app.DestroyEvent:
|
||||
return e.Err
|
||||
case app.ConfigEvent:
|
||||
// Send config event to logic goroutine
|
||||
// Store pixel dimensions from ConfigEvent
|
||||
pixelW = e.Config.Size.X
|
||||
pixelH = e.Config.Size.Y
|
||||
// Convert to DP using current scale (default 1.0 until first FrameEvent)
|
||||
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: unit.Dp(e.Config.Size.X),
|
||||
Height: unit.Dp(e.Config.Size.Y),
|
||||
Width: dpW,
|
||||
Height: dpH,
|
||||
}
|
||||
case app.FrameEvent:
|
||||
// Update scale from the frame context
|
||||
gtx := app.NewContext(&ops, e)
|
||||
scale = gtx.Metric.PxPerDp
|
||||
|
||||
// Reconvert pixel dimensions to DP with actual scale
|
||||
dpW = ui.ToDp(ui.Px(pixelW), scale)
|
||||
dpH = ui.ToDp(ui.Px(pixelH), scale)
|
||||
|
||||
// Send updated config to logic goroutine
|
||||
logic.ConfigChan() <- editor.ConfigEvent{
|
||||
Width: dpW,
|
||||
Height: dpH,
|
||||
}
|
||||
|
||||
// Acquire mutex, read frame, draw, release mutex
|
||||
mu.Lock()
|
||||
currentElems := elems
|
||||
mu.Unlock()
|
||||
|
||||
gtx := app.NewContext(&ops, e)
|
||||
renderer.Draw(gtx, currentElems)
|
||||
e.Frame(&ops)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user