Pad/cmd/pad/main.go
Greg Pomerantz 013416ee08 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>
2026-05-08 17:06:00 -04:00

87 lines
1.6 KiB
Go

package main
import (
"log"
"os"
"sync"
"gioui.org/app"
"gioui.org/op"
"gioui.org/text"
"gioui.org/unit"
"pad/internal/editor"
"pad/internal/ui"
)
func main() {
go func() {
w := new(app.Window)
w.Option(app.Title("Pad"))
w.Option(app.Size(unit.Dp(390), unit.Dp(844)))
if err := run(w); err != nil {
log.Fatal(err)
}
os.Exit(0)
}()
app.Main()
}
func run(w *app.Window) error {
var ops op.Ops
shaper := text.NewShaper()
renderer := ui.New(ui.Theme{FontSize: 14}, shaper)
// 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
// Start logic goroutine
go logic.Run()
// Start frame receiver goroutine
go frameReceiver(w, &mu, &elems, logic.FrameChan())
// Main event loop
for {
switch e := w.Event().(type) {
case app.DestroyEvent:
return e.Err
case app.ConfigEvent:
// Send config event to logic goroutine
logic.ConfigChan() <- editor.ConfigEvent{
Width: unit.Dp(e.Config.Size.X),
Height: unit.Dp(e.Config.Size.Y),
}
case app.FrameEvent:
// 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)
}
}
}
// 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 {
frame := <-frameChan
mu.Lock()
*elems = frame
w.Invalidate()
mu.Unlock()
}
}