From e1fc40e6c6acd35d1aea234a4fd3e29a3ffb8e97 Mon Sep 17 00:00:00 2001 From: Greg Date: Mon, 12 Aug 2019 12:19:16 -0400 Subject: [PATCH] Change Layout() type to Layout(Context) Context --- main.go | 125 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 79 insertions(+), 46 deletions(-) diff --git a/main.go b/main.go index f653705..14c92fc 100644 --- a/main.go +++ b/main.go @@ -23,12 +23,14 @@ type Context struct { cs layout.Constraints dims layout.Dimens faces measure.Faces + extra map[string]interface{} } -func NewContext(w *app.Window) *Context { - return &Context{ +func NewContext(w *app.Window) Context { + return Context{ ops: new(ui.Ops), q: w.Queue(), + extra: make(map[string]interface{}), } } @@ -39,36 +41,53 @@ func (ctx *Context) Reset(e app.DrawEvent) { ctx.faces.Reset(ctx.c) } -func (fw fWidget) Layout(c ui.Config, q input.Queue, ops *ui.Ops, cs layout.Constraints) layout.Dimens { - return fw.l(c, q, ops, cs) +func (fw fWidget) Layout(ctx Context) Context { + return fw.l(ctx) } 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 { l Layout } -type WidgetCombinator func(...Widget) Widget - -type Layout func(ui.Config, input.Queue, *ui.Ops, layout.Constraints) layout.Dimens +type Layout func(Context) Context type LayoutCombinator func(...Layout) Layout -func (ctx *Context) Do(ls ...Layout) { +func LayoutWithContext(ctx Context, ls ...Layout) Context { for _, l := range ls { - ctx.dims = l(ctx.c, ctx.q, ctx.ops, ctx.cs) + ctx = l(ctx) } + return ctx } type Label struct { l *text.Label } -func (l *Label) Layout(c ui.Config, q input.Queue, ops *ui.Ops, cs layout.Constraints) layout.Dimens { - return l.l.Layout(ops, cs) +func (l *Label) Layout(ctx Context) Context { + 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() { @@ -80,21 +99,16 @@ func main() { } ctx := NewContext(w) t := time.NewTicker(time.Second/30) - e1 := &text.Editor{ - Face: ctx.faces.For(regular, ui.Sp(24)), - SingleLine: true, - } - e1.SetText("hi there") - e1.Focus() + e1 := NewEditor(ctx.faces.For(regular, ui.Sp(24)), true) + e1.e.SetText("hi there") + e1.e.Focus() - e2 := &text.Editor{ - Face: ctx.faces.For(regular, ui.Sp(24)), - SingleLine: true, - } - e2.SetText("ok bye") - f := Flex(layout.Vertical, layout.Start, layout.Start) - 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)) + e2 := NewEditor( ctx.faces.For(regular, ui.Sp(24)), true) + e2.e.SetText("ok bye") + + f := NewFlex(layout.Vertical, layout.Start, layout.Start) + ins10a := NewInset(ui.Dp(10),ui.Dp(10),ui.Dp(10),ui.Dp(10)) + ins10b := NewInset(ui.Dp(10),ui.Dp(10),ui.Dp(10),ui.Dp(10)) btn := &Label{} btn.l = &text.Label{ @@ -113,11 +127,13 @@ func main() { return case app.DrawEvent: ctx.Reset(e) - ctx.Do( + LayoutWithContext(ctx, ins10a(f( - FlexChild{10,e1}, - FlexChild{5,ins10b(e2)}, - FlexChild{1,btn}, + e1, + Flexible(5), + ins10b(e2), + Flexible(10), + btn, )).Layout) w.Draw(ctx.ops) } @@ -132,43 +148,60 @@ type FlexChild struct { 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{ Axis: axis, MainAxisAlignment: mainAxisAlignment, CrossAxisAlignment: crossAxisAlignment, } - return func(ws ...FlexChild) Widget { + return func(ws ...Widget) Widget { ret := fWidget{} - ret.l = func(c ui.Config, q input.Queue, ops *ui.Ops, cs layout.Constraints) layout.Dimens { - f.Init(ops, cs) - cs = f.Rigid() - var dims layout.Dimens + ret.l = func(ctx Context) Context { + f.Init(ctx.ops, ctx.cs) fcs := make([]layout.FlexChild,len(ws)) for i, w := range ws { - if i > 0 { - cs = f.Flexible(w.x) + 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() } - dims = w.w.Layout(c, q, ops, cs) - fcs[i] = f.End(dims) + ctx = w.Layout(ctx) + fcs[i] = f.End(ctx.dims) } - return f.Layout(fcs...) + ctx.dims = f.Layout(fcs...) + delete(ctx.extra, "Flexible") + return ctx } 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 } return func(ws ...Widget) Widget { ret := fWidget{} - ret.l = func(c ui.Config, q input.Queue, ops *ui.Ops, cs layout.Constraints) layout.Dimens { - cs = ins.Begin(c, ops, cs) + ret.l = func(ctx Context) Context { + ctx.cs = ins.Begin(ctx.c, ctx.ops, ctx.cs) var dims layout.Dimens 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 }