Move Grid{} to a new file. Run go fmt.

This commit is contained in:
Greg 2019-08-27 09:24:30 -04:00
parent f96cdb619a
commit 9f60b99984
7 changed files with 544 additions and 398 deletions

View File

@ -103,7 +103,9 @@ func (sm *SelectableMonth) Weekdays() int {
for w := 0; w <= 6; w++ {
for d := 1; d < 6; d++ {
x := w*7 + d + 1 - sm.Pad
if x < 0 || x >= sm.DaysIn { break }
if x < 0 || x >= sm.DaysIn {
break
}
if sm.S[x] {
i++
}
@ -116,12 +118,22 @@ func (sm *SelectableMonth) Weekends() int {
var i int
for w := 0; w <= 6; w++ {
x := w*7 + 1 - sm.Pad
if x < 0 { continue }
if x >= sm.DaysIn { break }
if sm.S[x] { i++ }
if x < 0 {
continue
}
if x >= sm.DaysIn {
break
}
if sm.S[x] {
i++
}
x = w*7 + 6 + 1 - sm.Pad
if x < 0 || x >= sm.DaysIn { break }
if sm.S[x] { i++ }
if x < 0 || x >= sm.DaysIn {
break
}
if sm.S[x] {
i++
}
}
return i
}

View File

@ -4,8 +4,8 @@ package main
import (
"fmt"
"log"
"image/color"
"log"
"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/sfnt"
"golang.org/x/image/font/gofont/goregular"
"golang.org/x/image/font/sfnt"
)
var (
@ -104,7 +104,9 @@ 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}))
@ -143,7 +145,8 @@ func eventloop() {
}
resetCal()
for { select {
for {
select {
case e := <-w.Events():
switch e := e.(type) {
case app.DestroyEvent:
@ -193,6 +196,6 @@ func eventloop() {
}
sm.CheckSel()
}
}}
}
}
}

62
cmd/grid/grid.go Normal file
View File

@ -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.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/sfnt"
"golang.org/x/image/font/gofont/goregular"
"golang.org/x/image/font/sfnt"
)
var (
@ -64,6 +64,7 @@ func main() {
mux.Unlock()
}
}()
if len(os.Args) > 1 {
switch os.Args[1] {
case "", "main1":
log.Print("main1()")
@ -76,6 +77,7 @@ func main() {
hello [main1|main2]
`)
}
}
app.Main()
}
@ -141,11 +143,17 @@ func main1() {
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")
@ -287,11 +295,17 @@ func main2() {
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")
@ -306,4 +320,3 @@ func main2() {
}
}
}

View File

@ -1,8 +1,8 @@
package main
import (
"image/color"
"fmt"
"image/color"
"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/sfnt"
"golang.org/x/image/font/gofont/goregular"
"golang.org/x/image/font/sfnt"
)
var (
face text.Face
black color.RGBA = color.RGBA{A: 0xff, R: 0x00, G: 0x00, B: 0x00}
gray2 color.RGBA = color.RGBA{ A: 0xff, R: 0x70, G: 0x70, B: 0x70 }
gray1 color.RGBA = color.RGBA{ A: 0xff, R: 0x50, G: 0x50, B: 0x50 }
gray2 color.RGBA = color.RGBA{A: 0xff, R: 0xc0, G: 0xc0, B: 0xc0}
gray1 color.RGBA = color.RGBA{A: 0xff, R: 0x70, G: 0x70, B: 0x70}
)
type SLabel struct {
@ -87,7 +87,9 @@ 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))
@ -108,7 +110,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 * j), &sels[i][j])
labs[i][j] = NewSLabel(fmt.Sprintf("%03d", i*16+j), &sels[i][j])
}
}
@ -120,7 +122,8 @@ func eventloop() {
var oldInsets app.Insets
for { select {
for {
select {
case e := <-w.Events():
switch e := e.(type) {
case app.DestroyEvent:
@ -153,6 +156,6 @@ func eventloop() {
))))))).Layout(ctx)
ctx.Update()
}
}}
}
}
}

147
grid.go Normal file
View File

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

180
main.go
View File

@ -14,8 +14,8 @@ import (
"gioui.org/ui/text"
"gioui.org/ui/f32"
"gioui.org/ui/paint"
"gioui.org/ui/gesture"
"gioui.org/ui/paint"
"gioui.org/ui/pointer"
)
@ -29,7 +29,9 @@ var extra Extra
func (e *Extra) New() int {
e.Lock()
if e.data == nil { e.data = make([]interface{},0) }
if e.data == nil {
e.data = make([]interface{}, 0)
}
ret := e.max
e.max = e.max + 1
e.data = append(e.data, nil)
@ -80,8 +82,11 @@ type Label struct {
}
type FaceOpt struct{ face text.Face }
func Face(x text.Face) FaceOpt { return FaceOpt{x} }
type AlignOpt struct{ alignment text.Alignment }
func Align(x text.Alignment) AlignOpt { return AlignOpt{x} }
type LabelOpts struct {
@ -90,14 +95,17 @@ type LabelOpts struct {
AlignOpt
}
type LabelOption interface{ DoLabelOption(*LabelOpts) }
func (x FaceOpt) DoLabelOption(o *LabelOpts) { o.face = x.face }
func (x AlignOpt) DoLabelOption(o *LabelOpts) { o.alignment = x.alignment }
func (x ColorOpt) DoLabelOption(o *LabelOpts) { o.c = &x.c; }
func (x ColorOpt) DoLabelOption(o *LabelOpts) { o.c = &x.c }
func NewLabel(t string, lops ...LabelOption) *Label {
ret := &Label{}
opts := &LabelOpts{}
for _,o := range lops { o.DoLabelOption(opts) }
for _, o := range lops {
o.DoLabelOption(opts)
}
ret.l = &text.Label{
Face: opts.face,
Text: t,
@ -125,6 +133,7 @@ type Editor struct {
}
type SinglelineOpt struct{ singleline bool }
func Singleline(x bool) SinglelineOpt { return SinglelineOpt{x} }
type EditorOpts struct {
@ -132,13 +141,16 @@ type EditorOpts struct {
SinglelineOpt
}
type EditorOption interface{ DoEditorOption(*EditorOpts) }
func (x FaceOpt) DoEditorOption(o *EditorOpts) { o.face = x.face }
func (x SinglelineOpt) DoEditorOption(o *EditorOpts) { o.singleline = x.singleline }
func NewEditor(t string, eops ...EditorOption) *Editor {
ret := &Editor{}
opts := &EditorOpts{}
for _,o := range eops { o.DoEditorOption(opts) }
for _, o := range eops {
o.DoEditorOption(opts)
}
ret.e = &text.Editor{Face: opts.face, SingleLine: opts.singleline}
ret.SetText(t)
return ret
@ -187,17 +199,21 @@ func NewStack() Stack {
type List = WidgetCombinator
type AxisOpt struct{ axis layout.Axis }
func Axis(x layout.Axis) AxisOpt { return AxisOpt{x} }
type ListOpts struct {
AxisOpt
}
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) }
for _, o := range los {
o.DoListOption(opts)
}
l := layout.List{Axis: opts.axis}
return func(ws ...Widget) Widget {
return NewfWidget(func(ctx *Context) {
@ -235,6 +251,7 @@ func Rigid() Widget {
}
type AlignmentOpt struct{ alignment layout.Alignment }
func Alignment(x layout.Alignment) AlignmentOpt { return AlignmentOpt{x} }
type FlexOpts struct {
@ -244,13 +261,16 @@ type FlexOpts struct {
}
type FlexOption interface{ DoFlexOption(*FlexOpts) }
func (x AxisOpt) DoFlexOption(o *FlexOpts) { o.axis = x.axis }
func (x AlignmentOpt) DoFlexOption(o *FlexOpts) { o.alignment = x.alignment }
// 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,
@ -288,12 +308,19 @@ type InsetOpts struct {
type InsetOption interface{ DoInsetOption(*InsetOpts) }
type TopOpt struct{ top ui.Value }
func Top(x ui.Value) TopOpt { return TopOpt{x} }
type RightOpt struct{ right ui.Value }
func Right(x ui.Value) RightOpt { return RightOpt{x} }
type BottomOpt struct{ bottom ui.Value }
func Bottom(x ui.Value) BottomOpt { return BottomOpt{x} }
type LeftOpt struct{ left ui.Value }
func Left(x ui.Value) LeftOpt { return LeftOpt{x} }
func (x TopOpt) DoInsetOption(o *InsetOpts) { o.top = x.top }
@ -302,6 +329,7 @@ func (x BottomOpt) DoInsetOption(o *InsetOpts) { o.bottom = x.bottom }
func (x LeftOpt) DoInsetOption(o *InsetOpts) { o.left = x.left }
type SizeOpt struct{ size ui.Value }
func Size(x ui.Value) SizeOpt { return SizeOpt{x} }
func (x SizeOpt) DoInsetOption(o *InsetOpts) {
o.top = x.size
@ -313,7 +341,9 @@ 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) }
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) {
@ -357,16 +387,20 @@ type BackgroundOpts struct {
type BackgroundOption interface{ DoBackgroundOption(*BackgroundOpts) }
type ColorOpt struct{ c color.RGBA }
func Color(x color.RGBA) ColorOpt { return ColorOpt{x} }
func (x ColorOpt) DoBackgroundOption(o *BackgroundOpts) { o.c = x.c }
type RadiusOpt struct{ radius ui.Value }
func Radius(x ui.Value) RadiusOpt { return RadiusOpt{x} }
func (x RadiusOpt) DoBackgroundOption(o *BackgroundOpts) { o.radius = x.radius }
func NewBackground(bos ...BackgroundOption) WidgetCombinator {
opts := &BackgroundOpts{}
for _,o := range bos { o.DoBackgroundOption(opts) }
for _, o := range bos {
o.DoBackgroundOption(opts)
}
bg := &Background{
Color: opts.c,
Radius: opts.radius,
@ -451,131 +485,3 @@ func (w cWidget) Clicked(ctx *Context) bool {
func AsClickable(w Widget) cWidget {
return cWidget{w: w, click: new(gesture.Click)}
}
type Grid struct {
Cols int
Height, Width int
macro ui.MacroOp
ops *ui.Ops
cs layout.Constraints
mode gridMode
row, col int
}
type GridChild struct {
dims layout.Dimens
macro ui.MacroOp
}
type gridMode uint8
const (
modeNone gridMode = iota
modeBegun
)
func (g *Grid) Init(ops *ui.Ops, cs layout.Constraints) layout.Constraints {
g.mode = modeBegun
g.ops = ops
g.cs = cs
g.row, g.col = 0, 0
cs.Height.Min = 0
if g.Height != 0 { g.cs.Height.Max = g.Height }
if g.Width != 0 { g.cs.Width.Max = g.Width }
cs.Width.Max = g.cs.Width.Max / g.Cols
if g.Cols > 1 {
cs.Width.Min = cs.Width.Max
}
return cs
}
func (g *Grid) Begin() {
g.macro.Record(g.ops)
}
func (g *Grid) End(dims layout.Dimens) GridChild {
if g.mode != modeBegun {
panic("Must call Grid.Begin() before adding children.")
}
g.macro.Stop()
return GridChild{ dims: dims, macro: g.macro }
}
func (g *Grid) Layout(cs ...GridChild) layout.Dimens {
rowheight := 0
height := 0
var width float32
var maxwidth float32
for _,c := range cs {
var stack ui.StackOp
stack.Push(g.ops)
c.macro.Add(g.ops)
stack.Pop()
if c.dims.Size.Y > rowheight {
rowheight = c.dims.Size.Y
}
g.col = g.col+1
var x float32
if g.Cols == 1 {
x = float32(c.dims.Size.X)
if x > maxwidth { maxwidth = x }
} else {
x = float32(g.cs.Width.Max / g.Cols)
}
if g.col < g.Cols {
ui.TransformOp{}.Offset(f32.Point{X: x}).Add(g.ops)
width = width + x
} else {
g.col = 0
ui.TransformOp{}.Offset(f32.Point{ X: -width, Y: float32(rowheight) }).Add(g.ops)
g.row = g.row + 1
height = height + rowheight
width = 0
rowheight = 0
}
}
if height == 0 { height = rowheight }
g.mode = modeNone
var dwidth int
if g.Cols == 1 {
dwidth = int(maxwidth)
} else {
dwidth = g.cs.Width.Max
}
return layout.Dimens{ Size: image.Point{ dwidth, height } }
}
func toPointF(p image.Point) f32.Point {
return f32.Point{X: float32(p.X), Y: float32(p.Y)}
}
type GridOption interface { DoGridOption(*Grid) }
type HeightOpt struct { height int }
func Height(x int) HeightOpt { return HeightOpt{ x } }
func (x HeightOpt) DoGridOption(g *Grid) { g.Height = x.height }
func NewGrid(cols int, gops ...GridOption) WidgetCombinator {
g := &Grid{ Cols: cols }
for _, gop := range gops {
gop.DoGridOption(g)
}
gcs := make([]GridChild,0)
return func(ws ...Widget) Widget {
return NewfWidget(func(ctx *Context) {
cs := g.Init(ctx.ops, ctx.cs)
ctx.cs = cs
for _,w := range ws {
g.Begin()
w.Layout(ctx)
ctx.cs = cs // widget layout can modify constraints...
gcs = append(gcs,g.End(ctx.dims))
}
ctx.dims = g.Layout(gcs...)
gcs = gcs[0:0]
})
}
}