- Renderer computes word wrap via single LayoutString pass with
WrapHeuristically policy; glyphs drawn inline at FlagLineBreak
- Fixed line height (fontSize * 1.2), independent of glyph metrics
- Two-finger trackpad scroll via gesture.Scroll with vertical axis
- Display line feedback: renderer reports last glyph Y after each
Draw; logic uses it to clamp scroll offset so last line stops
at bottom of viewport with lineHeight/2 padding
- ScrollRange fix: {Min: -(1<<30), Max: 1<<30} ensures scroll
delta is consumed (empty range consumes nothing via clampSplit)
- Line counting fix: only FlagLineBreak increments count; buffer
flushes (32-glyph cap) draw but don't count
80 lines
1.7 KiB
Go
80 lines
1.7 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()))
|
|
logic := editor.NewLogic()
|
|
renderer := ui.New(ui.Theme{FontSize: 14}, shaper, logic.State())
|
|
var mu sync.Mutex
|
|
var elems []ui.Element
|
|
go logic.Run()
|
|
go frameReceiver(w, &mu, &elems, logic.FrameChan())
|
|
|
|
for {
|
|
switch e := w.Event().(type) {
|
|
case app.DestroyEvent:
|
|
return e.Err
|
|
case app.ConfigEvent:
|
|
// ConfigEvent: raw pixel dimensions only.
|
|
logic.ConfigChan() <- editor.ConfigEvent{
|
|
PixelWidth: e.Config.Size.X,
|
|
PixelHeight: e.Config.Size.Y,
|
|
}
|
|
case app.FrameEvent:
|
|
gtx := app.NewContext(&ops, e)
|
|
newScale := gtx.Metric.PxPerDp
|
|
curScale := logic.State().Scale()
|
|
if newScale != curScale {
|
|
logic.ConfigChan() <- editor.ScaleEvent{newScale}
|
|
}
|
|
mu.Lock()
|
|
currentElems := elems
|
|
renderer.Draw(gtx, currentElems)
|
|
logic.DisplayLineChan() <- int(renderer.LastLineY())
|
|
if events := renderer.CheckGestures(e.Source, gtx.Metric); len(events) > 0 {
|
|
logic.InputChan() <- events
|
|
}
|
|
e.Frame(&ops)
|
|
mu.Unlock()
|
|
}
|
|
}
|
|
}
|
|
|
|
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()
|
|
}
|
|
}
|