Compare commits

..

No commits in common. "635655e76873e4cc6b264699982e64f5850aa455" and "f96cdb619a32c5b7ee11e7b46af321ba2a129092" have entirely different histories.

7 changed files with 469 additions and 621 deletions

View File

@ -5,9 +5,9 @@ import (
)
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()
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 {
@ -19,7 +19,7 @@ type SelectableMonth struct {
}
func NewMonth(t time.Time) *SelectableMonth {
t = time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.Local)
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)
@ -28,7 +28,7 @@ func NewMonth(t time.Time) *SelectableMonth {
}
func _newMonth(y int, m time.Month) SelectableMonth {
return *NewMonth(time.Date(y, m, 1, 0, 0, 0, 0, time.Local))
return *NewMonth(time.Date(y,m,1,0,0,0,0,time.Local))
}
func (sm *SelectableMonth) Refresh() {
@ -36,21 +36,21 @@ func (sm *SelectableMonth) Refresh() {
}
func (sm *SelectableMonth) Previous() {
m, y := sm.Month-1, sm.Year
m, y := sm.Month - 1, sm.Year
if m > 11 {
m = 0
y += 1
}
*sm = _newMonth(y, m)
*sm = _newMonth(y,m)
}
func (sm *SelectableMonth) Next() {
m, y := sm.Month+1, sm.Year
if m < 0 {
m, y := sm.Month + 1, sm.Year
if m <0 {
m = 11
y -= 1
}
*sm = _newMonth(y, m)
*sm = _newMonth(y,m)
}
func (sm *SelectableMonth) SelectWeekday(d time.Weekday) *SelectableMonth {
@ -77,7 +77,7 @@ func (sm *SelectableMonth) DeselectWeekday(d time.Weekday) *SelectableMonth {
func (sm *SelectableMonth) CheckSel() *SelectableMonth {
alldays := func(d time.Weekday) bool {
for i := int(d) + 1 - sm.Pad; i <= 31; i += 7 {
for i := int(d) + 1 - sm.Pad; i <= 31; i+= 7 {
if i < 1 {
continue
}
@ -102,10 +102,8 @@ 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
}
x := w * 7 + d + 1 - sm.Pad
if x < 0 || x >= sm.DaysIn { break }
if sm.S[x] {
i++
}
@ -118,22 +116,12 @@ 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++
}
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
}
@ -169,19 +157,19 @@ func (d *Day) ifexists() *Day {
}
func (d *Day) Next() *Day {
ret := &Day{d.Num + 1, d.sm}
ret := &Day{d.Num+1, d.sm}
return ret.ifexists()
}
func (d *Day) Prev() *Day {
ret := &Day{d.Num - 1, d.sm}
ret := &Day{d.Num-1, d.sm}
return ret.ifexists()
}
func (sm *SelectableMonth) Days() []*Day {
ret := make([]*Day, 0)
ret := make([]*Day,0)
for d := sm.Day(1); d.exists(); d = d.Next() {
ret = append(ret, d)
ret = append(ret,d)
}
return ret
}
@ -223,9 +211,9 @@ func (sm *SelectableMonth) Week(i int) *Week {
}
func (sm *SelectableMonth) Weeks() []*Week {
ret := make([]*Week, 0)
ret := make([]*Week,0)
for w := sm.Week(1); w.exists(); w = w.Next() {
ret = append(ret, w)
ret = append(ret,w)
}
return ret
}
@ -234,28 +222,28 @@ func (w *Week) Day(i int) *Day {
if i < 1 || i > 7 {
return nil
}
wday := (w.Num-1)*7 + i
wday := (w.Num - 1) * 7 + i
if w.Num != 1 {
wday -= w.sm.Pad
}
ret := &Day{wday, w.sm}
ret := &Day{ wday, w.sm }
return ret.ifexists()
}
func (w *Week) Next() *Week {
ret := &Week{w.Num + 1, w.sm}
ret := &Week{ w.Num + 1, w.sm }
return ret.ifexists()
}
func (w *Week) Prev() *Week {
ret := &Week{w.Num - 1, w.sm}
ret := &Week{ w.Num - 1, w.sm }
return ret.ifexists()
}
func (w *Week) Days() []*Day {
ret := make([]*Day, 0)
ret := make([]*Day,0)
for d := w.Day(1); d.In(w); d = d.Next() {
ret = append(ret, d)
ret = append(ret,d)
}
return ret
}

View File

@ -4,8 +4,8 @@ package main
import (
"fmt"
"image/color"
"log"
"image/color"
"time"
gio "git.wow.st/gmp/giowrap"
@ -15,8 +15,8 @@ import (
"gioui.org/ui/layout"
"gioui.org/ui/text"
"golang.org/x/image/font/gofont/goregular"
"golang.org/x/image/font/sfnt"
"golang.org/x/image/font/gofont/goregular"
)
var (
@ -48,7 +48,7 @@ type SLabel struct {
func NewSLabel(t string, active *bool, lops ...gio.LabelOption) *SLabel {
ret := &SLabel{}
lops = append([]gio.LabelOption{gio.Face(face)}, lops...)
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},
@ -58,7 +58,7 @@ func NewSLabel(t string, active *bool, lops ...gio.LabelOption) *SLabel {
if *active {
bg.Color = color.RGBA{A: 0xff, R: 0xf0, G: 0xf0, B: 0xe0}
}
l := gio.AsClickable(gio.Enclose(bg, lbl))
l := gio.AsClickable(gio.Enclose(bg,lbl))
ret.w = l
ret.bg = bg
ret.active = active
@ -83,12 +83,12 @@ func (w *SLabel) Layout(ctx *gio.Context) {
func NewCal(sm *SelectableMonth) gio.Widget {
pad := gio.NewLabel("", gio.Face(face), gio.Align(text.End))
ds := make([]gio.Widget, 42)
ds := make([]gio.Widget,42)
for i := 0; i < len(ds); i++ {
if i < sm.Pad || i >= sm.DaysIn+sm.Pad {
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))
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...)
@ -104,9 +104,7 @@ 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}))
@ -115,7 +113,7 @@ func eventloop() {
f1 := gio.NewFlex(gio.Axis(layout.Vertical))
f2 := gio.NewFlex(gio.Axis(layout.Horizontal))
sidegrid := gio.NewGrid(1)
yrs := make([]gio.Widget, 6)
yrs := make([]gio.Widget,6)
curyr := sm.Year
topgrid := gio.NewGrid(3)
@ -127,42 +125,41 @@ func eventloop() {
f3 := gio.NewFlex(gio.Axis(layout.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))
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),
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():
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))
mth.SetText(fmt.Sprintf("%s %d",sm.Month.String(),sm.Year))
ows := sm.Ws
bg(margin(f1(
topgrid(lbtn, mth, rbtn),
topgrid(lbtn,mth,rbtn),
f2(
sidegrid(yrs...),
f3(
daygrid(sun, mon, tue, wed, thu, fri, sat),
daygrid(sun,mon,tue,wed,thu,fri,sat),
cal,
),
),
@ -187,7 +184,7 @@ func eventloop() {
sm.Next()
resetCal()
}
for i, y := range yrs {
for i,y := range yrs {
if y.(gio.Clickable).Clicked(ctx) {
sm.Year = curyr - i
sm.Refresh()
@ -196,6 +193,6 @@ func eventloop() {
}
sm.CheckSel()
}
}
}
}}
}

View File

@ -1,62 +0,0 @@
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.Dimens {
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.Dimens{
Size: image.Point{
X: cs.Width.Max,
Y: cs.Width.Max,
},
}
return ins.End(dims)
}

View File

@ -14,16 +14,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/gofont/goregular"
"golang.org/x/image/font/sfnt"
"golang.org/x/image/font/gofont/goregular"
)
var (
@ -56,27 +56,25 @@ func main() {
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)
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
switch os.Args[1] {
case "", "main1":
log.Print("main1()")
go main1()
case "main2":
log.Print("main2()")
go main2()
default:
log.Fatal(`Usage:
hello [main1|main2]
`)
case os.Args[1] == "main1":
log.Print("main1()")
go main1()
case os.Args[1] == "main2":
log.Print("main2()")
go main2()
}
app.Main()
}
@ -88,11 +86,11 @@ func main1() {
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))))
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))))
e2 := giowrap.NewEditor("text 2",giowrap.Face(ctx.Faces.For(regular, ui.Sp(24))))
f1 := giowrap.NewFlex(giowrap.Axis(layout.Vertical))
OuterInset := giowrap.NewInset(giowrap.Size(ui.Dp(10)))
@ -122,9 +120,9 @@ func main1() {
OuterInset(
f1(
e1,
giowrap.Flexible(0.33),
giowrap.Flexible(5),
InnerInset(e2),
giowrap.Flexible(0.67),
giowrap.Flexible(10),
f2(
InnerInset(btn1),
giowrap.Flexible(0.5),
@ -133,27 +131,21 @@ func main1() {
)).Layout(ctx)
ctx.Update()
if btn1.Clicked(ctx) {
log.Print("Clicked: " + e1.Text())
log.Print("Clicked: " + e1.Text() )
}
if btn2.Clicked(ctx) {
log.Print("Clicked: " + e2.Text())
log.Print("Clicked: " + e2.Text() )
}
}
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 profiled { continue }
if time.Since(startTime) < time.Second * 10 { continue }
if Profile {
profiled = true
f, err := os.Create("memprofile.pprof")
@ -178,7 +170,7 @@ func main2() {
if err != nil {
log.Fatal("Cannot parse font.")
}
t := time.NewTicker(time.Second / time.Duration(FPS))
t := time.NewTicker(time.Second/time.Duration(FPS))
e1 := &text.Editor{
Face: faces.For(regular, ui.Sp(24)),
SingleLine: true,
@ -193,14 +185,25 @@ func main2() {
e2.Focus()
e2.SetText("text 2")
btn1 := &Button{
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{
Face: faces.For(regular, ui.Sp(24)),
Label: "push1",
Text: "push1",
}
btn2 := &Button{
b1ins := layout.UniformInset(ui.Dp(4))
btn2 := text.Label{
Face: faces.For(regular, ui.Sp(24)),
Label: "push2",
Text: "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,61 +221,77 @@ func main2() {
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())
}
}
var dims layout.Dimens
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}
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)
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))
}
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) {
if ev.Type == gesture.TypeClick {
log.Print("Clicked: " + e1.Text() )
}
}
for ev, ok := click2.Next(q); ok; ev, ok = click2.Next(q) {
if ev.Type == gesture.TypeClick {
log.Print("Clicked: " + e2.Text() )
}
}
}
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 profiled { continue }
if time.Since(startTime) < time.Second * 10 { continue }
if Profile {
profiled = true
f, err := os.Create("memprofile.pprof")
@ -288,41 +307,3 @@ func main2() {
}
}
func layoutRRect(c ui.Config, ops *ui.Ops, cs layout.Constraints) layout.Dimens {
r := float32(c.Px(ui.Dp(4)))
sz := image.Point{X: cs.Width.Min, Y: cs.Height.Min}
w, h := float32(sz.X), float32(sz.Y)
giowrap.Rrect(ops, w, h, r, r, r, r)
paint.ColorOp{Color: color.RGBA{A: 0xff, R: 0x3c, G: 0x98, B: 0xc6}}.Add(ops)
paint.PaintOp{Rect: f32.Rectangle{Max: f32.Point{X: w, Y: h}}}.Add(ops)
return layout.Dimens{Size: sz}
}
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.Dimens {
ins := layout.UniformInset(ui.Dp(10))
cs = ins.Begin(c, ops, cs)
var dims layout.Dimens
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)
}

View File

@ -1,8 +1,8 @@
package main
import (
"fmt"
"image/color"
"fmt"
"log"
gio "git.wow.st/gmp/giowrap"
@ -12,15 +12,15 @@ import (
"gioui.org/ui/layout"
"gioui.org/ui/text"
"golang.org/x/image/font/gofont/goregular"
"golang.org/x/image/font/sfnt"
"golang.org/x/image/font/gofont/goregular"
)
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}
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 }
)
type SLabel struct {
@ -31,7 +31,7 @@ type SLabel struct {
func NewSLabel(t string, active *bool, lops ...gio.LabelOption) *SLabel {
ret := &SLabel{}
lops = append([]gio.LabelOption{gio.Face(face)}, lops...)
lops = append([]gio.LabelOption{gio.Face(face)},lops...)
lbl := gio.NewLabel(t, lops...)
bg := &gio.Background{
Color: gray2,
@ -41,7 +41,7 @@ func NewSLabel(t string, active *bool, lops ...gio.LabelOption) *SLabel {
if *active {
bg.Color = gray1
}
l := gio.AsClickable(gio.Enclose(bg, lbl))
l := gio.AsClickable(gio.Enclose(bg,lbl))
ret.w = l
ret.bg = bg
ret.active = active
@ -87,9 +87,7 @@ 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))
@ -110,7 +108,7 @@ func eventloop() {
labs[i] = make([]gio.Widget, numlabs)
sels[i] = make([]bool, numlabs)
for j := 0; j < numlabs; j++ {
labs[i][j] = NewSLabel(fmt.Sprintf("%03d", i*16+j), &sels[i][j])
labs[i][j] = NewSLabel(fmt.Sprintf("%03d",i * j), &sels[i][j])
}
}
@ -122,9 +120,8 @@ func eventloop() {
var oldInsets app.Insets
for {
select {
case e := <-w.Events():
for { select {
case e:= <-w.Events():
switch e := e.(type) {
case app.DestroyEvent:
return
@ -136,7 +133,7 @@ func eventloop() {
}
sysbg(sysinset(bg(margin(
f1(lbar, f2(topbar, sh(
f1(lbar,f2(topbar,sh(
sv(labs[0]...),
sv(labs[1]...),
sv(labs[2]...),
@ -156,6 +153,6 @@ func eventloop() {
))))))).Layout(ctx)
ctx.Update()
}
}
}
}}
}

147
grid.go
View File

@ -1,147 +0,0 @@
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.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]
})
}
}

268
main.go
View File

@ -14,8 +14,8 @@ import (
"gioui.org/ui/text"
"gioui.org/ui/f32"
"gioui.org/ui/gesture"
"gioui.org/ui/paint"
"gioui.org/ui/gesture"
"gioui.org/ui/pointer"
)
@ -29,12 +29,10 @@ 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)
e.data = append(e.data,nil)
e.Unlock()
return ret
}
@ -81,31 +79,25 @@ type Label struct {
l *text.Label
}
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 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 {
FaceOpt
c *color.RGBA
AlignOpt
}
type LabelOption interface{ DoLabelOption(*LabelOpts) }
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,
@ -132,26 +124,22 @@ type Editor struct {
e *text.Editor
}
type SinglelineOpt struct{ singleline bool }
func Singleline(x bool) SinglelineOpt { return SinglelineOpt{x} }
type SinglelineOpt struct { singleline bool }
func Singleline(x bool) SinglelineOpt { return SinglelineOpt{ x } }
type EditorOpts struct {
FaceOpt
SinglelineOpt
}
type EditorOption interface{ DoEditorOption(*EditorOpts) }
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)
}
ret.e = &text.Editor{Face: opts.face, SingleLine: opts.singleline}
for _,o := range eops { o.DoEditorOption(opts) }
ret.e = &text.Editor{ Face: opts.face, SingleLine: opts.singleline }
ret.SetText(t)
return ret
}
@ -169,7 +157,7 @@ type fWidget struct {
}
func NewfWidget(l Layout) fWidget {
return fWidget{l: l}
return fWidget{ l: l }
}
func (fw fWidget) Layout(ctx *Context) {
@ -179,13 +167,13 @@ func (fw fWidget) Layout(ctx *Context) {
type Stack WidgetCombinator
func NewStack() Stack {
s := layout.Stack{Alignment: layout.Center}
scs := make([]layout.StackChild, 0)
s := layout.Stack{ Alignment: layout.Center }
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 {
for _,w := range ws {
ctx.cs = s.Rigid()
w.Layout(ctx)
scs = append(scs, s.End(ctx.dims))
@ -198,23 +186,19 @@ func NewStack() Stack {
type List = WidgetCombinator
type AxisOpt struct{ axis layout.Axis }
func Axis(x layout.Axis) AxisOpt { return AxisOpt{x} }
type AxisOpt struct { axis layout.Axis }
func Axis(x layout.Axis) AxisOpt { return AxisOpt{ x } }
type ListOpts struct {
AxisOpt
}
type ListOption interface{ DoListOption(*ListOpts) }
type ListOption interface { DoListOption(*ListOpts) }
func (x AxisOpt) DoListOption(o *ListOpts) { o.axis = x.axis }
func NewList(los ...ListOption) List {
opts := &ListOpts{}
for _, o := range los {
o.DoListOption(opts)
}
l := layout.List{Axis: opts.axis}
for _,o := range los { o.DoListOption(opts) }
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() {
@ -250,9 +234,8 @@ func Rigid() Widget {
})
}
type AlignmentOpt struct{ alignment layout.Alignment }
func Alignment(x layout.Alignment) AlignmentOpt { return AlignmentOpt{x} }
type AlignmentOpt struct { alignment layout.Alignment }
func Alignment(x layout.Alignment) AlignmentOpt { return AlignmentOpt{ x } }
type FlexOpts struct {
AxisOpt
@ -260,17 +243,14 @@ type FlexOpts struct {
flexible float32
}
type FlexOption interface{ DoFlexOption(*FlexOpts) }
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 }
// NewFlex returns a WidgetCombinator that wraps the layout.Flex element.
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,
@ -278,7 +258,7 @@ func NewFlex(fos ...FlexOption) Flex {
index := extra.New()
extra.data[index] = opts
// do not call "make" inside the Layout function
fcs := make([]layout.FlexChild, 0)
fcs := make([]layout.FlexChild,0)
return func(ws ...Widget) Widget {
return NewfWidget(func(ctx *Context) {
@ -305,32 +285,24 @@ type InsetOpts struct {
top, right, bottom, left ui.Value
}
type InsetOption interface{ DoInsetOption(*InsetOpts) }
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} }
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} }
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
@ -341,10 +313,8 @@ func (x SizeOpt) DoInsetOption(o *InsetOpts) {
//NewInset returns a WidgetCombinator that wraps the layout.Inset element.
func NewInset(insos ...InsetOption) WidgetCombinator {
opts := &InsetOpts{}
for _, o := range insos {
o.DoInsetOption(opts)
}
ins := layout.Inset{Top: opts.top, Right: opts.right, Bottom: opts.bottom, Left: opts.left}
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) {
ctx.cs = ins.Begin(ctx.c, ctx.ops, ctx.cs)
@ -372,7 +342,7 @@ type Enclosure interface {
func Enclose(e Enclosure, ws ...Widget) Widget {
return NewfWidget(func(ctx *Context) {
e.Begin(ctx)
for _, w := range ws {
for _,w := range ws {
w.Layout(ctx)
}
e.End(ctx)
@ -384,23 +354,19 @@ type BackgroundOpts struct {
radius ui.Value
}
type BackgroundOption interface{ DoBackgroundOption(*BackgroundOpts) }
type BackgroundOption interface { DoBackgroundOption(*BackgroundOpts) }
type ColorOpt struct{ c color.RGBA }
func Color(x color.RGBA) ColorOpt { return ColorOpt{x} }
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} }
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,
@ -423,10 +389,10 @@ func (bg *Background) End(ctx *Context) {
stack.Push(ctx.ops)
w, h := float32(ctx.dims.Size.X), float32(ctx.dims.Size.Y)
if r := float32(ctx.c.Px(bg.Radius)); r > 0 {
if r > w/2 {
if r > w / 2 {
r = w / 2
}
if r > h/2 {
if r > h / 2 {
r = h / 2
}
Rrect(ctx.ops, w, h, r, r, r, r)
@ -467,7 +433,7 @@ type cWidget struct {
func (w cWidget) Layout(ctx *Context) {
w.w.Layout(ctx)
pointer.RectAreaOp{image.Rect(0, 0, ctx.dims.Size.X, ctx.dims.Size.Y)}.Add(ctx.ops)
pointer.RectAreaOp{image.Rect(0,0,ctx.dims.Size.X,ctx.dims.Size.Y)}.Add(ctx.ops)
w.click.Add(ctx.ops)
}
@ -483,5 +449,133 @@ 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)}
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]
})
}
}