Compare commits
5
Commits
635655e768
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
11e2dbad38 | ||
|
|
f134a1f21e | ||
|
|
dfc3f618c0 | ||
|
|
cf56e8c47e | ||
|
|
83c9d40500 |
@@ -1,4 +1,7 @@
|
||||
cmd/hello/hello
|
||||
cmd/cal/cal
|
||||
cmd/scroll/scroll
|
||||
cmd/grid/grid
|
||||
cmd/form/form
|
||||
*.apk
|
||||
memprofile*
|
||||
|
||||
+4
-5
@@ -20,7 +20,6 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
sel int32
|
||||
face text.Face
|
||||
)
|
||||
|
||||
@@ -112,8 +111,8 @@ func eventloop() {
|
||||
gio.Color(color.RGBA{A: 0xff, R: 0xf0, G: 0xf0, B: 0xe0}))
|
||||
|
||||
margin := gio.NewInset(gio.Size(ui.Dp(10)))
|
||||
f1 := gio.NewFlex(gio.Axis(layout.Vertical))
|
||||
f2 := gio.NewFlex(gio.Axis(layout.Horizontal))
|
||||
f1 := gio.NewFlex(gio.Vertical)
|
||||
f2 := gio.NewFlex(gio.Horizontal)
|
||||
sidegrid := gio.NewGrid(1)
|
||||
yrs := make([]gio.Widget, 6)
|
||||
curyr := sm.Year
|
||||
@@ -125,7 +124,7 @@ func eventloop() {
|
||||
|
||||
var cal gio.Widget
|
||||
|
||||
f3 := gio.NewFlex(gio.Axis(layout.Vertical))
|
||||
f3 := gio.NewFlex(gio.Vertical)
|
||||
daygrid := gio.NewGrid(7)
|
||||
sun := NewSLabel("Sun", &sm.Ws[time.Sunday], gio.Align(text.End))
|
||||
mon := NewSLabel("Mon", &sm.Ws[time.Monday], gio.Align(text.End))
|
||||
@@ -152,7 +151,7 @@ func eventloop() {
|
||||
case app.DestroyEvent:
|
||||
return
|
||||
case app.UpdateEvent:
|
||||
ctx.Reset(e)
|
||||
ctx.Reset(&e)
|
||||
|
||||
mth.SetText(fmt.Sprintf("%s %d", sm.Month.String(), sm.Year))
|
||||
|
||||
|
||||
@@ -0,0 +1,227 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"git.wow.st/gmp/giowrap"
|
||||
|
||||
"fmt"
|
||||
//"image"
|
||||
"image/color"
|
||||
"log"
|
||||
"os"
|
||||
"runtime"
|
||||
"runtime/pprof"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"gioui.org/ui"
|
||||
"gioui.org/ui/app"
|
||||
"gioui.org/ui/text"
|
||||
|
||||
"golang.org/x/image/font/gofont/goregular"
|
||||
"golang.org/x/image/font/sfnt"
|
||||
)
|
||||
|
||||
var (
|
||||
FPS int = 100
|
||||
frames int64
|
||||
frametime int64 // nanoseconds
|
||||
maxframetime int64
|
||||
mux sync.Mutex
|
||||
Profile bool = true
|
||||
RecordMallocs bool = false
|
||||
)
|
||||
|
||||
func NewButton(face text.Face, t string, c color.RGBA) giowrap.Clickable {
|
||||
lbl := giowrap.NewLabel(t, giowrap.Face(face), giowrap.Align(text.Middle))
|
||||
bg := giowrap.NewBackground(giowrap.Color(c), giowrap.Radius(ui.Dp(4)))
|
||||
ins := giowrap.NewInset(giowrap.Size(ui.Dp(4)))
|
||||
return giowrap.AsClickable(ins(bg(lbl)))
|
||||
}
|
||||
|
||||
func main() {
|
||||
go func() {
|
||||
ms := &runtime.MemStats{}
|
||||
var a1 uint64
|
||||
for {
|
||||
time.Sleep(time.Second * 3)
|
||||
mux.Lock()
|
||||
if frames == 0 {
|
||||
mux.Unlock()
|
||||
continue
|
||||
}
|
||||
runtime.ReadMemStats(ms)
|
||||
a1 = ms.Alloc
|
||||
runtime.GC()
|
||||
runtime.ReadMemStats(ms)
|
||||
log.Printf("Alloc: %d - %d = %d (%d per frame)", a1, ms.Alloc, a1-ms.Alloc, (a1-ms.Alloc)/(3*uint64(FPS)))
|
||||
log.Printf("Frametime: %d max Frametime: %d us\n", frametime/(frames*1000), maxframetime/1000)
|
||||
frames = 0
|
||||
frametime = 0
|
||||
maxframetime = 0
|
||||
mux.Unlock()
|
||||
}
|
||||
}()
|
||||
go main1()
|
||||
app.Main()
|
||||
}
|
||||
|
||||
func main1() {
|
||||
w := app.NewWindow()
|
||||
regular, err := sfnt.Parse(goregular.TTF)
|
||||
if err != nil {
|
||||
log.Fatal("Cannot parse font.")
|
||||
}
|
||||
ctx := giowrap.NewContext(w)
|
||||
t := time.NewTicker(time.Second / time.Duration(FPS))
|
||||
|
||||
graybg := giowrap.NewBackground(
|
||||
giowrap.Color(color.RGBA{A: 0xff, R: 0xd0, G: 0xd0, B: 0xd0}),
|
||||
giowrap.Radius(ui.Dp(4)))
|
||||
whitebg := giowrap.NewBackground(
|
||||
giowrap.Color(color.RGBA{A: 0xff, R: 0xff, G: 0xff, B: 0xff}),
|
||||
giowrap.Radius(ui.Dp(4)))
|
||||
margin := giowrap.NewInset(giowrap.Size(ui.Dp(10)))
|
||||
buffer := giowrap.NewInset(giowrap.Bottom(ui.Dp(10)))
|
||||
f1 := giowrap.NewFlex(giowrap.Vertical)
|
||||
row := giowrap.NewFlex(giowrap.Horizontal)
|
||||
|
||||
more := NewButton(ctx.Faces.For(regular, ui.Sp(24)),
|
||||
"more", color.RGBA{A: 0xff, R: 0x3c, G: 0x98, B: 0xc6})
|
||||
submit := NewButton(ctx.Faces.For(regular, ui.Sp(24)),
|
||||
"submit", color.RGBA{A: 0xff, R: 0x3c, G: 0x98, B: 0xc6})
|
||||
back := NewButton(ctx.Faces.For(regular, ui.Sp(24)),
|
||||
"back", color.RGBA{A: 0xff, R: 0x3c, G: 0x98, B: 0xc6})
|
||||
|
||||
numfields := 5
|
||||
eds := make([]*giowrap.Editor,0)
|
||||
btns := make([]giowrap.Clickable,0)
|
||||
entryrows := make([]giowrap.Widget,0)
|
||||
submitrows := make([]giowrap.Widget,0)
|
||||
|
||||
var entryPage giowrap.Widget
|
||||
var submitPage giowrap.Widget
|
||||
|
||||
newRow := func(i int) {
|
||||
eds = append(eds, giowrap.NewEditor(fmt.Sprintf("text %d",i), giowrap.Face(ctx.Faces.For(regular, ui.Sp(24)))))
|
||||
btns = append(btns, NewButton(ctx.Faces.For(regular, ui.Sp(18)), "X",
|
||||
color.RGBA{A: 0xff, R: 0x80, G: 0x80, B: 0x80}))
|
||||
}
|
||||
|
||||
for i := 0; i < numfields; i++ {
|
||||
newRow(i)
|
||||
}
|
||||
updatePages := func() {
|
||||
entryrows = entryrows[0:0]
|
||||
entryrows = append(entryrows, buffer(row(more, submit)))
|
||||
for i := 0; i < len(btns); i++ {
|
||||
entryrows = append(entryrows, row(btns[i], giowrap.FillWidth(whitebg(eds[i]))))
|
||||
}
|
||||
entryPage = graybg(margin(giowrap.VScroll(f1)(entryrows...)))
|
||||
|
||||
submitrows = submitrows[0:0]
|
||||
submitrows = append(submitrows, buffer(row(back)))
|
||||
for i := 0; i < len(btns); i++ {
|
||||
submitrows = append(submitrows, giowrap.NewLabel(eds[i].Text(), giowrap.Face(ctx.Faces.For(regular, ui.Sp(24)))))
|
||||
}
|
||||
submitPage = graybg(margin(giowrap.VScroll(f1)(submitrows...)))
|
||||
}
|
||||
updatePages()
|
||||
|
||||
page := &entryPage
|
||||
|
||||
profiled := false
|
||||
startTime := time.Now()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-t.C:
|
||||
w.Invalidate()
|
||||
case e := <-w.Events():
|
||||
mux.Lock()
|
||||
stime := time.Now()
|
||||
switch e := e.(type) {
|
||||
case app.DestroyEvent:
|
||||
return
|
||||
case app.UpdateEvent:
|
||||
profileNow := false
|
||||
var ms runtime.MemStats
|
||||
if Profile && time.Since(startTime) > time.Second*2 {
|
||||
startTime = time.Now()
|
||||
profileNow = true
|
||||
}
|
||||
if profileNow {
|
||||
if !profiled {
|
||||
prof(0)
|
||||
}
|
||||
runtime.ReadMemStats(&ms)
|
||||
if RecordMallocs {
|
||||
giowrap.RecordMallocs()
|
||||
}
|
||||
}
|
||||
ctx.Reset(&e)
|
||||
giowrap.Mallocs("pre-layout")
|
||||
(*page).Layout(ctx)
|
||||
giowrap.Mallocs("post-layout")
|
||||
ctx.Update()
|
||||
for i, w := range btns {
|
||||
if w.Clicked(ctx) {
|
||||
log.Printf("%d clicked\n",i)
|
||||
btns = append(btns[:i], btns[i+1:]...)
|
||||
eds = append(eds[:i], eds[i+1:]...)
|
||||
entryrows = append(entryrows[:i], entryrows[i+1:]...)
|
||||
numfields--
|
||||
updatePages()
|
||||
}
|
||||
}
|
||||
if more.Clicked(ctx) {
|
||||
log.Print("More")
|
||||
newRow(len(btns))
|
||||
updatePages()
|
||||
}
|
||||
if submit.Clicked(ctx) {
|
||||
log.Print("Submit")
|
||||
updatePages()
|
||||
page = &submitPage
|
||||
}
|
||||
if back.Clicked(ctx) {
|
||||
page = &entryPage
|
||||
}
|
||||
if profileNow {
|
||||
for _, i := range giowrap.AllMallocs {
|
||||
fmt.Printf("mallocs: %d (%s)\n", i.Value, i.Text)
|
||||
}
|
||||
giowrap.AllMallocs = nil
|
||||
mallocs := ms.Mallocs
|
||||
runtime.ReadMemStats(&ms)
|
||||
mallocs = ms.Mallocs - mallocs
|
||||
log.Printf("Mallocs = %d\n", mallocs)
|
||||
if !profiled {
|
||||
prof(1)
|
||||
profiled = true
|
||||
}
|
||||
}
|
||||
}
|
||||
dur := time.Since(stime).Nanoseconds()
|
||||
frames = frames + 1
|
||||
frametime = frametime + dur
|
||||
if dur > maxframetime {
|
||||
maxframetime = dur
|
||||
}
|
||||
mux.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func prof(x int) {
|
||||
names := []string{"memprofile-1.pprof", "memprofile-2.pprof"}
|
||||
f1, err := os.Create(names[x])
|
||||
if err != nil {
|
||||
log.Fatal("could not create memory profile: ", err)
|
||||
}
|
||||
if err := pprof.WriteHeapProfile(f1); err != nil {
|
||||
log.Fatal("could not write memory profile: ", err)
|
||||
}
|
||||
f1.Close()
|
||||
log.Print("Memory profile written")
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ func main() {
|
||||
app.Main()
|
||||
}
|
||||
|
||||
func layoutRect(c ui.Config, ops *ui.Ops, cs layout.Constraints) layout.Dimens {
|
||||
func layoutRect(c ui.Config, ops *ui.Ops, cs layout.Constraints) layout.Dimensions {
|
||||
ins := layout.UniformInset(ui.Px(10))
|
||||
cs = ins.Begin(c, ops, cs)
|
||||
paint.PaintOp{Rect: f32.Rectangle{
|
||||
@@ -52,7 +52,7 @@ func layoutRect(c ui.Config, ops *ui.Ops, cs layout.Constraints) layout.Dimens {
|
||||
Y: float32(cs.Width.Max),
|
||||
},
|
||||
}}.Add(ops)
|
||||
dims := layout.Dimens{
|
||||
dims := layout.Dimensions{
|
||||
Size: image.Point{
|
||||
X: cs.Width.Max,
|
||||
Y: cs.Width.Max,
|
||||
+303
-56
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"git.wow.st/gmp/giowrap"
|
||||
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"log"
|
||||
@@ -32,7 +33,8 @@ var (
|
||||
frametime int64 // nanoseconds
|
||||
maxframetime int64
|
||||
mux sync.Mutex
|
||||
Profile bool = false
|
||||
Profile bool = true
|
||||
RecordMallocs bool = false
|
||||
)
|
||||
|
||||
func NewButton(face text.Face, t string, c color.RGBA) giowrap.Clickable {
|
||||
@@ -69,7 +71,7 @@ func main() {
|
||||
fallthrough
|
||||
default:
|
||||
log.Fatal(`Usage:
|
||||
hello [main1|main2]
|
||||
hello [main1|main2|main3|main4]
|
||||
`)
|
||||
case os.Args[1] == "main1":
|
||||
log.Print("main1()")
|
||||
@@ -77,10 +79,17 @@ hello [main1|main2]
|
||||
case os.Args[1] == "main2":
|
||||
log.Print("main2()")
|
||||
go main2()
|
||||
case os.Args[1] == "main3":
|
||||
log.Print("main3()")
|
||||
go main3()
|
||||
case os.Args[1] == "main4":
|
||||
log.Print("main4()")
|
||||
go main4()
|
||||
}
|
||||
app.Main()
|
||||
}
|
||||
|
||||
// Example with functional layout
|
||||
func main1() {
|
||||
w := app.NewWindow()
|
||||
regular, err := sfnt.Parse(goregular.TTF)
|
||||
@@ -94,11 +103,233 @@ func main1() {
|
||||
|
||||
e2 := giowrap.NewEditor("text 2", giowrap.Face(ctx.Faces.For(regular, ui.Sp(24))))
|
||||
|
||||
f1 := giowrap.NewFlex(giowrap.Axis(layout.Vertical))
|
||||
f1 := giowrap.NewFlex(giowrap.Vertical)
|
||||
OuterInset := giowrap.NewInset(giowrap.Size(ui.Dp(10)))
|
||||
InnerInset := giowrap.NewInset(giowrap.Size(ui.Dp(10)))
|
||||
|
||||
f2 := giowrap.NewFlex(giowrap.Axis(layout.Horizontal))
|
||||
f2 := giowrap.NewFlex(giowrap.Horizontal)
|
||||
|
||||
btn1 := NewButton(ctx.Faces.For(regular, ui.Sp(24)),
|
||||
"push1", color.RGBA{A: 0xff, R: 0x3c, G: 0x98, B: 0xc6})
|
||||
btn2 := NewButton(ctx.Faces.For(regular, ui.Sp(24)),
|
||||
"push2", color.RGBA{A: 0xff, R: 0x3c, G: 0x98, B: 0xc6})
|
||||
|
||||
profiled := false
|
||||
startTime := time.Now()
|
||||
|
||||
page := OuterInset(
|
||||
f1(
|
||||
e1,
|
||||
giowrap.Flexible(0.33),
|
||||
InnerInset(e2),
|
||||
giowrap.Flexible(0.67),
|
||||
f2(
|
||||
InnerInset(btn1),
|
||||
giowrap.Flexible(0.50),
|
||||
InnerInset(btn2),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-t.C:
|
||||
w.Invalidate()
|
||||
case e := <-w.Events():
|
||||
mux.Lock()
|
||||
stime := time.Now()
|
||||
switch e := e.(type) {
|
||||
case app.DestroyEvent:
|
||||
return
|
||||
case app.UpdateEvent:
|
||||
profileNow := false
|
||||
var ms runtime.MemStats
|
||||
if Profile && time.Since(startTime) > time.Second*2 {
|
||||
startTime = time.Now()
|
||||
profileNow = true
|
||||
}
|
||||
if profileNow {
|
||||
if !profiled {
|
||||
prof(0)
|
||||
}
|
||||
runtime.ReadMemStats(&ms)
|
||||
if RecordMallocs {
|
||||
giowrap.RecordMallocs()
|
||||
}
|
||||
}
|
||||
giowrap.Mallocs("pre-reset")
|
||||
ctx.Reset(&e)
|
||||
giowrap.Mallocs("pre-layout")
|
||||
page.Layout(ctx)
|
||||
giowrap.Mallocs("post-layout")
|
||||
ctx.Update()
|
||||
giowrap.Mallocs("post-update")
|
||||
if btn1.Clicked(ctx) {
|
||||
log.Print("Clicked: " + e1.Text())
|
||||
}
|
||||
if btn2.Clicked(ctx) {
|
||||
log.Print("Clicked: " + e2.Text())
|
||||
}
|
||||
giowrap.Mallocs("post-clicked()")
|
||||
if profileNow {
|
||||
for _, i := range giowrap.AllMallocs {
|
||||
fmt.Printf("mallocs: %d (%s)\n", i.Value, i.Text)
|
||||
}
|
||||
giowrap.AllMallocs = nil
|
||||
mallocs := ms.Mallocs
|
||||
runtime.ReadMemStats(&ms)
|
||||
mallocs = ms.Mallocs - mallocs
|
||||
log.Printf("Mallocs = %d\n", mallocs)
|
||||
if !profiled {
|
||||
prof(1)
|
||||
profiled = true
|
||||
}
|
||||
}
|
||||
}
|
||||
dur := time.Since(stime).Nanoseconds()
|
||||
frames = frames + 1
|
||||
frametime = frametime + dur
|
||||
if dur > maxframetime {
|
||||
maxframetime = dur
|
||||
}
|
||||
mux.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func prof(x int) {
|
||||
names := []string{"memprofile-1.pprof", "memprofile-2.pprof"}
|
||||
f1, err := os.Create(names[x])
|
||||
if err != nil {
|
||||
log.Fatal("could not create memory profile: ", err)
|
||||
}
|
||||
if err := pprof.WriteHeapProfile(f1); err != nil {
|
||||
log.Fatal("could not write memory profile: ", err)
|
||||
}
|
||||
f1.Close()
|
||||
log.Print("Memory profile written")
|
||||
}
|
||||
|
||||
// Example showing use of WidgetCombinators in the main loop.
|
||||
func main2() {
|
||||
w := app.NewWindow()
|
||||
regular, err := sfnt.Parse(goregular.TTF)
|
||||
if err != nil {
|
||||
log.Fatal("Cannot parse font.")
|
||||
}
|
||||
ctx := giowrap.NewContext(w)
|
||||
t := time.NewTicker(time.Second / time.Duration(FPS))
|
||||
e1 := giowrap.NewEditor("text 1", giowrap.Face(ctx.Faces.For(regular, ui.Sp(24))))
|
||||
e1.Focus()
|
||||
|
||||
e2 := giowrap.NewEditor("text 2", giowrap.Face(ctx.Faces.For(regular, ui.Sp(24))))
|
||||
|
||||
f1 := giowrap.NewFlex(giowrap.Vertical)
|
||||
OuterInset := giowrap.NewInset(giowrap.Size(ui.Dp(10)))
|
||||
InnerInset := giowrap.NewInset(giowrap.Size(ui.Dp(10)))
|
||||
|
||||
f2 := giowrap.NewFlex(giowrap.Horizontal)
|
||||
|
||||
btn1 := NewButton(ctx.Faces.For(regular, ui.Sp(24)),
|
||||
"push1", color.RGBA{A: 0xff, R: 0x3c, G: 0x98, B: 0xc6})
|
||||
btn2 := NewButton(ctx.Faces.For(regular, ui.Sp(24)),
|
||||
"push2", color.RGBA{A: 0xff, R: 0x3c, G: 0x98, B: 0xc6})
|
||||
|
||||
profiled := false
|
||||
startTime := time.Now()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-t.C:
|
||||
w.Invalidate()
|
||||
case e := <-w.Events():
|
||||
mux.Lock()
|
||||
stime := time.Now()
|
||||
switch e := e.(type) {
|
||||
case app.DestroyEvent:
|
||||
return
|
||||
case app.UpdateEvent:
|
||||
profileNow := false
|
||||
var ms runtime.MemStats
|
||||
if Profile && time.Since(startTime) > time.Second*2 {
|
||||
startTime = time.Now()
|
||||
profileNow = true
|
||||
}
|
||||
if profileNow {
|
||||
if !profiled {
|
||||
prof(0)
|
||||
}
|
||||
runtime.ReadMemStats(&ms)
|
||||
if RecordMallocs {
|
||||
giowrap.RecordMallocs()
|
||||
}
|
||||
}
|
||||
giowrap.Mallocs("pre-reset")
|
||||
ctx.Reset(&e)
|
||||
giowrap.Mallocs("pre-layout")
|
||||
OuterInset(
|
||||
f1(
|
||||
e1,
|
||||
giowrap.Flexible(0.33),
|
||||
InnerInset(e2),
|
||||
giowrap.Flexible(0.67),
|
||||
f2(
|
||||
InnerInset(btn1),
|
||||
giowrap.Flexible(0.50),
|
||||
InnerInset(btn2),
|
||||
),
|
||||
),
|
||||
).Layout(ctx)
|
||||
giowrap.Mallocs("post-layout")
|
||||
ctx.Update()
|
||||
giowrap.Mallocs("post-update")
|
||||
if btn1.Clicked(ctx) {
|
||||
log.Print("Clicked: " + e1.Text())
|
||||
}
|
||||
if btn2.Clicked(ctx) {
|
||||
log.Print("Clicked: " + e2.Text())
|
||||
}
|
||||
giowrap.Mallocs("post-clicked()")
|
||||
if profileNow {
|
||||
for _, i := range giowrap.AllMallocs {
|
||||
fmt.Printf("mallocs: %d (%s)\n", i.Value, i.Text)
|
||||
}
|
||||
giowrap.AllMallocs = nil
|
||||
mallocs := ms.Mallocs
|
||||
runtime.ReadMemStats(&ms)
|
||||
mallocs = ms.Mallocs - mallocs
|
||||
log.Printf("Mallocs = %d\n", mallocs)
|
||||
if !profiled {
|
||||
prof(1)
|
||||
profiled = true
|
||||
}
|
||||
}
|
||||
}
|
||||
dur := time.Since(stime).Nanoseconds()
|
||||
frames = frames + 1
|
||||
frametime = frametime + dur
|
||||
if dur > maxframetime {
|
||||
maxframetime = dur
|
||||
}
|
||||
mux.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Example showing widget allocation within the main loop (causes unnecessary
|
||||
// memory allocations)
|
||||
func main3() {
|
||||
w := app.NewWindow()
|
||||
regular, err := sfnt.Parse(goregular.TTF)
|
||||
if err != nil {
|
||||
log.Fatal("Cannot parse font.")
|
||||
}
|
||||
ctx := giowrap.NewContext(w)
|
||||
t := time.NewTicker(time.Second / time.Duration(FPS))
|
||||
e1 := giowrap.NewEditor("text 1", giowrap.Face(ctx.Faces.For(regular, ui.Sp(24))))
|
||||
e1.Focus()
|
||||
|
||||
e2 := giowrap.NewEditor("text 2", giowrap.Face(ctx.Faces.For(regular, ui.Sp(24))))
|
||||
|
||||
btn1 := NewButton(ctx.Faces.For(regular, ui.Sp(24)),
|
||||
"push1", color.RGBA{A: 0xff, R: 0x3c, G: 0x98, B: 0xc6})
|
||||
@@ -118,17 +349,32 @@ func main1() {
|
||||
case app.DestroyEvent:
|
||||
return
|
||||
case app.UpdateEvent:
|
||||
ctx.Reset(e)
|
||||
OuterInset(
|
||||
f1(
|
||||
profileNow := false
|
||||
var ms runtime.MemStats
|
||||
if Profile && time.Since(startTime) > time.Second*2 {
|
||||
profileNow = true
|
||||
startTime = time.Now()
|
||||
}
|
||||
if profileNow {
|
||||
if !profiled {
|
||||
prof(0)
|
||||
}
|
||||
runtime.ReadMemStats(&ms)
|
||||
if RecordMallocs {
|
||||
giowrap.RecordMallocs()
|
||||
}
|
||||
}
|
||||
ctx.Reset(&e)
|
||||
giowrap.NewInset(giowrap.Size(ui.Dp(10)))(
|
||||
giowrap.NewFlex(giowrap.Vertical)(
|
||||
e1,
|
||||
giowrap.Flexible(0.33),
|
||||
InnerInset(e2),
|
||||
giowrap.Flexible(0.67),
|
||||
f2(
|
||||
InnerInset(btn1),
|
||||
giowrap.Flexible(0.5),
|
||||
InnerInset(btn2),
|
||||
//giowrap.Flexible(0.33),
|
||||
giowrap.NewInset(giowrap.Size(ui.Dp(10)))(e2),
|
||||
//giowrap.Flexible(0.67),
|
||||
giowrap.NewFlex(giowrap.Horizontal)(
|
||||
giowrap.NewInset(giowrap.Size(ui.Dp(10)))(btn1),
|
||||
//giowrap.Flexible(0.50),
|
||||
giowrap.NewInset(giowrap.Size(ui.Dp(10)))(btn2),
|
||||
),
|
||||
)).Layout(ctx)
|
||||
ctx.Update()
|
||||
@@ -138,6 +384,20 @@ func main1() {
|
||||
if btn2.Clicked(ctx) {
|
||||
log.Print("Clicked: " + e2.Text())
|
||||
}
|
||||
if profileNow {
|
||||
for _, i := range giowrap.AllMallocs {
|
||||
fmt.Printf("mallocs: %d (%s)\n", i.Value, i.Text)
|
||||
}
|
||||
giowrap.AllMallocs = nil
|
||||
mallocs := ms.Mallocs
|
||||
runtime.ReadMemStats(&ms)
|
||||
mallocs = ms.Mallocs - mallocs
|
||||
log.Printf("Mallocs = %d\n", mallocs)
|
||||
if !profiled {
|
||||
prof(1)
|
||||
profiled = true
|
||||
}
|
||||
}
|
||||
}
|
||||
dur := time.Since(stime).Nanoseconds()
|
||||
mux.Lock()
|
||||
@@ -148,28 +408,11 @@ func main1() {
|
||||
}
|
||||
mux.Unlock()
|
||||
}
|
||||
if profiled {
|
||||
continue
|
||||
}
|
||||
if time.Since(startTime) < time.Second*10 {
|
||||
continue
|
||||
}
|
||||
if Profile {
|
||||
profiled = true
|
||||
f, err := os.Create("memprofile.pprof")
|
||||
if err != nil {
|
||||
log.Fatal("could not create memory profile: ", err)
|
||||
}
|
||||
if err := pprof.WriteHeapProfile(f); err != nil {
|
||||
log.Fatal("could not write memory profile: ", err)
|
||||
}
|
||||
log.Print("Memory profile written")
|
||||
f.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main2() {
|
||||
// Example using the base Gio API.
|
||||
func main4() {
|
||||
w := app.NewWindow()
|
||||
q := w.Queue()
|
||||
ops := new(ui.Ops)
|
||||
@@ -215,6 +458,18 @@ func main2() {
|
||||
case app.DestroyEvent:
|
||||
return
|
||||
case app.UpdateEvent:
|
||||
profileNow := false
|
||||
var ms runtime.MemStats
|
||||
if Profile && time.Since(startTime) > time.Second*2 {
|
||||
profileNow = true
|
||||
startTime = time.Now()
|
||||
}
|
||||
if profileNow {
|
||||
if !profiled {
|
||||
prof(0)
|
||||
}
|
||||
runtime.ReadMemStats(&ms)
|
||||
}
|
||||
c := &e.Config
|
||||
ops.Reset()
|
||||
faces.Reset(c)
|
||||
@@ -229,7 +484,7 @@ func main2() {
|
||||
}
|
||||
}
|
||||
|
||||
var dims layout.Dimens
|
||||
var dims layout.Dimensions
|
||||
cs := layout.RigidConstraints(e.Size)
|
||||
{
|
||||
f1 := layout.Flex{Axis: layout.Vertical}
|
||||
@@ -257,6 +512,16 @@ func main2() {
|
||||
dims = ins.End(f1.Layout(c1, c2, c3))
|
||||
}
|
||||
w.Update(ops)
|
||||
if profileNow {
|
||||
mallocs := ms.Mallocs
|
||||
runtime.ReadMemStats(&ms)
|
||||
mallocs = ms.Mallocs - mallocs
|
||||
log.Printf("Mallocs = %d\n", mallocs)
|
||||
if !profiled {
|
||||
prof(1)
|
||||
profiled = true
|
||||
}
|
||||
}
|
||||
}
|
||||
dur := time.Since(stime).Nanoseconds()
|
||||
mux.Lock()
|
||||
@@ -267,35 +532,17 @@ func main2() {
|
||||
}
|
||||
mux.Unlock()
|
||||
}
|
||||
if profiled {
|
||||
continue
|
||||
}
|
||||
if time.Since(startTime) < time.Second*10 {
|
||||
continue
|
||||
}
|
||||
if Profile {
|
||||
profiled = true
|
||||
f, err := os.Create("memprofile.pprof")
|
||||
if err != nil {
|
||||
log.Fatal("could not create memory profile: ", err)
|
||||
}
|
||||
if err := pprof.WriteHeapProfile(f); err != nil {
|
||||
log.Fatal("could not write memory profile: ", err)
|
||||
}
|
||||
log.Print("Memory profile written")
|
||||
f.Close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func layoutRRect(c ui.Config, ops *ui.Ops, cs layout.Constraints) layout.Dimens {
|
||||
func layoutRRect(c ui.Config, ops *ui.Ops, cs layout.Constraints) layout.Dimensions {
|
||||
r := float32(c.Px(ui.Dp(4)))
|
||||
sz := image.Point{X: cs.Width.Min, Y: cs.Height.Min}
|
||||
w, h := float32(sz.X), float32(sz.Y)
|
||||
giowrap.Rrect(ops, w, h, r, r, r, r)
|
||||
paint.ColorOp{Color: color.RGBA{A: 0xff, R: 0x3c, G: 0x98, B: 0xc6}}.Add(ops)
|
||||
paint.PaintOp{Rect: f32.Rectangle{Max: f32.Point{X: w, Y: h}}}.Add(ops)
|
||||
return layout.Dimens{Size: sz}
|
||||
return layout.Dimensions{Size: sz}
|
||||
}
|
||||
|
||||
type Button struct {
|
||||
@@ -304,10 +551,10 @@ type Button struct {
|
||||
Click gesture.Click
|
||||
}
|
||||
|
||||
func (b *Button) Layout(c ui.Config, ops *ui.Ops, cs layout.Constraints) layout.Dimens {
|
||||
func (b *Button) Layout(c ui.Config, ops *ui.Ops, cs layout.Constraints) layout.Dimensions {
|
||||
ins := layout.UniformInset(ui.Dp(10))
|
||||
cs = ins.Begin(c, ops, cs)
|
||||
var dims layout.Dimens
|
||||
var dims layout.Dimensions
|
||||
st := layout.Stack{}
|
||||
st.Init(ops, cs)
|
||||
{
|
||||
|
||||
+58
-33
@@ -4,6 +4,8 @@ import (
|
||||
"fmt"
|
||||
"image/color"
|
||||
"log"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
gio "git.wow.st/gmp/giowrap"
|
||||
|
||||
@@ -95,22 +97,25 @@ func eventloop() {
|
||||
bg := gio.NewBackground(gio.Color(gray2))
|
||||
|
||||
margin := gio.NewInset(gio.Size(ui.Dp(10)))
|
||||
f1 := gio.NewFlex(gio.Axis(layout.Horizontal))
|
||||
f1 := gio.NewFlex(gio.Horizontal)
|
||||
lbar := gio.NewLabel(" ", gio.Face(face))
|
||||
f2 := gio.NewFlex(gio.Axis(layout.Vertical))
|
||||
f2 := gio.NewFlex(gio.Vertical)
|
||||
topbar := gio.NewLabel("Scroll X and Y. Click to select", gio.Face(face))
|
||||
f3 := gio.NewFlex(gio.Horizontal)
|
||||
f4 := gio.NewFlex(gio.Vertical)
|
||||
|
||||
sh := gio.HScroll(gio.NewFlex(gio.Axis(layout.Horizontal)))
|
||||
sv := gio.VScroll(gio.NewFlex(gio.Axis(layout.Vertical)))
|
||||
sh := gio.HScroll(gio.NewFlex(gio.Horizontal))
|
||||
sv := gio.VScroll(gio.NewFlex(gio.Vertical))
|
||||
|
||||
numlabs := 50
|
||||
labs := make([][]gio.Widget, numlabs)
|
||||
sels := make([][]bool, numlabs)
|
||||
for i := 0; i < 16; i++ {
|
||||
labs[i] = make([]gio.Widget, numlabs)
|
||||
sels[i] = make([]bool, numlabs)
|
||||
for j := 0; j < numlabs; j++ {
|
||||
labs[i][j] = NewSLabel(fmt.Sprintf("%03d", i*16+j), &sels[i][j])
|
||||
numrows := 16
|
||||
numcols := 50
|
||||
labs := make([][]gio.Widget, numrows)
|
||||
sels := make([][]bool, numrows)
|
||||
for i := 0; i < numrows; i++ {
|
||||
labs[i] = make([]gio.Widget, numcols)
|
||||
sels[i] = make([]bool, numcols)
|
||||
for j := 0; j < numcols; j++ {
|
||||
labs[i][j] = NewSLabel(fmt.Sprintf("%03d", i+j*numrows), &sels[i][j])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,8 +125,30 @@ func eventloop() {
|
||||
gio.Left(x.Left), gio.Right(x.Right))
|
||||
}
|
||||
|
||||
page := sysbg(sysinset(bg(margin(
|
||||
f1(lbar, f2(topbar, sh(sv(f3(
|
||||
f4(labs[0]...),
|
||||
f4(labs[1]...),
|
||||
f4(labs[2]...),
|
||||
f4(labs[3]...),
|
||||
f4(labs[4]...),
|
||||
f4(labs[5]...),
|
||||
f4(labs[6]...),
|
||||
f4(labs[7]...),
|
||||
f4(labs[8]...),
|
||||
f4(labs[9]...),
|
||||
f4(labs[10]...),
|
||||
f4(labs[11]...),
|
||||
f4(labs[12]...),
|
||||
f4(labs[13]...),
|
||||
f4(labs[14]...),
|
||||
f4(labs[15]...),
|
||||
)))))))))
|
||||
|
||||
var oldInsets app.Insets
|
||||
|
||||
stime := time.Now()
|
||||
sm := runtime.MemStats{}
|
||||
for {
|
||||
select {
|
||||
case e := <-w.Events():
|
||||
@@ -129,32 +156,30 @@ func eventloop() {
|
||||
case app.DestroyEvent:
|
||||
return
|
||||
case app.UpdateEvent:
|
||||
ctx.Reset(e)
|
||||
profileNow := time.Since(stime) > time.Second*2
|
||||
if profileNow {
|
||||
stime = time.Now()
|
||||
//gio.RecordMallocs()
|
||||
runtime.ReadMemStats(&sm)
|
||||
}
|
||||
gio.Mallocs("start")
|
||||
ctx.Reset(&e)
|
||||
if diffInsets(e.Insets, oldInsets) {
|
||||
oldInsets = e.Insets
|
||||
resetSysinset(e.Insets)
|
||||
}
|
||||
|
||||
sysbg(sysinset(bg(margin(
|
||||
f1(lbar, f2(topbar, sh(
|
||||
sv(labs[0]...),
|
||||
sv(labs[1]...),
|
||||
sv(labs[2]...),
|
||||
sv(labs[3]...),
|
||||
sv(labs[4]...),
|
||||
sv(labs[5]...),
|
||||
sv(labs[6]...),
|
||||
sv(labs[7]...),
|
||||
sv(labs[8]...),
|
||||
sv(labs[9]...),
|
||||
sv(labs[10]...),
|
||||
sv(labs[11]...),
|
||||
sv(labs[12]...),
|
||||
sv(labs[13]...),
|
||||
sv(labs[14]...),
|
||||
sv(labs[15]...),
|
||||
))))))).Layout(ctx)
|
||||
page.Layout(ctx)
|
||||
ctx.Update()
|
||||
gio.Mallocs("end")
|
||||
if profileNow {
|
||||
for _, m := range gio.AllMallocs {
|
||||
fmt.Printf("Mallocs: %d (%s)\n", m.Value, m.Text)
|
||||
}
|
||||
gio.AllMallocs = nil
|
||||
oldMallocs := sm.Mallocs
|
||||
runtime.ReadMemStats(&sm)
|
||||
fmt.Printf("Mallocs = %d\n", sm.Mallocs-oldMallocs)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
{
|
||||
ctx.Reset(&e) // ctx keeps a pointer to e.Config, so we can do...
|
||||
ctx.RigidConstraints()
|
||||
{
|
||||
f1 := layout.Flex{Axis: layout.Vertical} // maybe pass ctx in here?
|
||||
ins := layout.UniformInset(ui.Dp(10))
|
||||
ins.Begin(ctx) // containers like Insets and Flex keep a pointer to ctx
|
||||
f1.Init(ctx)
|
||||
f1.Rigid()
|
||||
editor1.Layout(ctx)
|
||||
f1.End()
|
||||
{
|
||||
f1.Flexible(0.33)
|
||||
ins := layout.UniformInset(ui.Dp(10))
|
||||
ins.Begin(ctx)
|
||||
editor2.Layout(ctx)
|
||||
ins.End()
|
||||
}
|
||||
f1.End()
|
||||
{
|
||||
f1.Flexible(0.67)
|
||||
f2 := layout.Flex{Axis: layout.Horizontal}
|
||||
f2.Init(ctx)
|
||||
f2.Rigid()
|
||||
button1.Layout(ctx)
|
||||
c1 := f2.End()
|
||||
f2.Flexible(0.5)
|
||||
button2.Layout(ctx)
|
||||
c2 := f2.End()
|
||||
f2.Layout(c1, c2)
|
||||
}
|
||||
c3 := f1.End()
|
||||
f1.Layout(c1, c2, c3)
|
||||
ins.End()
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ type Grid struct {
|
||||
}
|
||||
|
||||
type GridChild struct {
|
||||
dims layout.Dimens
|
||||
dims layout.Dimensions
|
||||
macro ui.MacroOp
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ func (g *Grid) Begin() {
|
||||
g.macro.Record(g.ops)
|
||||
}
|
||||
|
||||
func (g *Grid) End(dims layout.Dimens) GridChild {
|
||||
func (g *Grid) End(dims layout.Dimensions) GridChild {
|
||||
if g.mode != modeBegun {
|
||||
panic("Must call Grid.Begin() before adding children.")
|
||||
}
|
||||
@@ -65,7 +65,7 @@ func (g *Grid) End(dims layout.Dimens) GridChild {
|
||||
return GridChild{dims: dims, macro: g.macro}
|
||||
}
|
||||
|
||||
func (g *Grid) Layout(cs ...GridChild) layout.Dimens {
|
||||
func (g *Grid) Layout(cs ...GridChild) layout.Dimensions {
|
||||
rowheight := 0
|
||||
height := 0
|
||||
var width float32
|
||||
@@ -110,7 +110,7 @@ func (g *Grid) Layout(cs ...GridChild) layout.Dimens {
|
||||
} else {
|
||||
dwidth = g.cs.Width.Max
|
||||
}
|
||||
return layout.Dimens{Size: image.Point{dwidth, height}}
|
||||
return layout.Dimensions{Size: image.Point{dwidth, height}}
|
||||
}
|
||||
|
||||
func toPointF(p image.Point) f32.Point {
|
||||
@@ -124,24 +124,34 @@ type HeightOpt struct{ height int }
|
||||
func Height(x int) HeightOpt { return HeightOpt{x} }
|
||||
func (x HeightOpt) DoGridOption(g *Grid) { g.Height = x.height }
|
||||
|
||||
type gridWidget struct {
|
||||
g Grid
|
||||
gcs []GridChild
|
||||
ws []Widget
|
||||
}
|
||||
|
||||
func NewGrid(cols int, gops ...GridOption) WidgetCombinator {
|
||||
g := &Grid{Cols: cols}
|
||||
ret := &gridWidget{}
|
||||
ret.g = Grid{Cols: cols}
|
||||
for _, gop := range gops {
|
||||
gop.DoGridOption(g)
|
||||
gop.DoGridOption(&ret.g)
|
||||
}
|
||||
gcs := make([]GridChild, 0)
|
||||
ret.gcs = make([]GridChild, 0)
|
||||
return func(ws ...Widget) Widget {
|
||||
return NewfWidget(func(ctx *Context) {
|
||||
cs := g.Init(ctx.ops, ctx.cs)
|
||||
ctx.cs = cs
|
||||
for _, w := range ws {
|
||||
g.Begin()
|
||||
w.Layout(ctx)
|
||||
ctx.cs = cs // widget layout can modify constraints...
|
||||
gcs = append(gcs, g.End(ctx.dims))
|
||||
}
|
||||
ctx.dims = g.Layout(gcs...)
|
||||
gcs = gcs[0:0]
|
||||
})
|
||||
ret.ws = ws
|
||||
return ret
|
||||
}
|
||||
}
|
||||
|
||||
func (gw *gridWidget) Layout(ctx *Context) {
|
||||
cs := gw.g.Init(ctx.ops, ctx.cs)
|
||||
ctx.cs = cs
|
||||
for _, w := range gw.ws {
|
||||
gw.g.Begin()
|
||||
w.Layout(ctx)
|
||||
ctx.cs = cs // widget layout can modify constraints...
|
||||
gw.gcs = append(gw.gcs, gw.g.End(ctx.dims))
|
||||
}
|
||||
ctx.dims = gw.g.Layout(gw.gcs...)
|
||||
gw.gcs = gw.gcs[0:0]
|
||||
}
|
||||
|
||||
@@ -47,7 +47,8 @@ type Context struct {
|
||||
q input.Queue
|
||||
ops *ui.Ops
|
||||
cs layout.Constraints
|
||||
dims layout.Dimens
|
||||
dims layout.Dimensions
|
||||
container Widget
|
||||
}
|
||||
|
||||
func NewContext(w *app.Window) *Context {
|
||||
@@ -58,11 +59,13 @@ func NewContext(w *app.Window) *Context {
|
||||
}
|
||||
}
|
||||
|
||||
func (ctx *Context) Reset(e app.UpdateEvent) {
|
||||
func (ctx *Context) Reset(e *app.UpdateEvent) {
|
||||
Mallocs("ctx.Reset() ctx.c")
|
||||
ctx.c = &e.Config
|
||||
ctx.ops.Reset()
|
||||
ctx.cs = layout.RigidConstraints(e.Size)
|
||||
ctx.Faces.Reset(ctx.c)
|
||||
Mallocs("ctx.Reset() done")
|
||||
}
|
||||
|
||||
func (ctx *Context) Update() {
|
||||
@@ -121,6 +124,7 @@ func NewLabel(t string, lops ...LabelOption) *Label {
|
||||
}
|
||||
|
||||
func (l *Label) Layout(ctx *Context) {
|
||||
Mallocs("Label.Layout()")
|
||||
ctx.dims = l.l.Layout(ctx.ops, ctx.cs)
|
||||
}
|
||||
|
||||
@@ -157,6 +161,7 @@ func NewEditor(t string, eops ...EditorOption) *Editor {
|
||||
}
|
||||
|
||||
func (e *Editor) Layout(ctx *Context) {
|
||||
Mallocs("Editor.Layout()")
|
||||
ctx.dims = e.e.Layout(ctx.c, ctx.q, ctx.ops, ctx.cs)
|
||||
}
|
||||
|
||||
@@ -164,36 +169,34 @@ func (e *Editor) Text() string { return e.e.Text() }
|
||||
func (e *Editor) SetText(s string) { e.e.SetText(s) }
|
||||
func (e *Editor) Focus() { e.e.Focus() }
|
||||
|
||||
type fWidget struct {
|
||||
l Layout
|
||||
}
|
||||
|
||||
func NewfWidget(l Layout) fWidget {
|
||||
return fWidget{l: l}
|
||||
}
|
||||
|
||||
func (fw fWidget) Layout(ctx *Context) {
|
||||
fw.l(ctx)
|
||||
}
|
||||
|
||||
type Stack WidgetCombinator
|
||||
|
||||
type stackWidget struct {
|
||||
s layout.Stack
|
||||
scs []layout.StackChild
|
||||
ws []Widget
|
||||
}
|
||||
|
||||
func NewStack() Stack {
|
||||
s := layout.Stack{Alignment: layout.Center}
|
||||
scs := make([]layout.StackChild, 0)
|
||||
ret := &stackWidget{}
|
||||
ret.s = layout.Stack{Alignment: layout.Center}
|
||||
ret.scs = make([]layout.StackChild, 0)
|
||||
|
||||
return func(ws ...Widget) Widget {
|
||||
return NewfWidget(func(ctx *Context) {
|
||||
s.Init(ctx.ops, ctx.cs)
|
||||
for _, w := range ws {
|
||||
ctx.cs = s.Rigid()
|
||||
ret.ws = ws
|
||||
return ret
|
||||
}
|
||||
}
|
||||
|
||||
func (sw *stackWidget) Layout(ctx *Context) {
|
||||
sw.s.Init(ctx.ops, ctx.cs)
|
||||
for _, w := range sw.ws {
|
||||
ctx.cs = sw.s.Rigid()
|
||||
w.Layout(ctx)
|
||||
scs = append(scs, s.End(ctx.dims))
|
||||
}
|
||||
ctx.dims = s.Layout(scs...)
|
||||
scs = scs[0:0]
|
||||
})
|
||||
sw.scs = append(sw.scs, sw.s.End(ctx.dims))
|
||||
}
|
||||
ctx.dims = sw.s.Layout(sw.scs...)
|
||||
sw.scs = sw.scs[0:0]
|
||||
}
|
||||
|
||||
type List = WidgetCombinator
|
||||
@@ -202,6 +205,9 @@ type AxisOpt struct{ axis layout.Axis }
|
||||
|
||||
func Axis(x layout.Axis) AxisOpt { return AxisOpt{x} }
|
||||
|
||||
var Horizontal = AxisOpt{layout.Horizontal}
|
||||
var Vertical = AxisOpt{layout.Vertical}
|
||||
|
||||
type ListOpts struct {
|
||||
AxisOpt
|
||||
}
|
||||
@@ -209,22 +215,32 @@ type ListOption interface{ DoListOption(*ListOpts) }
|
||||
|
||||
func (x AxisOpt) DoListOption(o *ListOpts) { o.axis = x.axis }
|
||||
|
||||
type listWidget struct {
|
||||
l layout.List
|
||||
ws []Widget
|
||||
}
|
||||
|
||||
func NewList(los ...ListOption) List {
|
||||
ret := &listWidget{}
|
||||
opts := &ListOpts{}
|
||||
for _, o := range los {
|
||||
o.DoListOption(opts)
|
||||
}
|
||||
l := layout.List{Axis: opts.axis}
|
||||
ret.l = layout.List{Axis: opts.axis}
|
||||
|
||||
return func(ws ...Widget) Widget {
|
||||
return NewfWidget(func(ctx *Context) {
|
||||
for l.Init(ctx.c, ctx.q, ctx.ops, ctx.cs, len(ws)); l.More(); l.Next() {
|
||||
ctx.cs = l.Constraints()
|
||||
ws[l.Index()].Layout(ctx)
|
||||
l.End(ctx.dims)
|
||||
ret.ws = ws
|
||||
return ret
|
||||
}
|
||||
ctx.dims = l.Layout()
|
||||
})
|
||||
}
|
||||
|
||||
func (lw *listWidget) Layout(ctx *Context) {
|
||||
for lw.l.Init(ctx.c, ctx.q, ctx.ops, ctx.cs, len(lw.ws)); lw.l.More(); lw.l.Next() {
|
||||
ctx.cs = lw.l.Constraints()
|
||||
lw.ws[lw.l.Index()].Layout(ctx)
|
||||
lw.l.End(ctx.dims)
|
||||
}
|
||||
ctx.dims = lw.l.Layout()
|
||||
}
|
||||
|
||||
func HScroll(c WidgetCombinator) WidgetCombinator {
|
||||
@@ -237,19 +253,6 @@ func VScroll(c WidgetCombinator) WidgetCombinator {
|
||||
|
||||
type Flex = WidgetCombinator
|
||||
|
||||
// This "Widget" does nothing except set the Flexible field of a FlexOpts
|
||||
// struct within the Extra data structure
|
||||
func Flexible(v float32) Widget {
|
||||
return NewfWidget(func(ctx *Context) {
|
||||
extra.data[extra.cur].(*FlexOpts).flexible = v
|
||||
})
|
||||
}
|
||||
func Rigid() Widget {
|
||||
return NewfWidget(func(ctx *Context) {
|
||||
extra.data[extra.cur].(*FlexOpts).flexible = 0
|
||||
})
|
||||
}
|
||||
|
||||
type AlignmentOpt struct{ alignment layout.Alignment }
|
||||
|
||||
func Alignment(x layout.Alignment) AlignmentOpt { return AlignmentOpt{x} }
|
||||
@@ -265,7 +268,35 @@ type FlexOption interface{ DoFlexOption(*FlexOpts) }
|
||||
func (x AxisOpt) DoFlexOption(o *FlexOpts) { o.axis = x.axis }
|
||||
func (x AlignmentOpt) DoFlexOption(o *FlexOpts) { o.alignment = x.alignment }
|
||||
|
||||
type flexWidget struct {
|
||||
f func(*Context)
|
||||
flexible float32
|
||||
}
|
||||
|
||||
type flexOptWidget struct {
|
||||
flexible float32
|
||||
}
|
||||
|
||||
func (fo flexOptWidget) Layout(ctx *Context) {
|
||||
Mallocs("flexOptWidget.Layout()")
|
||||
ctx.container.(*flexWidget).flexible = fo.flexible
|
||||
}
|
||||
|
||||
// This "Widget" does nothing except set the Flexible field of a FlexOpts
|
||||
// struct within the Extra data structure
|
||||
func Flexible(v float32) Widget {
|
||||
return flexOptWidget{v}
|
||||
}
|
||||
func Rigid() Widget {
|
||||
return flexOptWidget{0}
|
||||
}
|
||||
|
||||
// NewFlex returns a WidgetCombinator that wraps the layout.Flex element.
|
||||
|
||||
func (fw *flexWidget) Layout(ctx *Context) {
|
||||
fw.f(ctx)
|
||||
}
|
||||
|
||||
func NewFlex(fos ...FlexOption) Flex {
|
||||
opts := &FlexOpts{}
|
||||
for _, o := range fos {
|
||||
@@ -275,29 +306,23 @@ func NewFlex(fos ...FlexOption) Flex {
|
||||
Axis: opts.axis,
|
||||
Alignment: opts.alignment,
|
||||
}
|
||||
index := extra.New()
|
||||
extra.data[index] = opts
|
||||
// do not call "make" inside the Layout function
|
||||
fcs := make([]layout.FlexChild, 0)
|
||||
|
||||
return func(ws ...Widget) Widget {
|
||||
return NewfWidget(func(ctx *Context) {
|
||||
// ensure child widgets write options to the right place
|
||||
extra.cur = index
|
||||
opts := extra.data[index].(*FlexOpts)
|
||||
ret := &flexWidget{}
|
||||
ret.f = func(ctx *Context) {
|
||||
oldContainer := ctx.container
|
||||
ctx.container = ret
|
||||
f.Init(ctx.ops, ctx.cs)
|
||||
for _, w := range ws {
|
||||
if opts.flexible != 0 {
|
||||
ctx.cs = f.Flexible(opts.flexible)
|
||||
} else {
|
||||
ctx.cs = f.Rigid()
|
||||
}
|
||||
w.Layout(ctx)
|
||||
fcs = append(fcs, f.End(ctx.dims))
|
||||
}
|
||||
ctx.dims = f.Layout(fcs...)
|
||||
fcs = fcs[0:0] // truncate
|
||||
})
|
||||
fcs = fcs[0:0]
|
||||
ctx.container = oldContainer
|
||||
}
|
||||
return ret
|
||||
}
|
||||
}
|
||||
|
||||
@@ -338,21 +363,34 @@ func (x SizeOpt) DoInsetOption(o *InsetOpts) {
|
||||
o.left = x.size
|
||||
}
|
||||
|
||||
type insetWidget func(*Context)
|
||||
|
||||
func (iw insetWidget) Layout(ctx *Context) {
|
||||
iw(ctx)
|
||||
}
|
||||
|
||||
//NewInset returns a WidgetCombinator that wraps the layout.Inset element.
|
||||
func NewInset(insos ...InsetOption) WidgetCombinator {
|
||||
opts := &InsetOpts{}
|
||||
func NewInset(insos ...InsetOption) func(...Widget) Widget {
|
||||
opts := InsetOpts{}
|
||||
for _, o := range insos {
|
||||
o.DoInsetOption(opts)
|
||||
o.DoInsetOption(&opts)
|
||||
}
|
||||
ins := layout.Inset{Top: opts.top, Right: opts.right, Bottom: opts.bottom, Left: opts.left}
|
||||
|
||||
return func(ws ...Widget) Widget {
|
||||
return NewfWidget(func(ctx *Context) {
|
||||
Mallocs("NewInset() return")
|
||||
var ret insetWidget
|
||||
ret = func(ctx *Context) {
|
||||
oldContainer := ctx.container
|
||||
ctx.container = ret
|
||||
ctx.cs = ins.Begin(ctx.c, ctx.ops, ctx.cs)
|
||||
for _, w := range ws {
|
||||
w.Layout(ctx)
|
||||
}
|
||||
ctx.dims = ins.End(ctx.dims)
|
||||
})
|
||||
ctx.container = oldContainer
|
||||
}
|
||||
return ret
|
||||
}
|
||||
}
|
||||
|
||||
@@ -369,14 +407,24 @@ type Enclosure interface {
|
||||
End(*Context)
|
||||
}
|
||||
|
||||
type encloseWidget struct {
|
||||
enc Enclosure
|
||||
ws []Widget
|
||||
}
|
||||
|
||||
func Enclose(e Enclosure, ws ...Widget) Widget {
|
||||
return NewfWidget(func(ctx *Context) {
|
||||
e.Begin(ctx)
|
||||
for _, w := range ws {
|
||||
return &encloseWidget{e, ws}
|
||||
}
|
||||
|
||||
func (e *encloseWidget) Layout(ctx *Context) {
|
||||
oldContainer := ctx.container
|
||||
ctx.container = e
|
||||
e.enc.Begin(ctx)
|
||||
for _, w := range e.ws {
|
||||
w.Layout(ctx)
|
||||
}
|
||||
e.End(ctx)
|
||||
})
|
||||
e.enc.End(ctx)
|
||||
ctx.container = oldContainer
|
||||
}
|
||||
|
||||
type BackgroundOpts struct {
|
||||
@@ -412,11 +460,14 @@ func NewBackground(bos ...BackgroundOption) WidgetCombinator {
|
||||
}
|
||||
|
||||
func (bg *Background) Begin(ctx *Context) {
|
||||
Mallocs("Background.Begin()")
|
||||
bg.macro.Record(ctx.ops)
|
||||
ctx.cs = bg.Inset.Begin(ctx.c, ctx.ops, ctx.cs)
|
||||
Mallocs("Background.Begin() done")
|
||||
}
|
||||
|
||||
func (bg *Background) End(ctx *Context) {
|
||||
Mallocs("Background.End()")
|
||||
ctx.dims = bg.Inset.End(ctx.dims)
|
||||
bg.macro.Stop()
|
||||
var stack ui.StackOp
|
||||
@@ -435,14 +486,19 @@ func (bg *Background) End(ctx *Context) {
|
||||
paint.PaintOp{Rect: f32.Rectangle{Max: f32.Point{X: w, Y: h}}}.Add(ctx.ops)
|
||||
bg.macro.Add(ctx.ops)
|
||||
stack.Pop()
|
||||
Mallocs("Background.End() done")
|
||||
}
|
||||
|
||||
// https://pomax.github.io/bezierinfo/#circles_cubic.
|
||||
func Rrect(ops *ui.Ops, width, height, se, sw, nw, ne float32) {
|
||||
Mallocs("Rrect() start")
|
||||
w, h := float32(width), float32(height)
|
||||
Mallocs("Rrect() const")
|
||||
const c = 0.55228475 // 4*(sqrt(2)-1)/3
|
||||
var b paint.PathBuilder
|
||||
Mallocs("Rrect() init")
|
||||
b.Init(ops)
|
||||
Mallocs("Rrect() move")
|
||||
b.Move(f32.Point{X: w, Y: h - se})
|
||||
b.Cube(f32.Point{X: 0, Y: se * c}, f32.Point{X: -se + se*c, Y: se}, f32.Point{X: -se, Y: se}) // SE
|
||||
b.Line(f32.Point{X: sw - w + se, Y: 0})
|
||||
@@ -452,6 +508,30 @@ func Rrect(ops *ui.Ops, width, height, se, sw, nw, ne float32) {
|
||||
b.Line(f32.Point{X: w - ne - nw, Y: 0})
|
||||
b.Cube(f32.Point{X: ne * c, Y: 0}, f32.Point{X: ne, Y: ne - ne*c}, f32.Point{X: ne, Y: ne}) // NE
|
||||
b.End()
|
||||
Mallocs("Rrect() end")
|
||||
}
|
||||
|
||||
type fillWidget struct {
|
||||
axis layout.Axis
|
||||
}
|
||||
|
||||
func FillWidth(ws ...Widget) Widget {
|
||||
return Enclose(fillWidget{layout.Horizontal}, ws...)
|
||||
}
|
||||
func FillHeight(ws ...Widget) Widget {
|
||||
return Enclose(fillWidget{layout.Vertical}, ws...)
|
||||
}
|
||||
|
||||
func (fw fillWidget) Begin(ctx *Context) {
|
||||
switch fw.axis {
|
||||
case layout.Horizontal:
|
||||
ctx.cs.Width.Min = ctx.cs.Width.Max
|
||||
case layout.Vertical:
|
||||
ctx.cs.Height.Min = ctx.cs.Height.Max
|
||||
}
|
||||
}
|
||||
|
||||
func (fw fillWidget) End(*Context) {
|
||||
}
|
||||
|
||||
type Clickable interface {
|
||||
@@ -465,13 +545,15 @@ type cWidget struct {
|
||||
click *gesture.Click
|
||||
}
|
||||
|
||||
func (w cWidget) Layout(ctx *Context) {
|
||||
func (w *cWidget) Layout(ctx *Context) {
|
||||
Mallocs("cWidget.Layout()")
|
||||
w.w.Layout(ctx)
|
||||
pointer.RectAreaOp{image.Rect(0, 0, ctx.dims.Size.X, ctx.dims.Size.Y)}.Add(ctx.ops)
|
||||
w.click.Add(ctx.ops)
|
||||
}
|
||||
|
||||
func (w cWidget) Clicked(ctx *Context) bool {
|
||||
Mallocs("cWidget.Clicked()")
|
||||
for e, ok := w.click.Next(ctx.q); ok; e, ok = w.click.Next(ctx.q) {
|
||||
if e.Type == gesture.TypeClick {
|
||||
ctx.w.Invalidate()
|
||||
@@ -482,6 +564,6 @@ func (w cWidget) Clicked(ctx *Context) bool {
|
||||
}
|
||||
|
||||
//Clickable converts any Widget into a clickable Widget.
|
||||
func AsClickable(w Widget) cWidget {
|
||||
return cWidget{w: w, click: new(gesture.Click)}
|
||||
func AsClickable(w Widget) *cWidget {
|
||||
return &cWidget{w: w, click: new(gesture.Click)}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package giowrap
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
)
|
||||
|
||||
type AllocEntry struct {
|
||||
Value uint64
|
||||
Text string
|
||||
}
|
||||
|
||||
var (
|
||||
memstats runtime.MemStats
|
||||
oldMallocs uint64
|
||||
AllMallocs []AllocEntry
|
||||
)
|
||||
|
||||
func RecordMallocs() {
|
||||
if AllMallocs == nil {
|
||||
AllMallocs = make([]AllocEntry, 0, 64)
|
||||
} else {
|
||||
AllMallocs = AllMallocs[0:0]
|
||||
}
|
||||
runtime.ReadMemStats(&memstats)
|
||||
oldMallocs = memstats.Mallocs
|
||||
}
|
||||
|
||||
func Mallocs(s string) {
|
||||
if AllMallocs == nil {
|
||||
return
|
||||
}
|
||||
runtime.ReadMemStats(&memstats)
|
||||
AllMallocs = append(AllMallocs, AllocEntry{memstats.Mallocs - oldMallocs, s})
|
||||
}
|
||||
Reference in New Issue
Block a user