Change Layout() type to Layout(Context) Context

This commit is contained in:
Greg 2019-08-12 12:19:16 -04:00
parent 396ee09d9f
commit e1fc40e6c6
1 changed files with 79 additions and 46 deletions

125
main.go
View File

@ -23,12 +23,14 @@ type Context struct {
cs layout.Constraints cs layout.Constraints
dims layout.Dimens dims layout.Dimens
faces measure.Faces faces measure.Faces
extra map[string]interface{}
} }
func NewContext(w *app.Window) *Context { func NewContext(w *app.Window) Context {
return &Context{ return Context{
ops: new(ui.Ops), ops: new(ui.Ops),
q: w.Queue(), q: w.Queue(),
extra: make(map[string]interface{}),
} }
} }
@ -39,36 +41,53 @@ func (ctx *Context) Reset(e app.DrawEvent) {
ctx.faces.Reset(ctx.c) ctx.faces.Reset(ctx.c)
} }
func (fw fWidget) Layout(c ui.Config, q input.Queue, ops *ui.Ops, cs layout.Constraints) layout.Dimens { func (fw fWidget) Layout(ctx Context) Context {
return fw.l(c, q, ops, cs) return fw.l(ctx)
} }
type Widget interface { type Widget interface {
Layout(ui.Config, input.Queue, *ui.Ops, layout.Constraints) layout.Dimens Layout(Context) Context
} }
type WidgetCombinator func(...Widget) Widget
type fWidget struct { type fWidget struct {
l Layout l Layout
} }
type WidgetCombinator func(...Widget) Widget type Layout func(Context) Context
type Layout func(ui.Config, input.Queue, *ui.Ops, layout.Constraints) layout.Dimens
type LayoutCombinator func(...Layout) Layout type LayoutCombinator func(...Layout) Layout
func (ctx *Context) Do(ls ...Layout) { func LayoutWithContext(ctx Context, ls ...Layout) Context {
for _, l := range ls { for _, l := range ls {
ctx.dims = l(ctx.c, ctx.q, ctx.ops, ctx.cs) ctx = l(ctx)
} }
return ctx
} }
type Label struct { type Label struct {
l *text.Label l *text.Label
} }
func (l *Label) Layout(c ui.Config, q input.Queue, ops *ui.Ops, cs layout.Constraints) layout.Dimens { func (l *Label) Layout(ctx Context) Context {
return l.l.Layout(ops, cs) ctx.dims = l.l.Layout(ctx.ops, ctx.cs)
return ctx
}
type Editor struct {
e *text.Editor
}
func NewEditor(face text.Face, singleline bool) *Editor {
ret := &Editor{}
ret.e = &text.Editor{ Face: face, SingleLine: singleline }
return ret
}
func (e *Editor) Layout(ctx Context) Context {
ctx.dims = e.e.Layout(ctx.c, ctx.q, ctx.ops, ctx.cs)
return ctx
} }
func main() { func main() {
@ -80,21 +99,16 @@ func main() {
} }
ctx := NewContext(w) ctx := NewContext(w)
t := time.NewTicker(time.Second/30) t := time.NewTicker(time.Second/30)
e1 := &text.Editor{ e1 := NewEditor(ctx.faces.For(regular, ui.Sp(24)), true)
Face: ctx.faces.For(regular, ui.Sp(24)), e1.e.SetText("hi there")
SingleLine: true, e1.e.Focus()
}
e1.SetText("hi there")
e1.Focus()
e2 := &text.Editor{ e2 := NewEditor( ctx.faces.For(regular, ui.Sp(24)), true)
Face: ctx.faces.For(regular, ui.Sp(24)), e2.e.SetText("ok bye")
SingleLine: true,
} f := NewFlex(layout.Vertical, layout.Start, layout.Start)
e2.SetText("ok bye") ins10a := NewInset(ui.Dp(10),ui.Dp(10),ui.Dp(10),ui.Dp(10))
f := Flex(layout.Vertical, layout.Start, layout.Start) ins10b := NewInset(ui.Dp(10),ui.Dp(10),ui.Dp(10),ui.Dp(10))
ins10a := Inset(ui.Dp(10),ui.Dp(10),ui.Dp(10),ui.Dp(10))
ins10b := Inset(ui.Dp(10),ui.Dp(10),ui.Dp(10),ui.Dp(10))
btn := &Label{} btn := &Label{}
btn.l = &text.Label{ btn.l = &text.Label{
@ -113,11 +127,13 @@ func main() {
return return
case app.DrawEvent: case app.DrawEvent:
ctx.Reset(e) ctx.Reset(e)
ctx.Do( LayoutWithContext(ctx,
ins10a(f( ins10a(f(
FlexChild{10,e1}, e1,
FlexChild{5,ins10b(e2)}, Flexible(5),
FlexChild{1,btn}, ins10b(e2),
Flexible(10),
btn,
)).Layout) )).Layout)
w.Draw(ctx.ops) w.Draw(ctx.ops)
} }
@ -132,43 +148,60 @@ type FlexChild struct {
w Widget w Widget
} }
func Flex(axis layout.Axis, mainAxisAlignment layout.MainAxisAlignment, crossAxisAlignment layout.CrossAxisAlignment) func(...FlexChild) Widget { func Flexible(v float32) Widget {
ret := fWidget{}
ret.l = func(ctx Context) Context {
ctx.extra["Flexible"] = v
return ctx
}
return ret
}
func NewFlex(axis layout.Axis, mainAxisAlignment layout.MainAxisAlignment, crossAxisAlignment layout.CrossAxisAlignment) WidgetCombinator {
f := layout.Flex{ f := layout.Flex{
Axis: axis, Axis: axis,
MainAxisAlignment: mainAxisAlignment, MainAxisAlignment: mainAxisAlignment,
CrossAxisAlignment: crossAxisAlignment, CrossAxisAlignment: crossAxisAlignment,
} }
return func(ws ...FlexChild) Widget { return func(ws ...Widget) Widget {
ret := fWidget{} ret := fWidget{}
ret.l = func(c ui.Config, q input.Queue, ops *ui.Ops, cs layout.Constraints) layout.Dimens { ret.l = func(ctx Context) Context {
f.Init(ops, cs) f.Init(ctx.ops, ctx.cs)
cs = f.Rigid()
var dims layout.Dimens
fcs := make([]layout.FlexChild,len(ws)) fcs := make([]layout.FlexChild,len(ws))
for i, w := range ws { for i, w := range ws {
if i > 0 { if v,ok := ctx.extra["Flexible"]; ok {
cs = f.Flexible(w.x) switch v := v.(type) {
case float32:
ctx.cs = f.Flexible(v)
default:
log.Fatal("Type error")
}
} else {
ctx.cs = f.Rigid()
} }
dims = w.w.Layout(c, q, ops, cs) ctx = w.Layout(ctx)
fcs[i] = f.End(dims) fcs[i] = f.End(ctx.dims)
} }
return f.Layout(fcs...) ctx.dims = f.Layout(fcs...)
delete(ctx.extra, "Flexible")
return ctx
} }
return ret return ret
} }
} }
func Inset(top, right, bottom, left ui.Value) WidgetCombinator { func NewInset(top, right, bottom, left ui.Value) WidgetCombinator {
ins := layout.Inset{ Top: top, Right: right, Bottom: bottom, Left: left } ins := layout.Inset{ Top: top, Right: right, Bottom: bottom, Left: left }
return func(ws ...Widget) Widget { return func(ws ...Widget) Widget {
ret := fWidget{} ret := fWidget{}
ret.l = func(c ui.Config, q input.Queue, ops *ui.Ops, cs layout.Constraints) layout.Dimens { ret.l = func(ctx Context) Context {
cs = ins.Begin(c, ops, cs) ctx.cs = ins.Begin(ctx.c, ctx.ops, ctx.cs)
var dims layout.Dimens var dims layout.Dimens
for _, w := range ws { for _, w := range ws {
dims = w.Layout(c, q, ops, cs) ctx = w.Layout(ctx)
} }
return ins.End(dims) ctx.dims = ins.End(dims)
return ctx
} }
return ret return ret
} }