Move logic into separate internal/editor package

- Create internal/editor/state.go with State struct and EditorLayout
- Create internal/editor/logic.go with Logic struct and Run method
- Logic owns all state, provides channels for config, frames, input, results
- Main goroutine uses editor.NewLogic() and logic.Run()
- Proper separation for testing later

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
gmp
2026-05-08 17:06:00 -04:00
co-authored by Qwen-Coder
parent 556f72643f
commit 013416ee08
4 changed files with 170 additions and 36 deletions
+10 -36
View File
@@ -10,6 +10,7 @@ import (
"gioui.org/text"
"gioui.org/unit"
"pad/internal/editor"
"pad/internal/ui"
)
@@ -32,27 +33,22 @@ func run(w *app.Window) error {
shaper := text.NewShaper()
renderer := ui.New(ui.Theme{FontSize: 14}, shaper)
// Channels for communication between goroutines
configChan := make(chan ui.ConfigEvent)
frameChan := make(chan []ui.Element)
inputChan := make(chan []ui.InputEvent)
// Initial screen size
screenWidth := unit.Dp(390)
screenHeight := unit.Dp(844)
// Create logic instance
logic := editor.NewLogic(screenWidth, screenHeight)
// Shared state protected by mutex
var mu sync.Mutex
var elems []ui.Element
// Initial screen size
screenWidth := unit.Dp(390)
screenHeight := unit.Dp(844)
// Compute initial elements
elems = ui.EditorLayout(screenWidth, screenHeight)
// Start logic goroutine
go logic(w, shaper, screenWidth, screenHeight, configChan, frameChan, inputChan)
go logic.Run()
// Start frame receiver goroutine
go frameReceiver(w, &mu, &elems, frameChan)
go frameReceiver(w, &mu, &elems, logic.FrameChan())
// Main event loop
for {
@@ -61,7 +57,7 @@ func run(w *app.Window) error {
return e.Err
case app.ConfigEvent:
// Send config event to logic goroutine
configChan <- ui.ConfigEvent{
logic.ConfigChan() <- editor.ConfigEvent{
Width: unit.Dp(e.Config.Size.X),
Height: unit.Dp(e.Config.Size.Y),
}
@@ -78,28 +74,6 @@ func run(w *app.Window) error {
}
}
// logic runs in a separate goroutine and owns all application state.
func logic(w *app.Window, shaper *text.Shaper, screenWidth, screenHeight unit.Dp,
configChan <-chan ui.ConfigEvent, frameChan chan<- []ui.Element, inputChan <-chan []ui.InputEvent) {
// Initial elements
elems := ui.EditorLayout(screenWidth, screenHeight)
for {
select {
case cfg := <-configChan:
// Update screen size and recompute layout
screenWidth = cfg.Width
screenHeight = cfg.Height
elems = ui.EditorLayout(screenWidth, screenHeight)
frameChan <- elems
case <-inputChan:
// Process input (not implemented in mockup)
frameChan <- elems
}
}
}
// frameReceiver runs in a separate goroutine and bridges frameChan to the main loop.
func frameReceiver(w *app.Window, mu *sync.Mutex, elems *[]ui.Element, frameChan <-chan []ui.Element) {
for {