Store scale in State, use it everywhere instead of gtx.Metric.PxPerDp

- State.Scale stores the scale factor (default 1.0)
- ScaleEvent applies SetScale() which updates State.Scale
- Logic.Scale() accessor returns current scale
- Renderer.SetScale() updates renderer's scale from State
- main.go: uses logic.Scale() to get scale, calls renderer.SetScale()
- No more references to gtx.Metric.PxPerDp in render.go or main.go
This commit is contained in:
gmp
2026-05-09 20:23:02 -04:00
parent ef31701d87
commit 68437c32a7
4 changed files with 28 additions and 20 deletions
+9 -5
View File
@@ -31,9 +31,7 @@ func (e ConfigEvent) apply(s *State) {
}
func (e ScaleEvent) apply(s *State) {
// Scale is used by the renderer, not stored in state.
// Layout is already computed in DP, so no recomputation needed.
_ = s.Elems // force use of state
s.SetScale(e.Scale)
}
// InputEvent represents a user input event routed to an element.
@@ -98,6 +96,13 @@ func (l *Logic) ScreenSize() (ui.Dp, ui.Dp) {
return l.state.ScreenWidth, l.state.ScreenHeight
}
// Scale returns the current scale factor (pixels per DP).
func (l *Logic) Scale() float32 {
l.mu.Lock()
defer l.mu.Unlock()
return l.state.Scale
}
// Run runs the logic goroutine loop.
func (l *Logic) Run() {
for {
@@ -109,8 +114,7 @@ func (l *Logic) Run() {
e.apply(l.state)
l.frameChan <- l.state.Elems
case ScaleEvent:
// Scale is used by the renderer via gtx.Metric.PxPerDp.
// No state change needed — layout is already in DP.
e.apply(l.state)
l.frameChan <- l.state.Elems
}
case <-l.inputChan:
+9
View File
@@ -9,19 +9,28 @@ import (
type State struct {
ScreenWidth ui.Dp
ScreenHeight ui.Dp
Scale float32 // pixels per DP, default 1.0
Elems []ui.Element
}
// NewState creates a new State with initial editor layout.
// screenWidth and screenHeight are in device-independent pixels (Dp).
// scale defaults to 1.0 until updated by ScaleEvent.
func NewState(screenWidth, screenHeight ui.Dp) *State {
return &State{
ScreenWidth: screenWidth,
ScreenHeight: screenHeight,
Scale: 1.0,
Elems: EditorLayout(screenWidth, screenHeight),
}
}
// SetScale updates the scale factor and recomputes layout.
func (s *State) SetScale(scale float32) {
s.Scale = scale
s.Elems = EditorLayout(s.ScreenWidth, s.ScreenHeight)
}
// EditorLayout computes regions for the editor page.
// It takes screen dimensions in Dp and returns []Element with regions in Dp.
// This function works exclusively in Dp — no pixel conversions.