Compare commits
7
Commits
f96cdb619a
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
11e2dbad38 | ||
|
|
f134a1f21e | ||
|
|
dfc3f618c0 | ||
|
|
cf56e8c47e | ||
|
|
83c9d40500 | ||
|
|
635655e768 | ||
|
|
9f60b99984 |
@@ -1,4 +1,7 @@
|
||||
cmd/hello/hello
|
||||
cmd/cal/cal
|
||||
cmd/scroll/scroll
|
||||
cmd/grid/grid
|
||||
cmd/form/form
|
||||
*.apk
|
||||
memprofile*
|
||||
|
||||
+18
-6
@@ -103,7 +103,9 @@ func (sm *SelectableMonth) Weekdays() int {
|
||||
for w := 0; w <= 6; w++ {
|
||||
for d := 1; d < 6; d++ {
|
||||
x := w*7 + d + 1 - sm.Pad
|
||||
if x < 0 || x >= sm.DaysIn { break }
|
||||
if x < 0 || x >= sm.DaysIn {
|
||||
break
|
||||
}
|
||||
if sm.S[x] {
|
||||
i++
|
||||
}
|
||||
@@ -116,12 +118,22 @@ func (sm *SelectableMonth) Weekends() int {
|
||||
var i int
|
||||
for w := 0; w <= 6; w++ {
|
||||
x := w*7 + 1 - sm.Pad
|
||||
if x < 0 { continue }
|
||||
if x >= sm.DaysIn { break }
|
||||
if sm.S[x] { i++ }
|
||||
if x < 0 {
|
||||
continue
|
||||
}
|
||||
if x >= sm.DaysIn {
|
||||
break
|
||||
}
|
||||
if sm.S[x] {
|
||||
i++
|
||||
}
|
||||
x = w*7 + 6 + 1 - sm.Pad
|
||||
if x < 0 || x >= sm.DaysIn { break }
|
||||
if sm.S[x] { i++ }
|
||||
if x < 0 || x >= sm.DaysIn {
|
||||
break
|
||||
}
|
||||
if sm.S[x] {
|
||||
i++
|
||||
}
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
+13
-11
@@ -4,8 +4,8 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"image/color"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
gio "git.wow.st/gmp/giowrap"
|
||||
@@ -15,12 +15,11 @@ import (
|
||||
"gioui.org/ui/layout"
|
||||
"gioui.org/ui/text"
|
||||
|
||||
"golang.org/x/image/font/sfnt"
|
||||
"golang.org/x/image/font/gofont/goregular"
|
||||
"golang.org/x/image/font/sfnt"
|
||||
)
|
||||
|
||||
var (
|
||||
sel int32
|
||||
face text.Face
|
||||
)
|
||||
|
||||
@@ -104,14 +103,16 @@ func eventloop() {
|
||||
|
||||
regular, err := sfnt.Parse(goregular.TTF)
|
||||
face = ctx.Faces.For(regular, ui.Sp(20))
|
||||
if err != nil { log.Fatal("Cannot parse font.") }
|
||||
if err != nil {
|
||||
log.Fatal("Cannot parse font.")
|
||||
}
|
||||
|
||||
bg := gio.NewBackground(
|
||||
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
|
||||
@@ -123,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))
|
||||
@@ -143,13 +144,14 @@ func eventloop() {
|
||||
}
|
||||
resetCal()
|
||||
|
||||
for { select {
|
||||
for {
|
||||
select {
|
||||
case e := <-w.Events():
|
||||
switch e := e.(type) {
|
||||
case app.DestroyEvent:
|
||||
return
|
||||
case app.UpdateEvent:
|
||||
ctx.Reset(e)
|
||||
ctx.Reset(&e)
|
||||
|
||||
mth.SetText(fmt.Sprintf("%s %d", sm.Month.String(), sm.Year))
|
||||
|
||||
@@ -193,6 +195,6 @@ func eventloop() {
|
||||
}
|
||||
sm.CheckSel()
|
||||
}
|
||||
}}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"image"
|
||||
|
||||
"gioui.org/ui"
|
||||
"gioui.org/ui/app"
|
||||
"gioui.org/ui/f32"
|
||||
"gioui.org/ui/layout"
|
||||
"gioui.org/ui/paint"
|
||||
|
||||
gio "git.wow.st/gmp/giowrap"
|
||||
)
|
||||
|
||||
func main() {
|
||||
go func() {
|
||||
w := app.NewWindow()
|
||||
ops := new(ui.Ops)
|
||||
gcs := make([]gio.GridChild, 0)
|
||||
|
||||
for e := range w.Events() {
|
||||
if e, ok := e.(app.UpdateEvent); ok {
|
||||
ops.Reset()
|
||||
c := &e.Config
|
||||
cs := layout.RigidConstraints(e.Size)
|
||||
nrows := 5
|
||||
ncols := 10
|
||||
grid := gio.Grid{Cols: ncols}
|
||||
cs = grid.Init(ops, cs)
|
||||
|
||||
for i := 0; i < nrows*ncols; i++ {
|
||||
grid.Begin()
|
||||
dims := layoutRect(c, ops, cs)
|
||||
gcs = append(gcs, grid.End(dims))
|
||||
}
|
||||
|
||||
grid.Layout(gcs...)
|
||||
w.Update(ops)
|
||||
gcs = gcs[0:0]
|
||||
}
|
||||
}
|
||||
}()
|
||||
app.Main()
|
||||
}
|
||||
|
||||
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{
|
||||
Max: f32.Point{
|
||||
X: float32(cs.Width.Max),
|
||||
Y: float32(cs.Width.Max),
|
||||
},
|
||||
}}.Add(ops)
|
||||
dims := layout.Dimensions{
|
||||
Size: image.Point{
|
||||
X: cs.Width.Max,
|
||||
Y: cs.Width.Max,
|
||||
},
|
||||
}
|
||||
return ins.End(dims)
|
||||
}
|
||||
+388
-122
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"git.wow.st/gmp/giowrap"
|
||||
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"log"
|
||||
@@ -14,16 +15,16 @@ import (
|
||||
|
||||
"gioui.org/ui"
|
||||
"gioui.org/ui/app"
|
||||
"gioui.org/ui/paint"
|
||||
"gioui.org/ui/f32"
|
||||
"gioui.org/ui/gesture"
|
||||
"gioui.org/ui/layout"
|
||||
"gioui.org/ui/measure"
|
||||
"gioui.org/ui/paint"
|
||||
"gioui.org/ui/pointer"
|
||||
"gioui.org/ui/text"
|
||||
|
||||
"golang.org/x/image/font/sfnt"
|
||||
"golang.org/x/image/font/gofont/goregular"
|
||||
"golang.org/x/image/font/sfnt"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -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 {
|
||||
@@ -64,21 +66,30 @@ func main() {
|
||||
mux.Unlock()
|
||||
}
|
||||
}()
|
||||
switch os.Args[1] {
|
||||
case "", "main1":
|
||||
log.Print("main1()")
|
||||
go main1()
|
||||
case "main2":
|
||||
log.Print("main2()")
|
||||
go main2()
|
||||
switch {
|
||||
case len(os.Args) < 2:
|
||||
fallthrough
|
||||
default:
|
||||
log.Fatal(`Usage:
|
||||
hello [main1|main2]
|
||||
hello [main1|main2|main3|main4]
|
||||
`)
|
||||
case os.Args[1] == "main1":
|
||||
log.Print("main1()")
|
||||
go main1()
|
||||
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)
|
||||
@@ -92,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})
|
||||
@@ -116,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(5),
|
||||
InnerInset(e2),
|
||||
giowrap.Flexible(10),
|
||||
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()
|
||||
@@ -136,32 +384,35 @@ 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()
|
||||
frames = frames + 1
|
||||
frametime = frametime + dur
|
||||
if dur > maxframetime { maxframetime = dur }
|
||||
if dur > maxframetime {
|
||||
maxframetime = dur
|
||||
}
|
||||
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)
|
||||
@@ -185,25 +436,14 @@ func main2() {
|
||||
e2.Focus()
|
||||
e2.SetText("text 2")
|
||||
|
||||
f1 := layout.Flex{ Axis: layout.Vertical }
|
||||
OuterInset := layout.UniformInset(ui.Dp(10))
|
||||
InnerInset := layout.UniformInset(ui.Dp(10))
|
||||
|
||||
f2 := layout.Flex{ Axis: layout.Horizontal }
|
||||
|
||||
btn1 := text.Label{
|
||||
btn1 := &Button{
|
||||
Face: faces.For(regular, ui.Sp(24)),
|
||||
Text: "push1",
|
||||
Label: "push1",
|
||||
}
|
||||
b1ins := layout.UniformInset(ui.Dp(4))
|
||||
btn2 := text.Label{
|
||||
btn2 := &Button{
|
||||
Face: faces.For(regular, ui.Sp(24)),
|
||||
Text: "push2",
|
||||
Label: "push2",
|
||||
}
|
||||
b2ins := layout.UniformInset(ui.Dp(4))
|
||||
var bg1, bg2 ui.MacroOp
|
||||
click1 := new(gesture.Click)
|
||||
click2 := new(gesture.Click)
|
||||
|
||||
profiled := false
|
||||
startTime := time.Now()
|
||||
@@ -218,92 +458,118 @@ 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)
|
||||
cs := layout.RigidConstraints(e.Size)
|
||||
cs = OuterInset.Begin(c, ops, cs)
|
||||
f1.Init(ops, cs)
|
||||
cs = f1.Rigid()
|
||||
dims := e1.Layout(c, q, ops, cs)
|
||||
f1c1 := f1.End(dims)
|
||||
cs = f1.Flexible(5)
|
||||
cs = InnerInset.Begin(c, ops, cs)
|
||||
dims = e2.Layout(c, q, ops, cs)
|
||||
dims = InnerInset.End(dims)
|
||||
f1c2 := f1.End(dims)
|
||||
cs = f1.Flexible(10)
|
||||
f2.Init(ops, cs)
|
||||
cs = f2.Rigid()
|
||||
cs = InnerInset.Begin(c, ops, cs)
|
||||
bg1.Record(ops)
|
||||
cs = b1ins.Begin(c, ops, cs)
|
||||
dims = btn1.Layout(ops, cs)
|
||||
dims = b1ins.End(dims)
|
||||
pointer.RectAreaOp{image.Rect(0,0,dims.Size.X,dims.Size.Y)}.Add(ops)
|
||||
click1.Add(ops)
|
||||
bg1.Stop()
|
||||
wi, h := float32(dims.Size.X), float32(dims.Size.Y)
|
||||
r := float32(c.Px(ui.Dp(4)))
|
||||
giowrap.Rrect(ops, wi, 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: wi, Y: h}}}.Add(ops)
|
||||
bg1.Add(ops)
|
||||
dims = InnerInset.End(dims)
|
||||
f2c1 := f2.End(dims)
|
||||
cs = f2.Flexible(0.5)
|
||||
cs = InnerInset.Begin(c, ops, cs)
|
||||
bg2.Record(ops)
|
||||
cs = b2ins.Begin(c, ops, cs)
|
||||
dims = btn2.Layout(ops, cs)
|
||||
dims = b2ins.End(dims)
|
||||
pointer.RectAreaOp{image.Rect(0,0,dims.Size.X,dims.Size.Y)}.Add(ops)
|
||||
click2.Add(ops)
|
||||
bg2.Stop()
|
||||
wi, h = float32(dims.Size.X), float32(dims.Size.Y)
|
||||
giowrap.Rrect(ops, wi, 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: wi, Y: h}}}.Add(ops)
|
||||
bg2.Add(ops)
|
||||
dims = InnerInset.End(dims)
|
||||
f2c2 := f2.End(dims)
|
||||
dims = f2.Layout(f2c1, f2c2)
|
||||
f1c3 := f1.End(dims)
|
||||
dims = f1.Layout(f1c1, f1c2, f1c3)
|
||||
dims = OuterInset.End(dims)
|
||||
w.Update(ops)
|
||||
for ev, ok := click1.Next(q); ok; ev, ok = click1.Next(q) {
|
||||
for ev, ok := btn1.Click.Next(q); ok; ev, ok = btn1.Click.Next(q) {
|
||||
if ev.Type == gesture.TypeClick {
|
||||
log.Print("Clicked: " + e1.Text())
|
||||
}
|
||||
}
|
||||
for ev, ok := click2.Next(q); ok; ev, ok = click2.Next(q) {
|
||||
for ev, ok := btn2.Click.Next(q); ok; ev, ok = btn2.Click.Next(q) {
|
||||
if ev.Type == gesture.TypeClick {
|
||||
log.Print("Clicked: " + e2.Text())
|
||||
}
|
||||
}
|
||||
|
||||
var dims layout.Dimensions
|
||||
cs := layout.RigidConstraints(e.Size)
|
||||
{
|
||||
f1 := layout.Flex{Axis: layout.Vertical}
|
||||
ins := layout.UniformInset(ui.Dp(10))
|
||||
f1.Init(ops, ins.Begin(c, ops, cs))
|
||||
c1 := f1.End(e1.Layout(c, q, ops, f1.Rigid()))
|
||||
{
|
||||
cs = f1.Flexible(0.33)
|
||||
ins := layout.UniformInset(ui.Dp(10))
|
||||
dims = ins.End(e2.Layout(c, q, ops, ins.Begin(c, ops, cs)))
|
||||
}
|
||||
c2 := f1.End(dims)
|
||||
{
|
||||
cs = f1.Flexible(0.67)
|
||||
f2 := layout.Flex{Axis: layout.Horizontal}
|
||||
f2.Init(ops, cs)
|
||||
|
||||
c1 := f2.End(btn1.Layout(c, ops, f2.Rigid()))
|
||||
|
||||
c2 := f2.End(btn2.Layout(c, ops, f2.Flexible(0.5)))
|
||||
|
||||
dims = f2.Layout(c1, c2)
|
||||
}
|
||||
c3 := f1.End(dims)
|
||||
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()
|
||||
frames = frames + 1
|
||||
frametime = frametime + dur
|
||||
if dur > maxframetime { maxframetime = dur }
|
||||
if dur > maxframetime {
|
||||
maxframetime = dur
|
||||
}
|
||||
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.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.Dimensions{Size: sz}
|
||||
}
|
||||
|
||||
type Button struct {
|
||||
Face text.Face
|
||||
Label string
|
||||
Click gesture.Click
|
||||
}
|
||||
|
||||
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.Dimensions
|
||||
st := layout.Stack{}
|
||||
st.Init(ops, cs)
|
||||
{
|
||||
cs = st.Rigid()
|
||||
l := text.Label{
|
||||
Face: b.Face,
|
||||
Text: b.Label,
|
||||
}
|
||||
ins := layout.UniformInset(ui.Dp(4))
|
||||
dims = ins.End(l.Layout(ops, ins.Begin(c, ops, cs)))
|
||||
pointer.RectAreaOp{image.Rect(0, 0, dims.Size.X, dims.Size.Y)}.Add(ops)
|
||||
b.Click.Add(ops)
|
||||
}
|
||||
c2 := st.End(dims)
|
||||
c1 := st.End(layoutRRect(c, ops, st.Expand()))
|
||||
dims = st.Layout(c1, c2)
|
||||
return ins.End(dims)
|
||||
}
|
||||
|
||||
+69
-41
@@ -1,9 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
"fmt"
|
||||
"image/color"
|
||||
"log"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
gio "git.wow.st/gmp/giowrap"
|
||||
|
||||
@@ -12,15 +14,15 @@ import (
|
||||
"gioui.org/ui/layout"
|
||||
"gioui.org/ui/text"
|
||||
|
||||
"golang.org/x/image/font/sfnt"
|
||||
"golang.org/x/image/font/gofont/goregular"
|
||||
"golang.org/x/image/font/sfnt"
|
||||
)
|
||||
|
||||
var (
|
||||
face text.Face
|
||||
black color.RGBA = color.RGBA{A: 0xff, R: 0x00, G: 0x00, B: 0x00}
|
||||
gray2 color.RGBA = color.RGBA{ A: 0xff, R: 0x70, G: 0x70, B: 0x70 }
|
||||
gray1 color.RGBA = color.RGBA{ A: 0xff, R: 0x50, G: 0x50, B: 0x50 }
|
||||
gray2 color.RGBA = color.RGBA{A: 0xff, R: 0xc0, G: 0xc0, B: 0xc0}
|
||||
gray1 color.RGBA = color.RGBA{A: 0xff, R: 0x70, G: 0x70, B: 0x70}
|
||||
)
|
||||
|
||||
type SLabel struct {
|
||||
@@ -87,28 +89,33 @@ func eventloop() {
|
||||
|
||||
regular, err := sfnt.Parse(goregular.TTF)
|
||||
face = ctx.Faces.For(regular, ui.Sp(16))
|
||||
if err != nil { log.Fatal("Cannot parse font.") }
|
||||
if err != nil {
|
||||
log.Fatal("Cannot parse font.")
|
||||
}
|
||||
|
||||
sysbg := gio.NewBackground(gio.Color(black))
|
||||
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 * 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])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,41 +125,62 @@ 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
|
||||
|
||||
for { select {
|
||||
stime := time.Now()
|
||||
sm := runtime.MemStats{}
|
||||
for {
|
||||
select {
|
||||
case e := <-w.Events():
|
||||
switch e := e.(type) {
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
package giowrap
|
||||
|
||||
import (
|
||||
"image"
|
||||
|
||||
"gioui.org/ui"
|
||||
"gioui.org/ui/f32"
|
||||
"gioui.org/ui/layout"
|
||||
)
|
||||
|
||||
type Grid struct {
|
||||
Cols int
|
||||
Height, Width int
|
||||
|
||||
macro ui.MacroOp
|
||||
ops *ui.Ops
|
||||
cs layout.Constraints
|
||||
mode gridMode
|
||||
row, col int
|
||||
}
|
||||
|
||||
type GridChild struct {
|
||||
dims layout.Dimensions
|
||||
macro ui.MacroOp
|
||||
}
|
||||
|
||||
type gridMode uint8
|
||||
|
||||
const (
|
||||
modeNone gridMode = iota
|
||||
modeBegun
|
||||
)
|
||||
|
||||
func (g *Grid) Init(ops *ui.Ops, cs layout.Constraints) layout.Constraints {
|
||||
g.mode = modeBegun
|
||||
g.ops = ops
|
||||
g.cs = cs
|
||||
g.row, g.col = 0, 0
|
||||
|
||||
cs.Height.Min = 0
|
||||
|
||||
if g.Height != 0 {
|
||||
g.cs.Height.Max = g.Height
|
||||
}
|
||||
if g.Width != 0 {
|
||||
g.cs.Width.Max = g.Width
|
||||
}
|
||||
|
||||
cs.Width.Max = g.cs.Width.Max / g.Cols
|
||||
if g.Cols > 1 {
|
||||
cs.Width.Min = cs.Width.Max
|
||||
}
|
||||
return cs
|
||||
}
|
||||
|
||||
func (g *Grid) Begin() {
|
||||
g.macro.Record(g.ops)
|
||||
}
|
||||
|
||||
func (g *Grid) End(dims layout.Dimensions) GridChild {
|
||||
if g.mode != modeBegun {
|
||||
panic("Must call Grid.Begin() before adding children.")
|
||||
}
|
||||
g.macro.Stop()
|
||||
return GridChild{dims: dims, macro: g.macro}
|
||||
}
|
||||
|
||||
func (g *Grid) Layout(cs ...GridChild) layout.Dimensions {
|
||||
rowheight := 0
|
||||
height := 0
|
||||
var width float32
|
||||
var maxwidth float32
|
||||
for _, c := range cs {
|
||||
var stack ui.StackOp
|
||||
stack.Push(g.ops)
|
||||
c.macro.Add(g.ops)
|
||||
stack.Pop()
|
||||
if c.dims.Size.Y > rowheight {
|
||||
rowheight = c.dims.Size.Y
|
||||
}
|
||||
g.col = g.col + 1
|
||||
var x float32
|
||||
if g.Cols == 1 {
|
||||
x = float32(c.dims.Size.X)
|
||||
if x > maxwidth {
|
||||
maxwidth = x
|
||||
}
|
||||
} else {
|
||||
x = float32(g.cs.Width.Max / g.Cols)
|
||||
}
|
||||
if g.col < g.Cols {
|
||||
ui.TransformOp{}.Offset(f32.Point{X: x}).Add(g.ops)
|
||||
width = width + x
|
||||
} else {
|
||||
g.col = 0
|
||||
ui.TransformOp{}.Offset(f32.Point{X: -width, Y: float32(rowheight)}).Add(g.ops)
|
||||
g.row = g.row + 1
|
||||
height = height + rowheight
|
||||
width = 0
|
||||
rowheight = 0
|
||||
}
|
||||
}
|
||||
if height == 0 {
|
||||
height = rowheight
|
||||
}
|
||||
g.mode = modeNone
|
||||
var dwidth int
|
||||
if g.Cols == 1 {
|
||||
dwidth = int(maxwidth)
|
||||
} else {
|
||||
dwidth = g.cs.Width.Max
|
||||
}
|
||||
return layout.Dimensions{Size: image.Point{dwidth, height}}
|
||||
}
|
||||
|
||||
func toPointF(p image.Point) f32.Point {
|
||||
return f32.Point{X: float32(p.X), Y: float32(p.Y)}
|
||||
}
|
||||
|
||||
type GridOption interface{ DoGridOption(*Grid) }
|
||||
|
||||
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 {
|
||||
ret := &gridWidget{}
|
||||
ret.g = Grid{Cols: cols}
|
||||
for _, gop := range gops {
|
||||
gop.DoGridOption(&ret.g)
|
||||
}
|
||||
ret.gcs = make([]GridChild, 0)
|
||||
return func(ws ...Widget) Widget {
|
||||
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]
|
||||
}
|
||||
@@ -14,8 +14,8 @@ import (
|
||||
"gioui.org/ui/text"
|
||||
|
||||
"gioui.org/ui/f32"
|
||||
"gioui.org/ui/paint"
|
||||
"gioui.org/ui/gesture"
|
||||
"gioui.org/ui/paint"
|
||||
"gioui.org/ui/pointer"
|
||||
)
|
||||
|
||||
@@ -29,7 +29,9 @@ var extra Extra
|
||||
|
||||
func (e *Extra) New() int {
|
||||
e.Lock()
|
||||
if e.data == nil { e.data = make([]interface{},0) }
|
||||
if e.data == nil {
|
||||
e.data = make([]interface{}, 0)
|
||||
}
|
||||
ret := e.max
|
||||
e.max = e.max + 1
|
||||
e.data = append(e.data, nil)
|
||||
@@ -45,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 {
|
||||
@@ -56,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() {
|
||||
@@ -80,8 +85,11 @@ type Label struct {
|
||||
}
|
||||
|
||||
type FaceOpt struct{ face text.Face }
|
||||
|
||||
func Face(x text.Face) FaceOpt { return FaceOpt{x} }
|
||||
|
||||
type AlignOpt struct{ alignment text.Alignment }
|
||||
|
||||
func Align(x text.Alignment) AlignOpt { return AlignOpt{x} }
|
||||
|
||||
type LabelOpts struct {
|
||||
@@ -90,14 +98,17 @@ type LabelOpts struct {
|
||||
AlignOpt
|
||||
}
|
||||
type LabelOption interface{ DoLabelOption(*LabelOpts) }
|
||||
|
||||
func (x FaceOpt) DoLabelOption(o *LabelOpts) { o.face = x.face }
|
||||
func (x AlignOpt) DoLabelOption(o *LabelOpts) { o.alignment = x.alignment }
|
||||
func (x ColorOpt) DoLabelOption(o *LabelOpts) { o.c = &x.c; }
|
||||
func (x ColorOpt) DoLabelOption(o *LabelOpts) { o.c = &x.c }
|
||||
|
||||
func NewLabel(t string, lops ...LabelOption) *Label {
|
||||
ret := &Label{}
|
||||
opts := &LabelOpts{}
|
||||
for _,o := range lops { o.DoLabelOption(opts) }
|
||||
for _, o := range lops {
|
||||
o.DoLabelOption(opts)
|
||||
}
|
||||
ret.l = &text.Label{
|
||||
Face: opts.face,
|
||||
Text: t,
|
||||
@@ -113,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)
|
||||
}
|
||||
|
||||
@@ -125,6 +137,7 @@ type Editor struct {
|
||||
}
|
||||
|
||||
type SinglelineOpt struct{ singleline bool }
|
||||
|
||||
func Singleline(x bool) SinglelineOpt { return SinglelineOpt{x} }
|
||||
|
||||
type EditorOpts struct {
|
||||
@@ -132,19 +145,23 @@ type EditorOpts struct {
|
||||
SinglelineOpt
|
||||
}
|
||||
type EditorOption interface{ DoEditorOption(*EditorOpts) }
|
||||
|
||||
func (x FaceOpt) DoEditorOption(o *EditorOpts) { o.face = x.face }
|
||||
func (x SinglelineOpt) DoEditorOption(o *EditorOpts) { o.singleline = x.singleline }
|
||||
|
||||
func NewEditor(t string, eops ...EditorOption) *Editor {
|
||||
ret := &Editor{}
|
||||
opts := &EditorOpts{}
|
||||
for _,o := range eops { o.DoEditorOption(opts) }
|
||||
for _, o := range eops {
|
||||
o.DoEditorOption(opts)
|
||||
}
|
||||
ret.e = &text.Editor{Face: opts.face, SingleLine: opts.singleline}
|
||||
ret.SetText(t)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (e *Editor) Layout(ctx *Context) {
|
||||
Mallocs("Editor.Layout()")
|
||||
ctx.dims = e.e.Layout(ctx.c, ctx.q, ctx.ops, ctx.cs)
|
||||
}
|
||||
|
||||
@@ -152,63 +169,78 @@ 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
|
||||
|
||||
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
|
||||
}
|
||||
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 }
|
||||
for _, o := range los {
|
||||
o.DoListOption(opts)
|
||||
}
|
||||
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 {
|
||||
@@ -221,20 +253,8 @@ 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} }
|
||||
|
||||
type FlexOpts struct {
|
||||
@@ -244,40 +264,65 @@ type FlexOpts struct {
|
||||
}
|
||||
|
||||
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 { o.DoFlexOption(opts) }
|
||||
for _, o := range fos {
|
||||
o.DoFlexOption(opts)
|
||||
}
|
||||
f := layout.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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -288,12 +333,19 @@ type InsetOpts struct {
|
||||
type InsetOption interface{ DoInsetOption(*InsetOpts) }
|
||||
|
||||
type TopOpt struct{ top ui.Value }
|
||||
|
||||
func Top(x ui.Value) TopOpt { return TopOpt{x} }
|
||||
|
||||
type RightOpt struct{ right ui.Value }
|
||||
|
||||
func Right(x ui.Value) RightOpt { return RightOpt{x} }
|
||||
|
||||
type BottomOpt struct{ bottom ui.Value }
|
||||
|
||||
func Bottom(x ui.Value) BottomOpt { return BottomOpt{x} }
|
||||
|
||||
type LeftOpt struct{ left ui.Value }
|
||||
|
||||
func Left(x ui.Value) LeftOpt { return LeftOpt{x} }
|
||||
|
||||
func (x TopOpt) DoInsetOption(o *InsetOpts) { o.top = x.top }
|
||||
@@ -302,6 +354,7 @@ func (x BottomOpt) DoInsetOption(o *InsetOpts) { o.bottom = x.bottom }
|
||||
func (x LeftOpt) DoInsetOption(o *InsetOpts) { o.left = x.left }
|
||||
|
||||
type SizeOpt struct{ size ui.Value }
|
||||
|
||||
func Size(x ui.Value) SizeOpt { return SizeOpt{x} }
|
||||
func (x SizeOpt) DoInsetOption(o *InsetOpts) {
|
||||
o.top = x.size
|
||||
@@ -310,19 +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{}
|
||||
for _,o := range insos { o.DoInsetOption(opts) }
|
||||
func NewInset(insos ...InsetOption) func(...Widget) Widget {
|
||||
opts := InsetOpts{}
|
||||
for _, o := range insos {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -339,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 {
|
||||
@@ -357,16 +435,20 @@ type BackgroundOpts struct {
|
||||
type BackgroundOption interface{ DoBackgroundOption(*BackgroundOpts) }
|
||||
|
||||
type ColorOpt struct{ c color.RGBA }
|
||||
|
||||
func Color(x color.RGBA) ColorOpt { return ColorOpt{x} }
|
||||
func (x ColorOpt) DoBackgroundOption(o *BackgroundOpts) { o.c = x.c }
|
||||
|
||||
type RadiusOpt struct{ radius ui.Value }
|
||||
|
||||
func Radius(x ui.Value) RadiusOpt { return RadiusOpt{x} }
|
||||
func (x RadiusOpt) DoBackgroundOption(o *BackgroundOpts) { o.radius = x.radius }
|
||||
|
||||
func NewBackground(bos ...BackgroundOption) WidgetCombinator {
|
||||
opts := &BackgroundOpts{}
|
||||
for _,o := range bos { o.DoBackgroundOption(opts) }
|
||||
for _, o := range bos {
|
||||
o.DoBackgroundOption(opts)
|
||||
}
|
||||
bg := &Background{
|
||||
Color: opts.c,
|
||||
Radius: opts.radius,
|
||||
@@ -378,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
|
||||
@@ -401,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})
|
||||
@@ -418,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 {
|
||||
@@ -431,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()
|
||||
@@ -448,134 +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) }
|
||||
}
|
||||
|
||||
type Grid struct {
|
||||
Cols int
|
||||
Height, Width int
|
||||
|
||||
macro ui.MacroOp
|
||||
ops *ui.Ops
|
||||
cs layout.Constraints
|
||||
mode gridMode
|
||||
row, col int
|
||||
}
|
||||
|
||||
type GridChild struct {
|
||||
dims layout.Dimens
|
||||
macro ui.MacroOp
|
||||
}
|
||||
|
||||
type gridMode uint8
|
||||
const (
|
||||
modeNone gridMode = iota
|
||||
modeBegun
|
||||
)
|
||||
|
||||
func (g *Grid) Init(ops *ui.Ops, cs layout.Constraints) layout.Constraints {
|
||||
g.mode = modeBegun
|
||||
g.ops = ops
|
||||
g.cs = cs
|
||||
g.row, g.col = 0, 0
|
||||
|
||||
cs.Height.Min = 0
|
||||
|
||||
if g.Height != 0 { g.cs.Height.Max = g.Height }
|
||||
if g.Width != 0 { g.cs.Width.Max = g.Width }
|
||||
|
||||
cs.Width.Max = g.cs.Width.Max / g.Cols
|
||||
if g.Cols > 1 {
|
||||
cs.Width.Min = cs.Width.Max
|
||||
}
|
||||
return cs
|
||||
}
|
||||
|
||||
func (g *Grid) Begin() {
|
||||
g.macro.Record(g.ops)
|
||||
}
|
||||
|
||||
func (g *Grid) End(dims layout.Dimens) GridChild {
|
||||
if g.mode != modeBegun {
|
||||
panic("Must call Grid.Begin() before adding children.")
|
||||
}
|
||||
g.macro.Stop()
|
||||
return GridChild{ dims: dims, macro: g.macro }
|
||||
}
|
||||
|
||||
func (g *Grid) Layout(cs ...GridChild) layout.Dimens {
|
||||
rowheight := 0
|
||||
height := 0
|
||||
var width float32
|
||||
var maxwidth float32
|
||||
for _,c := range cs {
|
||||
var stack ui.StackOp
|
||||
stack.Push(g.ops)
|
||||
c.macro.Add(g.ops)
|
||||
stack.Pop()
|
||||
if c.dims.Size.Y > rowheight {
|
||||
rowheight = c.dims.Size.Y
|
||||
}
|
||||
g.col = g.col+1
|
||||
var x float32
|
||||
if g.Cols == 1 {
|
||||
x = float32(c.dims.Size.X)
|
||||
if x > maxwidth { maxwidth = x }
|
||||
} else {
|
||||
x = float32(g.cs.Width.Max / g.Cols)
|
||||
}
|
||||
if g.col < g.Cols {
|
||||
ui.TransformOp{}.Offset(f32.Point{X: x}).Add(g.ops)
|
||||
width = width + x
|
||||
} else {
|
||||
g.col = 0
|
||||
ui.TransformOp{}.Offset(f32.Point{ X: -width, Y: float32(rowheight) }).Add(g.ops)
|
||||
g.row = g.row + 1
|
||||
height = height + rowheight
|
||||
width = 0
|
||||
rowheight = 0
|
||||
}
|
||||
}
|
||||
if height == 0 { height = rowheight }
|
||||
g.mode = modeNone
|
||||
var dwidth int
|
||||
if g.Cols == 1 {
|
||||
dwidth = int(maxwidth)
|
||||
} else {
|
||||
dwidth = g.cs.Width.Max
|
||||
}
|
||||
return layout.Dimens{ Size: image.Point{ dwidth, height } }
|
||||
}
|
||||
|
||||
func toPointF(p image.Point) f32.Point {
|
||||
return f32.Point{X: float32(p.X), Y: float32(p.Y)}
|
||||
}
|
||||
|
||||
type GridOption interface { DoGridOption(*Grid) }
|
||||
|
||||
type HeightOpt struct { height int }
|
||||
func Height(x int) HeightOpt { return HeightOpt{ x } }
|
||||
func (x HeightOpt) DoGridOption(g *Grid) { g.Height = x.height }
|
||||
|
||||
func NewGrid(cols int, gops ...GridOption) WidgetCombinator {
|
||||
g := &Grid{ Cols: cols }
|
||||
for _, gop := range gops {
|
||||
gop.DoGridOption(g)
|
||||
}
|
||||
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]
|
||||
})
|
||||
}
|
||||
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