Redesign ConfigEvent/Scale handling: ConfigEvent sets size, FrameEvent updates scale
- Logic layer has separate ConfigChan and ScaleChan - ConfigEvent: width/height in Dp, set on ConfigEvent - ScaleEvent: scale factor, set on FrameEvent when scale changes - Logic has ScreenSize() accessor for layout/rendering pipeline - Logic has SetScale() to update scale and recompute layout - main.go: ConfigEvent stores pixels, converts to DP using current scale - main.go: FrameEvent gets actual scale, sends ScaleEvent if changed
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package editor
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"pad/internal/ui"
|
||||
)
|
||||
|
||||
@@ -11,6 +13,11 @@ type ConfigEvent struct {
|
||||
Height ui.Dp
|
||||
}
|
||||
|
||||
// ScaleEvent represents a metric change (HiDPI scale factor).
|
||||
type ScaleEvent struct {
|
||||
Scale float32
|
||||
}
|
||||
|
||||
// InputEvent represents a user input event routed to an element.
|
||||
type InputEvent struct {
|
||||
ElementID string
|
||||
@@ -25,19 +32,22 @@ type ResultEvent struct {
|
||||
|
||||
// Logic runs the logic goroutine and provides channels for communication.
|
||||
type Logic struct {
|
||||
state *State
|
||||
state *State
|
||||
configChan chan ConfigEvent
|
||||
scaleChan chan ScaleEvent
|
||||
frameChan chan []ui.Element
|
||||
inputChan chan []InputEvent
|
||||
resultChan chan ResultEvent
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
// NewLogic creates a new Logic instance.
|
||||
// screenWidth and screenHeight are in device-independent pixels (Dp).
|
||||
func NewLogic(screenWidth, screenHeight ui.Dp) *Logic {
|
||||
return &Logic{
|
||||
state: NewState(screenWidth, screenHeight),
|
||||
state: NewState(screenWidth, screenHeight),
|
||||
configChan: make(chan ConfigEvent),
|
||||
scaleChan: make(chan ScaleEvent),
|
||||
frameChan: make(chan []ui.Element),
|
||||
inputChan: make(chan []InputEvent),
|
||||
resultChan: make(chan ResultEvent),
|
||||
@@ -49,6 +59,11 @@ func (l *Logic) ConfigChan() chan<- ConfigEvent {
|
||||
return l.configChan
|
||||
}
|
||||
|
||||
// ScaleChan returns the scale channel for the logic goroutine.
|
||||
func (l *Logic) ScaleChan() chan<- ScaleEvent {
|
||||
return l.scaleChan
|
||||
}
|
||||
|
||||
// FrameChan returns the frame channel for the logic goroutine.
|
||||
func (l *Logic) FrameChan() <-chan []ui.Element {
|
||||
return l.frameChan
|
||||
@@ -64,6 +79,22 @@ func (l *Logic) ResultChan() <-chan ResultEvent {
|
||||
return l.resultChan
|
||||
}
|
||||
|
||||
// ScreenSize returns the current screen dimensions in Dp.
|
||||
func (l *Logic) ScreenSize() (ui.Dp, ui.Dp) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
return l.state.ScreenWidth, l.state.ScreenHeight
|
||||
}
|
||||
|
||||
// SetScale updates the scale factor and recomputes layout.
|
||||
func (l *Logic) SetScale(scale float32) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
// Recompute layout with new scale (EditorLayout handles the conversion)
|
||||
l.state.Elems = EditorLayout(l.state.ScreenWidth, l.state.ScreenHeight)
|
||||
l.frameChan <- l.state.Elems
|
||||
}
|
||||
|
||||
// Run runs the logic goroutine loop.
|
||||
func (l *Logic) Run() {
|
||||
for {
|
||||
|
||||
Reference in New Issue
Block a user