114 lines
2.5 KiB
Go
114 lines
2.5 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()))
|
|
renderer := ui.New(ui.Theme{FontSize: 14}, shaper)
|
|
|
|
// Initial DP size from app.Size() — used before we know the scale factor
|
|
initialDPW := ui.Dp(390)
|
|
initialDPH := ui.Dp(844)
|
|
|
|
// Track pixel dimensions from ConfigEvent (for resize handling)
|
|
var pixelW, pixelH int
|
|
|
|
// Create logic instance with initial DP size and scale=1.0
|
|
logic := editor.NewLogic(initialDPW, initialDPH)
|
|
|
|
// 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:
|
|
// Store pixel dimensions from ConfigEvent
|
|
pixelW = e.Config.Size.X
|
|
pixelH = e.Config.Size.Y
|
|
// Convert to DP using current scale (starts at 1.0)
|
|
dpW := ui.ToDp(ui.Px(pixelW), 1.0)
|
|
dpH := ui.ToDp(ui.Px(pixelH), 1.0)
|
|
// Send config event to logic goroutine (in DP)
|
|
logic.ConfigChan() <- editor.ConfigEvent{
|
|
Width: dpW,
|
|
Height: dpH,
|
|
}
|
|
case app.FrameEvent:
|
|
gtx := app.NewContext(&ops, e)
|
|
|
|
// Get scale from State — this is updated by ScaleEvent
|
|
newScale := logic.Scale()
|
|
|
|
// Convert pixel dimensions to DP using actual scale
|
|
dpW := ui.ToDp(ui.Px(pixelW), newScale)
|
|
dpH := ui.ToDp(ui.Px(pixelH), newScale)
|
|
|
|
// Send updated config to logic goroutine
|
|
logic.ConfigChan() <- editor.ConfigEvent{
|
|
Width: dpW,
|
|
Height: dpH,
|
|
}
|
|
|
|
// Update renderer with current scale from State
|
|
renderer.SetScale(newScale)
|
|
|
|
// Acquire mutex, read frame, draw, release mutex
|
|
mu.Lock()
|
|
currentElems := elems
|
|
mu.Unlock()
|
|
|
|
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()
|
|
}
|
|
}
|