Pad/cmd/pad/main.go
Greg Pomerantz 8739bae517 refactor: unified Element interface with Draw method and Container type
- Add Draw(gtx, r *Renderer) to Element interface
- Add Container type that implements Element and holds []Element children
- Leaf types (Label, Icon, Button) implement Draw themselves
- Single recursive drawElement function in Renderer — no switch/case
- Remove StatusBar/BottomBar — replaced by Container
- Remove shaper.go — no longer needed with Gio material labels
- Simplify main.go — scale via ScaleProvider interface
- 5 files changed, shaper.go deleted
2026-05-18 11:25:43 -04:00

105 lines
2.2 KiB
Go

package main
import (
"log"
"os"
"sync"
"gioui.org/app"
"gioui.org/op"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/font/gofont"
"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(text.WithCollection(gofont.Collection()))
// Initial DP size from app.Size()
initialDPW := ui.Dp(390)
initialDPH := ui.Dp(844)
// Create logic instance
logic := editor.NewLogic(initialDPW, initialDPH)
// Create renderer — reads scale from State via ScaleProvider
renderer := ui.New(ui.Theme{FontSize: 14}, shaper, logic.State())
// Track pixel dimensions from ConfigEvent
var pixelW, pixelH int
// 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:
pixelW = e.Config.Size.X
pixelH = e.Config.Size.Y
// Convert pixels to DP using current scale
scale := logic.Scale()
dpW := ui.ToDp(ui.Px(pixelW), scale)
dpH := ui.ToDp(ui.Px(pixelH), scale)
logic.ConfigChan() <- editor.ConfigEvent{
Width: dpW,
Height: dpH,
}
case app.FrameEvent:
gtx := app.NewContext(&ops, e)
// Update scale if changed
newScale := gtx.Metric.PxPerDp
curScale := logic.Scale()
if newScale != curScale {
logic.ConfigChan() <- editor.ScaleEvent{newScale}
}
// Acquire mutex, read frame, draw, release mutex
mu.Lock()
currentElems := elems
renderer.Draw(gtx, currentElems)
e.Frame(&ops)
mu.Unlock()
}
}
}
// 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()
}
}