Compare commits
22
Commits
03740d940b
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
11e2dbad38 | ||
|
|
f134a1f21e | ||
|
|
dfc3f618c0 | ||
|
|
cf56e8c47e | ||
|
|
83c9d40500 | ||
|
|
635655e768 | ||
|
|
9f60b99984 | ||
|
|
f96cdb619a | ||
|
|
70e5529a9b | ||
|
|
62d0ceef66 | ||
|
|
014ace99a6 | ||
|
|
25847d5921 | ||
|
|
3724f62497 | ||
|
|
491f26dcfe | ||
|
|
5bef5fc95a | ||
|
|
a463c073ed | ||
|
|
bfd6ae49df | ||
|
|
716cd6b562 | ||
|
|
b9514a08ff | ||
|
|
e638a9dde4 | ||
|
|
92cb4d20a4 | ||
|
|
74951e8ce5 |
@@ -0,0 +1,7 @@
|
|||||||
|
cmd/hello/hello
|
||||||
|
cmd/cal/cal
|
||||||
|
cmd/scroll/scroll
|
||||||
|
cmd/grid/grid
|
||||||
|
cmd/form/form
|
||||||
|
*.apk
|
||||||
|
memprofile*
|
||||||
@@ -22,13 +22,6 @@ type Widget interface {
|
|||||||
Layout(Context) Context
|
Layout(Context) Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func LayoutWithContext(ctx Context, ws ...Widget) Context {
|
|
||||||
for _, w := range ws {
|
|
||||||
ctx = w.Layout(ctx)
|
|
||||||
}
|
|
||||||
return ctx
|
|
||||||
}
|
|
||||||
|
|
||||||
type WidgetCombinator func(...Widget) Widget
|
type WidgetCombinator func(...Widget) Widget
|
||||||
|
|
||||||
type Enclosure interface {
|
type Enclosure interface {
|
||||||
@@ -39,30 +32,32 @@ type Enclosure interface {
|
|||||||
func Enclose(e Enclosure, w ...Widget) Widget { ... }
|
func Enclose(e Enclosure, w ...Widget) Widget { ... }
|
||||||
```
|
```
|
||||||
|
|
||||||
The `Context` struct also contains a `map[string]interface{}` allowing arbitrary
|
The giowrap package also includes a private package-global variable called
|
||||||
data to be maintained. This can be used, for example, to define themes, and control
|
extra that is a []interface{}, allowing arbitrary data to be maintained by
|
||||||
the operation of a `WidgetCombinator` during operation. For example>
|
UI elements. Elements can request access to the structure when they are
|
||||||
|
allocated and they are returned a unique integer index into the slice. This
|
||||||
|
can be used, for example, to define themes, and control
|
||||||
|
the operation of a `WidgetCombinator` during operation. For example:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
type fWidget struct { l Layout }
|
type fWidget struct { l Layout }
|
||||||
func NewfWidget(l layout) fWidget { return fWidget{ l: l } }
|
func NewfWidget(l layout) fWidget { return fWidget{ l: l } }
|
||||||
|
|
||||||
func (f Flex) Flexible(v float32) Widget {
|
func Flexible(v float32) Widget {
|
||||||
return NewfWidget(func(ctx Context) Context {
|
return NewfWidget(func(ctx Context) Context {
|
||||||
ctx.extra["Flexible"] = v
|
extra.data[extra.cur].(*FlexOpts).flexible = v
|
||||||
return ctx
|
return ctx
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
...
|
...
|
||||||
ctx = giowrap.LayoutWithContext(ctx,
|
ctx = myFlex(
|
||||||
myFlex(
|
|
||||||
w1,
|
w1,
|
||||||
giowrap.Flexible(0.5),
|
giowrap.Flexible(0.5),
|
||||||
w2,
|
w2,
|
||||||
giowrap.Flexible(1),
|
giowrap.Flexible(1),
|
||||||
w3,
|
w3,
|
||||||
))
|
).Layout(ctx)
|
||||||
ctx.Draw()
|
ctx.Draw()
|
||||||
...
|
...
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -0,0 +1,275 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func _daysIn(t time.Time) int {
|
||||||
|
y, m, _ := t.Date()
|
||||||
|
t2 := time.Date(y, m+1, 1, 0, 0, 0, 0, time.UTC)
|
||||||
|
return t2.AddDate(0, 0, -1).Day()
|
||||||
|
}
|
||||||
|
|
||||||
|
type SelectableMonth struct {
|
||||||
|
Ws [7]bool
|
||||||
|
S [32]bool
|
||||||
|
Year, Pad, DaysIn int
|
||||||
|
Month time.Month
|
||||||
|
Weekday time.Weekday
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMonth(t time.Time) *SelectableMonth {
|
||||||
|
t = time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.Local)
|
||||||
|
ret := &SelectableMonth{}
|
||||||
|
ret.Year, ret.Month, ret.Weekday = t.Year(), t.Month(), t.Weekday()
|
||||||
|
ret.DaysIn = _daysIn(t)
|
||||||
|
ret.Pad = int(ret.Weekday)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func _newMonth(y int, m time.Month) SelectableMonth {
|
||||||
|
return *NewMonth(time.Date(y, m, 1, 0, 0, 0, 0, time.Local))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sm *SelectableMonth) Refresh() {
|
||||||
|
*sm = _newMonth(sm.Year, sm.Month)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sm *SelectableMonth) Previous() {
|
||||||
|
m, y := sm.Month-1, sm.Year
|
||||||
|
if m > 11 {
|
||||||
|
m = 0
|
||||||
|
y += 1
|
||||||
|
}
|
||||||
|
*sm = _newMonth(y, m)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sm *SelectableMonth) Next() {
|
||||||
|
m, y := sm.Month+1, sm.Year
|
||||||
|
if m < 0 {
|
||||||
|
m = 11
|
||||||
|
y -= 1
|
||||||
|
}
|
||||||
|
*sm = _newMonth(y, m)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sm *SelectableMonth) SelectWeekday(d time.Weekday) *SelectableMonth {
|
||||||
|
for i := int(d) + 1 - sm.Pad; i <= 31; i += 7 {
|
||||||
|
if i < 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
sm.S[i] = true
|
||||||
|
}
|
||||||
|
sm.Ws[d] = true
|
||||||
|
return sm
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sm *SelectableMonth) DeselectWeekday(d time.Weekday) *SelectableMonth {
|
||||||
|
for i := int(d) + 1 - sm.Pad; i <= 31; i += 7 {
|
||||||
|
if i < 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
sm.S[i] = false
|
||||||
|
}
|
||||||
|
sm.Ws[d] = false
|
||||||
|
return sm
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sm *SelectableMonth) CheckSel() *SelectableMonth {
|
||||||
|
alldays := func(d time.Weekday) bool {
|
||||||
|
for i := int(d) + 1 - sm.Pad; i <= 31; i += 7 {
|
||||||
|
if i < 1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !sm.S[i] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
for d := time.Sunday; d < 7; d++ {
|
||||||
|
if alldays(d) {
|
||||||
|
sm.Ws[d] = true
|
||||||
|
} else {
|
||||||
|
sm.Ws[d] = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sm
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sm *SelectableMonth) Weekdays() int {
|
||||||
|
var i 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 sm.S[x] {
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
|
||||||
|
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++
|
||||||
|
}
|
||||||
|
x = w*7 + 6 + 1 - sm.Pad
|
||||||
|
if x < 0 || x >= sm.DaysIn {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if sm.S[x] {
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
|
||||||
|
// a day within a SelectableMonth
|
||||||
|
type Day struct {
|
||||||
|
Num int
|
||||||
|
sm *SelectableMonth
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sm *SelectableMonth) Day(i int) *Day {
|
||||||
|
ret := &Day{Num: i, sm: sm}
|
||||||
|
return ret.ifexists()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Day) exists() bool {
|
||||||
|
if d == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if d.Num > d.sm.DaysIn || d.Num <= 0 {
|
||||||
|
return false
|
||||||
|
} else {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Day) ifexists() *Day {
|
||||||
|
if d.exists() {
|
||||||
|
return d
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Day) Next() *Day {
|
||||||
|
ret := &Day{d.Num + 1, d.sm}
|
||||||
|
return ret.ifexists()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Day) Prev() *Day {
|
||||||
|
ret := &Day{d.Num - 1, d.sm}
|
||||||
|
return ret.ifexists()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sm *SelectableMonth) Days() []*Day {
|
||||||
|
ret := make([]*Day, 0)
|
||||||
|
for d := sm.Day(1); d.exists(); d = d.Next() {
|
||||||
|
ret = append(ret, d)
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Day) Weekday() time.Weekday {
|
||||||
|
return time.Weekday((d.Num + d.sm.Pad - 1) % 7)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Day) Selected() bool {
|
||||||
|
return d.sm.S[d.Num]
|
||||||
|
}
|
||||||
|
|
||||||
|
// a week within a SelectableMonth
|
||||||
|
|
||||||
|
type Week struct {
|
||||||
|
Num int
|
||||||
|
sm *SelectableMonth
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Week) exists() bool {
|
||||||
|
if w == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return w.Day(1).exists() // first day of this week exists
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Week) ifexists() *Week {
|
||||||
|
if w.exists() {
|
||||||
|
return w
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// week of a month, starting with Sundays except for the first week
|
||||||
|
func (sm *SelectableMonth) Week(i int) *Week {
|
||||||
|
ret := &Week{i, sm}
|
||||||
|
return ret.ifexists()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (sm *SelectableMonth) Weeks() []*Week {
|
||||||
|
ret := make([]*Week, 0)
|
||||||
|
for w := sm.Week(1); w.exists(); w = w.Next() {
|
||||||
|
ret = append(ret, w)
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Week) Day(i int) *Day {
|
||||||
|
if i < 1 || i > 7 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
wday := (w.Num-1)*7 + i
|
||||||
|
if w.Num != 1 {
|
||||||
|
wday -= w.sm.Pad
|
||||||
|
}
|
||||||
|
ret := &Day{wday, w.sm}
|
||||||
|
return ret.ifexists()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Week) Next() *Week {
|
||||||
|
ret := &Week{w.Num + 1, w.sm}
|
||||||
|
return ret.ifexists()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Week) Prev() *Week {
|
||||||
|
ret := &Week{w.Num - 1, w.sm}
|
||||||
|
return ret.ifexists()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *Week) Days() []*Day {
|
||||||
|
ret := make([]*Day, 0)
|
||||||
|
for d := w.Day(1); d.In(w); d = d.Next() {
|
||||||
|
ret = append(ret, d)
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Day) In(w *Week) bool {
|
||||||
|
if d == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
fd := w.Day(1)
|
||||||
|
if d.Num < fd.Num {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if d.Num > fd.Num && d.Weekday() == time.Sunday {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
+200
@@ -0,0 +1,200 @@
|
|||||||
|
// +build darwin linux
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"image/color"
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
gio "git.wow.st/gmp/giowrap"
|
||||||
|
|
||||||
|
"gioui.org/ui"
|
||||||
|
"gioui.org/ui/app"
|
||||||
|
"gioui.org/ui/layout"
|
||||||
|
"gioui.org/ui/text"
|
||||||
|
|
||||||
|
"golang.org/x/image/font/gofont/goregular"
|
||||||
|
"golang.org/x/image/font/sfnt"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
face text.Face
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
log.Print("Staring event loop")
|
||||||
|
go eventloop()
|
||||||
|
app.Main()
|
||||||
|
log.Print("App closed")
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewButton(t string, lops ...gio.LabelOption) gio.Clickable {
|
||||||
|
lops = append([]gio.LabelOption{gio.Face(face), gio.Align(text.Middle)}, lops...)
|
||||||
|
lbl := gio.NewLabel(t, lops...)
|
||||||
|
bg := gio.NewBackground(
|
||||||
|
gio.Color(color.RGBA{A: 0xff, R: 0xf0, G: 0xf0, B: 0xe0}),
|
||||||
|
gio.Radius(ui.Dp(4)))
|
||||||
|
return gio.AsClickable(bg(lbl))
|
||||||
|
}
|
||||||
|
|
||||||
|
type SLabel struct {
|
||||||
|
w gio.Clickable
|
||||||
|
bg *gio.Background
|
||||||
|
active *bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSLabel(t string, active *bool, lops ...gio.LabelOption) *SLabel {
|
||||||
|
ret := &SLabel{}
|
||||||
|
lops = append([]gio.LabelOption{gio.Face(face)}, lops...)
|
||||||
|
lbl := gio.NewLabel(t, lops...)
|
||||||
|
bg := &gio.Background{
|
||||||
|
Color: color.RGBA{A: 0xff, R: 0xf0, G: 0xf0, B: 0xe0},
|
||||||
|
Radius: ui.Dp(4),
|
||||||
|
Inset: layout.UniformInset(ui.Dp(4)),
|
||||||
|
}
|
||||||
|
if *active {
|
||||||
|
bg.Color = color.RGBA{A: 0xff, R: 0xf0, G: 0xf0, B: 0xe0}
|
||||||
|
}
|
||||||
|
l := gio.AsClickable(gio.Enclose(bg, lbl))
|
||||||
|
ret.w = l
|
||||||
|
ret.bg = bg
|
||||||
|
ret.active = active
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *SLabel) Layout(ctx *gio.Context) {
|
||||||
|
if *w.active {
|
||||||
|
w.bg.Color = color.RGBA{A: 0xff, R: 0xe0, G: 0xe0, B: 0xe0}
|
||||||
|
} else {
|
||||||
|
w.bg.Color = color.RGBA{A: 0xff, R: 0xf0, G: 0xf0, B: 0xe0}
|
||||||
|
}
|
||||||
|
w.w.Layout(ctx)
|
||||||
|
if w.w.Clicked(ctx) {
|
||||||
|
if *w.active {
|
||||||
|
*w.active = false
|
||||||
|
} else {
|
||||||
|
*w.active = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCal(sm *SelectableMonth) gio.Widget {
|
||||||
|
pad := gio.NewLabel("", gio.Face(face), gio.Align(text.End))
|
||||||
|
ds := make([]gio.Widget, 42)
|
||||||
|
for i := 0; i < len(ds); i++ {
|
||||||
|
if i < sm.Pad || i >= sm.DaysIn+sm.Pad {
|
||||||
|
ds[i] = pad
|
||||||
|
} else {
|
||||||
|
ds[i] = NewSLabel(fmt.Sprintf("%d", i+1-sm.Pad), &sm.S[i+1-sm.Pad], gio.Align(text.End))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return gio.NewGrid(7)(ds...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func eventloop() {
|
||||||
|
w := app.NewWindow(
|
||||||
|
app.WithWidth(ui.Dp(600)),
|
||||||
|
app.WithHeight(ui.Dp(600)),
|
||||||
|
app.WithTitle("Calendar"))
|
||||||
|
ctx := gio.NewContext(w)
|
||||||
|
sm := NewMonth(time.Now())
|
||||||
|
|
||||||
|
regular, err := sfnt.Parse(goregular.TTF)
|
||||||
|
face = ctx.Faces.For(regular, ui.Sp(20))
|
||||||
|
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.Vertical)
|
||||||
|
f2 := gio.NewFlex(gio.Horizontal)
|
||||||
|
sidegrid := gio.NewGrid(1)
|
||||||
|
yrs := make([]gio.Widget, 6)
|
||||||
|
curyr := sm.Year
|
||||||
|
|
||||||
|
topgrid := gio.NewGrid(3)
|
||||||
|
lbtn := NewButton(" < ")
|
||||||
|
mth := gio.NewLabel("", gio.Face(face), gio.Align(text.Middle))
|
||||||
|
rbtn := NewButton(" > ")
|
||||||
|
|
||||||
|
var cal gio.Widget
|
||||||
|
|
||||||
|
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))
|
||||||
|
tue := NewSLabel("Tue", &sm.Ws[time.Tuesday], gio.Align(text.End))
|
||||||
|
wed := NewSLabel("Wed", &sm.Ws[time.Wednesday], gio.Align(text.End))
|
||||||
|
thu := NewSLabel("Thu", &sm.Ws[time.Thursday], gio.Align(text.End))
|
||||||
|
fri := NewSLabel("Fri", &sm.Ws[time.Friday], gio.Align(text.End))
|
||||||
|
sat := NewSLabel("Sat", &sm.Ws[time.Saturday], gio.Align(text.End))
|
||||||
|
|
||||||
|
resetCal := func() {
|
||||||
|
cal = NewCal(sm)
|
||||||
|
for i, _ := range yrs {
|
||||||
|
yrs[i] = NewButton(fmt.Sprintf("%d", curyr-i),
|
||||||
|
gio.Align(text.Start))
|
||||||
|
}
|
||||||
|
w.Invalidate()
|
||||||
|
}
|
||||||
|
resetCal()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case e := <-w.Events():
|
||||||
|
switch e := e.(type) {
|
||||||
|
case app.DestroyEvent:
|
||||||
|
return
|
||||||
|
case app.UpdateEvent:
|
||||||
|
ctx.Reset(&e)
|
||||||
|
|
||||||
|
mth.SetText(fmt.Sprintf("%s %d", sm.Month.String(), sm.Year))
|
||||||
|
|
||||||
|
ows := sm.Ws
|
||||||
|
bg(margin(f1(
|
||||||
|
topgrid(lbtn, mth, rbtn),
|
||||||
|
f2(
|
||||||
|
sidegrid(yrs...),
|
||||||
|
f3(
|
||||||
|
daygrid(sun, mon, tue, wed, thu, fri, sat),
|
||||||
|
cal,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
))).Layout(ctx)
|
||||||
|
ctx.Update()
|
||||||
|
|
||||||
|
for i := time.Sunday; i < 7; i++ {
|
||||||
|
switch {
|
||||||
|
case sm.Ws[i] && !ows[i]:
|
||||||
|
sm.SelectWeekday(i)
|
||||||
|
resetCal()
|
||||||
|
case !sm.Ws[i] && ows[i]:
|
||||||
|
sm.DeselectWeekday(i)
|
||||||
|
resetCal()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if lbtn.Clicked(ctx) {
|
||||||
|
sm.Previous()
|
||||||
|
resetCal()
|
||||||
|
}
|
||||||
|
if rbtn.Clicked(ctx) {
|
||||||
|
sm.Next()
|
||||||
|
resetCal()
|
||||||
|
}
|
||||||
|
for i, y := range yrs {
|
||||||
|
if y.(gio.Clickable).Clicked(ctx) {
|
||||||
|
sm.Year = curyr - i
|
||||||
|
sm.Refresh()
|
||||||
|
resetCal()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
+528
-28
@@ -3,73 +3,573 @@ package main
|
|||||||
import (
|
import (
|
||||||
"git.wow.st/gmp/giowrap"
|
"git.wow.st/gmp/giowrap"
|
||||||
|
|
||||||
|
"fmt"
|
||||||
|
"image"
|
||||||
|
"image/color"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
|
"runtime"
|
||||||
|
"runtime/pprof"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gioui.org/ui"
|
"gioui.org/ui"
|
||||||
"gioui.org/ui/app"
|
"gioui.org/ui/app"
|
||||||
|
"gioui.org/ui/f32"
|
||||||
|
"gioui.org/ui/gesture"
|
||||||
"gioui.org/ui/layout"
|
"gioui.org/ui/layout"
|
||||||
|
"gioui.org/ui/measure"
|
||||||
|
"gioui.org/ui/paint"
|
||||||
|
"gioui.org/ui/pointer"
|
||||||
"gioui.org/ui/text"
|
"gioui.org/ui/text"
|
||||||
|
|
||||||
"golang.org/x/image/font/sfnt"
|
|
||||||
"golang.org/x/image/font/gofont/goregular"
|
"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)))
|
||||||
|
return giowrap.AsClickable(bg(lbl))
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
go func() {
|
go func() {
|
||||||
w := app.NewWindow(nil)
|
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()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
switch {
|
||||||
|
case len(os.Args) < 2:
|
||||||
|
fallthrough
|
||||||
|
default:
|
||||||
|
log.Fatal(`Usage:
|
||||||
|
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)
|
regular, err := sfnt.Parse(goregular.TTF)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Cannot parse font.")
|
log.Fatal("Cannot parse font.")
|
||||||
}
|
}
|
||||||
ctx := giowrap.NewContext(w)
|
ctx := giowrap.NewContext(w)
|
||||||
t := time.NewTicker(time.Second/30)
|
t := time.NewTicker(time.Second / time.Duration(FPS))
|
||||||
e1 := giowrap.NewEditor(ctx.Faces.For(regular, ui.Sp(24)), true)
|
e1 := giowrap.NewEditor("text 1", giowrap.Face(ctx.Faces.For(regular, ui.Sp(24))))
|
||||||
e1.SetText("hi there")
|
|
||||||
e1.Focus()
|
e1.Focus()
|
||||||
|
|
||||||
e2 := giowrap.NewEditor(ctx.Faces.For(regular, ui.Sp(24)), true)
|
e2 := giowrap.NewEditor("text 2", giowrap.Face(ctx.Faces.For(regular, ui.Sp(24))))
|
||||||
e2.SetText("ok bye")
|
|
||||||
|
|
||||||
f := giowrap.NewFlex(layout.Vertical, layout.Start, layout.Start)
|
f1 := giowrap.NewFlex(giowrap.Vertical)
|
||||||
OuterInset := giowrap.NewInset(ui.Dp(10),ui.Dp(10),ui.Dp(10),ui.Dp(10))
|
OuterInset := giowrap.NewInset(giowrap.Size(ui.Dp(10)))
|
||||||
InnerInset := giowrap.NewInset(ui.Dp(10),ui.Dp(10),ui.Dp(10),ui.Dp(10))
|
InnerInset := giowrap.NewInset(giowrap.Size(ui.Dp(10)))
|
||||||
|
|
||||||
lbl := giowrap.NewButton(
|
f2 := giowrap.NewFlex(giowrap.Horizontal)
|
||||||
ctx.Faces.For(regular, ui.Sp(24)),
|
|
||||||
"centered",
|
btn1 := NewButton(ctx.Faces.For(regular, ui.Sp(24)),
|
||||||
text.Center,
|
"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),
|
||||||
|
),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
bg := giowrap.NewBackground(giowrap.Rgb(0x3c98c6), ui.Dp(4))
|
|
||||||
btn := bg(lbl)
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-t.C:
|
case <-t.C:
|
||||||
w.Invalidate()
|
w.Invalidate()
|
||||||
case e := <-w.Events():
|
case e := <-w.Events():
|
||||||
|
mux.Lock()
|
||||||
|
stime := time.Now()
|
||||||
switch e := e.(type) {
|
switch e := e.(type) {
|
||||||
case app.DestroyEvent:
|
case app.DestroyEvent:
|
||||||
return
|
return
|
||||||
case app.DrawEvent:
|
case app.UpdateEvent:
|
||||||
ctx.Reset(e)
|
profileNow := false
|
||||||
ctx = giowrap.LayoutWithContext(ctx,
|
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(
|
OuterInset(
|
||||||
f(
|
f1(
|
||||||
e1,
|
e1,
|
||||||
giowrap.Flexible(5),
|
giowrap.Flexible(0.33),
|
||||||
InnerInset(e2),
|
InnerInset(e2),
|
||||||
giowrap.Flexible(10),
|
giowrap.Flexible(0.67),
|
||||||
btn,
|
f2(
|
||||||
)))
|
InnerInset(btn1),
|
||||||
ctx.Draw()
|
giowrap.Flexible(0.50),
|
||||||
if lbl.Clicked(ctx) {
|
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})
|
||||||
|
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():
|
||||||
|
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 {
|
||||||
|
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),
|
||||||
|
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()
|
||||||
|
if btn1.Clicked(ctx) {
|
||||||
|
log.Print("Clicked: " + e1.Text())
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
mux.Unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Example using the base Gio API.
|
||||||
|
func main4() {
|
||||||
|
w := app.NewWindow()
|
||||||
|
q := w.Queue()
|
||||||
|
ops := new(ui.Ops)
|
||||||
|
var faces measure.Faces
|
||||||
|
regular, err := sfnt.Parse(goregular.TTF)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Cannot parse font.")
|
||||||
|
}
|
||||||
|
t := time.NewTicker(time.Second / time.Duration(FPS))
|
||||||
|
e1 := &text.Editor{
|
||||||
|
Face: faces.For(regular, ui.Sp(24)),
|
||||||
|
SingleLine: true,
|
||||||
|
}
|
||||||
|
e1.Focus()
|
||||||
|
e1.SetText("text 1")
|
||||||
|
|
||||||
|
e2 := &text.Editor{
|
||||||
|
Face: faces.For(regular, ui.Sp(24)),
|
||||||
|
SingleLine: true,
|
||||||
|
}
|
||||||
|
e2.Focus()
|
||||||
|
e2.SetText("text 2")
|
||||||
|
|
||||||
|
btn1 := &Button{
|
||||||
|
Face: faces.For(regular, ui.Sp(24)),
|
||||||
|
Label: "push1",
|
||||||
|
}
|
||||||
|
btn2 := &Button{
|
||||||
|
Face: faces.For(regular, ui.Sp(24)),
|
||||||
|
Label: "push2",
|
||||||
|
}
|
||||||
|
|
||||||
|
profiled := false
|
||||||
|
startTime := time.Now()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-t.C:
|
||||||
|
w.Invalidate()
|
||||||
|
case e := <-w.Events():
|
||||||
|
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 {
|
||||||
|
profileNow = true
|
||||||
|
startTime = time.Now()
|
||||||
|
}
|
||||||
|
if profileNow {
|
||||||
|
if !profiled {
|
||||||
|
prof(0)
|
||||||
|
}
|
||||||
|
runtime.ReadMemStats(&ms)
|
||||||
|
}
|
||||||
|
c := &e.Config
|
||||||
|
ops.Reset()
|
||||||
|
faces.Reset(c)
|
||||||
|
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 := btn2.Click.Next(q); ok; ev, ok = btn2.Click.Next(q) {
|
||||||
|
if ev.Type == gesture.TypeClick {
|
||||||
log.Print("Clicked: " + e2.Text())
|
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
|
||||||
|
}
|
||||||
|
mux.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
|
||||||
app.Main()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,186 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"image/color"
|
||||||
|
"log"
|
||||||
|
"runtime"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
gio "git.wow.st/gmp/giowrap"
|
||||||
|
|
||||||
|
"gioui.org/ui"
|
||||||
|
"gioui.org/ui/app"
|
||||||
|
"gioui.org/ui/layout"
|
||||||
|
"gioui.org/ui/text"
|
||||||
|
|
||||||
|
"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: 0xc0, G: 0xc0, B: 0xc0}
|
||||||
|
gray1 color.RGBA = color.RGBA{A: 0xff, R: 0x70, G: 0x70, B: 0x70}
|
||||||
|
)
|
||||||
|
|
||||||
|
type SLabel struct {
|
||||||
|
w gio.Clickable
|
||||||
|
bg *gio.Background
|
||||||
|
active *bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSLabel(t string, active *bool, lops ...gio.LabelOption) *SLabel {
|
||||||
|
ret := &SLabel{}
|
||||||
|
lops = append([]gio.LabelOption{gio.Face(face)}, lops...)
|
||||||
|
lbl := gio.NewLabel(t, lops...)
|
||||||
|
bg := &gio.Background{
|
||||||
|
Color: gray2,
|
||||||
|
Radius: ui.Dp(4),
|
||||||
|
Inset: layout.UniformInset(ui.Dp(4)),
|
||||||
|
}
|
||||||
|
if *active {
|
||||||
|
bg.Color = gray1
|
||||||
|
}
|
||||||
|
l := gio.AsClickable(gio.Enclose(bg, lbl))
|
||||||
|
ret.w = l
|
||||||
|
ret.bg = bg
|
||||||
|
ret.active = active
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *SLabel) Layout(ctx *gio.Context) {
|
||||||
|
if *w.active {
|
||||||
|
w.bg.Color = gray1
|
||||||
|
} else {
|
||||||
|
w.bg.Color = gray2
|
||||||
|
}
|
||||||
|
w.w.Layout(ctx)
|
||||||
|
if w.w.Clicked(ctx) {
|
||||||
|
if *w.active {
|
||||||
|
*w.active = false
|
||||||
|
} else {
|
||||||
|
*w.active = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
log.Print("Staring event loop")
|
||||||
|
go eventloop()
|
||||||
|
app.Main()
|
||||||
|
log.Print("App closed")
|
||||||
|
}
|
||||||
|
|
||||||
|
func diffInsets(x, y app.Insets) bool {
|
||||||
|
return x.Top != y.Top ||
|
||||||
|
x.Bottom != y.Bottom ||
|
||||||
|
x.Left != y.Left ||
|
||||||
|
x.Right != y.Right
|
||||||
|
}
|
||||||
|
|
||||||
|
func eventloop() {
|
||||||
|
w := app.NewWindow(
|
||||||
|
app.WithWidth(ui.Dp(400)),
|
||||||
|
app.WithHeight(ui.Dp(400)),
|
||||||
|
app.WithTitle("Tickets"))
|
||||||
|
ctx := gio.NewContext(w)
|
||||||
|
|
||||||
|
regular, err := sfnt.Parse(goregular.TTF)
|
||||||
|
face = ctx.Faces.For(regular, ui.Sp(16))
|
||||||
|
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.Horizontal)
|
||||||
|
lbar := gio.NewLabel(" ", gio.Face(face))
|
||||||
|
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.Horizontal))
|
||||||
|
sv := gio.VScroll(gio.NewFlex(gio.Vertical))
|
||||||
|
|
||||||
|
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])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sysinset := gio.NewInset(gio.Size(ui.Dp(0)))
|
||||||
|
resetSysinset := func(x app.Insets) {
|
||||||
|
sysinset = gio.NewInset(gio.Top(x.Top), gio.Bottom(x.Bottom),
|
||||||
|
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():
|
||||||
|
switch e := e.(type) {
|
||||||
|
case app.DestroyEvent:
|
||||||
|
return
|
||||||
|
case app.UpdateEvent:
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
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]
|
||||||
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
package giowrap
|
package giowrap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"image"
|
||||||
"image/color"
|
"image/color"
|
||||||
"log"
|
//"log"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"gioui.org/ui"
|
"gioui.org/ui"
|
||||||
"gioui.org/ui/app"
|
"gioui.org/ui/app"
|
||||||
@@ -12,11 +14,31 @@ import (
|
|||||||
"gioui.org/ui/text"
|
"gioui.org/ui/text"
|
||||||
|
|
||||||
"gioui.org/ui/f32"
|
"gioui.org/ui/f32"
|
||||||
gdraw "gioui.org/ui/draw"
|
|
||||||
"gioui.org/ui/gesture"
|
"gioui.org/ui/gesture"
|
||||||
|
"gioui.org/ui/paint"
|
||||||
"gioui.org/ui/pointer"
|
"gioui.org/ui/pointer"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Extra struct {
|
||||||
|
cur, max int
|
||||||
|
sync.Mutex
|
||||||
|
data []interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
var extra Extra
|
||||||
|
|
||||||
|
func (e *Extra) New() int {
|
||||||
|
e.Lock()
|
||||||
|
if e.data == nil {
|
||||||
|
e.data = make([]interface{}, 0)
|
||||||
|
}
|
||||||
|
ret := e.max
|
||||||
|
e.max = e.max + 1
|
||||||
|
e.data = append(e.data, nil)
|
||||||
|
e.Unlock()
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
type Context struct {
|
type Context struct {
|
||||||
Faces measure.Faces
|
Faces measure.Faces
|
||||||
|
|
||||||
@@ -25,180 +47,350 @@ type Context struct {
|
|||||||
q input.Queue
|
q input.Queue
|
||||||
ops *ui.Ops
|
ops *ui.Ops
|
||||||
cs layout.Constraints
|
cs layout.Constraints
|
||||||
dims layout.Dimens
|
dims layout.Dimensions
|
||||||
extra map[string]interface{}
|
container Widget
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewContext(w *app.Window) Context {
|
func NewContext(w *app.Window) *Context {
|
||||||
return Context{
|
return &Context{
|
||||||
w: w,
|
w: w,
|
||||||
ops: new(ui.Ops),
|
ops: new(ui.Ops),
|
||||||
q: w.Queue(),
|
q: w.Queue(),
|
||||||
extra: make(map[string]interface{}),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *Context) Reset(e app.DrawEvent) {
|
func (ctx *Context) Reset(e *app.UpdateEvent) {
|
||||||
|
Mallocs("ctx.Reset() ctx.c")
|
||||||
ctx.c = &e.Config
|
ctx.c = &e.Config
|
||||||
ctx.ops.Reset()
|
ctx.ops.Reset()
|
||||||
ctx.cs = layout.RigidConstraints(e.Size)
|
ctx.cs = layout.RigidConstraints(e.Size)
|
||||||
ctx.Faces.Reset(ctx.c)
|
ctx.Faces.Reset(ctx.c)
|
||||||
|
Mallocs("ctx.Reset() done")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx Context) Draw() {
|
func (ctx *Context) Update() {
|
||||||
ctx.w.Draw(ctx.ops)
|
ctx.w.Update(ctx.ops)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Layout func(Context) Context
|
type Layout func(*Context)
|
||||||
|
|
||||||
func LayoutWithContext(ctx Context, ws ...Widget) Context {
|
|
||||||
for _, w := range ws {
|
|
||||||
ctx = w.Layout(ctx)
|
|
||||||
}
|
|
||||||
return ctx
|
|
||||||
}
|
|
||||||
|
|
||||||
type fWidget struct {
|
|
||||||
l Layout
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewfWidget(l Layout) fWidget {
|
|
||||||
return fWidget{ l: l }
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fw fWidget) Layout(ctx Context) Context {
|
|
||||||
return fw.l(ctx)
|
|
||||||
}
|
|
||||||
|
|
||||||
type Widget interface {
|
type Widget interface {
|
||||||
Layout(Context) Context
|
Layout(*Context)
|
||||||
}
|
}
|
||||||
|
|
||||||
type WidgetCombinator func(...Widget) Widget
|
type WidgetCombinator func(...Widget) Widget
|
||||||
|
|
||||||
type Button struct {
|
type Label struct {
|
||||||
l *text.Label
|
l *text.Label
|
||||||
click gesture.Click
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewButton(face text.Face, t string, alignment text.Alignment) *Button {
|
type FaceOpt struct{ face text.Face }
|
||||||
ret := &Button{}
|
|
||||||
|
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 {
|
||||||
|
FaceOpt
|
||||||
|
c *color.RGBA
|
||||||
|
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 NewLabel(t string, lops ...LabelOption) *Label {
|
||||||
|
ret := &Label{}
|
||||||
|
opts := &LabelOpts{}
|
||||||
|
for _, o := range lops {
|
||||||
|
o.DoLabelOption(opts)
|
||||||
|
}
|
||||||
ret.l = &text.Label{
|
ret.l = &text.Label{
|
||||||
Face: face,
|
Face: opts.face,
|
||||||
Text: t,
|
Text: t,
|
||||||
Alignment: alignment,
|
Alignment: opts.alignment,
|
||||||
|
}
|
||||||
|
if opts.c != nil { // got a color option...
|
||||||
|
ops := new(ui.Ops)
|
||||||
|
ret.l.Material.Record(ops)
|
||||||
|
paint.ColorOp{Color: *opts.c}.Add(ops)
|
||||||
|
ret.l.Material.Stop()
|
||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Button) Layout(ctx Context) Context {
|
func (l *Label) Layout(ctx *Context) {
|
||||||
ctx.dims = b.l.Layout(ctx.ops, ctx.cs)
|
Mallocs("Label.Layout()")
|
||||||
pointer.RectAreaOp{Size: ctx.dims.Size}.Add(ctx.ops)
|
ctx.dims = l.l.Layout(ctx.ops, ctx.cs)
|
||||||
b.click.Add(ctx.ops)
|
|
||||||
return ctx
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Button) Clicked(ctx Context) bool {
|
func (l *Label) SetText(t string) {
|
||||||
for _,e := range b.click.Events(ctx.q) {
|
l.l.Text = t
|
||||||
if e.Type == gesture.TypeClick {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Editor struct {
|
type Editor struct {
|
||||||
e *text.Editor
|
e *text.Editor
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEditor(face text.Face, singleline bool) *Editor {
|
type SinglelineOpt struct{ singleline bool }
|
||||||
|
|
||||||
|
func Singleline(x bool) SinglelineOpt { return SinglelineOpt{x} }
|
||||||
|
|
||||||
|
type EditorOpts struct {
|
||||||
|
FaceOpt
|
||||||
|
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{}
|
ret := &Editor{}
|
||||||
ret.e = &text.Editor{ Face: face, SingleLine: singleline }
|
opts := &EditorOpts{}
|
||||||
|
for _, o := range eops {
|
||||||
|
o.DoEditorOption(opts)
|
||||||
|
}
|
||||||
|
ret.e = &text.Editor{Face: opts.face, SingleLine: opts.singleline}
|
||||||
|
ret.SetText(t)
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Editor) Layout(ctx Context) Context {
|
func (e *Editor) Layout(ctx *Context) {
|
||||||
|
Mallocs("Editor.Layout()")
|
||||||
ctx.dims = e.e.Layout(ctx.c, ctx.q, ctx.ops, ctx.cs)
|
ctx.dims = e.e.Layout(ctx.c, ctx.q, ctx.ops, ctx.cs)
|
||||||
return ctx
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Editor) Text() string {
|
func (e *Editor) Text() string { return e.e.Text() }
|
||||||
return e.e.Text()
|
func (e *Editor) SetText(s string) { e.e.SetText(s) }
|
||||||
|
func (e *Editor) Focus() { e.e.Focus() }
|
||||||
|
|
||||||
|
type Stack WidgetCombinator
|
||||||
|
|
||||||
|
type stackWidget struct {
|
||||||
|
s layout.Stack
|
||||||
|
scs []layout.StackChild
|
||||||
|
ws []Widget
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Editor) SetText(s string) {
|
func NewStack() Stack {
|
||||||
e.e.SetText(s)
|
ret := &stackWidget{}
|
||||||
}
|
ret.s = layout.Stack{Alignment: layout.Center}
|
||||||
|
ret.scs = make([]layout.StackChild, 0)
|
||||||
|
|
||||||
func (e *Editor) Focus() {
|
|
||||||
e.e.Focus()
|
|
||||||
}
|
|
||||||
|
|
||||||
func Rgb(c uint32) color.RGBA {
|
|
||||||
return Argb((0xff << 24) | c)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Argb(c uint32) color.RGBA {
|
|
||||||
return color.RGBA{A: uint8(c >> 24), R: uint8(c >> 16), G: uint8(c >> 8), B: uint8(c)}
|
|
||||||
}
|
|
||||||
|
|
||||||
type FlexChild struct {
|
|
||||||
x float32
|
|
||||||
w Widget
|
|
||||||
}
|
|
||||||
|
|
||||||
type Flex WidgetCombinator
|
|
||||||
|
|
||||||
func Flexible(v float32) Widget {
|
|
||||||
return NewfWidget(func(ctx Context) Context {
|
|
||||||
ctx.extra["Flexible"] = v
|
|
||||||
return ctx
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewFlex(axis layout.Axis, mainAxisAlignment layout.MainAxisAlignment, crossAxisAlignment layout.CrossAxisAlignment) Flex {
|
|
||||||
f := layout.Flex{
|
|
||||||
Axis: axis,
|
|
||||||
MainAxisAlignment: mainAxisAlignment,
|
|
||||||
CrossAxisAlignment: crossAxisAlignment,
|
|
||||||
}
|
|
||||||
return func(ws ...Widget) Widget {
|
return func(ws ...Widget) Widget {
|
||||||
return NewfWidget(func(ctx Context) Context {
|
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)
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
ret.l = layout.List{Axis: opts.axis}
|
||||||
|
|
||||||
|
return func(ws ...Widget) Widget {
|
||||||
|
ret.ws = ws
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
return NewList(Axis(layout.Horizontal))
|
||||||
|
}
|
||||||
|
|
||||||
|
func VScroll(c WidgetCombinator) WidgetCombinator {
|
||||||
|
return NewList(Axis(layout.Vertical))
|
||||||
|
}
|
||||||
|
|
||||||
|
type Flex = WidgetCombinator
|
||||||
|
|
||||||
|
type AlignmentOpt struct{ alignment layout.Alignment }
|
||||||
|
|
||||||
|
func Alignment(x layout.Alignment) AlignmentOpt { return AlignmentOpt{x} }
|
||||||
|
|
||||||
|
type FlexOpts struct {
|
||||||
|
AxisOpt
|
||||||
|
AlignmentOpt
|
||||||
|
flexible float32
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
f := layout.Flex{
|
||||||
|
Axis: opts.axis,
|
||||||
|
Alignment: opts.alignment,
|
||||||
|
}
|
||||||
|
fcs := make([]layout.FlexChild, 0)
|
||||||
|
return func(ws ...Widget) Widget {
|
||||||
|
ret := &flexWidget{}
|
||||||
|
ret.f = func(ctx *Context) {
|
||||||
|
oldContainer := ctx.container
|
||||||
|
ctx.container = ret
|
||||||
f.Init(ctx.ops, ctx.cs)
|
f.Init(ctx.ops, ctx.cs)
|
||||||
fcs := make([]layout.FlexChild,len(ws))
|
for _, w := range ws {
|
||||||
for i, w := range ws {
|
|
||||||
if v,ok := ctx.extra["Flexible"]; ok {
|
|
||||||
switch v := v.(type) {
|
|
||||||
case float32:
|
|
||||||
ctx.cs = f.Flexible(v)
|
|
||||||
default:
|
|
||||||
log.Fatal("Type error")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ctx.cs = f.Rigid()
|
ctx.cs = f.Rigid()
|
||||||
}
|
w.Layout(ctx)
|
||||||
ctx = w.Layout(ctx)
|
fcs = append(fcs, f.End(ctx.dims))
|
||||||
fcs[i] = f.End(ctx.dims)
|
|
||||||
}
|
}
|
||||||
ctx.dims = f.Layout(fcs...)
|
ctx.dims = f.Layout(fcs...)
|
||||||
delete(ctx.extra, "Flexible")
|
fcs = fcs[0:0]
|
||||||
return ctx
|
ctx.container = oldContainer
|
||||||
})
|
}
|
||||||
|
return ret
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewInset(top, right, bottom, left ui.Value) WidgetCombinator {
|
type InsetOpts struct {
|
||||||
ins := layout.Inset{ Top: top, Right: right, Bottom: bottom, Left: left }
|
top, right, bottom, left ui.Value
|
||||||
return func(ws ...Widget) Widget {
|
|
||||||
return NewfWidget(func(ctx Context) Context {
|
|
||||||
ctx.cs = ins.Begin(ctx.c, ctx.ops, ctx.cs)
|
|
||||||
var dims layout.Dimens
|
|
||||||
for _, w := range ws {
|
|
||||||
ctx = w.Layout(ctx)
|
|
||||||
}
|
}
|
||||||
ctx.dims = ins.End(dims)
|
|
||||||
return ctx
|
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 }
|
||||||
|
func (x RightOpt) DoInsetOption(o *InsetOpts) { o.right = x.right }
|
||||||
|
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
|
||||||
|
o.right = x.size
|
||||||
|
o.bottom = x.size
|
||||||
|
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) 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 {
|
||||||
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -211,42 +403,75 @@ type Background struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Enclosure interface {
|
type Enclosure interface {
|
||||||
Begin(Context) Context
|
Begin(*Context)
|
||||||
End(Context) Context
|
End(*Context)
|
||||||
|
}
|
||||||
|
|
||||||
|
type encloseWidget struct {
|
||||||
|
enc Enclosure
|
||||||
|
ws []Widget
|
||||||
}
|
}
|
||||||
|
|
||||||
func Enclose(e Enclosure, ws ...Widget) Widget {
|
func Enclose(e Enclosure, ws ...Widget) Widget {
|
||||||
return NewfWidget(func(ctx Context) Context {
|
return &encloseWidget{e, ws}
|
||||||
ctx = e.Begin(ctx)
|
|
||||||
for _,w := range ws {
|
|
||||||
ctx = w.Layout(ctx)
|
|
||||||
}
|
|
||||||
ctx = e.End(ctx)
|
|
||||||
return ctx
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBackground(color color.RGBA, radius ui.Value) WidgetCombinator {
|
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.enc.End(ctx)
|
||||||
|
ctx.container = oldContainer
|
||||||
|
}
|
||||||
|
|
||||||
|
type BackgroundOpts struct {
|
||||||
|
c color.RGBA
|
||||||
|
radius ui.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
bg := &Background{
|
bg := &Background{
|
||||||
Color: color,
|
Color: opts.c,
|
||||||
Radius: radius,
|
Radius: opts.radius,
|
||||||
Inset: layout.UniformInset(radius), // FIXME: need to be able to
|
Inset: layout.UniformInset(opts.radius),
|
||||||
} // do math ops on Values
|
}
|
||||||
return func(ws ...Widget) Widget {
|
return func(ws ...Widget) Widget {
|
||||||
return Enclose(bg, ws...)
|
return Enclose(bg, ws...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bg *Background) Begin(ctx Context) Context {
|
func (bg *Background) Begin(ctx *Context) {
|
||||||
|
Mallocs("Background.Begin()")
|
||||||
bg.macro.Record(ctx.ops)
|
bg.macro.Record(ctx.ops)
|
||||||
ctx.cs = bg.Inset.Begin(ctx.c, ctx.ops, ctx.cs)
|
ctx.cs = bg.Inset.Begin(ctx.c, ctx.ops, ctx.cs)
|
||||||
return ctx
|
Mallocs("Background.Begin() done")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bg *Background) End(ctx Context) Context {
|
func (bg *Background) End(ctx *Context) {
|
||||||
|
Mallocs("Background.End()")
|
||||||
ctx.dims = bg.Inset.End(ctx.dims)
|
ctx.dims = bg.Inset.End(ctx.dims)
|
||||||
bg.macro.Stop()
|
bg.macro.Stop()
|
||||||
//var stack ui.StackOp
|
var stack ui.StackOp
|
||||||
|
stack.Push(ctx.ops)
|
||||||
w, h := float32(ctx.dims.Size.X), float32(ctx.dims.Size.Y)
|
w, h := float32(ctx.dims.Size.X), float32(ctx.dims.Size.Y)
|
||||||
if r := float32(ctx.c.Px(bg.Radius)); r > 0 {
|
if r := float32(ctx.c.Px(bg.Radius)); r > 0 {
|
||||||
if r > w/2 {
|
if r > w/2 {
|
||||||
@@ -255,20 +480,25 @@ func (bg *Background) End(ctx Context) Context {
|
|||||||
if r > h/2 {
|
if r > h/2 {
|
||||||
r = h / 2
|
r = h / 2
|
||||||
}
|
}
|
||||||
rrect(ctx.ops, w, h, r, r, r, r)
|
Rrect(ctx.ops, w, h, r, r, r, r)
|
||||||
}
|
}
|
||||||
gdraw.ColorOp{Color: bg.Color}.Add(ctx.ops)
|
paint.ColorOp{Color: bg.Color}.Add(ctx.ops)
|
||||||
gdraw.DrawOp{Rect: f32.Rectangle{Max: f32.Point{X: w, Y: h}}}.Add(ctx.ops)
|
paint.PaintOp{Rect: f32.Rectangle{Max: f32.Point{X: w, Y: h}}}.Add(ctx.ops)
|
||||||
bg.macro.Add(ctx.ops)
|
bg.macro.Add(ctx.ops)
|
||||||
return ctx
|
stack.Pop()
|
||||||
|
Mallocs("Background.End() done")
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://pomax.github.io/bezierinfo/#circles_cubic.
|
// https://pomax.github.io/bezierinfo/#circles_cubic.
|
||||||
func rrect(ops *ui.Ops, width, height, se, sw, nw, ne float32) {
|
func Rrect(ops *ui.Ops, width, height, se, sw, nw, ne float32) {
|
||||||
|
Mallocs("Rrect() start")
|
||||||
w, h := float32(width), float32(height)
|
w, h := float32(width), float32(height)
|
||||||
|
Mallocs("Rrect() const")
|
||||||
const c = 0.55228475 // 4*(sqrt(2)-1)/3
|
const c = 0.55228475 // 4*(sqrt(2)-1)/3
|
||||||
var b gdraw.PathBuilder
|
var b paint.PathBuilder
|
||||||
|
Mallocs("Rrect() init")
|
||||||
b.Init(ops)
|
b.Init(ops)
|
||||||
|
Mallocs("Rrect() move")
|
||||||
b.Move(f32.Point{X: w, Y: h - se})
|
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.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})
|
b.Line(f32.Point{X: sw - w + se, Y: 0})
|
||||||
@@ -278,5 +508,62 @@ func rrect(ops *ui.Ops, width, height, se, sw, nw, ne float32) {
|
|||||||
b.Line(f32.Point{X: w - ne - nw, Y: 0})
|
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.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()
|
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 {
|
||||||
|
Widget
|
||||||
|
Clicked(*Context) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
//cWidget is a clickable Widget that provides the Clicked() method.
|
||||||
|
type cWidget struct {
|
||||||
|
w Widget
|
||||||
|
click *gesture.Click
|
||||||
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
//Clickable converts any Widget into a clickable Widget.
|
||||||
|
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