From 03740d940b15da4657456b73bfdd424039e171c3 Mon Sep 17 00:00:00 2001 From: Greg Date: Thu, 15 Aug 2019 09:27:04 -0400 Subject: [PATCH] Add README.md --- README.md | 68 +++++++++++++++++++++++++++++++++++++++++++++++ cmd/hello/main.go | 4 +-- main.go | 10 +++---- 3 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..340cdd9 --- /dev/null +++ b/README.md @@ -0,0 +1,68 @@ +# GIOWrap + +An experimental wrapper package for the Gio immediate mode GUI, designed to reduce +boilerplate. + +``` +go run ~/go/src/git.wow.st/gmp/giowrap/cmd/hello/main.go +``` + +The basic concept is to define a `Context` type that contains the Gio variables +required for layout and drawing. `Context` contains a pointer to `app.Window` and +`ui.Ops` so that, once layout is complete, you can call `Context.Draw()` to draw +your objects in your window. + +The following types and functions are defined in order to transform a `Context` +variable during layout: + +```go +type Layout func(Context) Context + +type Widget interface { + Layout(Context) Context +} + +func LayoutWithContext(ctx Context, ws ...Widget) Context { + for _, w := range ws { + ctx = w.Layout(ctx) + } + return ctx +} + +type WidgetCombinator func(...Widget) Widget + +type Enclosure interface { + Begin(Context) Context + End(Context) Context +} + +func Enclose(e Enclosure, w ...Widget) Widget { ... } +``` + +The `Context` struct also contains a `map[string]interface{}` allowing arbitrary +data to be maintained. This can be used, for example, to define themes, and control +the operation of a `WidgetCombinator` during operation. For example> + +```go +type fWidget struct { l Layout } +func NewfWidget(l layout) fWidget { return fWidget{ l: l } } + +func (f Flex) Flexible(v float32) Widget { + return NewfWidget(func(ctx Context) Context { + ctx.extra["Flexible"] = v + return ctx + }) +} + +... + ctx = giowrap.LayoutWithContext(ctx, + myFlex( + w1, + giowrap.Flexible(0.5), + w2, + giowrap.Flexible(1), + w3, + )) + ctx.Draw() +... +``` diff --git a/cmd/hello/main.go b/cmd/hello/main.go index 52cbdc3..7eb3e94 100644 --- a/cmd/hello/main.go +++ b/cmd/hello/main.go @@ -57,9 +57,9 @@ func main() { OuterInset( f( e1, - f.Flexible(5), + giowrap.Flexible(5), InnerInset(e2), - f.Flexible(10), + giowrap.Flexible(10), btn, ))) ctx.Draw() diff --git a/main.go b/main.go index 4af0bf0..5da3546 100644 --- a/main.go +++ b/main.go @@ -51,11 +51,9 @@ func (ctx Context) Draw() { type Layout func(Context) Context -type LayoutCombinator func(...Layout) Layout - -func LayoutWithContext(ctx Context, ls ...Widget) Context { - for _, l := range ls { - ctx = l.Layout(ctx) +func LayoutWithContext(ctx Context, ws ...Widget) Context { + for _, w := range ws { + ctx = w.Layout(ctx) } return ctx } @@ -151,7 +149,7 @@ type FlexChild struct { type Flex WidgetCombinator -func (f Flex) Flexible(v float32) Widget { +func Flexible(v float32) Widget { return NewfWidget(func(ctx Context) Context { ctx.extra["Flexible"] = v return ctx