From 92cb4d20a4543f44bd773651e49428d7e3c81ef9 Mon Sep 17 00:00:00 2001 From: Greg Date: Thu, 15 Aug 2019 18:32:01 -0400 Subject: [PATCH] Cleanups and comments. --- cmd/hello/main.go | 8 ++++---- main.go | 48 +++++++++++++++-------------------------------- 2 files changed, 19 insertions(+), 37 deletions(-) diff --git a/cmd/hello/main.go b/cmd/hello/main.go index 0ba352c..8aabb23 100644 --- a/cmd/hello/main.go +++ b/cmd/hello/main.go @@ -3,6 +3,7 @@ package main import ( "git.wow.st/gmp/giowrap" + "image/color" "log" "time" @@ -40,7 +41,7 @@ func main() { "push", text.Center, ) - bg := giowrap.NewBackground(giowrap.Rgb(0x3c98c6), ui.Dp(4)) + bg := giowrap.NewBackground(color.RGBA{A: 0xff, R: 0x3c, G: 0x98, B: 0xc6}, ui.Dp(4)) btn := giowrap.Clickable(bg(lbl)) for { @@ -53,15 +54,14 @@ func main() { return case app.DrawEvent: ctx = ctx.Reset(e) - ctx = giowrap.LayoutWithContext(ctx, - OuterInset( + ctx = OuterInset( f( e1, giowrap.Flexible(5), InnerInset(e2), giowrap.Flexible(10), btn, - ))) + )).Layout(ctx) ctx.Draw() if btn.Clicked(ctx) { log.Print("Clicked: " + e2.Text() ) diff --git a/main.go b/main.go index 7a61c9c..8e697af 100644 --- a/main.go +++ b/main.go @@ -52,25 +52,6 @@ func (ctx Context) Draw() { type Layout func(Context) 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 { Layout(Context) Context } @@ -111,28 +92,25 @@ func (e *Editor) Layout(ctx Context) Context { return ctx } -func (e *Editor) Text() string { - return e.e.Text() +func (e *Editor) Text() string { return e.e.Text() } +func (e *Editor) SetText(s string) { e.e.SetText(s) } +func (e *Editor) Focus() { e.e.Focus() } + +type fWidget struct { + l Layout } -func (e *Editor) SetText(s string) { - e.e.SetText(s) +func NewfWidget(l Layout) fWidget { + return fWidget{ l: l } } -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)} +func (fw fWidget) Layout(ctx Context) Context { + return fw.l(ctx) } type Flex WidgetCombinator +// This "Widget" does nothing except set the Flexible key of ctx.extra. func Flexible(v float32) Widget { return NewfWidget(func(ctx Context) Context { ctx.extra["Flexible"] = v @@ -140,6 +118,7 @@ func Flexible(v float32) Widget { }) } +// NewFlex returns a WidgetCombinator that wraps the layout.Flex element. func NewFlex(axis layout.Axis, mainAxisAlignment layout.MainAxisAlignment, crossAxisAlignment layout.CrossAxisAlignment) Flex { f := layout.Flex{ Axis: axis, @@ -171,6 +150,7 @@ func NewFlex(axis layout.Axis, mainAxisAlignment layout.MainAxisAlignment, cross } } +//NewInset returns a WidgetCombinator that wraps the layout.Inset element. 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 { @@ -264,6 +244,7 @@ func rrect(ops *ui.Ops, width, height, se, sw, nw, ne float32) { b.End() } +//cWidget is a clickable Widget that provides the Clicked() method. type cWidget struct { w Widget click *gesture.Click @@ -285,6 +266,7 @@ func (w cWidget) Clicked(ctx Context) bool { return false } +//Clickable converts any Widget into a clickable Widget. func Clickable(w Widget) cWidget { return cWidget{ w: w, click: new(gesture.Click) } }